Added osg::MemoryManger which is currently based on Paul Nettle's memory manager

published at flipcode.  I havn't adopted Paul's macro's for new/delete etc, but use
osg prefixed versions instead to allow greater flexiblity in handling include
ordering.

Have fixed a couple of new/delete[] problems which existed as a result.

To use the MemoryManager to track memory usage simply add
-DOSG_USE_MEMEORY_TRACKING to the compile line.

Have yet to move the osg from using new to osgNew, will do this next.
This commit is contained in:
Robert Osfield
2002-03-25 23:18:02 +00:00
parent 51b1b1eb38
commit 651a22453a
12 changed files with 1784 additions and 13 deletions

View File

@@ -11,6 +11,7 @@ YFLAGS = -d
LCINCS += -I/usr/local/include -I/usr/X11R6/include
LC++INCS += ${LCINCS}
CFLAGS = -O2 -W -Wall $(LCINCS)
#CFLAGS = -O2 -DOSG_USE_MEMORY_TRACKING -W -Wall $(LCINCS)
C++FLAGS = ${CFLAGS}
CPPFLAGS = ${CFLAGS}

View File

@@ -8,8 +8,8 @@ DEPFILES = $(OBJS:.o=.d)
C++ = CC
YFLAGS = -d
#CFLAGS = -O2 -n32 -MDupdate $(MAKEDEPEND) -LANG:std -OPT:Olimit=0 -DEBUG:woff=1682 -DEBUG:woff=3303
CFLAGS = -O2 -n32 -LANG:std -OPT:Olimit=0 -DEBUG:woff=1682 -DEBUG:woff=3303
#CFLAGS = -g -DOSG_USE_MEMORY_TRACKING -n32 -LANG:std -OPT:Olimit=0 -DEBUG:woff=1682 -DEBUG:woff=3303
C++FLAGS = ${CFLAGS}
CPPFLAGS = ${CFLAGS}

View File

@@ -8,8 +8,8 @@ DEPFILES = $(OBJS:.o=.d)
C++ = CC
YFLAGS = -d
#CFLAGS = -O2 64 -MDupdate $(MAKEDEPEND) -LANG:std -OPT:Olimit=0 -DEBUG:woff=1682 -DEBUG:woff=3303
CFLAGS = -O2 -64 -LANG:std -OPT:Olimit=0 -DEBUG:woff=1682 -DEBUG:woff=3303
#CFLAGS = -g -DOSG_USE_MEMORY_TRACKING -64 -LANG:std -OPT:Olimit=0 -DEBUG:woff=1682 -DEBUG:woff=3303
C++FLAGS = ${CFLAGS}
CPPFLAGS = ${CFLAGS}
@@ -17,6 +17,7 @@ SO_EXT = so
DL_EXT = so
LDFLAGS = -O2 -64 -LANG:std -OPT:Olimit=0
#LDFLAGS = -g -64 -LANG:std -OPT:Olimit=0
.SUFFIXES: .cpp .o
.cpp.o:

View File

@@ -11,15 +11,15 @@ YFLAGS = -d
LCINCS += -I/usr/X11R6/include
LC++INCS += ${LCINCS}
CFLAGS = -O2 -W -Wall $(LCINCS)
#CFLAGS = -g -W -Wall $(LCINCS)
#CFLAGS = -g -DOSG_USE_MEMORY_TRACKING -W -Wall $(LCINCS)
C++FLAGS = ${CFLAGS}
CPPFLAGS = ${CFLAGS}
SO_EXT = so
DL_EXT = so
LDFLAGS = -O2 -W -Wall -L/usr/X11R6/lib
#LDFLAGS = -g -W -Wall -L/usr/X11R6/lib
#LDFLAGS = -O2 -W -Wall -L/usr/X11R6/lib
LDFLAGS = -g -W -Wall -L/usr/X11R6/lib
LINKERARGS = -Xlinker
DYNAMICLIBRARYLIB = -ldl

View File

@@ -10,6 +10,7 @@ YFLAGS = -d
LCINCS += -I/usr/local/include -I/sw/include
LC++INCS += ${LCINCS}
CFLAGS = -O2 -W -Wall $(LCINCS) -D__DARWIN_OSX__
#CFLAGS = -g -DOSG_USE_MEMORY_TRACKING -W -Wall $(LCINCS) -D__DARWIN_OSX__
C++FLAGS = ${CFLAGS}
CPPFLAGS = ${CFLAGS}
# this distinction is necessary for Darwin / OS X as shared libs and loadable (dylib) modules

View File

@@ -225,6 +225,10 @@ SOURCE=..\..\src\osg\Material.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osg\MemoryManager.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osg\Matrix.cpp
# End Source File
# Begin Source File
@@ -485,6 +489,10 @@ SOURCE=..\..\Include\Osg\Material
# End Source File
# Begin Source File
SOURCE=..\..\Include\Osg\MemoryManager
# End Source File
# Begin Source File
SOURCE=..\..\include\osg\Math
# End Source File
# Begin Source File

