JSONMemory.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "JSONMemory.h"
  2. #ifdef JSON_MEMORY_MANAGE
  3. #include "JSONNode.h"
  4. void auto_expand::purge(void) json_nothrow {
  5. for(JSON_MAP(void *, void *)::iterator i = mymap.begin(), en = mymap.end(); i != en; ++i){
  6. #if defined(JSON_DEBUG) || defined(JSON_SAFE)
  7. void * temp = (void*)i -> first; //because its pass by reference
  8. libjson_free<void>(temp);
  9. #else
  10. libjson_free<void>((void*)i -> first);
  11. #endif
  12. }
  13. }
  14. void auto_expand_node::purge(void) json_nothrow {
  15. for(JSON_MAP(void *, JSONNode *)::iterator i = mymap.begin(), en = mymap.end(); i != en; ++i){
  16. JSONNode::deleteJSONNode((JSONNode *)i -> second);
  17. }
  18. }
  19. #ifdef JSON_STREAM
  20. #include "JSONStream.h"
  21. void auto_expand_stream::purge(void) json_nothrow {
  22. for(JSON_MAP(void *, JSONStream *)::iterator i = mymap.begin(), en = mymap.end(); i != en; ++i){
  23. JSONStream::deleteJSONStream((JSONStream *)i -> second);
  24. }
  25. }
  26. #endif
  27. #endif
  28. #if defined(JSON_MEMORY_CALLBACKS) || defined(JSON_MEMORY_POOL)
  29. #ifdef JSON_MEMORY_POOL
  30. #include "JSONMemoryPool.h"
  31. static bucket_pool_8<MEMPOOL_1, MEMPOOL_2, MEMPOOL_3, MEMPOOL_4, MEMPOOL_5, MEMPOOL_6, MEMPOOL_7, MEMPOOL_8> json_generic_mempool;
  32. //This class is only meant to initiate the mempool to start out using std::malloc/realloc/free
  33. class mempool_callback_setter {
  34. public:
  35. LIBJSON_OBJECT(mempool_callback_setter);
  36. inline mempool_callback_setter(void) json_nothrow {
  37. ` LIBJSON_CTOR;
  38. mempool_callbacks::set(std::malloc, std::realloc, std::free);
  39. }
  40. private:
  41. inline mempool_callback_setter(const mempool_callback_setter & o);
  42. inline mempool_callback_setter & operator = (const mempool_callback_setter & o);
  43. };
  44. static mempool_callback_setter __mempoolcallbacksetter;
  45. #endif
  46. #include "JSONSingleton.h"
  47. void * JSONMemory::json_malloc(size_t siz) json_nothrow {
  48. #ifdef JSON_MEMORY_POOL
  49. return json_generic_mempool.allocate(siz);
  50. #else
  51. if (json_malloc_t callback = JSONSingleton<json_malloc_t>::get()){
  52. #if(defined(JSON_DEBUG) && (!defined(JSON_MEMORY_CALLBACKS))) //in debug mode without mem callback, see if the malloc was successful
  53. void * result = callback(siz);
  54. JSON_ASSERT(result, JSON_TEXT("Out of memory"));
  55. return result;
  56. #else
  57. return callback(siz);
  58. #endif
  59. }
  60. #if(defined(JSON_DEBUG) && (!defined(JSON_MEMORY_CALLBACKS))) //in debug mode without mem callback, see if the malloc was successful
  61. void * result = std::malloc(siz);
  62. JSON_ASSERT(result, JSON_TEXT("Out of memory"));
  63. return result;
  64. #else
  65. return std::malloc(siz);
  66. #endif
  67. #endif
  68. }
  69. void JSONMemory::json_free(void * ptr) json_nothrow {
  70. #ifdef JSON_MEMORY_POOL
  71. json_generic_mempool.deallocate(ptr);
  72. #else
  73. if (json_free_t callback = JSONSingleton<json_free_t>::get()){
  74. callback(ptr);
  75. } else {
  76. std::free(ptr);
  77. }
  78. #endif
  79. }
  80. void * JSONMemory::json_realloc(void * ptr, size_t siz) json_nothrow {
  81. #ifdef JSON_MEMORY_POOL
  82. return json_generic_mempool.reallocate(ptr, siz);
  83. #else
  84. if (json_realloc_t callback = JSONSingleton<json_realloc_t>::get()){
  85. #if(defined(JSON_DEBUG) && (!defined(JSON_MEMORY_CALLBACKS))) //in debug mode without mem callback, see if the malloc was successful
  86. void * result = callback(ptr, siz);
  87. JSON_ASSERT(result, JSON_TEXT("Out of memory"));
  88. return result;
  89. #else
  90. return callback(ptr, siz);
  91. #endif
  92. }
  93. #if(defined(JSON_DEBUG) && (!defined(JSON_MEMORY_CALLBACKS))) //in debug mode without mem callback, see if the malloc was successful
  94. void * result = std::realloc(ptr, siz);
  95. JSON_ASSERT(result, JSON_TEXT("Out of memory"));
  96. return result;
  97. #else
  98. return std::realloc(ptr, siz);
  99. #endif
  100. #endif
  101. }
  102. #ifdef JSON_MEMORY_POOL
  103. //it is okay to pass null to these callbacks, no make sure they function exists
  104. static void * malloc_proxy(size_t siz) json_nothrow {
  105. if (json_malloc_t callback = JSONSingleton<json_malloc_t>::get()){
  106. return callback(siz);
  107. }
  108. return std::malloc(siz);
  109. }
  110. static void * realloc_proxy(void * ptr, size_t siz) json_nothrow {
  111. if (json_realloc_t callback = JSONSingleton<json_realloc_t>::get()){
  112. return callback(ptr, siz);
  113. }
  114. return std::realloc(ptr, siz);
  115. }
  116. static void free_proxy(void * ptr){
  117. if (json_free_t callback = JSONSingleton<json_free_t>::get()){
  118. callback(ptr);
  119. } else {
  120. std::free(ptr);
  121. }
  122. }
  123. #endif
  124. void JSONMemory::registerMemoryCallbacks(json_malloc_t mal, json_realloc_t real, json_free_t fre) json_nothrow {
  125. JSONSingleton<json_malloc_t>::set(mal);
  126. JSONSingleton<json_realloc_t>::set(real);
  127. JSONSingleton<json_free_t>::set(fre);
  128. #ifdef JSON_MEMORY_POOL
  129. mempool_callbacks::set(malloc_proxy, realloc_proxy, free_proxy);
  130. #endif
  131. }
  132. #endif