1 #include "JSONMemory.h"
3 #ifdef JSON_MEMORY_MANAGE
5 void auto_expand::purge(
void) json_nothrow {
6 for(JSON_MAP(
void *,
void *)::iterator i = mymap.begin(), en = mymap.end(); i != en; ++i){
7 #if defined(JSON_DEBUG) || defined(JSON_SAFE)
8 void * temp = (
void*)i -> first;
9 libjson_free<void>(temp);
11 libjson_free<void>((
void*)i -> first);
16 void auto_expand_node::purge(
void) json_nothrow {
17 for(JSON_MAP(
void *, JSONNode *)::iterator i = mymap.begin(), en = mymap.end(); i != en; ++i){
18 JSONNode::deleteJSONNode((JSONNode *)i -> second);
23 #include "JSONStream.h"
24 void auto_expand_stream::purge(
void) json_nothrow {
25 for(JSON_MAP(
void *, JSONStream *)::iterator i = mymap.begin(), en = mymap.end(); i != en; ++i){
26 JSONStream::deleteJSONStream((JSONStream *)i -> second);
32 #if defined(JSON_MEMORY_CALLBACKS) || defined(JSON_MEMORY_POOL)
34 #ifdef JSON_MEMORY_POOL
35 #include "JSONMemoryPool.h"
36 static bucket_pool_8<MEMPOOL_1, MEMPOOL_2, MEMPOOL_3, MEMPOOL_4, MEMPOOL_5, MEMPOOL_6, MEMPOOL_7, MEMPOOL_8> json_generic_mempool;
39 class mempool_callback_setter {
41 LIBJSON_OBJECT(mempool_callback_setter);
42 inline mempool_callback_setter(
void) json_nothrow {
44 mempool_callbacks::set(std::malloc, std::realloc, std::free);
47 inline mempool_callback_setter(
const mempool_callback_setter & o);
48 inline mempool_callback_setter & operator = (
const mempool_callback_setter & o);
50 static mempool_callback_setter __mempoolcallbacksetter;
53 #include "JSONSingleton.h"
55 void * JSONMemory::json_malloc(
size_t siz) json_nothrow {
56 #ifdef JSON_MEMORY_POOL
57 return json_generic_mempool.allocate(siz);
59 if (json_malloc_t callback = JSONSingleton<json_malloc_t>::get()){
60 #if(defined(JSON_DEBUG) && (!defined(JSON_MEMORY_CALLBACKS))) //in debug mode without mem callback, see if the malloc was successful
61 void * result = callback(siz);
62 JSON_ASSERT(result, JSON_TEXT(
"Out of memory"));
68 #if(defined(JSON_DEBUG) && (!defined(JSON_MEMORY_CALLBACKS))) //in debug mode without mem callback, see if the malloc was successful
69 void * result = std::malloc(siz);
70 JSON_ASSERT(result, JSON_TEXT(
"Out of memory"));
73 return std::malloc(siz);
78 void JSONMemory::json_free(
void * ptr) json_nothrow {
79 #ifdef JSON_MEMORY_POOL
80 json_generic_mempool.deallocate(ptr);
82 if (json_free_t callback = JSONSingleton<json_free_t>::get()){
90 void * JSONMemory::json_realloc(
void * ptr,
size_t siz) json_nothrow {
91 #ifdef JSON_MEMORY_POOL
92 return json_generic_mempool.reallocate(ptr, siz);
94 if (json_realloc_t callback = JSONSingleton<json_realloc_t>::get()){
95 #if(defined(JSON_DEBUG) && (!defined(JSON_MEMORY_CALLBACKS))) //in debug mode without mem callback, see if the malloc was successful
96 void * result = callback(ptr, siz);
97 JSON_ASSERT(result, JSON_TEXT(
"Out of memory"));
100 return callback(ptr, siz);
103 #if(defined(JSON_DEBUG) && (!defined(JSON_MEMORY_CALLBACKS))) //in debug mode without mem callback, see if the malloc was successful
104 void * result = std::realloc(ptr, siz);
105 JSON_ASSERT(result, JSON_TEXT(
"Out of memory"));
108 return std::realloc(ptr, siz);
113 #ifdef JSON_MEMORY_POOL
115 static void * malloc_proxy(
size_t siz) json_nothrow {
116 if (json_malloc_t callback = JSONSingleton<json_malloc_t>::get()){
117 return callback(siz);
119 return std::malloc(siz);
122 static void * realloc_proxy(
void * ptr,
size_t siz) json_nothrow {
123 if (json_realloc_t callback = JSONSingleton<json_realloc_t>::get()){
124 return callback(ptr, siz);
126 return std::realloc(ptr, siz);
129 static void free_proxy(
void * ptr){
130 if (json_free_t callback = JSONSingleton<json_free_t>::get()){
139 void JSONMemory::registerMemoryCallbacks(json_malloc_t mal, json_realloc_t real, json_free_t fre) json_nothrow {
140 JSONSingleton<json_malloc_t>::set(mal);
141 JSONSingleton<json_realloc_t>::set(real);
142 JSONSingleton<json_free_t>::set(fre);
143 #ifdef JSON_MEMORY_POOL
144 mempool_callbacks::set(malloc_proxy, realloc_proxy, free_proxy);