home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1997 October
/
Chip_1997-10_cd.bin
/
tema
/
sybase
/
powerj
/
hpp.z
/
WMEMORY.HPP
< prev
next >
Wrap
C/C++ Source or Header
|
1996-10-18
|
8KB
|
236 lines
/*************************************************************************
*
* WMemory
*
*************************************************************************/
#ifndef _WMEMORY_HPP_INCLUDED
#define _WMEMORY_HPP_INCLUDED
#ifndef _WNO_PRAGMA_PUSH
#pragma pack(push,8);
#pragma enum int;
#endif
#ifndef _WDEF_HPP_INCLUDED
# include "wdef.hpp"
#endif
#ifndef _WLLIST_HPP_INCLUDED
# include "wllist.hpp"
#endif
//
// Memory tracking flags
//
#define WMEMORY_CHECKFOROVERWRITE 0x00000001L
#define WMEMORY_TRACKMEMORY 0x00000002L
#define WMEMORY_TRAPNOMEMORY 0x00000004L
#define WMEMORY_WALKHEAPS 0x00000008L
#define WMEMORY_USE_HEAPALLOC 0x00000010L
#define WMEMORY_PRINT_ALL 0x80000000L
#define WMEMORY_ZERO_MEMORY 0x40000000L
#define WMEMORY_ALLCHECKS (WMEMORY_CHECKFOROVERWRITE|\
WMEMORY_TRACKMEMORY|\
WMEMORY_WALKHEAPS)
#define WMEMORY_NOCHECKS 0x00000000L
//
// WMemoryAllocator -- Use this structure to define the low-level
// C-type functions used to allocate memory.
// You must declare the structure statically.
//
typedef void * WHeapHandle;
typedef void * __stdcall (*WHeapAllocator)( WHeapHandle heap, WULong memSize );
typedef void * __stdcall (*WHeapReallocator)( WHeapHandle heap, void *mem,
WULong newSize );
typedef WBool __stdcall (*WHeapDeallocator)( WHeapHandle heap, void *mem );
typedef WBool __stdcall (*WHeapValidator)( WHeapHandle heap, void *mem );
typedef WHeapHandle __stdcall (*WHeapCreator)( WULong initialSize,
WULong maximumSize );
typedef WBool __stdcall (*WHeapDestructor)( WHeapHandle heap );
struct WCMCLASS WMemoryHeap {
wllist_link link;
const WChar *heapName;
WHeapHandle heap;
WULong initialSize;
WULong maximumSize;
WHeapAllocator allocate;
WHeapDeallocator deallocate;
WHeapReallocator reallocate;
WHeapValidator validate;
WHeapCreator create;
WHeapDestructor destruct;
};
//
// Memory dumping -- for debugging purposes.
//
typedef WBool WCMDEF (*WMemoryDumper)( void *memory,
const WChar **memoryType,
WChar *buffer, WULong len );
#ifdef _DEBUG
#define WAttachDumper(mem,dump) WMemory::AttachDumpRoutine((void*)mem,dump)
#else
#define WAttachDumper(mem,dump)
#endif
//
// WMemory
//
class WCMCLASS WMemory {
public:
/**********************************************************
* Constructors and Destructors
*********************************************************/
WMemory();
virtual ~WMemory();
/**********************************************************
* Properties
*********************************************************/
// ArrayHeap
static WMemoryHeap *GetArrayHeap();
static WBool SetArrayHeap( WMemoryHeap *heap );
// Heap
static WMemoryHeap *GetHeap();
static WBool SetHeap( WMemoryHeap *heap );
// CurrentMemoryUsage
static WULong GetCurrentMemoryUsage();
// PeakMemoryUsage
static WULong GetPeakMemoryUsage();
/**********************************************************
* Methods
*********************************************************/
// Allocate
//
// Allocate memory from a given heap. If no heap
// specified, use the thread default.
static void *Allocate( WULong size, WBool array,
WMemoryHeap *allocator=NULL );
static void *Allocate( WULong size, WBool array,
const WChar *functionName,
const WChar *fileName, WULong line,
WMemoryHeap *allocator=NULL );
// AttachDumpRoutine
//
// After a piece of memory has been allocated, call
// this routine to attach a specific dump routine to it.
static WBool AttachDumpRoutine( void *memory,
WMemoryDumper dumpRoutine );
// Deallocate
//
// Deallocate a specific piece of memory.
static WBool Deallocate( void *memory, WBool array );
static WBool Deallocate( void *memory, WBool array,
const WChar *functionName,
const WChar *fileName, WULong line );
// Reallocate
//
// Reallocate a specific piece of memory.
static void *Reallocate( void *memory, WULong newSize,
WBool array );
static void *Reallocate( void *memory, WULong newSize,
WBool array,
const WChar *functionName,
const WChar *fileName, WULong line );
// Validate
//
// Check to see if a piece of memory is valid. If NULL
// is passed, checks whole heap. Only implemented in
// the debugging DLL.
static WBool Validate( void *memory, WMemoryHeap *allocator );
/**********************************************************
* Internal
*********************************************************/
// LogNextDelete
//
// Set the location that the next delete will come from.
// Used in debugging mode with memory tracking.
static WBool LogNextDelete( void *ptr, const WChar *functionName,
const WChar *fileName, WULong line );
// FreeDeleteLog
//
// Free the delete log for a thread. Call occasionally.
static WBool FreeDeleteLog();
/**********************************************************
* Data members
*********************************************************/
public:
};
//
// Use these macros to redefine new & delete
//
#define WREDEFINENEW \
void *operator new( size_t size ) \
{ return WMemory::Allocate( size, FALSE ); } \
void *operator new( size_t size, const WChar *funcName, \
const WChar *fileName, WULong line ) \
{ return WMemory::Allocate( size, FALSE, funcName, \
fileName, line ); } \
void *operator new( size_t, void *p ) { return p; } \
void *operator new []( size_t size ) \
{ return WMemory::Allocate( size, TRUE ); } \
void *operator new []( size_t size, const WChar *funcName,\
const WChar *fileName, WULong line ) \
{ return WMemory::Allocate( size, TRUE, funcName,\
fileName, line ); } \
void *operator new []( size_t, void *p ) { return p; }
#define WREDEFINEDELETE \
void operator delete( void *p ) { WMemory::Deallocate( p, FALSE ); } \
void operator delete []( void *p ) { WMemory::Deallocate( p, TRUE ); }
#define W_REDEFINENEW WREDEFINENEW
#define W_REDEFINEDELETE WREDEFINEDELETE
#ifndef _WNO_PRAGMA_PUSH
#pragma enum pop;
#pragma pack(pop);
#endif
#endif // _WMEMORY_HPP_INCLUDED