1 /*-------------------------------------------------------------------------
2 newalloc.c - SDCC Memory allocation functions
4 These functions are wrappers for the standard malloc, realloc and free
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
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 -------------------------------------------------------------------------*/
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.
43 #define REALLOC realloc
56 static int _allocs
[TRACESIZE
];
60 _dumpTrace (int code
, void *parg
)
63 for (i
= 0; i
< TRACESIZE
; i
++)
67 printf ("%u %u\n", _allocs
[i
], i
);
70 printf ("%u above\n", _above
);
76 static int registered
;
80 on_exit (_dumpTrace
, NULL
);
88 if (size
>= TRACESIZE
)
100 -------------------------------------------------------------------------------
101 Clear_realloc - Reallocate a memory block and clear any memory added with
102 out of memory error detection
104 -------------------------------------------------------------------------------
108 Clear_realloc (void *OldPtr
, size_t OldSize
, size_t NewSize
)
112 NewPtr
= REALLOC (OldPtr
, NewSize
);
116 printf ("ERROR - No more memory\n");
117 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
122 if (NewSize
> OldSize
)
123 memset ((char *) NewPtr
+ OldSize
, 0x00, NewSize
- OldSize
);
129 -------------------------------------------------------------------------------
130 Safe_realloc - Reallocate a memory block with out of memory error detection
132 -------------------------------------------------------------------------------
136 Safe_realloc (void *OldPtr
, size_t NewSize
)
140 NewPtr
= REALLOC (OldPtr
, NewSize
);
144 printf ("ERROR - No more memory\n");
145 /* werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
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 -------------------------------------------------------------------------------
161 Safe_calloc (size_t Elements
, size_t Size
)
165 NewPtr
= MALLOC (Elements
* Size
);
167 _log (Elements
* Size
);
172 printf ("ERROR - No more memory\n");
173 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
177 memset (NewPtr
, 0, Elements
* Size
);
183 -------------------------------------------------------------------------------
184 Safe_malloc - Allocate a block of memory from the application heap
185 and checking for out of memory errors.
187 -------------------------------------------------------------------------------
191 Safe_malloc (size_t Size
)
195 NewPtr
= MALLOC (Size
);
203 printf ("ERROR - No more memory\n");
204 /* werror(E_OUT_OF_MEM,__FILE__,Size);*/
212 Safe_alloc (size_t Size
)
214 return Safe_calloc (1, Size
);
224 Safe_strndup (const char *sz
, size_t size
)
229 pret
= Safe_alloc (size
+ 1);
230 strncpy (pret
, sz
, size
);
237 Safe_strdup (const char *sz
)
241 return Safe_strndup (sz
, strlen (sz
));
245 traceAlloc (allocTrace
* ptrace
, void *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
;
263 freeTrace (allocTrace
* ptrace
)
268 for (i
= 0; i
< ptrace
->num
; i
++)
270 Safe_free (ptrace
->palloced
[i
]);
274 Safe_free (ptrace
->palloced
);
275 ptrace
->palloced
= NULL
;