2 ===============================================================================
3 NEWALLOC - SDCC Memory allocation functions
5 These functions are wrappers for the standard malloc, realloc and free
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 In other words, you are welcome to use, share and improve this program.
24 You are forbidden to forbid anyone else to use, share and improve
25 what you give them. Help stamp out software-hoarding!
27 ===============================================================================
40 #define MALLOC GC_malloc
41 #define REALLOC GC_realloc
42 /* PENDING: This is a mild hack. If we try to GC_free something
43 allocated with malloc() then the program will segfault. Might as
44 well drop it and let the garbase collector take care of things.
51 #define REALLOC realloc
64 static int _allocs[TRACESIZE];
68 _dumpTrace(int code, void *parg)
71 for (i = 0; i < TRACESIZE; i++)
75 printf("%u %u\n", _allocs[i], i);
78 printf("%u above\n", _above);
84 static int registered;
88 on_exit(_dumpTrace, NULL);
96 if (size >= TRACESIZE)
108 -------------------------------------------------------------------------------
109 Clear_realloc - Reallocate a memory block and clear any memory added with
110 out of memory error detection
112 -------------------------------------------------------------------------------
115 void *Clear_realloc(void *OldPtr,size_t OldSize,size_t NewSize)
120 NewPtr = REALLOC(OldPtr,NewSize) ;
124 printf("ERROR - No more memory\n") ;
125 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
130 if (NewSize > OldSize)
131 memset((char *) NewPtr + OldSize,0x00,NewSize - OldSize) ;
136 -------------------------------------------------------------------------------
137 Safe_realloc - Reallocate a memory block with out of memory error detection
139 -------------------------------------------------------------------------------
142 void *Safe_realloc(void *OldPtr,size_t NewSize)
147 NewPtr = REALLOC(OldPtr,NewSize) ;
151 printf("ERROR - No more memory\n") ;
152 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
159 -------------------------------------------------------------------------------
160 Safe_calloc - Allocate a block of memory from the application heap, clearing
161 all data to zero and checking for out of memory errors.
163 -------------------------------------------------------------------------------
166 void *Safe_calloc(size_t Elements,size_t Size)
171 NewPtr = MALLOC(Elements*Size) ;
178 printf("ERROR - No more memory\n") ;
179 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
183 memset(NewPtr, 0, Elements*Size);
188 -------------------------------------------------------------------------------
189 Safe_malloc - Allocate a block of memory from the application heap
190 and checking for out of memory errors.
192 -------------------------------------------------------------------------------
195 void *Safe_malloc(size_t Size)
200 NewPtr = MALLOC(Size) ;
208 printf("ERROR - No more memory\n") ;
209 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
216 void *Safe_alloc(size_t Size)
218 return Safe_calloc(1, Size);
221 void Safe_free(void *p)
226 char *Safe_strdup(const char *sz)
231 pret = Safe_alloc(strlen(sz) +1);
237 void *traceAlloc(allocTrace *ptrace, void *p)
242 /* Also handles where max == 0 */
243 if (ptrace->num == ptrace->max)
245 /* Add an offset to handle max == 0 */
246 ptrace->max = (ptrace->max+2)*2;
247 ptrace->palloced = Safe_realloc(ptrace->palloced, ptrace->max * sizeof(*ptrace->palloced));
249 ptrace->palloced[ptrace->num++] = p;
254 void freeTrace(allocTrace *ptrace)
259 for (i = 0; i < ptrace->num; i++)
261 Safe_free(ptrace->palloced[i]);
265 Safe_free(ptrace->palloced);
266 ptrace->palloced = NULL;