Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Created Jan 22, 2017
Embed
What would you like to do?
gd mm
diff --git a/src/gdhelpers.c b/src/gdhelpers.c
index 20fff5c..a64278b 100644
--- a/src/gdhelpers.c
+++ b/src/gdhelpers.c
@@ -14,6 +14,33 @@
#define SEP_TEST (separators[*((unsigned char *) s)])
+static inline void* gd_malloc_wrapper(size_t size)
+{
+ return malloc(size);
+}
+
+static inline void* gd_calloc_wrapper(size_t count, size_t size)
+{
+ return calloc(count, size);
+}
+
+static inline void* gd_realloc_wrapper(void *ptr, size_t size)
+{
+ return realloc(ptr, size);
+}
+
+static inline void gd_free_wrapper(void *ptr)
+{
+ free(ptr);
+}
+
+gd_mm_handlers_t mm = {
+ gd_malloc_wrapper,
+ gd_calloc_wrapper,
+ gd_realloc_wrapper,
+ gd_free_wrapper
+};
+
char *
gd_strtok_r (char *s, char *sep, char **state)
{
@@ -66,27 +93,27 @@ gd_strtok_r (char *s, char *sep, char **state)
void * gdCalloc (size_t nmemb, size_t size)
{
- return calloc (nmemb, size);
+ return mm._calloc (nmemb, size);
}
void *
gdMalloc (size_t size)
{
- return malloc (size);
+ return mm._malloc (size);
}
void *
gdRealloc (void *ptr, size_t size)
{
- return realloc (ptr, size);
+ return mm._realloc (ptr, size);
}
void *
gdReallocEx (void *ptr, size_t size)
{
- void *newPtr = gdRealloc (ptr, size);
+ void *newPtr = mm._realloc (ptr, size);
if (!newPtr && ptr)
- gdFree(ptr);
+ mm._free(ptr);
return newPtr;
}
@@ -112,7 +139,10 @@ gdReallocEx (void *ptr, size_t size)
*/
BGD_DECLARE(void) gdFree (void *ptr)
{
- free (ptr);
+ mm._free (ptr);
}
-
+void gdMemorySetup(gd_mm_handlers_t *handlers)
+{
+ mm = *handlers;
+}
diff --git a/src/gdhelpers.h b/src/gdhelpers.h
index 2a96b8b..1a53420 100644
--- a/src/gdhelpers.h
+++ b/src/gdhelpers.h
@@ -69,6 +69,20 @@ extern "C" {
#define DPI2DPCM(dpi) (unsigned int)((dpi)/2.54 + 0.5)
#define DPI2DPM(dpi) (unsigned int)((dpi)/0.0254 + 0.5)
+ typedef void* (*gdMallocFunction)(size_t size);
+ typedef void* (*gdCallocFunction)(size_t count, size_t size);
+ typedef void* (*gdReallocFunction)(void * ptr, size_t size);
+ typedef void (*gdFreeFunction)(void *ptr);
+
+ typedef struct {
+ gdMallocFunction _malloc;
+ gdCallocFunction _calloc;
+ gdReallocFunction _realloc;
+ gdFreeFunction _free;
+ } gd_mm_handlers_t;
+
+ void gdMemorySetup(gd_mm_handlers_t *mm);
+
#endif /* GDHELPERS_H */
#ifdef __cplusplus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment