From 154874afda4a8df885e51c01f7681f04fb0b8e61 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sat, 9 Apr 2022 09:43:53 +0200 Subject: neue Verzeichnisstruktur --- src/allocator.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/allocator.h (limited to 'src/allocator.h') diff --git a/src/allocator.h b/src/allocator.h new file mode 100644 index 0000000..dd9bb16 --- /dev/null +++ b/src/allocator.h @@ -0,0 +1,30 @@ +#pragma once + +#include /* size_t */ + +struct allocator { + void *(*allocate)(struct allocator *allocator, size_t n); + void (*deallocate)(struct allocator *allocator, void *ptr); +}; + +extern struct allocator default_allocator; + +/* allocator interface */ +void *allocator_malloc(struct allocator *allocator, size_t n); +void allocator_free(struct allocator *allocator, void *ptr); + +/* helper functions */ +char *allocator_strdup(struct allocator *allocator, const char *str); +void *allocator_malloc_zero(struct allocator *allocator, size_t n); + +#define ALLOCATE(n) \ + allocator_malloc(&allocator_default_allocator, (n)) + +#define ALLOCATE_ZERO(n) \ + allocator_malloc_zero(&allocator_default_allocator, (n)) + +#define FREE(ptr) \ + (allocator_free(&allocator_default_allocator, (ptr)), (ptr) = NULL) + +#define NEW(ptr) ((ptr) = ALLOCATE(sizeof *(ptr))) +#define NEW0(ptr) ((ptr) = ALLOCATE_ZERO(sizeof *(ptr))) -- cgit v1.3