Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / util / NewAlloc.c
blobef34c4d04348ba31c2d6e5792e890bea953710ab
1 /*-------------------------------------------------------------------------
2 newalloc.c - SDCC Memory allocation functions
4 These functions are wrappers for the standard malloc, realloc and free
5 functions.
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 -------------------------------------------------------------------------*/
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <memory.h>
26 #include <assert.h>
27 #include "newalloc.h"
29 #if OPT_ENABLE_LIBGC
30 #include <gc/gc.h>
32 #define MALLOC GC_malloc
33 #define REALLOC GC_realloc
34 /* PENDING: This is a mild hack. If we try to GC_free something
35 allocated with malloc() then the program will segfault. Might as
36 well drop it and let the garbase collector take care of things.
38 #define FREE(_a)
40 #else
42 #define MALLOC malloc
43 #define REALLOC realloc
44 #define FREE free
46 #endif
48 #define TRACEMALLOC 0
50 #if TRACEMALLOC
51 enum
53 TRACESIZE = 4096
56 static int _allocs[TRACESIZE];
57 static int _above;
59 static void
60 _dumpTrace (int code, void *parg)
62 int i;
63 for (i = 0; i < TRACESIZE; i++)
65 if (_allocs[i])
67 printf ("%u %u\n", _allocs[i], i);
70 printf ("%u above\n", _above);
73 static void
74 _log (int size)
76 static int registered;
78 if (registered == 0)
80 on_exit (_dumpTrace, NULL);
81 registered = 1;
83 if (size == 12)
85 _above++;
88 if (size >= TRACESIZE)
90 _above++;
92 else
94 _allocs[size]++;
97 #endif
100 -------------------------------------------------------------------------------
101 Clear_realloc - Reallocate a memory block and clear any memory added with
102 out of memory error detection
104 -------------------------------------------------------------------------------
107 void *
108 Clear_realloc (void *OldPtr, size_t OldSize, size_t NewSize)
110 void *NewPtr;
112 NewPtr = REALLOC (OldPtr, NewSize);
114 if (!NewPtr)
116 printf ("ERROR - No more memory\n");
117 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
118 exit (1);
121 if (NewPtr)
122 if (NewSize > OldSize)
123 memset ((char *) NewPtr + OldSize, 0x00, NewSize - OldSize);
125 return NewPtr;
129 -------------------------------------------------------------------------------
130 Safe_realloc - Reallocate a memory block with out of memory error detection
132 -------------------------------------------------------------------------------
135 void *
136 Safe_realloc (void *OldPtr, size_t NewSize)
138 void *NewPtr;
140 NewPtr = REALLOC (OldPtr, NewSize);
142 if (!NewPtr)
144 printf ("ERROR - No more memory\n");
145 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
146 exit (1);
149 return NewPtr;
153 -------------------------------------------------------------------------------
154 Safe_calloc - Allocate a block of memory from the application heap, clearing
155 all data to zero and checking for out of memory errors.
157 -------------------------------------------------------------------------------
160 void *
161 Safe_calloc (size_t Elements, size_t Size)
163 void *NewPtr;
165 NewPtr = MALLOC (Elements * Size);
166 #if TRACEMALLOC
167 _log (Elements * Size);
168 #endif
170 if (!NewPtr)
172 printf ("ERROR - No more memory\n");
173 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
174 exit (1);
177 memset (NewPtr, 0, Elements * Size);
179 return NewPtr;
183 -------------------------------------------------------------------------------
184 Safe_malloc - Allocate a block of memory from the application heap
185 and checking for out of memory errors.
187 -------------------------------------------------------------------------------
190 void *
191 Safe_malloc (size_t Size)
193 void *NewPtr;
195 NewPtr = MALLOC (Size);
197 #if TRACEMALLOC
198 _log (Size);
199 #endif
201 if (!NewPtr)
203 printf ("ERROR - No more memory\n");
204 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
205 exit (1);
208 return NewPtr;
211 void *
212 Safe_alloc (size_t Size)
214 return Safe_calloc (1, Size);
217 void
218 Safe_free (void *p)
220 FREE (p);
223 char *
224 Safe_strndup (const char *sz, size_t size)
226 char *pret;
227 assert (sz);
229 pret = Safe_alloc (size + 1);
230 strncpy (pret, sz, size);
231 pret[size] = '\0';
233 return pret;
236 char *
237 Safe_strdup (const char *sz)
239 assert (sz);
241 return Safe_strndup (sz, strlen (sz));
244 void *
245 traceAlloc (allocTrace * ptrace, void *p)
247 assert (ptrace);
248 assert (p);
250 /* Also handles where max == 0 */
251 if (ptrace->num == ptrace->max)
253 /* Add an offset to handle max == 0 */
254 ptrace->max = (ptrace->max + 2) * 2;
255 ptrace->palloced = Safe_realloc (ptrace->palloced, ptrace->max * sizeof (*ptrace->palloced));
257 ptrace->palloced[ptrace->num++] = p;
259 return p;
262 void
263 freeTrace (allocTrace * ptrace)
265 int i;
266 assert (ptrace);
268 for (i = 0; i < ptrace->num; i++)
270 Safe_free (ptrace->palloced[i]);
272 ptrace->num = 0;
274 Safe_free (ptrace->palloced);
275 ptrace->palloced = NULL;
276 ptrace->max = 0;