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
53 #define REALLOC realloc
66 static int _allocs[TRACESIZE];
70 _dumpTrace(int code, void *parg)
73 for (i = 0; i < TRACESIZE; i++)
77 printf("%u %u\n", _allocs[i], i);
80 printf("%u above\n", _above);
86 static int registered;
90 on_exit(_dumpTrace, NULL);
98 if (size >= TRACESIZE)
110 -------------------------------------------------------------------------------
111 Clear_realloc - Reallocate a memory block and clear any memory added with
112 out of memory error detection
114 -------------------------------------------------------------------------------
117 void *Clear_realloc(void *OldPtr,size_t OldSize,size_t NewSize)
122 NewPtr = REALLOC(OldPtr,NewSize) ;
126 printf("ERROR - No more memory\n") ;
127 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
132 if (NewSize > OldSize)
133 memset((char *) NewPtr + OldSize,0x00,NewSize - OldSize) ;
138 -------------------------------------------------------------------------------
139 Safe_realloc - Reallocate a memory block with out of memory error detection
141 -------------------------------------------------------------------------------
144 void *Safe_realloc(void *OldPtr,size_t NewSize)
149 NewPtr = REALLOC(OldPtr,NewSize) ;
153 printf("ERROR - No more memory\n") ;
154 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
161 -------------------------------------------------------------------------------
162 Safe_calloc - Allocate a block of memory from the application heap, clearing
163 all data to zero and checking for out of memory errors.
165 -------------------------------------------------------------------------------
168 void *Safe_calloc(size_t Elements,size_t Size)
173 NewPtr = MALLOC(Elements*Size) ;
180 printf("ERROR - No more memory\n") ;
181 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
185 memset(NewPtr, 0, Elements*Size);
190 -------------------------------------------------------------------------------
191 Safe_malloc - Allocate a block of memory from the application heap
192 and checking for out of memory errors.
194 -------------------------------------------------------------------------------
197 void *Safe_malloc(size_t Size)
202 NewPtr = MALLOC(Size) ;
210 printf("ERROR - No more memory\n") ;
211 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
218 void *Safe_alloc(size_t Size)
220 return Safe_calloc(1, Size);
223 void Safe_free(void *p)
228 char *Safe_strdup(const char *sz)
233 pret = Safe_alloc(strlen(sz) +1);
239 void *traceAlloc(allocTrace *ptrace, void *p)
244 /* Also handles where max == 0 */
245 if (ptrace->num == ptrace->max)
247 /* Add an offset to handle max == 0 */
248 ptrace->max = (ptrace->max+2)*2;
249 ptrace->palloced = Safe_realloc(ptrace->palloced, ptrace->max * sizeof(*ptrace->palloced));
251 ptrace->palloced[ptrace->num++] = p;
256 void freeTrace(allocTrace *ptrace)
261 for (i = 0; i < ptrace->num; i++)
263 Safe_free(ptrace->palloced[i]);
267 Safe_free(ptrace->palloced);
268 ptrace->palloced = NULL;