aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2015-12-22 13:01:36 +0200
committerPetri Lehtinen <petri@digip.org>2015-12-22 13:01:36 +0200
commit4f49c07781e120252a1f2dd7b5a9d6e855a07b65 (patch)
tree010021acac3d0883646163d2dad7902acadecaeb
parente44b2231b50aea5de78b7ea2debec0d5327cd711 (diff)
parent245e532934fc9a6df18cf246c29c79863d6ce714 (diff)
downloadjansson-4f49c07781e120252a1f2dd7b5a9d6e855a07b65.zip
jansson-4f49c07781e120252a1f2dd7b5a9d6e855a07b65.tar.gz
jansson-4f49c07781e120252a1f2dd7b5a9d6e855a07b65.tar.bz2
Merge pull request #264 from npmccallum/master
Add json_get_alloc_funcs() to allow alloc function fetching
-rw-r--r--doc/apiref.rst5
-rw-r--r--src/jansson.def1
-rw-r--r--src/jansson.h1
-rw-r--r--src/memory.c8
-rw-r--r--test/suites/api/test_memory_funcs.c7
5 files changed, 21 insertions, 1 deletions
diff --git a/doc/apiref.rst b/doc/apiref.rst
index a921ef7..0aca86f 100644
--- a/doc/apiref.rst
+++ b/doc/apiref.rst
@@ -1570,6 +1570,11 @@ behavior is needed.
Jansson's API functions to ensure that all memory operations use
the same functions.
+.. function:: void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn)
+
+ Fetch the current malloc_fn and free_fn used. Either parameter
+ may be NULL.
+
**Examples:**
Circumvent problems with different CRT heaps on Windows by using
diff --git a/src/jansson.def b/src/jansson.def
index da4cfd4..c43eb07 100644
--- a/src/jansson.def
+++ b/src/jansson.def
@@ -66,4 +66,5 @@ EXPORTS
json_unpack_ex
json_vunpack_ex
json_set_alloc_funcs
+ json_get_alloc_funcs
diff --git a/src/jansson.h b/src/jansson.h
index 6f7fd07..ee60794 100644
--- a/src/jansson.h
+++ b/src/jansson.h
@@ -289,6 +289,7 @@ typedef void *(*json_malloc_t)(size_t);
typedef void (*json_free_t)(void *);
void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);
+void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn);
#ifdef __cplusplus
}
diff --git a/src/memory.c b/src/memory.c
index ca44d6b..e2368cf 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -59,3 +59,11 @@ void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
do_malloc = malloc_fn;
do_free = free_fn;
}
+
+void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn)
+{
+ if (malloc_fn)
+ *malloc_fn = do_malloc;
+ if (free_fn)
+ *free_fn = do_free;
+}
diff --git a/test/suites/api/test_memory_funcs.c b/test/suites/api/test_memory_funcs.c
index a760c08..047ca67 100644
--- a/test/suites/api/test_memory_funcs.c
+++ b/test/suites/api/test_memory_funcs.c
@@ -36,10 +36,15 @@ static void my_free(void *ptr)
static void test_simple()
{
+ json_malloc_t mfunc = NULL;
+ json_free_t ffunc = NULL;
+
json_set_alloc_funcs(my_malloc, my_free);
+ json_get_alloc_funcs(&mfunc, &ffunc);
create_and_free_complex_object();
- if(malloc_called != 1 || free_called != 1)
+ if (malloc_called != 1 || free_called != 1
+ || mfunc != my_malloc || ffunc != my_free)
fail("Custom allocation failed");
}