2002-03-26 07:18:02 +08:00
|
|
|
//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]
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-03-29 20:23:54 +08:00
|
|
|
#ifndef _H_MMGR
|
|
|
|
#define _H_MMGR
|
2002-03-26 07:18:02 +08:00
|
|
|
|
2002-06-21 02:17:36 +08:00
|
|
|
|
2002-04-16 05:03:32 +08:00
|
|
|
#include <osg/Export>
|
|
|
|
|
2002-03-27 07:52:52 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <new>
|
|
|
|
|
2002-06-21 02:17:36 +08:00
|
|
|
|
2002-03-26 07:18:02 +08:00
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-03-29 20:23:54 +08:00
|
|
|
typedef struct tag_au
|
2002-03-26 07:18:02 +08:00
|
|
|
{
|
2002-03-29 20:23:54 +08:00
|
|
|
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;
|
2002-03-26 07:18:02 +08:00
|
|
|
} sAllocUnit;
|
|
|
|
|
2002-03-29 20:23:54 +08:00
|
|
|
typedef struct
|
2002-03-26 07:18:02 +08:00
|
|
|
{
|
2002-03-29 20:23:54 +08:00
|
|
|
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;
|
2002-03-26 07:18:02 +08:00
|
|
|
} sMStats;
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// External constants
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-04-16 05:03:32 +08:00
|
|
|
SG_EXPORT extern const unsigned int m_alloc_unknown;
|
|
|
|
SG_EXPORT extern const unsigned int m_alloc_new;
|
|
|
|
SG_EXPORT extern const unsigned int m_alloc_new_array;
|
|
|
|
SG_EXPORT extern const unsigned int m_alloc_malloc;
|
|
|
|
SG_EXPORT extern const unsigned int m_alloc_calloc;
|
|
|
|
SG_EXPORT extern const unsigned int m_alloc_realloc;
|
|
|
|
SG_EXPORT extern const unsigned int m_alloc_delete;
|
|
|
|
SG_EXPORT extern const unsigned int m_alloc_delete_array;
|
|
|
|
SG_EXPORT extern const unsigned int m_alloc_free;
|
2002-03-26 07:18:02 +08:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Used by the macros
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-04-24 22:52:53 +08:00
|
|
|
SG_EXPORT extern void m_setOwner(const char *file, const unsigned int line);
|
2002-03-26 07:18:02 +08:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Allocation breakpoints
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-04-16 05:03:32 +08:00
|
|
|
SG_EXPORT extern bool &m_breakOnRealloc(void *reportedAddress);
|
|
|
|
SG_EXPORT extern bool &m_breakOnDealloc(void *reportedAddress);
|
2002-06-21 02:17:36 +08:00
|
|
|
SG_EXPORT extern void m_breakOnAllocation(unsigned int count);
|
2002-03-26 07:18:02 +08:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// The meat of the memory tracking software
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-06-21 02:17:36 +08:00
|
|
|
SG_EXPORT extern void *m_allocator(const char *sourceFile, const unsigned int sourceLine,
|
|
|
|
const unsigned int allocationType, const size_t reportedSize);
|
|
|
|
SG_EXPORT extern void *m_reallocator(const char *sourceFile, const unsigned int sourceLine,
|
|
|
|
const unsigned int reallocationType, const size_t reportedSize, void *reportedAddress);
|
|
|
|
SG_EXPORT extern void m_deallocator(const char *sourceFile, const unsigned int sourceLine,
|
|
|
|
const unsigned int deallocationType, const void *reportedAddress);
|
2002-03-26 07:18:02 +08:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Utilitarian functions
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-06-21 02:17:36 +08:00
|
|
|
SG_EXPORT extern bool m_validateAddress(const void *reportedAddress);
|
|
|
|
SG_EXPORT extern bool m_validateAllocUnit(const sAllocUnit *allocUnit);
|
|
|
|
SG_EXPORT extern bool m_validateAllAllocUnits();
|
2002-03-26 07:18:02 +08:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Unused RAM calculations
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-04-16 05:03:32 +08:00
|
|
|
SG_EXPORT extern unsigned int m_calcUnused(const sAllocUnit *allocUnit);
|
|
|
|
SG_EXPORT extern unsigned int m_calcAllUnused();
|
2002-03-26 07:18:02 +08:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Logging and reporting
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-04-16 05:03:32 +08:00
|
|
|
SG_EXPORT extern void m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix = "");
|
|
|
|
SG_EXPORT extern void m_dumpMemoryReport(const char *filename = "memreport.log", const bool overwrite = true);
|
2002-06-21 02:17:36 +08:00
|
|
|
SG_EXPORT extern sMStats m_getMemoryStatistics();
|
2002-03-26 07:18:02 +08:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Variations of global operators new & delete
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-03-27 07:52:52 +08:00
|
|
|
#ifdef OSG_USE_MEMORY_MANAGER
|
2002-03-26 07:18:02 +08:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
2002-06-21 02:17:36 +08:00
|
|
|
|
2002-04-24 22:52:53 +08:00
|
|
|
#define osgNew (m_setOwner (__FILE__,__LINE__),false) ? NULL : new
|
|
|
|
#define osgDelete (m_setOwner (__FILE__,__LINE__),false) ? m_setOwner("",0) : delete
|
|
|
|
#define osgMalloc(sz) m_allocator (__FILE__,__LINE__,m_alloc_malloc,sz)
|
|
|
|
#define osgCalloc(sz) m_allocator (__FILE__,__LINE__,m_alloc_calloc,sz)
|
|
|
|
#define osgRealloc(ptr,sz) m_reallocator(__FILE__,__LINE__,m_alloc_realloc,sz,ptr)
|
|
|
|
#define osgFree(ptr) m_deallocator(__FILE__,__LINE__,m_alloc_free,ptr)
|
2002-03-26 07:18:02 +08:00
|
|
|
|
2002-06-21 02:17:36 +08:00
|
|
|
#else // OSG_USE_MEMORY_MANAGER
|
2002-03-26 07:18:02 +08:00
|
|
|
|
2002-05-14 18:20:55 +08:00
|
|
|
#define osgNew new
|
|
|
|
#define osgDelete delete
|
2002-03-29 20:23:54 +08:00
|
|
|
#define osgMalloc(sz) malloc(sz)
|
|
|
|
#define osgCalloc(sz) calloc(sz)
|
2002-05-14 18:20:55 +08:00
|
|
|
#define osgRealloc(ptr,sz) realloc(ptr,sz)
|
|
|
|
#define osgFree(ptr) free(ptr)
|
2002-03-26 07:18:02 +08:00
|
|
|
|
2002-06-21 02:17:36 +08:00
|
|
|
#endif // OSG_USE_MEMORY_MANAGER
|
2002-03-26 07:18:02 +08:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// mmgr.h - End of file
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif // _H_MMGR
|