Skip to content

Commit

Permalink
get TRACE_MALLOCS to compile again (but still crashes for some reason)
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Jul 26, 2011
1 parent 91c9490 commit 8f5a9eb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 13 deletions.
8 changes: 6 additions & 2 deletions src/shogun/base/DynArray.h
Expand Up @@ -69,7 +69,7 @@ template <class T> class DynArray
*
* @return total array size (including granularity buffer)
*/
inline int32_t get_array_size(void) const
inline int32_t get_array_size() const
{
return num_elements;
}
Expand All @@ -78,7 +78,7 @@ template <class T> class DynArray
*
* @return number of elements
*/
inline int32_t get_num_elements(void) const
inline int32_t get_num_elements() const
{
return last_element_idx+1;
}
Expand Down Expand Up @@ -273,11 +273,15 @@ template <class T> class DynArray
{
array=p;
if (new_num_elements > num_elements)
{
memset(&array[num_elements], 0,
(new_num_elements-num_elements)*sizeof(T));
}
else if (n+1<new_num_elements)
{
memset(&array[n+1], 0,
(new_num_elements-n-1)*sizeof(T));
}

//in case of shrinking we must adjust last element idx
if (n-1<last_element_idx)
Expand Down
7 changes: 4 additions & 3 deletions src/shogun/base/init.cpp
Expand Up @@ -15,15 +15,16 @@
#include <shogun/base/Parallel.h>
#include <shogun/base/Version.h>

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

namespace shogun
{
Parallel* sg_parallel=NULL;
SGIO* sg_io=NULL;
Version* sg_version=NULL;
CMath* sg_math=NULL;
#ifdef TRACE_MEMORY_ALLOCS
CSet<MemoryBlock>* sg_mallocs=NULL;
#endif

/// function called to print normal messages
void (*sg_print_message)(FILE* target, const char* str) = NULL;
Expand Down
9 changes: 4 additions & 5 deletions src/shogun/lib/Set.h
Expand Up @@ -13,7 +13,7 @@

#include <shogun/lib/common.h>
#include <shogun/mathematics/Math.h>
#include <shogun/lib/DynamicArray.h>
#include <shogun/base/DynArray.h>
#include <shogun/base/SGObject.h>

namespace shogun
Expand All @@ -29,14 +29,13 @@ template <class T> class CSet : public CSGObject
/** Default constructor */
CSet()
{
array = new CDynamicArray<T>();
SG_REF(array);
array = new DynArray<T>();
}

/** Default destructor */
~CSet()
{
SG_UNREF(array);
delete array;
}

/** Add an element to the set
Expand Down Expand Up @@ -109,7 +108,7 @@ template <class T> class CSet : public CSGObject

protected:
/** dynamic array the set is based on */
CDynamicArray<T>* array;
DynArray<T>* array;
};
}
#endif //_SET_H_
2 changes: 2 additions & 0 deletions src/shogun/lib/common.h
Expand Up @@ -43,6 +43,8 @@
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>

#undef __STDC_FORMAT_MACROS

/**
* Implementations tend to follow IEEE754
* @see http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=4610935
Expand Down
20 changes: 20 additions & 0 deletions src/shogun/lib/memory.cpp
Expand Up @@ -85,6 +85,10 @@ void operator delete[](void *p)
void* sg_malloc(size_t size)
{
void* p=malloc(size);
#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
sg_mallocs->add(MemoryBlock(p,size));
#endif

if (!p)
{
Expand All @@ -104,6 +108,10 @@ void* sg_malloc(size_t size)
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));
#endif

if (!p)
{
Expand All @@ -124,13 +132,25 @@ void* sg_calloc(size_t num, size_t size)

void sg_free(void* ptr)
{
#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
sg_mallocs->remove(MemoryBlock(ptr));
#endif
free(ptr);
}

void* sg_realloc(void* ptr, size_t size)
{
void* p=realloc(ptr, size);

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

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

if (!p && (size || !ptr))
{
const size_t buf_len=128;
Expand Down
14 changes: 11 additions & 3 deletions src/shogun/lib/memory.h
Expand Up @@ -43,18 +43,24 @@ class MemoryBlock
{
ptr=p;
size=0;
file=NULL;
line=NULL;
}

MemoryBlock(void* p, size_t sz)
MemoryBlock(void* p, size_t sz, const char* fname=NULL, const char* lineinfo=NULL)
{
ptr=p;
size=sz;
file=fname;
line=lineinfo;
}

MemoryBlock(const MemoryBlock &b)
{
ptr=b.ptr;
size=b.size;
file=b.file;
line=b.line;
}


Expand All @@ -65,15 +71,17 @@ class MemoryBlock

void display()
{
printf("Object at %p of size %lld bytes\n", ptr, (long long int) size);
printf("Object at %p of size %lld bytes (allocated in %s:%s)\n", ptr, (long long int) size, line, file);
}

protected:
void* ptr;
size_t size;
const char* file;
const char* line;
};
}

void list_memory_allocs();
}
#endif
#endif // __MEMORY_H__
4 changes: 4 additions & 0 deletions src/shogun/ui/SGInterface.cpp
Expand Up @@ -7,6 +7,7 @@
#include <shogun/lib/ShogunException.h>
#include <shogun/mathematics/Math.h>
#include <shogun/lib/Hash.h>
#include <shogun/lib/Set.h>
#include <shogun/lib/Signal.h>

#include <shogun/classifier/svm/SVM.h>
Expand Down Expand Up @@ -7531,6 +7532,9 @@ bool CSGInterface::cmd_help()

return true;
}
#ifdef TRACE_MEMORY_ALLOCS
extern CSet<MemoryBlock>* sg_mallocs;
#endif

bool CSGInterface::cmd_whos()
{
Expand Down

0 comments on commit 8f5a9eb

Please sign in to comment.