diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-07-22 17:30:45 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-07-22 17:30:45 +0200 |
| commit | ac55496d881e0a17b3eff85f1faae5aafbc53b50 (patch) | |
| tree | 65b954e994d7bbfc76bc959876e28f7ea9fa708c /allocator.h | |
| download | data-structures-ac55496d881e0a17b3eff85f1faae5aafbc53b50.tar.gz data-structures-ac55496d881e0a17b3eff85f1faae5aafbc53b50.tar.bz2 data-structures-ac55496d881e0a17b3eff85f1faae5aafbc53b50.zip | |
erster Commit
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 | ||
