PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PMemoryHeap Class Reference

Memory heap checking class. More...

#include <object.h>

Collaboration diagram for PMemoryHeap:

Classes

struct  Header
 
struct  State
 

Public Types

enum  Validation {
  Ok, Bad, Trashed, Corrupt,
  Inactive
}
 Validation result. More...
 
typedef unsigned alloc_t
 

Public Member Functions

 ~PMemoryHeap ()
 

Static Public Member Functions

static void * Allocate (size_t nSize, const char *file, int line, const char *className)
 Allocate a memory block. More...
 
static void * Allocate (size_t count, size_t iSize, const char *file, int line)
 Allocate a memory block. More...
 
static void * Reallocate (void *ptr, size_t nSize, const char *file, int line)
 Change the size of an allocated memory block. More...
 
static void Deallocate (void *ptr, const char *className)
 Free a memory block. More...
 
static Validation Validate (const void *ptr, const char *className, ostream *error)
 Validate the memory pointer. More...
 
static PBoolean ValidateHeap (ostream *error=NULL)
 Validate all objects in memory. More...
 
static PBoolean SetIgnoreAllocations (PBoolean ignore)
 Ignore/Monitor allocations. More...
 
static void DumpStatistics ()
 Get memory check system statistics. More...
 
static void DumpStatistics (ostream &strm)
 Get memory check system statistics. More...
 
static void GetState (State &state)
 
static void DumpObjectsSince (const State &when)
 Dump allocated objects. More...
 
static void DumpObjectsSince (const State &when, ostream &strm)
 Dump allocated objects. More...
 
static void SetAllocationBreakpoint (alloc_t point)
 Set break point allocation number. More...
 

Protected Types

enum  Flags { NoLeakPrint = 1 }
 
enum  { e_Destroyed =-1, e_Disabled, e_TrackOnly, e_Active }
 

Protected Member Functions

 PMemoryHeap ()
 Initialise the memory checking subsystem. More...
 
void * InternalAllocate (size_t nSize, const char *file, int line, const char *className, bool zeroFill)
 
void * InternalReallocate (void *ptr, size_t nSize, const char *file, int line)
 
void InternalDeallocate (void *ptr, const char *className)
 
Validation InternalValidate (const void *ptr, const char *className, ostream *error)
 
bool InternalValidateHeap (ostream *error)
 
void InternalDumpStatistics (ostream &strm)
 
void InternalDumpObjectsSince (DWORD objectNumber, ostream &strm)
 
void Lock ()
 
void Unlock ()
 

Static Protected Member Functions

static PMemoryHeapGetInstance ()
 

Protected Attributes

enum PMemoryHeap:: { ... }  m_state
 
Headerm_listHead
 
Headerm_listTail
 
alloc_t m_allocationRequest
 
alloc_t m_allocationBreakpoint
 
alloc_t m_firstRealObject
 
uint8_t m_flags
 
char m_allocFillChar
 
char m_freeFillChar
 
size_t m_currentMemoryUsage
 
size_t m_peakMemoryUsage
 
alloc_t m_currentObjects
 
alloc_t m_peakObjects
 
alloc_t m_totalObjects
 
ostream * m_leakDumpStream
 

Detailed Description

Memory heap checking class.

This class implements the memory heap checking and validation functions. It maintains lists of allocated block so that memory leaks can be detected. It also initialises memory on allocation and deallocation to help catch errors involving the use of dangling pointers.

Member Typedef Documentation

typedef unsigned PMemoryHeap::alloc_t

Member Enumeration Documentation

anonymous enum
protected
Enumerator
e_Destroyed 
e_Disabled 
e_TrackOnly 
e_Active 
enum PMemoryHeap::Flags
protected
Enumerator
NoLeakPrint 

Validation result.

Enumerator
Ok 
Bad 
Trashed 
Corrupt 
Inactive 

Constructor & Destructor Documentation

PMemoryHeap::PMemoryHeap ( )
protected

Initialise the memory checking subsystem.

PMemoryHeap::~PMemoryHeap ( )

Member Function Documentation

static void* PMemoryHeap::Allocate ( size_t  nSize,
const char *  file,
int  line,
const char *  className 
)
static

Allocate a memory block.

This allocates a new memory block and keeps track of it. The memory block is filled with the value in the allocFillChar member variable to help detect uninitialised structures.

Returns
pointer to newly allocated memory block.
Parameters
nSizeNumber of bytes to allocate.
fileSource file name for allocating function.
lineSource file line for allocating function.
classNameClass name for allocating function.

Referenced by operator new(), and operator new[]().

static void* PMemoryHeap::Allocate ( size_t  count,
size_t  iSize,
const char *  file,
int  line 
)
static

Allocate a memory block.

This allocates a new memory block and keeps track of it. The memory block is filled with the value in the allocFillChar member variable to help detect uninitialised structures.

Returns
pointer to newly allocated memory block.
Parameters
countNumber of items to allocate.
iSizeSize in bytes of each item.
fileSource file name for allocating function.
lineSource file line for allocating function.
static void PMemoryHeap::Deallocate ( void *  ptr,
const char *  className 
)
static

Free a memory block.

The memory is deallocated, a warning is displayed if it was never allocated. The block of memory is filled with the value in the freeFillChar member variable.

