OpenSceneGraph/include/osg/MemoryManager
2002-12-05 12:43:08 +00:00

155 lines
7.7 KiB
Plaintext

//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
#include <osg/Export>
#include <stdlib.h>
#include <new>
// ---------------------------------------------------------------------------------------------------------------------------------
// 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;
bool printedOutUnintializedInfo;
} 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 SG_EXPORT const unsigned int m_alloc_unknown;
extern SG_EXPORT const unsigned int m_alloc_new;
extern SG_EXPORT const unsigned int m_alloc_new_array;
extern SG_EXPORT const unsigned int m_alloc_malloc;
extern SG_EXPORT const unsigned int m_alloc_calloc;
extern SG_EXPORT const unsigned int m_alloc_realloc;
extern SG_EXPORT const unsigned int m_alloc_delete;
extern SG_EXPORT const unsigned int m_alloc_delete_array;
extern SG_EXPORT const unsigned int m_alloc_free;
// ---------------------------------------------------------------------------------------------------------------------------------
// Used by the macros
// ---------------------------------------------------------------------------------------------------------------------------------
extern SG_EXPORT void m_setOwner(const char *file, unsigned int line);
// ---------------------------------------------------------------------------------------------------------------------------------
// Allocation breakpoints
// ---------------------------------------------------------------------------------------------------------------------------------
extern SG_EXPORT bool &m_breakOnRealloc(void *reportedAddress);
extern SG_EXPORT bool &m_breakOnDealloc(void *reportedAddress);
extern SG_EXPORT void m_breakOnAllocation(unsigned int count);
// ---------------------------------------------------------------------------------------------------------------------------------
// The meat of the memory tracking software
// ---------------------------------------------------------------------------------------------------------------------------------
extern SG_EXPORT void *m_allocator(const char *sourceFile, unsigned int sourceLine,
unsigned int allocationType, const size_t reportedSize);
extern SG_EXPORT void *m_reallocator(const char *sourceFile, unsigned int sourceLine,
unsigned int reallocationType, const size_t reportedSize, void *reportedAddress);
extern SG_EXPORT void m_deallocator(const char *sourceFile, unsigned int sourceLine,
unsigned int deallocationType, const void *reportedAddress);
// ---------------------------------------------------------------------------------------------------------------------------------
// Utilitarian functions
// ---------------------------------------------------------------------------------------------------------------------------------
extern SG_EXPORT bool m_validateAddress(const void *reportedAddress);
extern SG_EXPORT bool m_validateAllocUnit(const sAllocUnit *allocUnit);
extern SG_EXPORT bool m_validateAllAllocUnits();
// ---------------------------------------------------------------------------------------------------------------------------------
// Unused RAM calculations
// ---------------------------------------------------------------------------------------------------------------------------------
extern SG_EXPORT unsigned int m_calcUnused(const sAllocUnit *allocUnit);
extern SG_EXPORT unsigned int m_calcAllUnused();
// ---------------------------------------------------------------------------------------------------------------------------------
// Logging and reporting
// ---------------------------------------------------------------------------------------------------------------------------------
extern SG_EXPORT void m_dumpAllocUnit(const sAllocUnit *allocUnit, const char *prefix = "");
extern SG_EXPORT void m_dumpMemoryReport(const char *filename = "memreport.log", bool overwrite = true);
extern SG_EXPORT sMStats m_getMemoryStatistics();
// ---------------------------------------------------------------------------------------------------------------------------------
// Variations of global operators new & delete
// ---------------------------------------------------------------------------------------------------------------------------------
#ifdef OSG_USE_MEMORY_MANAGER
// ---------------------------------------------------------------------------------------------------------------------------------
// Macros -- "Kids, please don't try this at home. We're trained professionals here." :)
// ---------------------------------------------------------------------------------------------------------------------------------
#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)
#else // OSG_USE_MEMORY_MANAGER
#define osgNew new
#define osgDelete delete
#define osgMalloc(sz) malloc(sz)
#define osgCalloc(sz) calloc(sz)
#define osgRealloc(ptr,sz) realloc(ptr,sz)
#define osgFree(ptr) free(ptr)
#endif // OSG_USE_MEMORY_MANAGER
// ---------------------------------------------------------------------------------------------------------------------------------
// mmgr.h - End of file
// ---------------------------------------------------------------------------------------------------------------------------------
#endif // _H_MMGR