11 #include <shogun/lib/config.h> 23 #include <jemalloc/jemalloc.h> 25 #include <gperftools/tcmalloc.h> 30 #ifdef TRACE_MEMORY_ALLOCS 34 MemoryBlock::MemoryBlock() : ptr(NULL), size(0), file(NULL),
35 line(-1), is_sgobject(false)
39 MemoryBlock::MemoryBlock(
void* p) : ptr(p), size(0), file(NULL),
40 line(-1), is_sgobject(false)
44 MemoryBlock::MemoryBlock(
void* p,
size_t sz,
const char* fname,
int linenr) :
45 ptr(p), size(sz), file(fname), line(linenr), is_sgobject(false)
49 MemoryBlock::MemoryBlock(
const MemoryBlock &b)
55 is_sgobject=b.is_sgobject;
64 void MemoryBlock::display()
68 printf(
"Memory block at %p of size %lld bytes (allocated in %s line %d)\n",
69 ptr, (
long long int) size, file, line);
76 printf(
"SGObject '%s' at %p of size %lld bytes with %d ref's\n",
77 obj->
get_name(), obj, (
long long int) size, obj->ref_count());
81 printf(
"Object at %p of size %lld bytes\n",
82 ptr, (
long long int) size);
87 void MemoryBlock::set_sgobject()
94 void*
operator new(
size_t size)
96 void*
operator new(
size_t size)
throw (std::bad_alloc)
99 #if defined(USE_JEMALLOC) 100 void *p=je_malloc(size);
101 #elif defined(USE_TCMALLOC) 102 void *p=tc_malloc(size);
104 void *p=malloc(size);
107 #ifdef TRACE_MEMORY_ALLOCS 109 sg_mallocs->
add(p, MemoryBlock(p,size));
113 const size_t buf_len=128;
115 size_t written=snprintf(buf, buf_len,
116 "Out of memory error, tried to allocate %lld bytes using new().\n", (
long long int) size);
126 void operator delete(
void *p)
throw()
128 #ifdef TRACE_MEMORY_ALLOCS 133 #if defined(USE_JEMALLOC) 135 #elif defined(USE_TCMALLOC) 143 void*
operator new[](
size_t size)
145 void*
operator new[](
size_t size)
throw(std::bad_alloc)
148 #if defined(USE_JEMALLOC) 149 void *p=je_malloc(size);
150 #elif defined(USE_TCMALLOC) 151 void *p=tc_malloc(size);
153 void *p=malloc(size);
156 #ifdef TRACE_MEMORY_ALLOCS 158 sg_mallocs->
add(p, MemoryBlock(p,size));
163 const size_t buf_len=128;
165 size_t written=snprintf(buf, buf_len,
166 "Out of memory error, tried to allocate %lld bytes using new[].\n", (
long long int) size);
176 void operator delete[](
void *p)
throw()
178 #ifdef TRACE_MEMORY_ALLOCS 183 #if defined(USE_JEMALLOC) 185 #elif defined(USE_TCMALLOC) 195 #ifdef TRACE_MEMORY_ALLOCS
196 ,
const char* file,
int line
200 #if defined(USE_JEMALLOC) 201 void* p=je_malloc(size);
202 #elif defined(USE_TCMALLOC) 203 void *p=tc_malloc(size);
205 void* p=malloc(size);
207 #ifdef TRACE_MEMORY_ALLOCS 209 sg_mallocs->
add(p, MemoryBlock(p,size, file, line));
214 const size_t buf_len=128;
216 size_t written=snprintf(buf, buf_len,
217 "Out of memory error, tried to allocate %lld bytes using malloc.\n", (
long long int) size);
228 #ifdef TRACE_MEMORY_ALLOCS
229 ,
const char* file,
int line
233 #if defined(USE_JEMALLOC) 234 void* p=je_calloc(num, size);
235 #elif defined(USE_TCMALLOC) 236 void* p=tc_calloc(num, size);
238 void* p=calloc(num, size);
241 #ifdef TRACE_MEMORY_ALLOCS 243 sg_mallocs->
add(p, MemoryBlock(p,size, file, line));
248 const size_t buf_len=128;
250 size_t written=snprintf(buf, buf_len,
251 "Out of memory error, tried to allocate %lld bytes using calloc.\n",
252 (
long long int) size);
265 #ifdef TRACE_MEMORY_ALLOCS 270 #if defined(USE_JEMALLOC) 272 #elif defined(USE_TCMALLOC) 280 #ifdef TRACE_MEMORY_ALLOCS
281 ,
const char* file,
int line
285 #if defined(USE_JEMALLOC) 286 void* p=je_realloc(ptr, size);
287 #elif defined(USE_TCMALLOC) 288 void* p=tc_realloc(ptr, size);
290 void* p=realloc(ptr, size);
293 #ifdef TRACE_MEMORY_ALLOCS 298 sg_mallocs->
add(p, MemoryBlock(p,size, file, line));
301 if (!p && (size || !ptr))
303 const size_t buf_len=128;
305 size_t written=snprintf(buf, buf_len,
306 "Out of memory error, tried to allocate %lld bytes using realloc.\n", (
long long int) size);
316 #ifdef TRACE_MEMORY_ALLOCS 317 void list_memory_allocs()
324 printf(
"%d Blocks are allocated:\n", num);
327 for (int32_t i=0; i<size; i++)
337 #ifdef TRACE_MEMORY_ALLOCS 338 #define SG_SPECIALIZED_MALLOC(type) \ 339 template<> type* sg_generic_malloc<type >(size_t len, const char* file, int line) \ 341 return new type[len](); \ 344 template<> type* sg_generic_calloc<type >(size_t len, const char* file, int line) \ 346 return new type[len](); \ 349 template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len, const char* file, int line) \ 351 type* new_ptr = new type[len](); \ 352 size_t min_len=old_len; \ 355 for (size_t i=0; i<min_len; i++) \ 361 template<> void sg_generic_free<type >(type* ptr) \ 366 #else // TRACE_MEMORY_ALLOCS 368 #define SG_SPECIALIZED_MALLOC(type) \ 369 template<> type* sg_generic_malloc<type >(size_t len) \ 371 return new type[len](); \ 374 template<> type* sg_generic_calloc<type >(size_t len) \ 376 return new type[len](); \ 379 template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len) \ 381 type* new_ptr = new type[len](); \ 382 size_t min_len=old_len; \ 385 for (size_t i=0; i<min_len; i++) \ 391 template<> void sg_generic_free<type >(type* ptr) \ 395 #endif // TRACE_MEMORY_ALLOCS 441 #undef SG_SPECIALIZED_MALLOC 444 void* shogun::get_copy(
void* src,
size_t len)
446 void* copy=SG_MALLOC(uint8_t, len);
447 sg_memcpy(copy, src, len);
451 char* shogun::get_strdup(
const char* str)
456 return (
char*) get_copy((
void*) str, strlen(str)+1);
virtual const char * get_name() const =0
void * sg_calloc(size_t num, size_t size)
int32_t get_array_size() const
bool operator==(const Any &lhs, const Any &rhs)
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
void remove(const K &key)
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry<T>* vector is orde...
void * sg_malloc(size_t size)
Class SGObject is the base class of all shogun objects.
int32_t get_num_elements() const
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
T * get_element_ptr(int32_t index)
all of classes and functions are contained in the shogun namespace
int32_t add(const K &key, const T &data)
#define SG_SPECIALIZED_MALLOC(type)
void * sg_realloc(void *ptr, size_t size)