diff options
| author | Thomas Schmucker <ts@its1.de> | 2022-04-09 09:46:17 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2022-04-09 09:46:17 +0200 |
| commit | 38f5e364f73967c01f9ed442fe6646e70cb1dde0 (patch) | |
| tree | 274817eb2793584b0e3d856de5504a614f2b4e89 /src/allocator.h | |
| parent | 2863f4f1d2a6a6a8704454824d6c291d2e0d4b5c (diff) | |
| parent | 154874afda4a8df885e51c01f7681f04fb0b8e61 (diff) | |
| download | data-structures-38f5e364f73967c01f9ed442fe6646e70cb1dde0.tar.gz data-structures-38f5e364f73967c01f9ed442fe6646e70cb1dde0.tar.bz2 data-structures-38f5e364f73967c01f9ed442fe6646e70cb1dde0.zip | |
Merge branch 'rework-directory-structure'
Diffstat (limited to 'src/allocator.h')
| -rw-r--r-- | src/allocator.h | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/allocator.h b/src/allocator.h new file mode 100644 index 0000000..dd9bb16 --- /dev/null +++ b/src/allocator.h | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <stddef.h> /* size_t */ | ||
| 4 | |||
| 5 | struct allocator { | ||
| 6 | void *(*allocate)(struct allocator *allocator, size_t n); | ||
| 7 | void (*deallocate)(struct allocator *allocator, void *ptr); | ||
| 8 | }; | ||
| 9 | |||
| 10 | extern struct allocator default_allocator; | ||
| 11 | |||
| 12 | /* allocator interface */ | ||
| 13 | void *allocator_malloc(struct allocator *allocator, size_t n); | ||
| 14 | void allocator_free(struct allocator *allocator, void *ptr); | ||
| 15 | |||
| 16 | /* helper functions */ | ||
| 17 | char *allocator_strdup(struct allocator *allocator, const char *str); | ||
| 18 | void *allocator_malloc_zero(struct allocator *allocator, size_t n); | ||
| 19 | |||
| 20 | #define ALLOCATE(n) \ | ||
| 21 | allocator_malloc(&allocator_default_allocator, (n)) | ||
| 22 | |||
| 23 | #define ALLOCATE_ZERO(n) \ | ||
| 24 | allocator_malloc_zero(&allocator_default_allocator, (n)) | ||
| 25 | |||
| 26 | #define FREE(ptr) \ | ||
| 27 | (allocator_free(&allocator_default_allocator, (ptr)), (ptr) = NULL) | ||
| 28 | |||
| 29 | #define NEW(ptr) ((ptr) = ALLOCATE(sizeof *(ptr))) | ||
| 30 | #define NEW0(ptr) ((ptr) = ALLOCATE_ZERO(sizeof *(ptr))) | ||
