23 #include <jemalloc/jemalloc.h>
25 #include <gperftools/tcmalloc.h>
28 using namespace shogun;
30 #ifdef TRACE_MEMORY_ALLOCS
33 MemoryBlock::MemoryBlock() : ptr(NULL), size(0), file(NULL),
34 line(-1), is_sgobject(false)
38 MemoryBlock::MemoryBlock(
void* p) : ptr(p), size(0), file(NULL),
39 line(-1), is_sgobject(false)
43 MemoryBlock::MemoryBlock(
void* p,
size_t sz,
const char* fname,
int linenr) :
44 ptr(p), size(sz), file(fname), line(linenr), is_sgobject(false)
48 MemoryBlock::MemoryBlock(
const MemoryBlock &b)
54 is_sgobject=b.is_sgobject;
58 bool MemoryBlock::operator==(
const MemoryBlock &b)
const
63 void MemoryBlock::display()
67 printf(
"Memory block at %p of size %lld bytes (allocated in %s line %d)\n",
68 ptr, (
long long int) size, file, line);
75 printf(
"SGObject '%s' at %p of size %lld bytes with %d ref's\n",
76 obj->
get_name(), obj, (
long long int) size, obj->ref_count());
80 printf(
"Object at %p of size %lld bytes\n",
81 ptr, (
long long int) size);
86 void MemoryBlock::set_sgobject()
93 void*
operator new(
size_t size)
95 void*
operator new(
size_t size)
throw (std::bad_alloc)
98 #if defined(USE_JEMALLOC)
99 void *p=je_malloc(size);
100 #elif defined(USE_TCMALLOC)
101 void *p=tc_malloc(size);
103 void *p=malloc(size);
106 #ifdef TRACE_MEMORY_ALLOCS
108 sg_mallocs->add(p, MemoryBlock(p,size));
112 const size_t buf_len=128;
114 size_t written=snprintf(buf, buf_len,
115 "Out of memory error, tried to allocate %lld bytes using new().\n", (
long long int) size);
125 void operator delete(
void *p)
throw()
127 #ifdef TRACE_MEMORY_ALLOCS
129 sg_mallocs->remove(p);
132 #if defined(USE_JEMALLOC)
134 #elif defined(USE_TCMALLOC)
142 void*
operator new[](
size_t size)
144 void*
operator new[](
size_t size)
throw(std::bad_alloc)
147 #if defined(USE_JEMALLOC)
148 void *p=je_malloc(size);
149 #elif defined(USE_TCMALLOC)
150 void *p=tc_malloc(size);
152 void *p=malloc(size);
155 #ifdef TRACE_MEMORY_ALLOCS
157 sg_mallocs->add(p, MemoryBlock(p,size));
162 const size_t buf_len=128;
164 size_t written=snprintf(buf, buf_len,
165 "Out of memory error, tried to allocate %lld bytes using new[].\n", (
long long int) size);
175 void operator delete[](
void *p)
throw()
177 #ifdef TRACE_MEMORY_ALLOCS
179 sg_mallocs->remove(p);
182 #if defined(USE_JEMALLOC)
184 #elif defined(USE_TCMALLOC)
194 #ifdef TRACE_MEMORY_ALLOCS
195 ,
const char* file,
int line
199 #if defined(USE_JEMALLOC)
200 void* p=je_malloc(size);
201 #elif defined(USE_TCMALLOC)
202 void *p=tc_malloc(size);
204 void* p=malloc(size);
206 #ifdef TRACE_MEMORY_ALLOCS
208 sg_mallocs->add(p, MemoryBlock(p,size, file, line));
213 const size_t buf_len=128;
215 size_t written=snprintf(buf, buf_len,
216 "Out of memory error, tried to allocate %lld bytes using malloc.\n", (
long long int) size);
227 #ifdef TRACE_MEMORY_ALLOCS
228 ,
const char* file,
int line
232 #if defined(USE_JEMALLOC)
233 void* p=je_calloc(num, size);
234 #elif defined(USE_TCMALLOC)
235 void* p=tc_calloc(num, size);
237 void* p=calloc(num, size);
240 #ifdef TRACE_MEMORY_ALLOCS
242 sg_mallocs->add(p, MemoryBlock(p,size, file, line));
247 const size_t buf_len=128;
249 size_t written=snprintf(buf, buf_len,
250 "Out of memory error, tried to allocate %lld bytes using calloc.\n",
251 (
long long int) size);
264 #ifdef TRACE_MEMORY_ALLOCS
266 sg_mallocs->remove(ptr);
269 #if defined(USE_JEMALLOC)
271 #elif defined(USE_TCMALLOC)
279 #ifdef TRACE_MEMORY_ALLOCS
280 ,
const char* file,
int line
284 #if defined(USE_JEMALLOC)
285 void* p=je_realloc(ptr, size);
286 #elif defined(USE_TCMALLOC)
287 void* p=tc_realloc(ptr, size);
289 void* p=realloc(ptr, size);
292 #ifdef TRACE_MEMORY_ALLOCS
294 sg_mallocs->remove(ptr);
297 sg_mallocs->add(p, MemoryBlock(p,size, file, line));
300 if (!p && (size || !ptr))
302 const size_t buf_len=128;
304 size_t written=snprintf(buf, buf_len,
305 "Out of memory error, tried to allocate %lld bytes using realloc.\n", (
long long int) size);
315 #ifdef TRACE_MEMORY_ALLOCS
316 void list_memory_allocs()
321 int32_t num=sg_mallocs->get_num_elements();
322 int32_t size=sg_mallocs->get_array_size();
323 printf(
"%d Blocks are allocated:\n", num);
326 for (int32_t i=0; i<size; i++)
328 temp=sg_mallocs->get_element_ptr(i);
336 #ifdef TRACE_MEMORY_ALLOCS
337 #define SG_SPECIALIZED_MALLOC(type) \
338 template<> type* sg_generic_malloc<type >(size_t len, const char* file, int line) \
340 return new type[len](); \
343 template<> type* sg_generic_calloc<type >(size_t len, const char* file, int line) \
345 return new type[len](); \
348 template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len, const char* file, int line) \
350 type* new_ptr = new type[len](); \
351 size_t min_len=old_len; \
354 for (size_t i=0; i<min_len; i++) \
360 template<> void sg_generic_free<type >(type* ptr) \
365 #else // TRACE_MEMORY_ALLOCS
367 #define SG_SPECIALIZED_MALLOC(type) \
368 template<> type* sg_generic_malloc<type >(size_t len) \
370 return new type[len](); \
373 template<> type* sg_generic_calloc<type >(size_t len) \
375 return new type[len](); \
378 template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len) \
380 type* new_ptr = new type[len](); \
381 size_t min_len=old_len; \
384 for (size_t i=0; i<min_len; i++) \
390 template<> void sg_generic_free<type >(type* ptr) \
394 #endif // TRACE_MEMORY_ALLOCS
440 #undef SG_SPECIALIZED_MALLOC
443 void* shogun::get_copy(
void* src,
size_t len)
445 void* copy=SG_MALLOC(uint8_t, len);
446 memcpy(copy, src, len);
450 char* shogun::get_strdup(
const char* str)
455 return (
char*) get_copy((
void*) str, strlen(str)+1);