diff options
| -rw-r--r-- | src/allocator.c | 16 | ||||
| -rw-r--r-- | src/allocator.h | 3 |
2 files changed, 17 insertions, 2 deletions
diff --git a/src/allocator.c b/src/allocator.c index d6aeea9..1627089 100644 --- a/src/allocator.c +++ b/src/allocator.c | |||
| @@ -1,23 +1,31 @@ | |||
| 1 | #include "allocator.h" | 1 | #include "allocator.h" |
| 2 | 2 | ||
| 3 | #include <assert.h> | ||
| 3 | #include <stdlib.h> /* malloc */ | 4 | #include <stdlib.h> /* malloc */ |
| 4 | 5 | ||
| 6 | #ifndef UNUSED | ||
| 7 | # define UNUSED(var) (void) (var) | ||
| 8 | #endif | ||
| 9 | |||
| 5 | /* Interface Code */ | 10 | /* Interface Code */ |
| 6 | static void * | 11 | static void * |
| 7 | default_allocate(struct allocator *allocator, size_t n) | 12 | default_allocate(struct allocator *allocator, size_t n) |
| 8 | { | 13 | { |
| 9 | (void) allocator; | 14 | UNUSED(allocator); |
| 10 | return malloc(n); | 15 | return malloc(n); |
| 11 | } | 16 | } |
| 12 | 17 | ||
| 13 | static void | 18 | static void |
| 14 | default_deallocate(struct allocator *allocator, void *ptr) | 19 | default_deallocate(struct allocator *allocator, void *ptr) |
| 15 | { | 20 | { |
| 16 | (void) allocator; | 21 | UNUSED(allocator); |
| 17 | free(ptr); | 22 | free(ptr); |
| 18 | } | 23 | } |
| 19 | 24 | ||
| 20 | struct allocator allocator_default_allocator = { | 25 | struct allocator allocator_default_allocator = { |
| 26 | #ifndef NDEBUG | ||
| 27 | .self = &allocator_default_allocator, | ||
| 28 | #endif | ||
| 21 | .allocate = &default_allocate, | 29 | .allocate = &default_allocate, |
| 22 | .deallocate = &default_deallocate | 30 | .deallocate = &default_deallocate |
| 23 | }; | 31 | }; |
| @@ -32,6 +40,8 @@ allocator_malloc(struct allocator *allocator, size_t n) | |||
| 32 | allocator = &allocator_default_allocator; | 40 | allocator = &allocator_default_allocator; |
| 33 | } | 41 | } |
| 34 | 42 | ||
| 43 | assert(allocator->self == allocator); | ||
| 44 | |||
| 35 | return allocator->allocate(allocator, n); | 45 | return allocator->allocate(allocator, n); |
| 36 | } | 46 | } |
| 37 | 47 | ||
| @@ -42,6 +52,8 @@ allocator_free(struct allocator *allocator, void *ptr) | |||
| 42 | allocator = &allocator_default_allocator; | 52 | allocator = &allocator_default_allocator; |
| 43 | } | 53 | } |
| 44 | 54 | ||
| 55 | assert(allocator->self == allocator); | ||
| 56 | |||
| 45 | allocator->deallocate(allocator, ptr); | 57 | allocator->deallocate(allocator, ptr); |
| 46 | } | 58 | } |
| 47 | 59 | ||
diff --git a/src/allocator.h b/src/allocator.h index dd9bb16..2c0c5b9 100644 --- a/src/allocator.h +++ b/src/allocator.h | |||
| @@ -3,6 +3,9 @@ | |||
| 3 | #include <stddef.h> /* size_t */ | 3 | #include <stddef.h> /* size_t */ |
| 4 | 4 | ||
| 5 | struct allocator { | 5 | struct allocator { |
| 6 | #ifndef NDEBUG | ||
| 7 | struct allocator *self; | ||
| 8 | #endif | ||
| 6 | void *(*allocate)(struct allocator *allocator, size_t n); | 9 | void *(*allocate)(struct allocator *allocator, size_t n); |
| 7 | void (*deallocate)(struct allocator *allocator, void *ptr); | 10 | void (*deallocate)(struct allocator *allocator, void *ptr); |
| 8 | }; | 11 | }; |
