17 using namespace shogun;
19 #ifdef TRACE_MEMORY_ALLOCS
22 MemoryBlock::MemoryBlock(
void* p) : ptr(p), size(0), file(NULL),
23 line(-1), is_sgobject(false)
27 MemoryBlock::MemoryBlock(
void* p,
size_t sz,
const char* fname,
int linenr) :
28 ptr(p), size(sz), file(fname), line(linenr), is_sgobject(false)
32 MemoryBlock::MemoryBlock(
const MemoryBlock &b)
38 is_sgobject=b.is_sgobject;
42 bool MemoryBlock::operator==(
const MemoryBlock &b)
const
47 void MemoryBlock::display()
51 printf(
"Memory block at %p of size %lld bytes (allocated in %s line %d)\n",
52 ptr, (
long long int) size, file, line);
59 printf(
"SGObject '%s' at %p of size %lld bytes with %d ref's\n",
60 obj->
get_name(), obj, (
long long int) size, obj->ref_count());
64 printf(
"Object at %p of size %lld bytes\n",
65 ptr, (
long long int) size);
70 void MemoryBlock::set_sgobject()
76 void*
operator new(
size_t size)
throw (std::bad_alloc)
79 #ifdef TRACE_MEMORY_ALLOCS
81 sg_mallocs->add(MemoryBlock(p,size));
85 const size_t buf_len=128;
87 size_t written=snprintf(buf, buf_len,
88 "Out of memory error, tried to allocate %lld bytes using new().\n", (
long long int) size);
98 void operator delete(
void *p)
throw()
100 #ifdef TRACE_MEMORY_ALLOCS
102 sg_mallocs->remove(MemoryBlock(p,
true));
107 void*
operator new[](
size_t size)
throw(std::bad_alloc)
109 void *p=malloc(size);
110 #ifdef TRACE_MEMORY_ALLOCS
112 sg_mallocs->add(MemoryBlock(p,size));
117 const size_t buf_len=128;
119 size_t written=snprintf(buf, buf_len,
120 "Out of memory error, tried to allocate %lld bytes using new[].\n", (
long long int) size);
130 void operator delete[](
void *p)
throw()
132 #ifdef TRACE_MEMORY_ALLOCS
134 sg_mallocs->remove(MemoryBlock(p,
false));
140 #ifdef TRACE_MEMORY_ALLOCS
141 ,
const char* file,
int line
145 void* p=malloc(size);
146 #ifdef TRACE_MEMORY_ALLOCS
148 sg_mallocs->add(MemoryBlock(p,size, file, line));
153 const size_t buf_len=128;
155 size_t written=snprintf(buf, buf_len,
156 "Out of memory error, tried to allocate %lld bytes using malloc.\n", (
long long int) size);
167 #ifdef TRACE_MEMORY_ALLOCS
168 ,
const char* file,
int line
172 void* p=calloc(num, size);
173 #ifdef TRACE_MEMORY_ALLOCS
175 sg_mallocs->add(MemoryBlock(p,size, file, line));
180 const size_t buf_len=128;
182 size_t written=snprintf(buf, buf_len,
183 "Out of memory error, tried to allocate %lld bytes using calloc.\n",
184 (
long long int) size);
197 #ifdef TRACE_MEMORY_ALLOCS
199 sg_mallocs->remove(MemoryBlock(ptr,
false));
205 #ifdef TRACE_MEMORY_ALLOCS
206 ,
const char* file,
int line
210 void* p=realloc(ptr, size);
212 #ifdef TRACE_MEMORY_ALLOCS
214 sg_mallocs->remove(MemoryBlock(ptr,
false));
217 sg_mallocs->add(MemoryBlock(p,size, file, line));
220 if (!p && (size || !ptr))
222 const size_t buf_len=128;
224 size_t written=snprintf(buf, buf_len,
225 "Out of memory error, tried to allocate %lld bytes using realloc.\n", (
long long int) size);
235 #ifdef TRACE_MEMORY_ALLOCS
236 void list_memory_allocs()
240 int32_t num=sg_mallocs->get_num_elements();
241 printf(
"%d Blocks are allocated:\n", num);
244 for (int32_t i=0; i<num; i++)
245 sg_mallocs->get_element(i).display();