1#ifndef PRISM_INTERNAL_ALLOCATOR_DEBUG_H
2#define PRISM_INTERNAL_ALLOCATOR_DEBUG_H
9pm_allocator_debug_malloc(
size_t size) {
10 size_t *memory =
xmalloc(size +
sizeof(
size_t));
16pm_allocator_debug_calloc(
size_t nmemb,
size_t size) {
17 size_t total_size = nmemb * size;
18 void *ptr = pm_allocator_debug_malloc(total_size);
19 memset(ptr, 0, total_size);
24pm_allocator_debug_realloc(
void *ptr,
size_t size) {
26 return pm_allocator_debug_malloc(size);
29 size_t *memory = (
size_t *)ptr;
30 void *raw_memory = memory - 1;
31 memory = (
size_t *)
xrealloc(raw_memory, size +
sizeof(
size_t));
37pm_allocator_debug_free(
void *ptr) {
39 size_t *memory = (
size_t *)ptr;
45pm_allocator_debug_free_sized(
void *ptr,
size_t old_size) {
47 size_t *memory = (
size_t *)ptr;
48 if (old_size != memory[-1]) {
49 fprintf(stderr,
"[BUG] buffer %p was allocated with size %lu but freed with size %lu\n", ptr, memory[-1], old_size);
52 xfree_sized(memory - 1, old_size +
sizeof(
size_t));
57pm_allocator_debug_realloc_sized(
void *ptr,
size_t size,
size_t old_size) {
60 fprintf(stderr,
"[BUG] realloc_sized called with NULL pointer and old size %lu\n", old_size);
63 return pm_allocator_debug_malloc(size);
66 size_t *memory = (
size_t *)ptr;
67 if (old_size != memory[-1]) {
68 fprintf(stderr,
"[BUG] buffer %p was allocated with size %lu but realloced with size %lu\n", ptr, memory[-1], old_size);
71 return pm_allocator_debug_realloc(ptr, size);
81#define xmalloc pm_allocator_debug_malloc
82#define xrealloc pm_allocator_debug_realloc
83#define xcalloc pm_allocator_debug_calloc
84#define xfree pm_allocator_debug_free
85#define xrealloc_sized pm_allocator_debug_realloc_sized
86#define xfree_sized pm_allocator_debug_free_sized
#define xfree
Old name of ruby_xfree.
#define xrealloc
Old name of ruby_xrealloc.
#define xmalloc
Old name of ruby_xmalloc.