6 static int malloc_called = 0;
7 static int free_called = 0;
10 static void create_and_free_complex_object()
14 obj = json_pack("{s:i,s:n,s:b,s:b,s:{s:s},s:[i,i,i]",
19 "alice", "bar", "baz",
25 static void *my_malloc(size_t size)
31 static void my_free(void *ptr)
37 static void test_simple()
39 json_set_alloc_funcs(my_malloc, my_free);
40 create_and_free_complex_object();
42 if(malloc_called != 27 || free_called != 27)
43 fail("Custom allocation failed");
48 Test the secure memory functions code given in the API reference
49 documentation, but by using plain memset instead of
53 static void *secure_malloc(size_t size)
55 /* Store the memory area size in the beginning of the block */
56 void *ptr = malloc(size + 8);
57 *((size_t *)ptr) = size;
61 static void secure_free(void *ptr)
66 size = *((size_t *)ptr);
68 /*guaranteed_*/memset(ptr, 0, size);
72 static void test_secure_funcs(void)
74 json_set_alloc_funcs(secure_malloc, secure_free);
75 create_and_free_complex_object();