From ac55496d881e0a17b3eff85f1faae5aafbc53b50 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 22 Jul 2020 17:30:45 +0200 Subject: erster Commit --- allocator.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 allocator.h (limited to 'allocator.h') diff --git a/allocator.h b/allocator.h new file mode 100644 index 0000000..3cff385 --- /dev/null +++ b/allocator.h @@ -0,0 +1,31 @@ +#ifndef ITS1_ALLOCATOR_H_INCLUDED +#define ITS1_ALLOCATOR_H_INCLUDED + +struct its1_allocator { + void *(*allocate)(struct its1_allocator *allocator, size_t n); + void (*deallocate)(struct its1_allocator *allocator, void *ptr); +}; + +extern struct its1_allocator its1_default_allocator; + +/* allocator interface */ +void *its1_malloc(struct its1_allocator *allocator, size_t n); +void its1_free(struct its1_allocator *allocator, void *ptr); + +/* helper functions */ +char *its1_strdup(struct its1_allocator *allocator, const char *str); +void *its1_malloc_zero(struct its1_allocator *allocator, size_t n); + +#define ALLOCATE(n) \ + its1_malloc(&its1_default_allocator, (n)) + +#define ALLOCATE_ZERO(n) \ + its1_malloc_zero(&its1_default_allocator, (n)) + +#define FREE(ptr) \ + (its1_free(&its1_default_allocator, (ptr)), (ptr) = NULL) + +#define NEW(ptr) ((ptr) = ALLOCATE(sizeof *(ptr))) +#define NEW0(ptr) ((ptr) = ALLOCATE_ZERO(sizeof *(ptr))) + +#endif -- cgit v1.3