17 using namespace shogun;
19 #ifdef TRACE_MEMORY_ALLOCS
22 MemoryBlock::MemoryBlock() : ptr(NULL), size(0), file(NULL),
23 line(-1), is_sgobject(false)
27 MemoryBlock::MemoryBlock(
void* p) : ptr(p), size(0), file(NULL),
28 line(-1), is_sgobject(false)
32 MemoryBlock::MemoryBlock(
void* p,
size_t sz,
const char* fname,
int linenr) :
33 ptr(p), size(sz), file(fname), line(linenr), is_sgobject(false)
37 MemoryBlock::MemoryBlock(
const MemoryBlock &b)
43 is_sgobject=b.is_sgobject;
47 bool MemoryBlock::operator==(
const MemoryBlock &b)
const
52 void MemoryBlock::display()
56 printf(
"Memory block at %p of size %lld bytes (allocated in %s line %d)\n",
57 ptr, (
long long int) size, file, line);
64 printf(
"SGObject '%s' at %p of size %lld bytes with %d ref's\n",
65 obj->
get_name(), obj, (
long long int) size, obj->ref_count());
69 printf(
"Object at %p of size %lld bytes\n",
70 ptr, (
long long int) size);
75 void MemoryBlock::set_sgobject()
81 void*
operator new(
size_t size)
throw (std::bad_alloc)
84 #ifdef TRACE_MEMORY_ALLOCS
86 sg_mallocs->add(p, MemoryBlock(p,size));
90 const size_t buf_len=128;
92 size_t written=snprintf(buf, buf_len,
93 "Out of memory error, tried to allocate %lld bytes using new().\n", (
long long int) size);
103 void operator delete(
void *p)
throw()
105 #ifdef TRACE_MEMORY_ALLOCS
107 sg_mallocs->remove(p);
112 void*
operator new[](
size_t size)
throw(std::bad_alloc)
114 void *p=malloc(size);
115 #ifdef TRACE_MEMORY_ALLOCS
117 sg_mallocs->add(p, MemoryBlock(p,size));
122 const size_t buf_len=128;
124 size_t written=snprintf(buf, buf_len,
125 "Out of memory error, tried to allocate %lld bytes using new[].\n", (
long long int) size);
135 void operator delete[](
void *p)
throw()
137 #ifdef TRACE_MEMORY_ALLOCS
139 sg_mallocs->remove(p);
145 #ifdef TRACE_MEMORY_ALLOCS
146 ,
const char* file,
int line
150 void* p=malloc(size);
151 #ifdef TRACE_MEMORY_ALLOCS
153 sg_mallocs->add(p, MemoryBlock(p,size, file, line));
158 const size_t buf_len=128;
160 size_t written=snprintf(buf, buf_len,
161 "Out of memory error, tried to allocate %lld bytes using malloc.\n", (
long long int) size);
172 #ifdef TRACE_MEMORY_ALLOCS
173 ,
const char* file,
int line
177 void* p=calloc(num, size);
178 #ifdef TRACE_MEMORY_ALLOCS
180 sg_mallocs->add(p, MemoryBlock(p,size, file, line));
185 const size_t buf_len=128;
187 size_t written=snprintf(buf, buf_len,
188 "Out of memory error, tried to allocate %lld bytes using calloc.\n",
189 (
long long int) size);
202 #ifdef TRACE_MEMORY_ALLOCS
204 sg_mallocs->remove(ptr);
210 #ifdef TRACE_MEMORY_ALLOCS
211 ,
const char* file,
int line
215 void* p=realloc(ptr, size);
217 #ifdef TRACE_MEMORY_ALLOCS
219 sg_mallocs->remove(ptr);
222 sg_mallocs->add(p, MemoryBlock(p,size, file, line));
225 if (!p && (size || !ptr))
227 const size_t buf_len=128;
229 size_t written=snprintf(buf, buf_len,
230 "Out of memory error, tried to allocate %lld bytes using realloc.\n", (
long long int) size);
240 #ifdef TRACE_MEMORY_ALLOCS
241 void list_memory_allocs()
246 int32_t num=sg_mallocs->get_num_elements();
247 int32_t size=sg_mallocs->get_array_size();
248 printf(
"%d Blocks are allocated:\n", num);
251 for (int32_t i=0; i<size; i++)
253 temp=sg_mallocs->get_element_ptr(i);