diff options
Diffstat (limited to 'allocator.h')
| -rw-r--r-- | allocator.h | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/allocator.h b/allocator.h index 3cff385..fc28833 100644 --- a/allocator.h +++ b/allocator.h | |||
| @@ -1,20 +1,22 @@ | |||
| 1 | #ifndef ITS1_ALLOCATOR_H_INCLUDED | 1 | #ifndef ITS1_ALLOCATOR_H_INCLUDED |
| 2 | #define ITS1_ALLOCATOR_H_INCLUDED | 2 | #define ITS1_ALLOCATOR_H_INCLUDED |
| 3 | 3 | ||
| 4 | #include <stddef.h> /* size_t */ | ||
| 5 | |||
| 4 | struct its1_allocator { | 6 | struct its1_allocator { |
| 5 | void *(*allocate)(struct its1_allocator *allocator, size_t n); | 7 | void *(*allocate)(struct its1_allocator *allocator, size_t n); |
| 6 | void (*deallocate)(struct its1_allocator *allocator, void *ptr); | 8 | void (*deallocate)(struct its1_allocator *allocator, void *ptr); |
| 7 | }; | 9 | }; |
| 8 | 10 | ||
| 9 | extern struct its1_allocator its1_default_allocator; | 11 | extern struct its1_allocator its1_default_allocator; |
| 10 | 12 | ||
| 11 | /* allocator interface */ | 13 | /* allocator interface */ |
| 12 | void *its1_malloc(struct its1_allocator *allocator, size_t n); | 14 | void *its1_malloc(struct its1_allocator *allocator, size_t n); |
| 13 | void its1_free(struct its1_allocator *allocator, void *ptr); | 15 | void its1_free(struct its1_allocator *allocator, void *ptr); |
| 14 | 16 | ||
| 15 | /* helper functions */ | 17 | /* helper functions */ |
| 16 | char *its1_strdup(struct its1_allocator *allocator, const char *str); | 18 | char *its1_strdup(struct its1_allocator *allocator, const char *str); |
| 17 | void *its1_malloc_zero(struct its1_allocator *allocator, size_t n); | 19 | void *its1_malloc_zero(struct its1_allocator *allocator, size_t n); |
| 18 | 20 | ||
| 19 | #define ALLOCATE(n) \ | 21 | #define ALLOCATE(n) \ |
| 20 | its1_malloc(&its1_default_allocator, (n)) | 22 | its1_malloc(&its1_default_allocator, (n)) |
| @@ -25,7 +27,7 @@ void *its1_malloc_zero(struct its1_allocator *allocator, size_t n); | |||
| 25 | #define FREE(ptr) \ | 27 | #define FREE(ptr) \ |
| 26 | (its1_free(&its1_default_allocator, (ptr)), (ptr) = NULL) | 28 | (its1_free(&its1_default_allocator, (ptr)), (ptr) = NULL) |
| 27 | 29 | ||
| 28 | #define NEW(ptr) ((ptr) = ALLOCATE(sizeof *(ptr))) | 30 | #define NEW(ptr) ((ptr) = ALLOCATE(sizeof *(ptr))) |
| 29 | #define NEW0(ptr) ((ptr) = ALLOCATE_ZERO(sizeof *(ptr))) | 31 | #define NEW0(ptr) ((ptr) = ALLOCATE_ZERO(sizeof *(ptr))) |
| 30 | 32 | ||
| 31 | #endif | 33 | #endif |
