8 #if defined(JSON_DEBUG) || defined(JSON_SAFE)
9 #define JSON_FREE_PASSTYPE &
11 #define JSON_FREE_PASSTYPE
14 #if defined(JSON_MEMORY_CALLBACKS) || defined(JSON_MEMORY_POOL)
17 static void * json_malloc(
size_t siz) json_malloc_attr;
18 static void * json_realloc(
void * ptr,
size_t siz) json_malloc_attr;
19 static void json_free(
void * ptr) json_nothrow;
20 static void registerMemoryCallbacks(json_malloc_t mal, json_realloc_t real, json_free_t fre) json_nothrow json_cold;
25 template <
typename T>
static inline T * json_malloc(
size_t count) json_malloc_attr;
26 template <
typename T>
static inline T * json_malloc(
size_t count) json_nothrow {
27 return (T *)JSONMemory::json_malloc(
sizeof(T) * count);
30 template <
typename T>
static inline T * json_realloc(T * ptr,
size_t count) json_malloc_attr;
31 template <
typename T>
static inline T * json_realloc(T * ptr,
size_t count) json_nothrow {
32 return (T *)JSONMemory::json_realloc(ptr,
sizeof(T) * count);
35 template <
typename T>
static inline void libjson_free(T * JSON_FREE_PASSTYPE ptr) json_nothrow {
36 JSONMemory::json_free(ptr);
37 #if defined(JSON_DEBUG) || defined(JSON_SAFE) //in debug or safe mode, set the pointer to 0 so that it can't be used again
43 template <
typename T>
static inline T * json_malloc(
size_t count) json_malloc_attr;
44 template <
typename T>
static inline T * json_malloc(
size_t count) json_nothrow {
45 #ifdef JSON_DEBUG //in debug mode, see if the malloc was successful
46 void * result = std::malloc(count *
sizeof(T));
47 JSON_ASSERT(result != 0, JSON_TEXT(
"Out of memory"));
48 #ifdef JSON_NULL_MEMORY
49 std::memset(result,
'\0', count *
sizeof(T));
53 return (T *)std::malloc(count *
sizeof(T));
57 template <
typename T>
static inline void libjson_free(T * JSON_FREE_PASSTYPE ptr) json_nothrow {
59 #if defined(JSON_DEBUG) || defined(JSON_SAFE) //in debug or safe mode, set the pointer to 0 so that it can't be used again
64 template <
typename T>
static inline T * json_realloc(T * ptr,
size_t count) json_malloc_attr;
65 template <
typename T>
static inline T * json_realloc(T * ptr,
size_t count) json_nothrow {
66 #ifdef JSON_DEBUG //in debug mode, check the results of realloc to be sure it was successful
67 void * result = std::realloc(ptr, count *
sizeof(T));
68 JSON_ASSERT(result != 0, JSON_TEXT(
"Out of memory"));
71 return (T *)std::realloc(ptr, count *
sizeof(T));
76 #ifdef JSON_MEMORY_MANAGE
82 auto_expand(
void) json_nothrow : mymap(){ LIBJSON_CTOR;}
83 ~
auto_expand(
void) json_nothrow { purge(); LIBJSON_DTOR; }
84 void purge(
void) json_nothrow;
85 inline void clear(
void) json_nothrow { purge(); mymap.clear(); }
86 inline void * insert(
void * ptr) json_nothrow { mymap[ptr] = ptr;
return ptr; }
87 inline void remove(
void * ptr) json_nothrow {
88 JSON_MAP(
void *,
void *)::iterator i = mymap.find(ptr);
89 JSON_ASSERT(i != mymap.end(), JSON_TEXT(
"Removing a non-managed item"));
92 JSON_MAP(
void *,
void *) mymap;
103 void purge(
void) json_nothrow ;
104 inline void clear(
void) json_nothrow { purge(); mymap.clear(); }
105 inline JSONNode * insert(
JSONNode * ptr) json_nothrow { mymap[ptr] = ptr;
return ptr; }
106 inline void remove(
void * ptr) json_nothrow {
107 JSON_MAP(
void *,
JSONNode *)::iterator i = mymap.find(ptr);
108 if(json_likely(i != mymap.end())) mymap.erase(i);
123 void purge(
void) json_nothrow ;
124 inline void clear(
void) json_nothrow { purge(); mymap.clear(); }
126 inline void remove(
void * ptr) json_nothrow {
127 JSON_MAP(
void *,
JSONStream *)::iterator i = mymap.find(ptr);
128 if(json_likely(i != mymap.end())) mymap.erase(i);
139 template <
typename T>
143 json_auto(
void) json_nothrow : ptr(0){ LIBJSON_CTOR; }
144 json_auto(
size_t count) json_nothrow : ptr(json_malloc<T>(count)){ LIBJSON_CTOR; }
145 json_auto(T * arg) json_nothrow : ptr(arg){ LIBJSON_CTOR; }
147 libjson_free<T>(ptr);
150 inline void set(T * p) json_nothrow{
160 static inline void clearString(
json_string & str) json_nothrow {
161 #ifdef JSON_LESS_MEMORY
169 static inline void shrinkString(
json_string & str) json_nothrow {
170 #ifdef JSON_LESS_MEMORY
171 if (str.capacity() != str.length()) str =
json_string(str.begin(), str.end());
Definition: JSONStream.h:25
Definition: JSONMemory.h:118
Definition: JSONMemory.h:140
Definition: StringTest.h:22
Definition: JSONNode.h:132
Definition: JSONMemory.h:79
Definition: JSONMemory.h:98