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>
47 #define MALLOC GC_malloc
48 #define REALLOC GC_realloc
49 /* PENDING: This is a mild hack. If we try to GC_free something
50 allocated with malloc() then the program will segfault. Might as
51 well drop it and let the garbase collector take care of things.
58 #define REALLOC realloc
71 static int _allocs[TRACESIZE];
75 _dumpTrace(int code, void *parg)
78 for (i = 0; i < TRACESIZE; i++)
82 printf("%u %u\n", _allocs[i], i);
85 printf("%u above\n", _above);
91 static int registered;
95 on_exit(_dumpTrace, NULL);
103 if (size >= TRACESIZE)
115 -------------------------------------------------------------------------------
116 Clear_realloc - Reallocate a memory block and clear any memory added with
117 out of memory error detection
119 -------------------------------------------------------------------------------
122 void *Clear_realloc(void *OldPtr,size_t OldSize,size_t NewSize)
127 NewPtr = REALLOC(OldPtr,NewSize) ;
131 printf("ERROR - No more memory\n") ;
132 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
137 if (NewSize > OldSize)
138 memset((char *) NewPtr + OldSize,0x00,NewSize - OldSize) ;
143 -------------------------------------------------------------------------------
144 Safe_realloc - Reallocate a memory block with out of memory error detection
146 -------------------------------------------------------------------------------
149 void *Safe_realloc(void *OldPtr,size_t NewSize)
154 NewPtr = REALLOC(OldPtr,NewSize) ;
158 printf("ERROR - No more memory\n") ;
159 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
166 -------------------------------------------------------------------------------
167 Safe_calloc - Allocate a block of memory from the application heap, clearing
168 all data to zero and checking for out of memory errors.
170 -------------------------------------------------------------------------------
173 void *Safe_calloc(size_t Elements,size_t Size)
178 NewPtr = MALLOC(Elements*Size) ;
185 printf("ERROR - No more memory\n") ;
186 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
190 memset(NewPtr, 0, Elements*Size);
195 -------------------------------------------------------------------------------
196 Safe_malloc - Allocate a block of memory from the application heap
197 and checking for out of memory errors.
199 -------------------------------------------------------------------------------
202 void *Safe_malloc(size_t Size)
207 NewPtr = MALLOC(Size) ;
215 printf("ERROR - No more memory\n") ;
216 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
223 void *Safe_alloc(size_t Size)
225 return Safe_calloc(1, Size);
228 void Safe_free(void *p)
233 char *Safe_strdup(const char *sz)
238 pret = Safe_alloc(strlen(sz) +1);
244 void *traceAlloc(allocTrace *ptrace, void *p)
249 /* Also handles where max == 0 */
250 if (ptrace->num == ptrace->max)
252 /* Add an offset to handle max == 0 */
253 ptrace->max = (ptrace->max+2)*2;
254 ptrace->palloced = Safe_realloc(ptrace->palloced, ptrace->max * sizeof(*ptrace->palloced));
256 ptrace->palloced[ptrace->num++] = p;
261 void freeTrace(allocTrace *ptrace)
266 for (i = 0; i < ptrace->num; i++)
268 Safe_free(ptrace->palloced[i]);
272 Safe_free(ptrace->palloced);
273 ptrace->palloced = NULL;