The R Project SVN R-packages

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7747 ripley 1
#include "JSONMemory.h"
2
 
3
#ifdef JSON_MEMORY_MANAGE
4
    #include "JSONNode.h"
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;  //because its pass by reference
9
			 libjson_free<void>(temp);
10
		  #else
11
			 libjson_free<void>((void*)i -> first);
12
		  #endif
13
	   }
14
    }
15
 
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);
19
	   }
20
    }
21
 
22
    #ifdef JSON_STREAM
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);
27
		  }
28
	   }
29
    #endif
30
#endif
31
 
32
#if defined(JSON_MEMORY_CALLBACKS) || defined(JSON_MEMORY_POOL)
33
 
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;
37
 
38
	//This class is only meant to initiate the mempool to start out using std::malloc/realloc/free
39
	class mempool_callback_setter {
40
    public:
41
		LIBJSON_OBJECT(mempool_callback_setter);
42
        inline mempool_callback_setter(void) json_nothrow {
43
		`	LIBJSON_CTOR;
44
            mempool_callbacks::set(std::malloc, std::realloc, std::free);
45
        }
46
    private:
47
        inline mempool_callback_setter(const mempool_callback_setter & o);
48
        inline mempool_callback_setter & operator = (const mempool_callback_setter & o);
49
    };
50
    static mempool_callback_setter __mempoolcallbacksetter;
51
#endif
52
 
53
#include "JSONSingleton.h"
54
 
55
void * JSONMemory::json_malloc(size_t siz) json_nothrow {
56
    #ifdef JSON_MEMORY_POOL
57
		return json_generic_mempool.allocate(siz);
58
	#else
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"));
63
		      return result;
64
	       #else
65
		      return callback(siz);
66
	       #endif
67
        }
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"));
71
	       return result;
72
        #else
73
	       return std::malloc(siz);
74
        #endif
75
    #endif
76
}
77
 
78
void JSONMemory::json_free(void * ptr) json_nothrow {
79
    #ifdef JSON_MEMORY_POOL
80
		json_generic_mempool.deallocate(ptr);
81
	#else
82
        if (json_free_t callback = JSONSingleton<json_free_t>::get()){
83
	       callback(ptr);
84
        } else {
85
	       std::free(ptr);
86
        }
87
    #endif
88
}
89
 
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);
93
    #else
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"));
98
	          return result;
99
           #else
100
	          return callback(ptr, siz);
101
           #endif
102
        }
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"));
106
           return result;
107
        #else
108
           return std::realloc(ptr, siz);
109
        #endif
110
    #endif
111
}
112
 
113
#ifdef JSON_MEMORY_POOL
114
    //it is okay to pass null to these callbacks, no make sure they function exists
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);
118
       }
119
       return std::malloc(siz);
120
    }
121
 
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);
125
       }
126
       return std::realloc(ptr, siz);
127
    }
128
 
129
    static void free_proxy(void * ptr){
130
        if (json_free_t callback = JSONSingleton<json_free_t>::get()){
131
	       callback(ptr);
132
        } else {
133
	       std::free(ptr);
134
        }
135
    }
136
#endif
137
 
138
 
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);
145
    #endif
146
}
147
 
148
 
149
#endif