Skip to content

Commit

Permalink
CSet<MemoryBlock> to CMap<void*, MemoryBlock>
Browse files Browse the repository at this point in the history
  • Loading branch information
gsomix committed May 7, 2012
1 parent 15cb774 commit 074c5da
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -1021,8 +1021,8 @@ void CSGObject::save_serializable_post() throw (ShogunException)
}

#ifdef TRACE_MEMORY_ALLOCS
#include <shogun/lib/Set.h>
extern CSet<shogun::MemoryBlock>* sg_mallocs;
#include <shogun/lib/Map.h>
extern CMap<void*, shogun::MemoryBlock>* sg_mallocs;
#endif

void CSGObject::init()
Expand All @@ -1034,7 +1034,7 @@ void CSGObject::init()
#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
{
int32_t idx=sg_mallocs->index_of(MemoryBlock(this));
int32_t idx=sg_mallocs->index_of(this);
if (idx>-1)
{
MemoryBlock* b=sg_mallocs->get_element_ptr(idx);
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/base/init.cpp
Expand Up @@ -11,12 +11,12 @@
#include <shogun/base/init.h>
#include <shogun/mathematics/Math.h>
#include <shogun/lib/memory.h>
#include <shogun/lib/Set.h>
#include <shogun/lib/Map.h>
#include <shogun/base/Parallel.h>
#include <shogun/base/Version.h>

#ifdef TRACE_MEMORY_ALLOCS
shogun::CSet<shogun::MemoryBlock>* sg_mallocs=NULL;
shogun::CMap<void*, shogun::MemoryBlock>* sg_mallocs=NULL;
#endif

namespace shogun
Expand Down Expand Up @@ -54,7 +54,7 @@ namespace shogun
sg_math = new shogun::CMath();
#ifdef TRACE_MEMORY_ALLOCS
if (!sg_mallocs)
sg_mallocs = new shogun::CSet<MemoryBlock>(631, 1024, false);
sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false);

SG_REF(sg_mallocs);
#endif
Expand Down Expand Up @@ -84,7 +84,7 @@ namespace shogun
{
#ifdef TRACE_MEMORY_ALLOCS
list_memory_allocs();
shogun::CSet<shogun::MemoryBlock>* mallocs=sg_mallocs;
shogun::CMap<void*, shogun::MemoryBlock>* mallocs=sg_mallocs;
sg_mallocs=NULL;
SG_UNREF(mallocs);
#endif
Expand Down
29 changes: 17 additions & 12 deletions src/shogun/lib/memory.cpp
Expand Up @@ -11,13 +11,13 @@
#include <shogun/lib/ShogunException.h>
#include <shogun/lib/memory.h>
#include <shogun/lib/common.h>
#include <shogun/lib/Set.h>
#include <shogun/lib/Map.h>
#include <shogun/base/SGObject.h>

using namespace shogun;

#ifdef TRACE_MEMORY_ALLOCS
extern CSet<shogun::MemoryBlock>* sg_mallocs;
extern CMap<void*, shogun::MemoryBlock>* sg_mallocs;

MemoryBlock::MemoryBlock(void* p) : ptr(p), size(0), file(NULL),
line(-1), is_sgobject(false)
Expand Down Expand Up @@ -78,7 +78,7 @@ void* operator new(size_t size) throw (std::bad_alloc)
void *p=malloc(size);
#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
sg_mallocs->add(MemoryBlock(p,size));
sg_mallocs->add(p, MemoryBlock(p,size));
#endif
if (!p)
{
Expand All @@ -99,7 +99,7 @@ void operator delete(void *p) throw()
{
#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
sg_mallocs->remove(MemoryBlock(p, true));
sg_mallocs->remove(p);
#endif
free(p);
}
Expand All @@ -109,7 +109,7 @@ void* operator new[](size_t size) throw(std::bad_alloc)
void *p=malloc(size);
#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
sg_mallocs->add(MemoryBlock(p,size));
sg_mallocs->add(p, MemoryBlock(p,size));
#endif

if (!p)
Expand All @@ -131,7 +131,7 @@ void operator delete[](void *p) throw()
{
#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
sg_mallocs->remove(MemoryBlock(p, false));
sg_mallocs->remove(p);
#endif
free(p);
}
Expand All @@ -145,7 +145,7 @@ void* sg_malloc(size_t size
void* p=malloc(size);
#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
sg_mallocs->add(MemoryBlock(p,size, file, line));
sg_mallocs->add(p, MemoryBlock(p,size, file, line));
#endif

if (!p)
Expand All @@ -172,7 +172,7 @@ void* sg_calloc(size_t num, size_t size
void* p=calloc(num, size);
#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
sg_mallocs->add(MemoryBlock(p,size, file, line));
sg_mallocs->add(p, MemoryBlock(p,size, file, line));
#endif

if (!p)
Expand All @@ -196,7 +196,7 @@ void sg_free(void* ptr)
{
#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
sg_mallocs->remove(MemoryBlock(ptr, false));
sg_mallocs->remove(ptr);
#endif
free(ptr);
}
Expand All @@ -211,10 +211,10 @@ void* sg_realloc(void* ptr, size_t size

#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
sg_mallocs->remove(MemoryBlock(ptr, false));
sg_mallocs->remove(ptr);

if (sg_mallocs)
sg_mallocs->add(MemoryBlock(p,size, file, line));
sg_mallocs->add(p, MemoryBlock(p,size, file, line));
#endif

if (!p && (size || !ptr))
Expand All @@ -235,14 +235,19 @@ void* sg_realloc(void* ptr, size_t size
#ifdef TRACE_MEMORY_ALLOCS
void list_memory_allocs()
{
MemoryBlock* temp;
if (sg_mallocs)
{
int32_t num=sg_mallocs->get_num_elements();
printf("%d Blocks are allocated:\n", num);


for (int32_t i=0; i<num; i++)
sg_mallocs->get_element(i).display();
{
temp=sg_mallocs->get_element_ptr(i);
if (temp!=NULL)
temp->display();
}
}
}
#endif
4 changes: 2 additions & 2 deletions src/shogun/ui/SGInterface.cpp
Expand Up @@ -9,7 +9,7 @@
#include <shogun/lib/ShogunException.h>
#include <shogun/mathematics/Math.h>
#include <shogun/lib/Hash.h>
#include <shogun/lib/Set.h>
#include <shogun/lib/Map.h>
#include <shogun/lib/Signal.h>

#include <shogun/classifier/svm/SVM.h>
Expand Down Expand Up @@ -7618,7 +7618,7 @@ bool CSGInterface::cmd_help()
return true;
}
#ifdef TRACE_MEMORY_ALLOCS
extern CSet<MemoryBlock>* sg_mallocs;
extern CMap<void*, MemoryBlock>* sg_mallocs;
#endif

bool CSGInterface::cmd_whos()
Expand Down

0 comments on commit 074c5da

Please sign in to comment.