19 #ifndef __HERMES_COMMON_MEMORY_HANDLING_H
20 #define __HERMES_COMMON_MEMORY_HANDLING_H
33 #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 7) || (__GNUC__ >= 5))
34 # define HACK_GCC_ITS_CPP0X 1
36 #if defined(nullptr_t) || (__cplusplus >= 199711L) || defined(HACK_GCC_ITS_CPP0X)
37 #include <type_traits>
39 template<
typename ArrayItem>
43 static const bool value =
true;
45 #define static_assert(expr, msg) true
51 HERMES_COMMON_API
extern pj_caching_pool HermesCommonMemoryPoolCache;
58 pj_caching_pool_init(&HermesCommonMemoryPoolCache, NULL, 1024 * 1024 * 1024);
60 pj_thread_desc rtpdesc;
62 pj_bzero(rtpdesc,
sizeof(rtpdesc));
63 if (!pj_thread_is_registered())
64 pj_thread_register(NULL, rtpdesc, &thread);
66 this->globalPool = pj_pool_create(&HermesCommonMemoryPoolCache.factory,
75 pj_pool_release(this->globalPool);
76 pj_caching_pool_destroy(&HermesCommonMemoryPoolCache);
79 inline void* pool_calloc(pj_size_t count, pj_size_t elem)
81 return pj_pool_calloc(globalPool, count, elem);
84 inline void* pool_alloc(pj_size_t size)
86 return pj_pool_alloc(globalPool, size);
89 pj_pool_t* globalPool;
91 HERMES_COMMON_API
extern GlobalPoolCache hermesCommonGlobalPoolCache;
94 template<
typename Caller,
typename ArrayItem>
95 ArrayItem* calloc_with_check(
int size, Caller*
const caller,
bool force_malloc =
false)
101 new_array = (ArrayItem*)calloc(size,
sizeof(ArrayItem));
105 new_array = (ArrayItem*)hermesCommonGlobalPoolCache.pool_calloc(size,
sizeof(ArrayItem));
107 new_array =
new ArrayItem[size];
108 memset(new_array, 0,
sizeof(ArrayItem)* size);
121 template<
typename ArrayItem>
122 ArrayItem* calloc_with_check(
int size,
bool force_malloc =
false)
126 ArrayItem* new_array;
128 new_array = (ArrayItem*)calloc(size,
sizeof(ArrayItem));
132 new_array = (ArrayItem*)hermesCommonGlobalPoolCache.pool_calloc(size,
sizeof(ArrayItem));
134 new_array =
new ArrayItem[size];
135 memset(new_array, 0,
sizeof(ArrayItem)* size);
147 template<
typename Caller,
typename ArrayItem>
148 ArrayItem* malloc_with_check(
int size, Caller*
const caller,
bool force_malloc =
false)
153 ArrayItem* new_array;
156 new_array = (ArrayItem*)malloc(size *
sizeof(ArrayItem));
159 new_array = (ArrayItem*)hermesCommonGlobalPoolCache.pool_alloc(size *
sizeof(ArrayItem));
161 new_array =
new ArrayItem[size];
174 template<
typename ArrayItem>
175 ArrayItem* malloc_with_check(
int size,
bool force_malloc =
false)
179 ArrayItem* new_array;
181 new_array = (ArrayItem*)malloc(size *
sizeof(ArrayItem));
184 new_array = (ArrayItem*)hermesCommonGlobalPoolCache.pool_alloc(size *
sizeof(ArrayItem));
186 new_array =
new ArrayItem[size];
197 template<
typename ArrayItem>
198 ArrayItem* malloc_with_check_direct_size(
int size)
202 ArrayItem* new_array;
204 new_array = (ArrayItem*)hermesCommonGlobalPoolCache.pool_alloc(size *
sizeof(ArrayItem));
206 new_array = (ArrayItem*)malloc(size);
217 template<
typename ArrayItem>
218 ArrayItem* calloc_with_check_direct_size(
int size)
222 ArrayItem* new_array;
224 new_array = (ArrayItem*)hermesCommonGlobalPoolCache.pool_calloc(size *
sizeof(ArrayItem));
226 new_array = (ArrayItem*)malloc(size);
228 memset(new_array, 0, size);
239 template<
typename Caller,
typename ArrayItem>
240 ArrayItem* realloc_with_check(ArrayItem*& original_array,
int new_size, Caller*
const caller)
245 ArrayItem* new_array = (ArrayItem*)realloc(original_array, new_size *
sizeof(ArrayItem));
247 return original_array = new_array;
256 template<
typename ArrayItem>
257 ArrayItem* realloc_with_check(ArrayItem*& original_array,
int new_size)
262 ArrayItem* new_array = (ArrayItem*)realloc(original_array, new_size *
sizeof(ArrayItem));
264 return original_array = new_array;
272 template<
typename ArrayItem>
273 void free_with_check(ArrayItem*& ptr,
bool force_malloc =
false)
General namespace for the Hermes library.
Exception interface Basically a std::exception, but with a constructor with string and with print_msg...
File containing definition of exceptions classes.