Parameters
ptrPointer to memory block to deallocate.
classNameClass name for deallocating function.
static void PMemoryHeap::DumpObjectsSince ( const State when)
static

Dump allocated objects.

Dump ojects allocated and not deallocated since the specified object number. This would be a value returned by the GetAllocationRequest() function.

Output is to the default stream.

Parameters
whenMemory state to begin dump from.
static void PMemoryHeap::DumpObjectsSince ( const State when,
ostream &  strm 
)
static

Dump allocated objects.

Dump ojects allocated and not deallocated since the specified object number. This would be a value returned by the GetAllocationRequest() function.

Parameters
whenMemory state to begin dump from.
strmStream to output dump
static void PMemoryHeap::DumpStatistics ( )
static

Get memory check system statistics.

Dump statistics output to the default stream.

static void PMemoryHeap::DumpStatistics ( ostream &  strm)
static

Get memory check system statistics.

Dump statistics output to the specified stream.

Parameters
strmStream to output to
static PMemoryHeap& PMemoryHeap::GetInstance ( )
staticprotected
static void PMemoryHeap::GetState ( State state)
static
Parameters
stateMemory state
void* PMemoryHeap::InternalAllocate ( size_t  nSize,
const char *  file,
int  line,
const char *  className,
bool  zeroFill 
)
protected
void PMemoryHeap::InternalDeallocate ( void *  ptr,
const char *  className 
)
protected
void PMemoryHeap::InternalDumpObjectsSince ( DWORD  objectNumber,
ostream &  strm 
)
protected
void PMemoryHeap::InternalDumpStatistics ( ostream &  strm)
protected
void* PMemoryHeap::InternalReallocate ( void *  ptr,
size_t  nSize,
const char *  file,
int  line 
)
protected
Validation PMemoryHeap::InternalValidate ( const void *  ptr,
const char *  className,
ostream *  error 
)
protected
bool PMemoryHeap::InternalValidateHeap ( ostream *  error)
protected
void PMemoryHeap::Lock ( )
protected
static void* PMemoryHeap::Reallocate ( void *  ptr,
size_t  nSize,
const char *  file,
int  line 
)
static

Change the size of an allocated memory block.

This allocates a new memory block and keeps track of it. The memory block is filled with the value in the allocFillChar member variable to help detect uninitialised structures.

Returns
pointer to reallocated memory block. Note this may {not} be the same as the pointer passed into the function.
Parameters
ptrPointer to memory block to reallocate.
nSizeNew number of bytes to allocate.
fileSource file name for allocating function.
lineSource file line for allocating function.
static void PMemoryHeap::SetAllocationBreakpoint ( alloc_t  point)
static

Set break point allocation number.

Set the allocation request number to cause an assert. This allows a developer to cause a halt in a debugger on a certain allocation allowing them to determine memory leaks allocation point.

Parameters
pointAllocation number to stop at.

Referenced by PMemoryAllocationBreakpoint::PMemoryAllocationBreakpoint().

static PBoolean PMemoryHeap::SetIgnoreAllocations ( PBoolean  ignore)
static

Ignore/Monitor allocations.

Set internal flag so that allocations are not included in the memory leak check on program termination. Returns the previous state.

Parameters
ignoreNew flag for allocation ignoring.

Referenced by PMemoryHeapIgnoreAllocationsForScope::~PMemoryHeapIgnoreAllocationsForScope().

void PMemoryHeap::Unlock ( )
protected
static Validation PMemoryHeap::Validate ( const void *  ptr,
const char *  className,
ostream *  error 
)
static

Validate the memory pointer.

The ptr parameter is validated as a currently allocated heap variable.

Returns
Ok for pointer is in heap, Bad for pointer is not in the heap or Trashed if the pointer is in the heap but has overwritten the guard bytes before or after the actual data part of the memory block.
Parameters
ptrPointer to memory block to check
classNameClass name it should be.
errorStream to receive error message (may be NULL)

Referenced by PSingleton< Type, atomic< unsigned >, Creator >::PSingleton().

static PBoolean PMemoryHeap::ValidateHeap ( ostream *  error = NULL)
static

Validate all objects in memory.

This effectively calls Validate() on every object in the heap.

Returns
true if every object in heap is Ok.
Parameters
errorStream to output, use default if NULL

Member Data Documentation

alloc_t PMemoryHeap::m_allocationBreakpoint
protected
alloc_t PMemoryHeap::m_allocationRequest
protected
char PMemoryHeap::m_allocFillChar
protected
size_t PMemoryHeap::m_currentMemoryUsage
protected
alloc_t PMemoryHeap::m_currentObjects
protected
alloc_t PMemoryHeap::m_firstRealObject
protected
uint8_t PMemoryHeap::m_flags
protected
char PMemoryHeap::m_freeFillChar
protected
ostream* PMemoryHeap::m_leakDumpStream
protected
Header* PMemoryHeap::m_listHead
protected
Header* PMemoryHeap::m_listTail
protected
size_t PMemoryHeap::m_peakMemoryUsage
protected
alloc_t PMemoryHeap::m_peakObjects
protected
enum { ... } PMemoryHeap::m_state
alloc_t PMemoryHeap::m_totalObjects
protected

The documentation for this class was generated from the following file: