diff options
| author | Thomas Schmucker <ts@its1.de> | 2022-01-16 11:29:59 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2022-01-16 11:29:59 +0100 |
| commit | 1bb9e12dca59f6e97d14c938d37b9b492a9e546f (patch) | |
| tree | 5100a6a48dd3da6c253eb3a81f3ba5560f84048f /allocator.c | |
| parent | 85d7942c1c5c1b92add8a4dc70279e91b6f9cdaf (diff) | |
| parent | 62b5acfcd5296a8f648716e7a51f00563be1668a (diff) | |
| download | data-structures-1bb9e12dca59f6e97d14c938d37b9b492a9e546f.tar.gz data-structures-1bb9e12dca59f6e97d14c938d37b9b492a9e546f.tar.bz2 data-structures-1bb9e12dca59f6e97d14c938d37b9b492a9e546f.zip | |
Merge branch 'cleanup/allocator'
Diffstat (limited to 'allocator.c')
| -rw-r--r-- | allocator.c | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/allocator.c b/allocator.c index 6d13a17..98fa69f 100644 --- a/allocator.c +++ b/allocator.c | |||
| @@ -1,23 +1,23 @@ | |||
| 1 | #include "allocator.h" | 1 | #include "allocator.h" |
| 2 | 2 | ||
| 3 | #include <stdlib.h> | 3 | #include <stdlib.h> /* malloc */ |
| 4 | 4 | ||
| 5 | /* Interface Code */ | 5 | /* Interface Code */ |
| 6 | static void * | 6 | static void * |
| 7 | default_allocate(struct its1_allocator *allocator, size_t n) | 7 | default_allocate(struct allocator *allocator, size_t n) |
| 8 | { | 8 | { |
| 9 | (void) allocator; | 9 | (void) allocator; |
| 10 | return malloc(n); | 10 | return malloc(n); |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | static void | 13 | static void |
| 14 | default_deallocate(struct its1_allocator *allocator, void *ptr) | 14 | default_deallocate(struct allocator *allocator, void *ptr) |
| 15 | { | 15 | { |
| 16 | (void) allocator; | 16 | (void) allocator; |
| 17 | free(ptr); | 17 | free(ptr); |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | struct its1_allocator its1_default_allocator = { | 20 | struct allocator allocator_default_allocator = { |
| 21 | .allocate = &default_allocate, | 21 | .allocate = &default_allocate, |
| 22 | .deallocate = &default_deallocate | 22 | .deallocate = &default_deallocate |
| 23 | }; | 23 | }; |
| @@ -26,34 +26,34 @@ struct its1_allocator its1_default_allocator = { | |||
| 26 | #include <string.h> | 26 | #include <string.h> |
| 27 | 27 | ||
| 28 | void * | 28 | void * |
| 29 | its1_malloc(struct its1_allocator *allocator, size_t n) | 29 | allocator_malloc(struct allocator *allocator, size_t n) |
| 30 | { | 30 | { |
| 31 | if ( allocator == NULL ) { | 31 | if ( !allocator ) { |
| 32 | allocator = &its1_default_allocator; | 32 | allocator = &allocator_default_allocator; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | return allocator->allocate(allocator, n); | 35 | return allocator->allocate(allocator, n); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | void | 38 | void |
| 39 | its1_free(struct its1_allocator *allocator, void *ptr) | 39 | allocator_free(struct allocator *allocator, void *ptr) |
| 40 | { | 40 | { |
| 41 | if ( allocator == NULL ) { | 41 | if ( !allocator ) { |
| 42 | allocator = &its1_default_allocator; | 42 | allocator = &allocator_default_allocator; |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | allocator->deallocate(allocator, ptr); | 45 | allocator->deallocate(allocator, ptr); |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | char * | 48 | char * |
| 49 | its1_strdup(struct its1_allocator *allocator, const char *str) | 49 | allocator_strdup(struct allocator *allocator, const char *str) |
| 50 | { | 50 | { |
| 51 | size_t n; | 51 | size_t n; |
| 52 | char * dest; | 52 | char * dest; |
| 53 | 53 | ||
| 54 | n = strlen(str) + 1; | 54 | n = strlen(str) + 1; |
| 55 | dest = its1_malloc(allocator, n); | 55 | dest = allocator_malloc(allocator, n); |
| 56 | if ( dest != NULL ) { | 56 | if ( dest ) { |
| 57 | memcpy(dest, str, n); | 57 | memcpy(dest, str, n); |
| 58 | } | 58 | } |
| 59 | 59 | ||
| @@ -61,12 +61,12 @@ its1_strdup(struct its1_allocator *allocator, const char *str) | |||
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | void * | 63 | void * |
| 64 | its1_malloc_zero(struct its1_allocator *allocator, size_t n) | 64 | allocator_malloc_zero(struct allocator *allocator, size_t n) |
| 65 | { | 65 | { |
| 66 | void *ptr; | 66 | void *ptr; |
| 67 | 67 | ||
| 68 | ptr = its1_malloc(allocator, n); | 68 | ptr = allocator_malloc(allocator, n); |
| 69 | if ( ptr != NULL ) { | 69 | if ( ptr ) { |
| 70 | memset(ptr, 0, n); | 70 | memset(ptr, 0, n); |
| 71 | } | 71 | } |
| 72 | 72 | ||
