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 ===============================================================================
31 #if defined(__APPLE__) && defined(__MACH__)
32 #include <sys/malloc.h>
46 #define MALLOC GC_malloc
47 #define REALLOC GC_realloc
48 /* PENDING: This is a mild hack. If we try to GC_free something
49 allocated with malloc() then the program will segfault. Might as
50 well drop it and let the garbase collector take care of things.
57 #define REALLOC realloc
70 static int _allocs[TRACESIZE];
74 _dumpTrace(int code, void *parg)
77 for (i = 0; i < TRACESIZE; i++)
81 printf("%u %u\n", _allocs[i], i);
84 printf("%u above\n", _above);
90 static int registered;
94 on_exit(_dumpTrace, NULL);
102 if (size >= TRACESIZE)
114 -------------------------------------------------------------------------------
115 Clear_realloc - Reallocate a memory block and clear any memory added with
116 out of memory error detection
118 -------------------------------------------------------------------------------
121 void *Clear_realloc(void *OldPtr,size_t OldSize,size_t NewSize)
126 NewPtr = REALLOC(OldPtr,NewSize) ;
130 printf("ERROR - No more memory\n") ;
131 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
136 if (NewSize > OldSize)
137 memset((char *) NewPtr + OldSize,0x00,NewSize - OldSize) ;
142 -------------------------------------------------------------------------------
143 Safe_realloc - Reallocate a memory block with out of memory error detection
145 -------------------------------------------------------------------------------
148 void *Safe_realloc(void *OldPtr,size_t NewSize)
153 NewPtr = REALLOC(OldPtr,NewSize) ;
157 printf("ERROR - No more memory\n") ;
158 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
165 -------------------------------------------------------------------------------
166 Safe_calloc - Allocate a block of memory from the application heap, clearing
167 all data to zero and checking for out of memory errors.
169 -------------------------------------------------------------------------------
172 void *Safe_calloc(size_t Elements,size_t Size)
177 NewPtr = MALLOC(Elements*Size) ;
184 printf("ERROR - No more memory\n") ;
185 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
189 memset(NewPtr, 0, Elements*Size);
194 -------------------------------------------------------------------------------
195 Safe_malloc - Allocate a block of memory from the application heap
196 and checking for out of memory errors.
198 -------------------------------------------------------------------------------
201 void *Safe_malloc(size_t Size)
206 NewPtr = MALLOC(Size) ;
214 printf("ERROR - No more memory\n") ;
215 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
222 void *Safe_alloc(size_t Size)
224 return Safe_calloc(1, Size);
227 void Safe_free(void *p)
232 char *Safe_strdup(const char *sz)
237 pret = Safe_alloc(strlen(sz) +1);
243 void *traceAlloc(allocTrace *ptrace, void *p)
248 /* Also handles where max == 0 */
249 if (ptrace->num == ptrace->max)
251 /* Add an offset to handle max == 0 */
252 ptrace->max = (ptrace->max+2)*2;
253 ptrace->palloced = Safe_realloc(ptrace->palloced, ptrace->max * sizeof(*ptrace->palloced));
255 ptrace->palloced[ptrace->num++] = p;
260 void freeTrace(allocTrace *ptrace)
265 for (i = 0; i < ptrace->num; i++)
267 Safe_free(ptrace->palloced[i]);
271 Safe_free(ptrace->palloced);
272 ptrace->palloced = NULL;