157
include/osg/MemoryManager Normal file
View File

@@ -0,0 +1,157 @@
//C++
// ---------------------------------------------------------------------------------------------------------------------------------
// Copyright 2000, Paul Nettle. All rights reserved.
//
// You are free to use this source code in any commercial or non-commercial product.
//
// mmgr.h - Memory manager & tracking software
//
// The most recent version of this software can be found at: ftp://ftp.GraphicsPapers.com/pub/ProgrammingTools/MemoryManagers/
//
// [NOTE: Best when viewed with 8-character tabs]
// ---------------------------------------------------------------------------------------------------------------------------------
#ifndef _H_MMGR
#define _H_MMGR
// ---------------------------------------------------------------------------------------------------------------------------------
// For systems that don't have the __FUNCTION__ variable, we can just define it here
// ---------------------------------------------------------------------------------------------------------------------------------
#define __FUNCTION__ "??"
// ---------------------------------------------------------------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------------------------------------------------------------
typedef struct tag_au
{
size_t actualSize;
size_t reportedSize;
void *actualAddress;
void *reportedAddress;
char sourceFile[40];
char sourceFunc[40];
unsigned int sourceLine;
unsigned int allocationType;
bool breakOnDealloc;
bool breakOnRealloc;
unsigned int allocationNumber;
struct tag_au *next;
struct tag_au *prev;
} sAllocUnit;
typedef struct
{
unsigned int totalReportedMemory;
unsigned int totalActualMemory;
unsigned int peakReportedMemory;
unsigned int peakActualMemory;
unsigned int accumulatedReportedMemory;
unsigned int accumulatedActualMemory;
unsigned int accumulatedAllocUnitCount;
unsigned int totalAllocUnitCount;
unsigned int peakAllocUnitCount;
} sMStats;
// ---------------------------------------------------------------------------------------------------------------------------------
// External constants
// ---------------------------------------------------------------------------------------------------------------------------------
extern const unsigned int m_alloc_unknown;
extern const unsigned int m_alloc_new;
extern const unsigned int m_alloc_new_array;
extern const unsigned int m_alloc_malloc;
extern const unsigned int m_alloc_calloc;
extern const unsigned int m_alloc_realloc;
extern const unsigned int m_alloc_delete;
extern const unsigned int m_alloc_delete_array;
extern const unsigned int m_alloc_free;
// ---------------------------------------------------------------------------------------------------------------------------------
// Used by the macros
// ---------------------------------------------------------------------------------------------------------------------------------
void m_setOwner(const char *file, const unsigned int line, const char *func);
// ---------------------------------------------------------------------------------------------------------------------------------
// Allocation breakpoints
// ---------------------------------------------------------------------------------------------------------------------------------
bool &m_breakOnRealloc(void *reportedAddress);
bool &m_breakOnDealloc(void *reportedAddress);
// ---------------------------------------------------------------------------------------------------------------------------------
// The meat of the memory tracking software
// ---------------------------------------------------------------------------------------------------------------------------------
void *m_allocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
const unsigned int allocationType, const size_t reportedSize);
void *m_reallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress);
void m_deallocator(const char *sourceFile, const unsigned int sourceLine, const char *sourceFunc,
const unsigned int deallocationType, const void *reportedAddress);
// ---------------------------------------------------------------------------------------------------------------------------------
// Utilitarian functions
// ---------------------------------------------------------------------------------------------------------------------------------
bool m_validateAddress(const void *reportedAddress);
bool m_validateAllocUnit(const sAllocUnit *allocUnit);
bool m_validateAllAllocUnits();
// ---------------------------------------------------------------------------------------------------------------------------------
// Unused RAM calculations
// ---------------------------------------------------------------------------------------------------------------------------------
unsigned int m_calcUnused(const sAllocUnit *allocUnit);
unsigned int m_calcAllUnused();
// ---------------------------------------------------------------------------------------------------------------------------------
// Logging and reporting
// ---------------------------------------------------------------------------------------------------------------------------------
void m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix = "");
void m_dumpMemoryReport(const char *filename = "memreport.log", const bool overwrite = true);
sMStats m_getMemoryStatistics();
// ---------------------------------------------------------------------------------------------------------------------------------
// Variations of global operators new & delete
// ---------------------------------------------------------------------------------------------------------------------------------
#ifdef OSG_USE_MEMORY_TRACKING
void *operator new(size_t reportedSize);
void *operator new[](size_t reportedSize);
void *operator new(size_t reportedSize, const char *sourceFile, int sourceLine);
void *operator new[](size_t reportedSize, const char *sourceFile, int sourceLine);
void operator delete(void *reportedAddress);
void operator delete[](void *reportedAddress);
// ---------------------------------------------------------------------------------------------------------------------------------
// Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
// ---------------------------------------------------------------------------------------------------------------------------------
#define osgNew (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new
#define osgDelete (m_setOwner (__FILE__,__LINE__,__FUNCTION__),false) ? m_setOwner("",0,"") : delete
#define osgMalloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_malloc,sz)
#define osgCalloc(sz) m_allocator (__FILE__,__LINE__,__FUNCTION__,m_alloc_calloc,sz)
#define osgRealloc(ptr,sz) m_reallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_realloc,sz,ptr)
#define osgFree(ptr) m_deallocator(__FILE__,__LINE__,__FUNCTION__,m_alloc_free,ptr)
#else
#define osgNew new
#define osgDelete delete
#define osgMalloc(sz) malloc(sz)
#define osgCalloc(sz) calloc(sz)
#define osgRealloc(ptr,sz) realloc(sz)
#define osgFree(ptr) free(sz)
#endif
// ---------------------------------------------------------------------------------------------------------------------------------
// mmgr.h - End of file
// ---------------------------------------------------------------------------------------------------------------------------------
#endif // _H_MMGR

