diff options
| author | Thomas Schmucker <ts@its1.de> | 2026-06-21 17:12:55 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2026-06-21 17:12:55 +0200 |
| commit | f67c54eb50b488ecd2e92ae4c495e89bd3da5663 (patch) | |
| tree | f452270f69a727f7c515ff9c00c4022e0758a5a5 /src/allocator.c | |
| parent | c353b1a162065e4efb7da30d8e57631654ebd6fc (diff) | |
| download | data-structures-master.tar.gz data-structures-master.tar.bz2 data-structures-master.zip | |
Diffstat (limited to 'src/allocator.c')
| -rw-r--r-- | src/allocator.c | 16 |
1 files changed, 14 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 | ||
