make cx_allocator_class shared

Sun, 07 Feb 2021 17:17:46 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 07 Feb 2021 17:17:46 +0100
changeset 396
3539dd99ab92
parent 395
dc1bfe5ffd38
child 397
cfc1193b1e65

make cx_allocator_class shared

src/allocator.c file | annotate | diff | comparison | revisions
src/cx/allocator.h file | annotate | diff | comparison | revisions
test/test_allocator.c file | annotate | diff | comparison | revisions
--- a/src/allocator.c	Sun Feb 07 16:52:21 2021 +0100
+++ b/src/allocator.c	Sun Feb 07 17:17:46 2021 +0100
@@ -30,13 +30,15 @@
 
 #include <stdlib.h>
 
+cx_allocator_class cx_default_allocator_class = {
+        cx_malloc_stdlib,
+        cx_realloc_stdlib,
+        cx_calloc_stdlib,
+        cx_free_stdlib
+};
+
 struct cx_allocator_s cx_default_allocator = {
-        {
-            cx_malloc_stdlib,
-            cx_realloc_stdlib,
-            cx_calloc_stdlib,
-            cx_free_stdlib
-        },
+        &cx_default_allocator_class,
         NULL
 };
 CxAllocator cxDefaultAllocator = &cx_default_allocator;
@@ -58,17 +60,17 @@
 }
 
 void* cxMalloc(CxAllocator allocator, size_t n) {
-    return allocator->allocatorClass.malloc(allocator, n);
+    return allocator->cl->malloc(allocator->data, n);
 }
 
 void* cxRealloc(CxAllocator allocator, void* mem, size_t n) {
-    return allocator->allocatorClass.realloc(allocator, mem, n);
+    return allocator->cl->realloc(allocator->data, mem, n);
 }
 
 void* cxCalloc(CxAllocator allocator, size_t nelem, size_t n) {
-    return allocator->allocatorClass.calloc(allocator, nelem, n);
+    return allocator->cl->calloc(allocator->data, nelem, n);
 }
 
 void cxFree(CxAllocator allocator, void* mem) {
-    allocator->allocatorClass.free(allocator, mem);
+    allocator->cl->free(allocator->data, mem);
 }
\ No newline at end of file
--- a/src/cx/allocator.h	Sun Feb 07 16:52:21 2021 +0100
+++ b/src/cx/allocator.h	Sun Feb 07 17:17:46 2021 +0100
@@ -44,18 +44,20 @@
 void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n);
 void cx_free_stdlib(cx_allocator a, void* mem);
 
-struct cx_allocator_class {
+typedef struct {
     cx_malloc_func malloc;
     cx_realloc_func realloc;
     cx_calloc_func calloc;
     cx_free_func free;
-};
+} cx_allocator_class;
+
+extern cx_allocator_class cx_default_allocator_class;
 
 /* HIGH LEVEL API */
 
 struct cx_allocator_s {
-    struct cx_allocator_class allocatorClass;
-    void* data;
+    cx_allocator_class *cl;
+    cx_allocator data;
 };
 typedef struct cx_allocator_s* CxAllocator;
 
--- a/test/test_allocator.c	Sun Feb 07 16:52:21 2021 +0100
+++ b/test/test_allocator.c	Sun Feb 07 17:17:46 2021 +0100
@@ -31,11 +31,11 @@
 #include <CUnit/Basic.h>
 
 void test_default_allocator_available(void) {
-    struct cx_allocator_class clazz = cxDefaultAllocator->allocatorClass;
-    CU_ASSERT_PTR_EQUAL(clazz.malloc, cx_malloc_stdlib)
-    CU_ASSERT_PTR_EQUAL(clazz.realloc, cx_realloc_stdlib)
-    CU_ASSERT_PTR_EQUAL(clazz.calloc, cx_calloc_stdlib)
-    CU_ASSERT_PTR_EQUAL(clazz.free, cx_free_stdlib)
+    cx_allocator_class* clazz = cxDefaultAllocator->cl;
+    CU_ASSERT_PTR_EQUAL(clazz->malloc, cx_malloc_stdlib)
+    CU_ASSERT_PTR_EQUAL(clazz->realloc, cx_realloc_stdlib)
+    CU_ASSERT_PTR_EQUAL(clazz->calloc, cx_calloc_stdlib)
+    CU_ASSERT_PTR_EQUAL(clazz->free, cx_free_stdlib)
 }
 
 void test_default_malloc(void) {

mercurial