/[ascend]/trunk/base/generic/utilities/ascMalloc.h
ViewVC logotype

Contents of /trunk/base/generic/utilities/ascMalloc.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 16 - (show annotations) (download) (as text)
Thu Nov 25 22:52:42 2004 UTC (19 years, 6 months ago) by ben.allan
File MIME type: text/x-chdr
File size: 9366 byte(s)
replaced nonportable strdup with ascend equivalent.
1 /*
2 * Ascend Memory Allocation Routines
3 * by Tom Epperly
4 * Created: 2/6/90
5 * Version: $Revision: 1.2 $
6 * Version control file: $RCSfile: ascMalloc.h,v $
7 * Date last modified: $Date: 1997/07/18 11:44:50 $
8 * Last modified by: $Author: mthomas $
9 *
10 * This file is part of the Ascend Language Interpreter.
11 *
12 * Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly
13 *
14 * The Ascend Language Interpreter is free software; you can redistribute
15 * it and/or modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * The Ascend Language Interpreter is distributed in hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with the program; if not, write to the Free Software Foundation,
26 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
27 * COPYING.
28 *
29 */
30
31 #ifndef __ASCMALLOC_H_SEEN__
32 #define __ASCMALLOC_H_SEEN__
33
34
35 extern char *ascstrdup(char *s);
36 /*^ as strdup. */
37
38 extern char *asc_memcpy(char *,char *,unsigned int);
39 /*
40 * char *memcpy(s1, s2, n)
41 * char *s1, *s2;
42 * int n;
43 * memcpy() copies n characters from memory area s2 to s1. It
44 * returns s1. This version of memcpy handles overlapping memory
45 * ranges ok. It could be more efficient internally. As it is, it
46 * moves data a char at a time.
47 */
48
49 extern char *ascreallocPUREF(char *,size_t,size_t);
50 /*
51 * char *ascreallocPUREF(ptr,oldbytes,newbytes)
52 * char *ptr;
53 * size_t oldbytes,newbytes;
54 * Behaves as realloc, except you must tell us what the old size was
55 * and the OS realloc doesn't get used.
56 * For debugging only.
57 */
58
59 extern void ascstatus(CONST char *);
60 /*
61 * void ascstatus(msg)
62 * const char *msg;
63 * If MALLOC_DEBUG is not defined, this does nothing.
64 * Otherwise, it prints the message followed by a newline to the log and
65 * to the screen. It then prints various statistics about virtual memory
66 * usuage to the screen and to the log.
67 */
68
69 extern void ascshutdown(CONST char *);
70 /*
71 * void ascshutdown(CONST char *)
72 * const char *msg;
73 * If MALLOC_DEBUG is not defined, this does nothing.
74 * Otherwise, it does what ascstatus does plus, it lists blocks of memory
75 * that are still allocated.
76 */
77
78 extern unsigned long ascmeminuse();
79 /*
80 * The memory total reporting function is ascmeminuse().
81 * The following macros have the listed effects if they are defined at
82 * C compile time.
83 *
84 * MOD_ASCMALLOC patches around a bug in OSF, AIX allocators
85 * MALLOC_DEBUG runs a tracking file of all ascmalloc functions
86 * ALLOCATED_TESTS forces a lot of extra assertions when added with
87 * the MALLOC_DEBUG macro.
88 *
89 * If MALLOC_DEBUG is undefined, we will report 0 memory allocated.
90 */
91
92 /*
93 * all the bcopy/bzero functions have been replaced with their mem
94 * counterparts for ansi compliance
95 */
96
97 /*
98 * The following define is for debugging purposes only.
99 * In some OS, realloc fools purify into thinking there
100 * is a memory leak.
101 * If MOD_REALLOC is defined at compile time for all
102 * software referencing this header, then all occurences
103 * of ascrealloc will use a homegrown realloc (which is
104 * somewhat more expensive than most system supplied ones)
105 * that does not fool purify.
106 * Leaks of memory reported around realloc when MOD_REALLOC
107 * is defined should be real leaks and not OS noise.
108 * MALLOC_DEBUG overrides MOD_REALLOC, but we never use
109 * MALLOC_DEBUG and purify together.
110 * The homegrown realloc needs more information (the oldsize).
111 * ascreallocPURE should not be used on a regular basis.
112 */
113 #ifndef MOD_REALLOC
114 #define ascreallocPURE(p,nold,nnew) ascrealloc((p),(nnew))
115 #else
116 #define ascreallocPURE(p,nold,nnew) ascreallocPUREF((p),(nold),(nnew))
117 #endif
118
119 /*
120 * The next line switches between the normal ascmalloc.h and the
121 * alpha and rs6000 ascmalloc declarations.
122 */
123 #ifndef MOD_ASCMALLOC
124 /* heres the normal version: */
125 #ifdef MALLOC_DEBUG
126 #define asccalloc(p,q) \
127 asccallocf(p,q,__FILE__,__LINE__)
128 extern VOIDPTR asccallocf(unsigned,unsigned,CONST char *,int);
129
130 #define ascmalloc(p) \
131 ascmallocf(p,__FILE__,__LINE__)
132 extern VOIDPTR ascmallocf(unsigned,CONST char *,int);
133
134 #define ascrealloc(p,q) \
135 ascreallocf(p,q,__FILE__,__LINE__)
136 extern VOIDPTR ascreallocf(VOIDPTR,unsigned,CONST char *,int);
137
138 #define ascfree(p) \
139 ascfreef(p,__FILE__,__LINE__)
140 extern void ascfreef(VOIDPTR,CONST char *,int);
141
142 #define ascbcopy(p,q,r) \
143 ascbcopyf(p,q,r,__FILE__,__LINE__)
144 extern void ascbcopyf(CONST VOIDPTR,VOIDPTR,int,CONST char *,int);
145
146 #define ascbzero(p,q) \
147 ascbzerof(p,q,__FILE__,__LINE__)
148 extern void ascbzerof(VOIDPTR,int,CONST char *,int);
149
150 #define ascbfill(p,q) memset((char *)p,255,q)
151
152 extern int AllocatedMemory(CONST VOIDPTR,unsigned);
153 /*
154 * return values
155 * 0 no memory is used
156 * 1 the memory block is wholly contained in an allocated block
157 * 2 the memory block equals a element of the memory list
158 * -1 the memory block is partially contained in an allocated block
159 */
160
161 extern int InMemoryBlock(CONST VOIDPTR,CONST VOIDPTR);
162 /*
163 * extern int InMemoryBlock(ptr1,ptr2)
164 * Return true if ptr2 is in the memory block headed by ptr1, otherwise
165 * return false.
166 */
167
168 #ifdef ALLOCATED_TESTS
169 #define AssertAllocatedMemory(p,q) \
170 assert(AllocatedMemory((VOIDPTR)(p),(unsigned)(q))==2)
171
172 #define AssertMemory(p) \
173 assert(AllocatedMemory((VOIDPTR)(p),0))
174
175 #define AssertContainedMemory(p,q) \
176 assert(AllocatedMemory((VOIDPTR)(p),(unsigned)(q))>0)
177
178 #define AssertContainedIn(p,q) \
179 assert(InMemoryBlock((VOIDPTR)(p),(VOIDPTR)(q)))
180 #else /* ALLOCATED_TESTS */
181 #define AssertAllocatedMemory(p,q)
182
183 #define AssertMemory(p)
184
185 #define AssertContainedMemory(p,q)
186
187 #define AssertContainedIn(p,q)
188 #endif /* ALLOCATED_TESTS */
189 #else /* MALLOC_DEBUG */
190
191 #define ascmalloc(x) malloc(x)
192
193 #if (defined(sun) && !defined(__SVR4))
194 #define ascrealloc(x,y) ((x)!=NULL ? realloc(x,y) : malloc(y))
195 #else
196 #define ascrealloc(x,y) realloc(x,y)
197 #endif
198
199 #define ascfree(x) free(x)
200
201 #define asccalloc(p,q) calloc(p,q)
202
203 #define ascbcopy(p,q,r) memcpy((void *)q,(void *)p,r)
204
205 #define ascbzero(p,q) memset((char *)p,0,q)
206
207 #define ascbfill(p,q) memset((char *)p,255,q)
208
209 #define AllocatedMemory(p,q) (1)
210
211 #define InMemoryBlock(p,q) (1)
212
213 #define AssertAllocatedMemory(p,q)
214
215 #define AssertMemory(p)
216
217 #define AssertContainedMemory(p,q)
218
219 #define AssertContainedIn(p,q)
220 #endif /* MALLOC_DEBUG */
221 #else /* MOD_ASCMALLOC */
222 /* here starts the modified version of ascmalloc headers */
223 #ifndef MAX
224 #define MAX(x,y) (((x) > (y)) ? (x) : (y))
225 #endif
226
227 #ifdef MALLOC_DEBUG
228 #define asccalloc(p,q) \
229 asccallocf(MAX(p,1),q,__FILE__,__LINE__)
230 extern VOIDPTR asccallocf(unsigned,unsigned,CONST char *,int);
231
232 #define ascmalloc(p) \
233 ascmallocf(MAX(p,1),__FILE__,__LINE__)
234 extern VOIDPTR ascmallocf(unsigned,CONST char *,int);
235
236 #define ascrealloc(p,q) \
237 ascreallocf(p,MAX(q,1),__FILE__,__LINE__)
238 extern VOIDPTR ascreallocf(VOIDPTR,unsigned,CONST char *,int);
239
240 #define ascfree(p) \
241 ascfreef(p,__FILE__,__LINE__)
242 extern void ascfreef(VOIDPTR,CONST char *,int);
243
244 #define ascbcopy(p,q,r) \
245 ascbcopyf(p,q,r,__FILE__,__LINE__)
246 extern void ascbcopyf(CONST VOIDPTR,VOIDPTR,int,CONST char *,int);
247
248 #define ascbzero(p,q) \
249 ascbzerof(p,q,__FILE__,__LINE__)
250 extern void ascbzerof(VOIDPTR,int,CONST char *,int);
251
252 #define ascbfill(p,q) memset((char *)p,255,q)
253
254 extern int AllocatedMemory(CONST VOIDPTR,unsigned);
255 /*
256 * return values
257 * 0 no memory is used
258 * 1 the memory block is wholly contained in an allocated block
259 * 2 the memory block equals a element of the memory list
260 * -1 the memory block is partially contained in an allocated block
261 */
262
263 extern int InMemoryBlock(CONST VOIDPTR,CONST VOIDPTR);
264 /*
265 * extern int InMemoryBlock(ptr1,ptr2)
266 * Return true if ptr2 is in the memory block headed by ptr1, otherwise
267 * return false.
268 */
269
270 #ifdef ALLOCATED_TESTS
271 #define AssertAllocatedMemory(p,q) \
272 assert(AllocatedMemory((VOIDPTR)(p),(unsigned)(MAX(q,1)))==2)
273
274 #define AssertMemory(p) \
275 assert(AllocatedMemory((VOIDPTR)(p),0))
276
277 #define AssertContainedMemory(p,q) \
278 assert(AllocatedMemory((VOIDPTR)(p),(unsigned)(q))>0)
279
280 #define AssertContainedIn(p,q) \
281 assert(InMemoryBlock((VOIDPTR)(p),(VOIDPTR)(q)))
282 #else /* ALLOCATED_TESTS */
283 #define AssertAllocatedMemory(p,q)
284
285 #define AssertMemory(p)
286
287 #define AssertContainedMemory(p,q)
288
289 #define AssertContainedIn(p,q)
290 #endif /* ALLOCATED_TESTS */
291 #else /* MALLOC_DEBUG */
292
293 #define ascmalloc(x) malloc(MAX(x,1))
294
295 #define ascrealloc(x,y) realloc(x,MAX(y,1))
296
297 #define ascfree(x) free(x)
298
299 #define asccalloc(p,q) calloc(MAX(p,1),q)
300
301 #define ascbcopy(p,q,r) memcpy((void *)q,(void *)p,r)
302
303 #define ascbzero(p,q) memset((char *)p,0,q)
304
305 #define ascbfill(p,q) memset((char *)p,255,q)
306
307 #define AllocatedMemory(p,q) (1)
308
309 #define InMemoryBlock(p,q) (1)
310
311 #define AssertAllocatedMemory(p,q)
312
313 #define AssertMemory(p)
314
315 #define AssertContainedMemory(p,q)
316
317 #define AssertContainedIn(p,q)
318 #endif /* MALLOC_DEBUG */
319 #endif /* MOD_ASCMALLOC */
320 #endif /* __ASCMALLOC_H_SEEN__ */

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22