6#ifndef PRISM_DEBUG_ALLOCATOR_H
7#define PRISM_DEBUG_ALLOCATOR_H
14pm_debug_malloc(
size_t size)
16 size_t *memory =
xmalloc(size +
sizeof(
size_t));
22pm_debug_calloc(
size_t nmemb,
size_t size)
24 size_t total_size = nmemb * size;
25 void *ptr = pm_debug_malloc(total_size);
26 memset(ptr, 0, total_size);
31pm_debug_realloc(
void *ptr,
size_t size)
34 return pm_debug_malloc(size);
37 size_t *memory = (
size_t *)ptr;
38 void *raw_memory = memory - 1;
39 memory = (
size_t *)
xrealloc(raw_memory, size +
sizeof(
size_t));
45pm_debug_free(
void *ptr)
48 size_t *memory = (
size_t *)ptr;
54pm_debug_free_sized(
void *ptr,
size_t old_size)
57 size_t *memory = (
size_t *)ptr;
58 if (old_size != memory[-1]) {
59 fprintf(stderr,
"[BUG] buffer %p was allocated with size %lu but freed with size %lu\n", ptr, memory[-1], old_size);
62 xfree_sized(memory - 1, old_size +
sizeof(
size_t));
67pm_debug_realloc_sized(
void *ptr,
size_t size,
size_t old_size)
71 fprintf(stderr,
"[BUG] realloc_sized called with NULL pointer and old size %lu\n", old_size);
74 return pm_debug_malloc(size);
77 size_t *memory = (
size_t *)ptr;
78 if (old_size != memory[-1]) {
79 fprintf(stderr,
"[BUG] buffer %p was allocated with size %lu but realloced with size %lu\n", ptr, memory[-1], old_size);
82 return pm_debug_realloc(ptr, size);
92#define xmalloc pm_debug_malloc
93#define xrealloc pm_debug_realloc
94#define xcalloc pm_debug_calloc
95#define xfree pm_debug_free
96#define xrealloc_sized pm_debug_realloc_sized
97#define xfree_sized pm_debug_free_sized
#define xfree
Old name of ruby_xfree.
#define xrealloc
Old name of ruby_xrealloc.
#define xmalloc
Old name of ruby_xmalloc.