aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2026-06-21 17:12:55 +0200
committerThomas Schmucker <ts@its1.de>2026-06-21 17:12:55 +0200
commitf67c54eb50b488ecd2e92ae4c495e89bd3da5663 (patch)
treef452270f69a727f7c515ff9c00c4022e0758a5a5
parentc353b1a162065e4efb7da30d8e57631654ebd6fc (diff)
downloaddata-structures-f67c54eb50b488ecd2e92ae4c495e89bd3da5663.tar.gz
data-structures-f67c54eb50b488ecd2e92ae4c495e89bd3da5663.tar.bz2
data-structures-f67c54eb50b488ecd2e92ae4c495e89bd3da5663.zip
cleanup allocator interfaceHEADmaster
-rw-r--r--src/allocator.c16
-rw-r--r--src/allocator.h3
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 */
6static void * 11static void *
7default_allocate(struct allocator *allocator, size_t n) 12default_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
13static void 18static void
14default_deallocate(struct allocator *allocator, void *ptr) 19default_deallocate(struct allocator *allocator, void *ptr)
15{ 20{
16 (void) allocator; 21 UNUSED(allocator);
17 free(ptr); 22 free(ptr);
18} 23}
19 24
20struct allocator allocator_default_allocator = { 25struct 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
5struct allocator { 5struct 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};