summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2020-07-22 07:56:29 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2020-07-22 07:58:50 +0200
commitd19d20fbd638c3edb0e2dea1589455546a309382 (patch)
treef0320e8d21a8b3ece6349f955b685b2994f72292
parent6ca0f4910364f4522ad91b973e5ce18cbbee0eed (diff)
Add ability to provide custom alloc/free at compile time
-rw-r--r--genx.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/genx.c b/genx.c
index 1da1f61..5253f1a 100644
--- a/genx.c
+++ b/genx.c
@@ -12,6 +12,10 @@
# define GENX_SNPRINTF 1
#endif
+#if defined(GENX_CUSTOM_ALLOC) != defined(GENX_CUSTOM_FREE)
+# error both GENX_CUSTOM_ALLOC and GENX_CUSTOM_FREE must be defined
+#endif
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -170,7 +174,11 @@ static void * allocate(genxWriter w, size_t bytes)
if (w->alloc)
return (void *) (*w->alloc)(w->userData, bytes);
else
+#ifdef GENX_CUSTOM_ALLOC
+ return (void *) GENX_CUSTOM_ALLOC(bytes);
+#else
return (void *) malloc(bytes);
+#endif
}
static void deallocate(genxWriter w, void * data)
@@ -178,7 +186,12 @@ static void deallocate(genxWriter w, void * data)
if (w->dealloc)
(*w->dealloc)(w->userData, data);
else if (w->alloc == NULL)
+#ifdef GENX_CUSTOM_FREE
+ GENX_CUSTOM_FREE(data);
+#else
free(data);
+#endif
+
}
static utf8 copy(genxWriter w, constUtf8 from)
@@ -568,7 +581,11 @@ genxWriter genxNew(genxAlloc alloc, genxDealloc dealloc, void * userData)
if (alloc)
w = (genxWriter) (*alloc)(userData, sizeof(struct genxWriter_rec));
else
+#ifdef GENX_CUSTOM_ALLOC
+ w = (genxWriter) GENX_CUSTOM_ALLOC(sizeof(struct genxWriter_rec));
+#else
w = (genxWriter) malloc(sizeof(struct genxWriter_rec));
+#endif
if (w == NULL)
return NULL;