View File

@@ -75,7 +75,7 @@ Node *makeTerrain( void )
Vec3 *v = new Vec3[m*n];
Vec2 *t = new Vec2[m*n];
Vec4 *col = new Vec4[1];
osg::ushort *cidx = new osg::ushort;
osg::ushort *cidx = new osg::ushort[1];
osg::ushort *idx = new osg::ushort[m*n*2];
int *lens = new int[m];

View File

@@ -68,19 +68,19 @@ osg::Drawable* createMirrorSurface(float xMin,float xMax,float yMin,float yMax,f
// set up the geoset.
osg::GeoSet* gset = new osg::GeoSet;
osg::Vec3* coords = new osg::Vec3 [4];
osg::Vec3* coords = new osg::Vec3[4];
coords[0].set(xMin,yMax,z);
coords[1].set(xMin,yMin,z);
coords[2].set(xMax,yMin,z);
coords[3].set(xMax,yMax,z);
gset->setCoords(coords);
osg::Vec3* norms = new osg::Vec3 [1];
osg::Vec3* norms = new osg::Vec3[1];
norms[0].set(0.0f,0.0f,1.0f);
gset->setNormals(norms);
gset->setNormalBinding(osg::GeoSet::BIND_OVERALL);
osg::Vec2* tcoords = new osg::Vec2 [4];
osg::Vec2* tcoords = new osg::Vec2[4];
tcoords[0].set(0.0f,1.0f);
tcoords[1].set(0.0f,0.0f);
tcoords[2].set(1.0f,0.0f);
@@ -88,7 +88,7 @@ osg::Drawable* createMirrorSurface(float xMin,float xMax,float yMin,float yMax,f
gset->setTextureCoords(tcoords);
gset->setTextureBinding(osg::GeoSet::BIND_PERVERTEX);
osg::Vec4* colours = new osg::Vec4;
osg::Vec4* colours = new osg::Vec4[1];
colours->set(1.0f,1.0f,1.0,1.0f);
gset->setColors(colours);
gset->setColorBinding(osg::GeoSet::BIND_OVERALL);

View File

@@ -205,14 +205,14 @@ Geode* osg::createGeodeForImage(osg::Image* image,const float s,const float t)
osg::GeoSet* gset = new osg::GeoSet;
gset->setStateSet(dstate);
osg::Vec3* coords = new Vec3 [4];
osg::Vec3* coords = new Vec3[4];
coords[0].set(-x,0.0f,y);
coords[1].set(-x,0.0f,-y);
coords[2].set(x,0.0f,-y);
coords[3].set(x,0.0f,y);
gset->setCoords(coords);
osg::Vec2* tcoords = new Vec2 [4];
osg::Vec2* tcoords = new Vec2[4];
tcoords[0].set(0.0f,1.0f);
tcoords[1].set(0.0f,0.0f);
tcoords[2].set(1.0f,0.0f);
@@ -220,7 +220,7 @@ Geode* osg::createGeodeForImage(osg::Image* image,const float s,const float t)
gset->setTextureCoords(tcoords);
gset->setTextureBinding(osg::GeoSet::BIND_PERVERTEX);
osg::Vec4* colours = new Vec4;
osg::Vec4* colours = new Vec4[1];
colours->set(1.0f,1.0f,1.0,1.0f);
gset->setColors(colours);
gset->setColorBinding(osg::GeoSet::BIND_OVERALL);

View File

@@ -36,6 +36,7 @@ C++FILES = \
LineWidth.cpp\
LOD.cpp\
Material.cpp\
MemoryManager.cpp\
Matrix.cpp\
Node.cpp\
NodeCallback.cpp\
@@ -111,6 +112,7 @@ TARGET_INCLUDE_FILES = \
osg/Math\
osg/Matrix\
osg/MemoryAdapter\
osg/MemoryManager\
osg/Node\
osg/NodeCallback\
osg/NodeVisitor\

1601
src/osg/MemoryManager.cpp Normal file

File diff suppressed because it is too large Load Diff