2013-04-26 20:36:54 -07:00
|
|
|
#include <string.h>
|
2012-11-14 14:05:25 -05:00
|
|
|
#include <stdlib.h>
|
2012-10-10 15:58:03 +02:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
2013-05-25 03:35:42 +02:00
|
|
|
//#define DEBUG__MEMFILL 0xFF
|
|
|
|
|
|
2012-10-10 15:58:03 +02:00
|
|
|
static void* system_alloc(HAllocator *allocator, size_t size) {
|
2013-04-26 20:36:54 -07:00
|
|
|
|
2013-05-25 03:35:42 +02:00
|
|
|
void* ptr = malloc(size + sizeof(size_t));
|
|
|
|
|
#ifdef DEBUG__MEMFILL
|
|
|
|
|
memset(ptr, DEBUG__MEMFILL, size + sizeof(size_t));
|
|
|
|
|
#endif
|
2013-04-26 20:36:54 -07:00
|
|
|
*(size_t*)ptr = size;
|
|
|
|
|
return ptr + sizeof(size_t);
|
2012-10-10 15:58:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void* system_realloc(HAllocator *allocator, void* ptr, size_t size) {
|
2013-04-26 20:36:54 -07:00
|
|
|
if (ptr == NULL)
|
|
|
|
|
return system_alloc(allocator, size);
|
|
|
|
|
ptr = realloc(ptr - sizeof(size_t), size + sizeof(size_t));
|
|
|
|
|
*(size_t*)ptr = size;
|
2013-05-25 03:35:42 +02:00
|
|
|
#ifdef DEBUG__MEMFILL
|
|
|
|
|
size_t old_size = *(size_t*)ptr;
|
2013-04-26 20:36:54 -07:00
|
|
|
if (size > old_size)
|
2013-05-25 03:35:42 +02:00
|
|
|
memset(ptr+sizeof(size_t)+old_size, DEBUG__MEMFILL, size - old_size);
|
|
|
|
|
#endif
|
2013-04-26 20:36:54 -07:00
|
|
|
return ptr + sizeof(size_t);
|
2012-10-10 15:58:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void system_free(HAllocator *allocator, void* ptr) {
|
2013-05-23 23:26:22 +02:00
|
|
|
if (ptr != NULL)
|
|
|
|
|
free(ptr - sizeof(size_t));
|
2012-10-10 15:58:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAllocator system_allocator = {
|
|
|
|
|
.alloc = system_alloc,
|
|
|
|
|
.realloc = system_realloc,
|
|
|
|
|
.free = system_free,
|
|
|
|
|
};
|