Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: allocate.c | |
5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
7 | * | |
8 | * The above named program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public | |
10 | * License version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * The above named program is distributed in the hope that it will be | |
13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public | |
18 | * License along with this work; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | * | |
21 | * ========== Copyright Header End ============================================ | |
22 | */ | |
23 | /* | |
24 | * Copyright 2006 Sun Microsystems, Inc. All rights reserved. | |
25 | * Use is subject to license terms. | |
26 | */ | |
27 | ||
28 | #pragma ident "@(#)allocate.c 1.6 06/08/15 SMI" | |
29 | ||
30 | #include <string.h> | |
31 | #include <malloc.h> | |
32 | #include <unistd.h> | |
33 | #include <stdlib.h> | |
34 | ||
35 | #include "basics.h" | |
36 | #include "fatal.h" | |
37 | #include "allocate.h" | |
38 | ||
39 | ||
40 | /* | |
41 | * Simple allocation routines | |
42 | */ | |
43 | ||
44 | ||
45 | ||
46 | void * xmalloc(size_t size, int linen, char * filen) | |
47 | { | |
48 | void *p; | |
49 | ||
50 | if (size<=0) { | |
51 | if (size<0) fatal("xmalloc of negative size (%d) at %d in %s", | |
52 | linen, filen); | |
53 | warning("xmalloc of zero size at %d in %s", linen, filen); | |
54 | return (void*)0; | |
55 | } | |
56 | ||
57 | p = malloc(size); | |
58 | if (p == (void*)0) fatal("malloc of %d at %d in %s", size, linen, filen); | |
59 | ||
60 | return p; | |
61 | } | |
62 | ||
63 | ||
64 | void * xcalloc(size_t num, size_t size, int linen, char * filen) | |
65 | { | |
66 | void *p; | |
67 | ||
68 | if (size<=0 || num<=0) | |
69 | fatal("xcalloc(%d,%d) : one of number or size is <= 0 at line %d of %s", | |
70 | num, size, linen, filen); | |
71 | ||
72 | p = calloc(num, size); | |
73 | if (p == (void*)0) fatal("calloc of %d of size %d at %d in %s", num, size, linen, filen); | |
74 | ||
75 | return p; | |
76 | } | |
77 | ||
78 | ||
79 | ||
80 | void xfree(void * p, int linen, char * filen) | |
81 | { | |
82 | if (p == (void*)0) { | |
83 | warning("xfree of null pointer at %d in %s", linen, filen); | |
84 | return; | |
85 | } | |
86 | ||
87 | free(p); | |
88 | } | |
89 | ||
90 | ||
91 | void * xrealloc(void * oldp, size_t size, int linen, char * filen) | |
92 | { | |
93 | void *p; | |
94 | ||
95 | if (size <= 0) { | |
96 | if (size == 0) { | |
97 | xfree(oldp, linen, filen); | |
98 | warning("xrealloc to zero size at %d in %s", linen, filen); | |
99 | return (void*)0; | |
100 | } | |
101 | ||
102 | fatal("xrealloc to negative size %d at %d in %s", size, linen, filen); | |
103 | } | |
104 | ||
105 | if (oldp == (void*)0) { | |
106 | p = malloc(size); | |
107 | } else { | |
108 | p = realloc(oldp, size); | |
109 | } | |
110 | ||
111 | if (p == (void*)0) fatal("xrealloc failed @ %d in %s", linen, filen); | |
112 | ||
113 | return p; | |
114 | } | |
115 | ||
116 | ||
117 | ||
118 | char * xstrdup(char * strp, int linen, char * filen) | |
119 | { | |
120 | char * p; | |
121 | ||
122 | p = strdup(strp); | |
123 | if (p == (char*)0) fatal("xstrdup @ %d in %s failed", linen, filen); | |
124 | ||
125 | return p; | |
126 | } | |
127 |