This commit is contained in:
2025-10-03 20:55:18 +02:00
parent 5fed6d24d8
commit 8ada613a3e
3 changed files with 34 additions and 34 deletions
+10 -10
View File
@@ -7,17 +7,17 @@
// Contains all operations an allocator can do. Similar interface to sdtlibs // Contains all operations an allocator can do. Similar interface to sdtlibs
// malloc, realloc and free. // malloc, realloc and free.
typedef struct allocator_vtbl { typedef struct allocator_vtbl {
void *(*alloc)(void *this, size_t bytes, const char *file, int line); void *(*alloc)(void *this, size_t bytes, const char *file, int line);
void *(*resize)(void *this, void *old_ptr, size_t bytes, const char *file, int line); void *(*resize)(void *this, void *old_ptr, size_t bytes, const char *file, int line);
void (*free)(void *this, void *ptr, const char *file, int line); void (*free)(void *this, void *ptr, const char *file, int line);
} allocator_vtbl_t; } allocator_vtbl_t;
// An instance of an allocator. // An instance of an allocator.
typedef struct allocator { typedef struct allocator {
// pointer to the behind-the-scenes data that an allocator may store. // pointer to the behind-the-scenes data that an allocator may store.
void *const this; void *const this;
// pointer to the method implementations of an allocator. // pointer to the method implementations of an allocator.
const allocator_vtbl_t *const vtbl; const allocator_vtbl_t *const vtbl;
} allocator_t; } allocator_t;
void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line); void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line);
@@ -32,13 +32,13 @@ extern const allocator_t allocator_stdlib;
#ifdef ALLOCATOR_IMPLEMENTATION #ifdef ALLOCATOR_IMPLEMENTATION
void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line) { void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line) {
return this.vtbl->alloc(this.this, bytes, file, line); return this.vtbl->alloc(this.this, bytes, file, line);
} }
void *allocator_resize_func(allocator_t this, void *old_ptr, size_t bytes, const char *file, int line) { void *allocator_resize_func(allocator_t this, void *old_ptr, size_t bytes, const char *file, int line) {
return this.vtbl->resize(this.this, old_ptr, bytes, file, line); return this.vtbl->resize(this.this, old_ptr, bytes, file, line);
} }
void allocator_free_func(allocator_t this, void *ptr, const char *file, int line) { void allocator_free_func(allocator_t this, void *ptr, const char *file, int line) {
this.vtbl->free(this.this, ptr, file, line); this.vtbl->free(this.this, ptr, file, line);
} }
#include "std_allocator.c" #include "std_allocator.c"
+17 -17
View File
@@ -1,32 +1,32 @@
#include "allocator.h" #include "allocator.h"
static void *stdlib_alloc(void *this, size_t bytes, const char *file, int line) { static void *stdlib_alloc(void *this, size_t bytes, const char *file, int line) {
(void)this; (void)this;
(void)file; (void)file;
(void)line; (void)line;
return malloc(bytes); return malloc(bytes);
} }
static void *stdlib_resize(void *this, void *old_ptr, size_t bytes, const char *file, int line) { static void *stdlib_resize(void *this, void *old_ptr, size_t bytes, const char *file, int line) {
(void)this; (void)this;
(void)file; (void)file;
(void)line; (void)line;
return realloc(old_ptr, bytes); return realloc(old_ptr, bytes);
} }
static void stdlib_free(void *this, void *ptr, const char *file, int line) { static void stdlib_free(void *this, void *ptr, const char *file, int line) {
(void)this; (void)this;
(void)file; (void)file;
(void)line; (void)line;
free(ptr); free(ptr);
} }
static const allocator_vtbl_t stdlib_vtbl = { static const allocator_vtbl_t stdlib_vtbl = {
.alloc = stdlib_alloc, .alloc = stdlib_alloc,
.resize = stdlib_resize, .resize = stdlib_resize,
.free = stdlib_free, .free = stdlib_free,
}; };
const allocator_t allocator_stdlib = (allocator_t) { const allocator_t allocator_stdlib = (allocator_t) {
.this=NULL, .this=NULL,
.vtbl=&stdlib_vtbl, .vtbl=&stdlib_vtbl,
}; };
+7 -7
View File
@@ -3,12 +3,12 @@
#include "allocator.h" #include "allocator.h"
Test(std_allocator, test) { Test(std_allocator, test) {
allocator_t this = allocator_stdlib; allocator_t this = allocator_stdlib;
void *ptr = allocator_alloc(this, 10); void *ptr = allocator_alloc(this, 10);
cr_assert(ptr != ((void *)0), "non null from malloc"); cr_assert(ptr != ((void *)0), "non null from malloc");
void *new_ptr = allocator_resize(this, ptr, 1024*1024*500); void *new_ptr = allocator_resize(this, ptr, 1024*1024*500);
cr_assert(new_ptr != ((void *)0), "non null from realloc"); cr_assert(new_ptr != ((void *)0), "non null from realloc");
cr_assert(new_ptr != ptr, "realloc is not the same ptr as malloc"); cr_assert(new_ptr != ptr, "realloc is not the same ptr as malloc");
allocator_free(this, new_ptr); allocator_free(this, new_ptr);
} }