|
| 1 | +/** |
| 2 | + * @file debug_allocator.h |
| 3 | + * |
| 4 | + * Decorate allocation function to ensure sizes are correct. |
| 5 | + */ |
| 6 | +#ifndefPRISM_DEBUG_ALLOCATOR_H |
| 7 | +#definePRISM_DEBUG_ALLOCATOR_H |
| 8 | + |
| 9 | +#include<string.h> |
| 10 | +#include<stdio.h> |
| 11 | +#include<stdlib.h> |
| 12 | + |
| 13 | +staticinlinevoid* |
| 14 | +pm_debug_malloc(size_tsize) |
| 15 | +{ |
| 16 | +size_t*memory=xmalloc(size+sizeof(size_t)); |
| 17 | +memory[0] =size; |
| 18 | +returnmemory+1; |
| 19 | +} |
| 20 | + |
| 21 | +staticinlinevoid* |
| 22 | +pm_debug_calloc(size_tnmemb, size_tsize) |
| 23 | +{ |
| 24 | +size_ttotal_size=nmemb*size; |
| 25 | +void*ptr=pm_debug_malloc(total_size); |
| 26 | +memset(ptr, 0, total_size); |
| 27 | +returnptr; |
| 28 | +} |
| 29 | + |
| 30 | +staticinlinevoid* |
| 31 | +pm_debug_realloc(void*ptr, size_tsize) |
| 32 | +{ |
| 33 | +if (ptr==NULL) { |
| 34 | +returnpm_debug_malloc(size); |
| 35 | + } |
| 36 | + |
| 37 | +size_t*memory= (size_t*)ptr; |
| 38 | +void*raw_memory=memory-1; |
| 39 | +memory= (size_t*)xrealloc(raw_memory, size+sizeof(size_t)); |
| 40 | +memory[0] =size; |
| 41 | +returnmemory+1; |
| 42 | +} |
| 43 | + |
| 44 | +staticinlinevoid |
| 45 | +pm_debug_free(void*ptr) |
| 46 | +{ |
| 47 | +if (ptr!=NULL) { |
| 48 | +size_t*memory= (size_t*)ptr; |
| 49 | +xfree(memory-1); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +staticinlinevoid |
| 54 | +pm_debug_free_sized(void*ptr, size_told_size) |
| 55 | +{ |
| 56 | +if (ptr!=NULL) { |
| 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); |
| 60 | +abort(); |
| 61 | + } |
| 62 | +xfree_sized(memory-1, old_size+sizeof(size_t)); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +staticinlinevoid* |
| 67 | +pm_debug_realloc_sized(void*ptr, size_tsize, size_told_size) |
| 68 | +{ |
| 69 | +if (ptr==NULL) { |
| 70 | +if (old_size!=0) { |
| 71 | +fprintf(stderr, "[BUG] realloc_sized called with NULL pointer and old size %lu\n", old_size); |
| 72 | +abort(); |
| 73 | + } |
| 74 | +returnpm_debug_malloc(size); |
| 75 | + } |
| 76 | + |
| 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); |
| 80 | +abort(); |
| 81 | + } |
| 82 | +returnpm_debug_realloc(ptr, size); |
| 83 | +} |
| 84 | + |
| 85 | +#undef xmalloc |
| 86 | +#undef xrealloc |
| 87 | +#undef xcalloc |
| 88 | +#undef xfree |
| 89 | +#undef xrealloc_sized |
| 90 | +#undef xfree_sized |
| 91 | + |
| 92 | +#definexmalloc pm_debug_malloc |
| 93 | +#definexrealloc pm_debug_realloc |
| 94 | +#definexcalloc pm_debug_calloc |
| 95 | +#definexfree pm_debug_free |
| 96 | +#definexrealloc_sized pm_debug_realloc_sized |
| 97 | +#definexfree_sized pm_debug_free_sized |
| 98 | + |
| 99 | +#endif |
0 commit comments