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>
45 #define MALLOC GC_malloc
46 #define REALLOC GC_realloc
47 /* PENDING: This is a mild hack. If we try to GC_free something
48 allocated with malloc() then the program will segfault. Might as
49 well drop it and let the garbase collector take care of things.
56 #define REALLOC realloc
69 static int _allocs[TRACESIZE];
73 _dumpTrace(int code, void *parg)
76 for (i = 0; i < TRACESIZE; i++)
80 printf("%u %u\n", _allocs[i], i);
83 printf("%u above\n", _above);
89 static int registered;
93 on_exit(_dumpTrace, NULL);
101 if (size >= TRACESIZE)
113 -------------------------------------------------------------------------------
114 Clear_realloc - Reallocate a memory block and clear any memory added with
115 out of memory error detection
117 -------------------------------------------------------------------------------
120 void *Clear_realloc(void *OldPtr,size_t OldSize,size_t NewSize)
125 NewPtr = REALLOC(OldPtr,NewSize) ;
129 printf("ERROR - No more memory\n") ;
130 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
135 if (NewSize > OldSize)
136 memset((char *) NewPtr + OldSize,0x00,NewSize - OldSize) ;
141 -------------------------------------------------------------------------------
142 Safe_realloc - Reallocate a memory block with out of memory error detection
144 -------------------------------------------------------------------------------
147 void *Safe_realloc(void *OldPtr,size_t NewSize)
152 NewPtr = REALLOC(OldPtr,NewSize) ;
156 printf("ERROR - No more memory\n") ;
157 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
164 -------------------------------------------------------------------------------
165 Safe_calloc - Allocate a block of memory from the application heap, clearing
166 all data to zero and checking for out of memory errors.
168 -------------------------------------------------------------------------------
171 void *Safe_calloc(size_t Elements,size_t Size)
176 NewPtr = MALLOC(Elements*Size) ;
183 printf("ERROR - No more memory\n") ;
184 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
188 memset(NewPtr, 0, Elements*Size);
193 -------------------------------------------------------------------------------
194 Safe_malloc - Allocate a block of memory from the application heap
195 and checking for out of memory errors.
197 -------------------------------------------------------------------------------
200 void *Safe_malloc(size_t Size)
205 NewPtr = MALLOC(Size) ;
213 printf("ERROR - No more memory\n") ;
214 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
221 void *Safe_alloc(size_t Size)
223 return Safe_calloc(1, Size);
226 void Safe_free(void *p)
231 char *Safe_strdup(const char *sz)
236 pret = Safe_alloc(strlen(sz) +1);
242 void *traceAlloc(allocTrace *ptrace, void *p)
247 /* Also handles where max == 0 */
248 if (ptrace->num == ptrace->max)
250 /* Add an offset to handle max == 0 */
251 ptrace->max = (ptrace->max+2)*2;
252 ptrace->palloced = Safe_realloc(ptrace->palloced, ptrace->max * sizeof(*ptrace->palloced));
254 ptrace->palloced[ptrace->num++] = p;
259 void freeTrace(allocTrace *ptrace)
264 for (i = 0; i < ptrace->num; i++)
266 Safe_free(ptrace->palloced[i]);
270 Safe_free(ptrace->palloced);
271 ptrace->palloced = NULL;