67 | | =item C<void * mem__internal_allocate(size_t size, const char *file, int line)> |
68 | | |
69 | | Calls C<malloc> to allocate memory from the system, Panics if there is no |
70 | | memory available. If C<DETAIL_MEMORY_DEBUG> macro is defined, prints |
71 | | debug information to C<STDERR>. |
72 | | |
73 | | =cut |
74 | | |
75 | | */ |
76 | | |
77 | | PARROT_MALLOC |
78 | | PARROT_CANNOT_RETURN_NULL |
79 | | void * |
80 | | mem__internal_allocate(size_t size, ARGIN(const char *file), int line) |
81 | | { |
82 | | ASSERT_ARGS(mem__internal_allocate) |
83 | | void * const ptr = malloc((size_t)size); |
84 | | #ifdef DETAIL_MEMORY_DEBUG |
85 | | fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", |
86 | | size, ptr, file, line); |
87 | | #else |
88 | | UNUSED(file); |
89 | | UNUSED(line); |
90 | | #endif |
91 | | if (!ptr) |
92 | | PANIC_OUT_OF_MEM(size); |
93 | | return ptr; |
94 | | } |
95 | | |
96 | | /* |
97 | | |
125 | | =item C<void * mem__internal_allocate_zeroed(size_t size, const char *file, int |
126 | | line)> |
127 | | |
128 | | Uses C<calloc> to allocate system memory. Guaranteed to succeed, Panics |
129 | | otherwise. If C<DETAIL_MEMORY_DEBUG> macro is defined, prints |
130 | | debug information to C<STDERR>. |
131 | | |
132 | | =cut |
133 | | |
134 | | */ |
135 | | |
136 | | PARROT_MALLOC |
137 | | PARROT_CANNOT_RETURN_NULL |
138 | | void * |
139 | | mem__internal_allocate_zeroed(size_t size, ARGIN(const char *file), int line) |
140 | | { |
141 | | ASSERT_ARGS(mem__internal_allocate_zeroed) |
142 | | void * const ptr = calloc(1, (size_t)size); |
143 | | #ifdef DETAIL_MEMORY_DEBUG |
144 | | fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", |
145 | | size, ptr, file, line); |
146 | | #else |
147 | | UNUSED(file); |
148 | | UNUSED(line); |
149 | | #endif |
150 | | if (!ptr) |
151 | | PANIC_OUT_OF_MEM(size); |
152 | | return ptr; |
153 | | } |
154 | | |
155 | | /* |
156 | | |
229 | | =item C<void * mem__internal_realloc(void *from, size_t size, const char *file, |
230 | | int line)> |
231 | | |
232 | | Resizes a chunk of system memory. Unlike C<realloc>, it can handle a |
233 | | NULL pointer, in which case a new memory block is allocated for the |
234 | | requested size. If C<DETAIL_MEMORY_DEBUG> macro is defined, debug |
235 | | information is printed to C<STDERR>. |
236 | | |
237 | | =cut |
238 | | |
239 | | */ |
240 | | |
241 | | PARROT_MALLOC |
242 | | PARROT_CANNOT_RETURN_NULL |
243 | | void * |
244 | | mem__internal_realloc(ARGFREE(void *from), size_t size, |
245 | | ARGIN(const char *file), int line) |
246 | | { |
247 | | ASSERT_ARGS(mem__internal_realloc) |
248 | | void * const ptr = realloc(from, size); |
249 | | #ifdef DETAIL_MEMORY_DEBUG |
250 | | fprintf(stderr, "internal free of %p (realloc -- %i bytes) (%s/%d)\n", |
251 | | from, size, file, line); |
252 | | fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", |
253 | | size, ptr, file, line); |
254 | | #else |
255 | | UNUSED(file); |
256 | | UNUSED(line); |
257 | | #endif |
258 | | if (!ptr) |
259 | | PANIC_OUT_OF_MEM(size); |
260 | | return ptr; |
261 | | } |
262 | | |
263 | | /* |
264 | | |
265 | | =item C<void * mem__internal_realloc_zeroed(void *from, size_t size, size_t |
266 | | old_size, const char *file, int line)> |
267 | | |
268 | | Reallocates a given buffer of size C<old_size> to C<size>. If the new size |
269 | | is larger then the old size, the difference is filled with zeros. Contains |
270 | | debugging information, and can print filename and line number where it is |
271 | | used if C<DETAIL_MEMORY_DEBUG> is defined. |
272 | | |
273 | | =cut |
274 | | |
275 | | */ |
276 | | |
277 | | PARROT_MALLOC |
278 | | PARROT_CANNOT_RETURN_NULL |
279 | | void * |
280 | | mem__internal_realloc_zeroed(ARGFREE(void *from), size_t size, size_t old_size, |
281 | | ARGIN(const char *file), int line) |
282 | | { |
283 | | ASSERT_ARGS(mem__internal_realloc_zeroed) |
284 | | void * const ptr = realloc(from, size); |
285 | | #ifdef DETAIL_MEMORY_DEBUG |
286 | | fprintf(stderr, "internal free of %p (realloc -- %i bytes) (%s/%d)\n", |
287 | | from, size, file, line); |
288 | | fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", |
289 | | size, ptr, file, line); |
290 | | #else |
291 | | UNUSED(file); |
292 | | UNUSED(line); |
293 | | #endif |
294 | | if (!ptr) |
295 | | PANIC_OUT_OF_MEM(size); |
296 | | if (size > old_size) |
297 | | memset((char*)ptr + old_size, 0, size - old_size); |
298 | | |
299 | | return ptr; |
300 | | } |
301 | | |
302 | | /* |
303 | | |
326 | | =item C<void mem__internal_free(void *from, const char *file, int line)> |
327 | | |
328 | | Frees a chunk of memory back to the system. If |
329 | | C<DETAIL_MEMORY_DEBUG> macro is defined, prints debug information to |
330 | | C<STDERR>. |
331 | | |
332 | | =cut |
333 | | |
334 | | */ |
335 | | |
336 | | void |
337 | | mem__internal_free(ARGFREE(void *from), ARGIN(const char *file), int line) |
338 | | { |
339 | | ASSERT_ARGS(mem__internal_free) |
340 | | #ifdef DETAIL_MEMORY_DEBUG |
341 | | fprintf(stderr, "Internal free of %p (%s/%d)\n", from, file, line); |
342 | | #else |
343 | | UNUSED(file); |
344 | | UNUSED(line); |
345 | | #endif |
346 | | free(from); |
347 | | } |
348 | | |
349 | | /* |
350 | | |