diff options
Diffstat (limited to 'allocator.c')
-rw-r--r-- | allocator.c | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/allocator.c b/allocator.c index 9f58ee4..4cde1a9 100644 --- a/allocator.c +++ b/allocator.c @@ -22,6 +22,8 @@ static void *allocr_malloc(Allocator *a, size_t bytes) { (void)a; return err_malloc(bytes); #else + if (bytes == 0) + return NULL; if (a == NULL) return err_malloc(bytes); /* position in this page to return */ @@ -52,6 +54,7 @@ static void *allocr_calloc(Allocator *a, size_t n, size_t sz) { #if NO_ALLOCATOR a = NULL; #endif + if (n == 0 || sz == 0) return NULL; if (a == NULL) return err_calloc(n, sz); /* OPTIM: use calloc */ size_t bytes = n * sz; @@ -60,27 +63,33 @@ static void *allocr_calloc(Allocator *a, size_t n, size_t sz) { return data; } +static void allocr_free(Allocator *a, void *data, size_t size) { +#if NO_ALLOCATOR + a = NULL; +#endif + if (a == NULL) { + free(data); + } + /* OPTIM */ + (void)size; +} + /* OPTIM */ static void *allocr_realloc(Allocator *a, void *data, size_t old_size, size_t new_size) { #if NO_ALLOCATOR a = NULL; #endif + if (new_size == 0) { + allocr_free(a, data, old_size); + return NULL; + } if (a == NULL) return err_realloc(data, new_size); void *ret = allocr_malloc(a, new_size); memcpy(ret, data, old_size); return ret; } -static void allocr_free(Allocator *a, void *data, size_t size) { -#if NO_ALLOCATOR - a = NULL; -#endif - if (a == NULL) { - free(data); - } - /* OPTIM */ - (void)size; -} + static void allocr_free_all(Allocator *a) { for (Page *page = a->first; page;) { |