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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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