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.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/allocator.c (limited to 'src/allocator.c') diff --git a/src/allocator.c b/src/allocator.c new file mode 100644 index 0000000..98fa69f --- /dev/null +++ b/src/allocator.c @@ -0,0 +1,79 @@ +#include "allocator.h" + +#include /* malloc */ + +/* Interface Code */ +static void * +default_allocate(struct allocator *allocator, size_t n) +{ + (void) allocator; + return malloc(n); +} + +static void +default_deallocate(struct allocator *allocator, void *ptr) +{ + (void) allocator; + free(ptr); +} + +struct allocator allocator_default_allocator = { + .allocate = &default_allocate, + .deallocate = &default_deallocate +}; + +/* Libary Code */ +#include + +void * +allocator_malloc(struct allocator *allocator, size_t n) +{ + if ( !allocator ) { + allocator = &allocator_default_allocator; + } + + return allocator->allocate(allocator, n); +} + +void +allocator_free(struct allocator *allocator, void *ptr) +{ + if ( !allocator ) { + allocator = &allocator_default_allocator; + } + + allocator->deallocate(allocator, ptr); +} + +char * +allocator_strdup(struct allocator *allocator, const char *str) +{ + size_t n; + char * dest; + + n = strlen(str) + 1; + dest = allocator_malloc(allocator, n); + if ( dest ) { + memcpy(dest, str, n); + } + + return dest; +} + +void * +allocator_malloc_zero(struct allocator *allocator, size_t n) +{ + void *ptr; + + ptr = allocator_malloc(allocator, n); + if ( ptr ) { + memset(ptr, 0, n); + } + + return ptr; +} + +int +main() +{ +} -- cgit v1.3