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.c | |
| 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.c')
| -rw-r--r-- | src/allocator.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/allocator.c b/src/allocator.c new file mode 100644 index 0000000..98fa69f --- /dev/null +++ b/src/allocator.c | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | #include "allocator.h" | ||
| 2 | |||
| 3 | #include <stdlib.h> /* malloc */ | ||
| 4 | |||
| 5 | /* Interface Code */ | ||
| 6 | static void * | ||
| 7 | default_allocate(struct allocator *allocator, size_t n) | ||
| 8 | { | ||
| 9 | (void) allocator; | ||
| 10 | return malloc(n); | ||
| 11 | } | ||
| 12 | |||
| 13 | static void | ||
| 14 | default_deallocate(struct allocator *allocator, void *ptr) | ||
| 15 | { | ||
| 16 | (void) allocator; | ||
| 17 | free(ptr); | ||
| 18 | } | ||
| 19 | |||
| 20 | struct allocator allocator_default_allocator = { | ||
| 21 | .allocate = &default_allocate, | ||
| 22 | .deallocate = &default_deallocate | ||
| 23 | }; | ||
| 24 | |||
| 25 | /* Libary Code */ | ||
| 26 | #include <string.h> | ||
| 27 | |||
| 28 | void * | ||
| 29 | allocator_malloc(struct allocator *allocator, size_t n) | ||
| 30 | { | ||
| 31 | if ( !allocator ) { | ||
| 32 | allocator = &allocator_default_allocator; | ||
| 33 | } | ||
| 34 | |||
| 35 | return allocator->allocate(allocator, n); | ||
| 36 | } | ||
| 37 | |||
| 38 | void | ||
| 39 | allocator_free(struct allocator *allocator, void *ptr) | ||
| 40 | { | ||
| 41 | if ( !allocator ) { | ||
| 42 | allocator = &allocator_default_allocator; | ||
| 43 | } | ||
| 44 | |||
| 45 | allocator->deallocate(allocator, ptr); | ||
| 46 | } | ||
| 47 | |||
| 48 | char * | ||
| 49 | allocator_strdup(struct allocator *allocator, const char *str) | ||
| 50 | { | ||
| 51 | size_t n; | ||
| 52 | char * dest; | ||
| 53 | |||
| 54 | n = strlen(str) + 1; | ||
| 55 | dest = allocator_malloc(allocator, n); | ||
| 56 | if ( dest ) { | ||
| 57 | memcpy(dest, str, n); | ||
| 58 | } | ||
| 59 | |||
| 60 | return dest; | ||
| 61 | } | ||
| 62 | |||
| 63 | void * | ||
| 64 | allocator_malloc_zero(struct allocator *allocator, size_t n) | ||
| 65 | { | ||
| 66 | void *ptr; | ||
| 67 | |||
| 68 | ptr = allocator_malloc(allocator, n); | ||
| 69 | if ( ptr ) { | ||
| 70 | memset(ptr, 0, n); | ||
| 71 | } | ||
| 72 | |||
| 73 | return ptr; | ||
| 74 | } | ||
| 75 | |||
| 76 | int | ||
| 77 | main() | ||
| 78 | { | ||
| 79 | } | ||
