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