12#define GUARD_SIZE (sizeof(size_t)*3)
13#define PREFIX_SIZE (sizeof(size_t)*2)
14#define CHECK1 ((size_t)0xDEADBEEF)
15#define CHECK2 ((size_t)0x600DCAFE)
17#ifndef MAX_ALLOC_BYTES
18#define MAX_ALLOC_BYTES 16*1024*1024
43 return (
size + 1023) / 1024 * 1024;
65 ((
size_t*)buf)[0] =
size;
66 ((
size_t*)buf)[1] =
CHECK1;
86 size_t size = ((
size_t*)buf)[0];
88 assert(((
size_t*)buf)[1] ==
CHECK1);
92 ((
size_t*)buf)[1] = 0;
93 ((
size_t*)(buf +
size))[2] = 0;
108 else if (ptr &&
size)
112 size_t oldsize = ((
size_t*)buf)[0];
113 assert(((
size_t*)buf)[1] ==
CHECK1);
114 assert(((
size_t*)(buf + oldsize))[2] ==
CHECK2);
123 if (new_rounded != old_rounded)
125 buf = realloc(buf, new_rounded);
135 if (
DEBUG_MALLOC) fprintf(
stderr,
"Realloc 0x%04x/%u to %u failed\n", (
unsigned)(uintptr_t)ptr, (
unsigned)oldsize, (
unsigned)
size);
139 ((
size_t*)buf)[0] =
size;
140 ((
size_t*)buf)[1] =
CHECK1;
145 if (
DEBUG_MALLOC) fprintf(
stderr,
"Realloc 0x%04x/%u to 0x%04x/%u\n", (
unsigned)(uintptr_t)ptr, (
unsigned)oldsize, (
unsigned)(uintptr_t)(buf +
PREFIX_SIZE), (
unsigned)
size);
148 else if (ptr && !
size)
171 return ((
size_t*)buf)[0];
void * malloc_with_check(size_t size)
size_t get_allocation_size(const void *mem)
static size_t g_max_alloc_bytes
void free_with_check(void *mem)
size_t get_max_alloc_bytes()
void * realloc_with_check(void *ptr, size_t size)
static size_t g_alloc_bytes
static size_t g_alloc_count
static size_t round_blocksize(size_t size)
void set_max_alloc_bytes(size_t max_bytes)