MemoryManagement
[System Utilities]

Collaboration diagram for MemoryManagement:


Data Structures

struct  psMemBlock
 Book-keeping data for storage allocator. More...

Defines

#define P_PS_MEMMAGIC   (psU32)0xdeadbeef

Typedefs

typedef unsigned long psMemId
 typedef for memory identification numbers. Guaranteed to be some variety of integer.
typedef unsigned long psReferenceCount
 typedef for a memory block's reference count. Guaranteed to be some variety of integer.
typedef void(*) psFreeFunc (void *ptr)
 typedef for deallocator.
typedef psMemId(*) psMemAllocCallback (const psMemBlock *ptr)
 prototype of a basic callback used by memory functions
typedef psMemId(*) psMemFreeCallback (const psMemBlock *ptr)
 prototype of memory free callback used by memory functions
typedef void(*) psMemProblemCallback (psMemBlock *ptr, const char *file, unsigned int lineno)
 prototype of a callback used in error conditions
typedef psPtr(*) psMemExhaustedCallback (size_t size)
 prototype of a callback function used when memory runs out

Functions

psPtr psAlloc (size_t size)
 Memory allocation.
void psMemSetDeallocator (psPtr ptr, psFreeFunc freeFunc)
 Set the deallocator routine.
psFreeFunc psMemGetDeallocator (const psPtr ptr)
 Get the deallocator routine.
bool psMemSetThreadSafety (bool safe)
 Activate or Deactivate thread safety and mutex locking in the memory management.
bool psMemGetThreadSafety (void)
 Get the current state of thread safety and mutex locking in the memory management.
void psMemSetPersistent (psPtr ptr, bool value,)
 Set the memory as persistent so that it is ignored when detecting memory leaks.
bool p_psMemAllocatePersistent (bool is_persistent)
 Set whether allocated memory is persistent Should all memory allocated be persistent?
bool psMemGetPersistent (psPtr ptr,)
 Get the memory's persistent flag.
psPtr psRealloc (psPtr ptr, size_t size)
 Memory re-allocation.
void psFree (psPtr ptr)
 Free memory.
int psMemCheckLeaks (psMemId id0, psMemBlock ***array, FILE *fd, bool persistence)
 Check for memory leaks.
int psMemCheckCorruption (FILE *output, bool abort_on_error)
 Check for memory corruption.
psReferenceCount psMemGetRefCounter (const psPtr ptr)
 Return reference counter.
psPtr psMemIncrRefCounter (const psPtr ptr)
 Increment reference counter and return the pointer.
psPtr psMemDecrRefCounter (psPtr ptr)
 Decrement reference counter and return the pointer.
psMemExhaustedCallback psMemExhaustedCallbackSet (psMemExhaustedCallback func)
 Set callback for out-of-memory.
psMemAllocCallback psMemAllocCallbackSet (psMemAllocCallback func)
 Set call back for when a particular memory block is allocated.
psMemFreeCallback psMemFreeCallbackSet (psMemFreeCallback func)
 Set call back for when a particular memory block is freed.
psMemId psMemGetId (void)
 get next memory ID
psMemId psMemGetLastId (void)
 get the last memory ID used
psMemId psMemAllocCallbackSetID (psMemId id)
 set p_psMemAllocID to specific id
psMemId psMemFreeCallbackSetID (psMemId id)
 set p_psMemFreeID to id
size_t psMemStats (const bool print, size_t *allocated, size_t *persistent)
 return statistics on memory usage persistent memory that's currently allocated
int psMemBlockPrint (FILE *output, const psMemBlock *memBlock)
 print detailed information about a psMemBlock


Define Documentation

#define P_PS_MEMMAGIC   (psU32)0xdeadbeef

Definition at line 37 of file psMemory.h.


Typedef Documentation

typedef void(*) psFreeFunc(void *ptr)

typedef for deallocator.

Definition at line 46 of file psMemory.h.

typedef psMemId(*) psMemAllocCallback(const psMemBlock *ptr)

prototype of a basic callback used by memory functions

See also:
psMemAllocCallbackSet

Definition at line 85 of file psMemory.h.

typedef psPtr(*) psMemExhaustedCallback(size_t size)

prototype of a callback function used when memory runs out

Returns:
psPtr pointer to requested buffer of the size size_t, or NULL if memory could not be found.
See also:
psMemExhaustedCallbackSet

Definition at line 116 of file psMemory.h.

typedef psMemId(*) psMemFreeCallback(const psMemBlock *ptr)

prototype of memory free callback used by memory functions

See also:
psMemFreeCallbackSet

Definition at line 93 of file psMemory.h.

typedef unsigned long psMemId

typedef for memory identification numbers. Guaranteed to be some variety of integer.

Definition at line 40 of file psMemory.h.

typedef void(*) psMemProblemCallback(psMemBlock *ptr,const char *file,unsigned int lineno)

prototype of a callback used in error conditions

This callback should not try to call psAlloc or psFree.

See also:
psMemProblemCallbackSet

Definition at line 103 of file psMemory.h.

typedef unsigned long psReferenceCount

typedef for a memory block's reference count. Guaranteed to be some variety of integer.

Definition at line 43 of file psMemory.h.


Function Documentation

bool p_psMemAllocatePersistent ( bool  is_persistent  ) 

Set whether allocated memory is persistent Should all memory allocated be persistent?

Set whether allocated memory is persistent. The defeault is false.

Returns:
bool: The previous value of whether all allocated memory is persistent

psPtr psAlloc ( size_t  size  ) 

Memory allocation.

This operates much like malloc(), but is guaranteed to return a non-NULL value.

Returns:
psPtr pointer to the allocated buffer. This will not be NULL.
See also:
psFree
Parameters:
size  Size required

void psFree ( psPtr  ptr  ) 

Free memory.

This operates much like free().

See also:
psAlloc, psRealloc
Parameters:
ptr  Pointer to free, if NULL, function returns immediately.

psMemAllocCallback psMemAllocCallbackSet ( psMemAllocCallback  func  ) 

Set call back for when a particular memory block is allocated.

A private variable, p_psMemAllocID, can be used to trace the allocation and freeing of specific memory blocks. If p_psMemAllocID is set and a memory block with that ID is allocated, psMemAllocCallback is called just before memory is returned to the calling function.

Returns:
psMemAllocCallback old psMemAllocCallback function
Parameters:
func  Function to run at memory allocation of specific mem block

psMemId psMemAllocCallbackSetID ( psMemId  id  ) 

set p_psMemAllocID to specific id

A private variable, p_psMemAllocID, can be used to trace the allocation and freeing of specific memory blocks. If p_psMemAllocID is set and a memory block with that ID is allocated, psMemAllocCallback is called just before memory is returned to the calling function.

Returns:
psMemId
See also:
psMemAllocCallbackSet
Parameters:
id  ID to set

int psMemBlockPrint ( FILE *  output,
const psMemBlock memBlock 
)

print detailed information about a psMemBlock

This function prints a detailed description of a psMemBlock to output.

Returns:
the return status of fprintf()
Parameters:
output  FILE to write information too
memBlock  psMemBlock to be examined

int psMemCheckCorruption ( FILE *  output,
bool  abort_on_error 
)

Check for memory corruption.

Scans all currently allocated memory buffers and checks for corruptions, i.e., invalid markers that signify a buffer under/overflow.

Returns:
int
Parameters:
output  FILE to write corrupted blocks too
abort_on_error  Abort on detecting corruption?

int psMemCheckLeaks ( psMemId  id0,
psMemBlock ***  array,
FILE *  fd,
bool  persistence 
)

Check for memory leaks.

This scans for allocated memory buffers not freed with an ID not less than id0. This is used to check for memory leaks by:

  1. before a block of code to be checked, store the current ID count via psGetMemId
  2. after the block of code to be checked, call this function using the ID stored above. If all memory in the block that was allocated has been freed, this call should output nothing and return 0.

If memory leaks are found, the Memory Problem callback will be called as well.

Returns:
int number of memory blocks found as 'leaks', i.e., the number of currently allocated memory blocks above id0 that have not been freed.
See also:
psAlloc, psFree, psgetMemId, psMemProblemCallbackSet
Parameters:
id0  don't list blocks with id < id0
array  pointer to array of pointers to leaked blocks, or NULL
fd  print list of leaks to fd (or NULL)
persistence  make check across all object even persistent ones

psPtr psMemDecrRefCounter ( psPtr  ptr  ) 

Decrement reference counter and return the pointer.

Returns:
psPtr the pointer deremented in refCount, or NULL if pointer is fully dereferenced.
Parameters:
ptr  Pointer to decrement refCounter, and return

psMemExhaustedCallback psMemExhaustedCallbackSet ( psMemExhaustedCallback  func  ) 

Set callback for out-of-memory.

If not enough memory is available to satisfy a request by psAlloc or psRealloc, these functions attempt to find an alternative solution by calling the psMemExhaustedCallback, a function which may be set by the programmer in appropriate circumstances, rather than immediately fail. The typical use of such a feature may be when a program needs a large chunk of memory to do an operation, but the exact size is not critical. This feature gives the programmer the opportunity to make a smaller request and try again, limiting the size of the operating buffer.

Returns:
psMemExhaustedCallback old psMemExhaustedCallback function
Parameters:
func  Function to run at memory exhaustion

psMemFreeCallback psMemFreeCallbackSet ( psMemFreeCallback  func  ) 

Set call back for when a particular memory block is freed.

A private variable, p_psMemFreeID, can be used to trace the freeing of specific memory blocks. If p_psMemFreeID is set and the memory block with the ID is about to be freed, the psMemFreeCallback callback is called just before the memory block is freed.

Returns:
psMemFreeCallback old psMemFreeCallback function
Parameters:
func  Function to run at memory free of specific mem block

psMemId psMemFreeCallbackSetID ( psMemId  id  ) 

set p_psMemFreeID to id

A private variable, p_psMemFreeID, can be used to trace the freeing of specific memory blocks. If p_psMemFreeID is set and the memory block with the ID is about to be freed, the psMemFreeCallback callback is called just before the memory block is freed.

Returns:
psMemId the old p_psMemFreeID
See also:
psMemFreeCallbackSet
Parameters:
id  ID to set

psFreeFunc psMemGetDeallocator ( const psPtr  ptr  ) 

Get the deallocator routine.

This function returns the deallocator for a memory block. A deallocator routine can optionally be assigned to a memory block to ensure that associated memory blocks also get freed, e.g., memory buffers referenced within a struct.

Returns:
psFreeFunc the routine to be called at deallocation.
Parameters:
ptr  the memory block

psMemId psMemGetId ( void   ) 

get next memory ID

Returns:
psMemId the next memory ID to be used

psMemId psMemGetLastId ( void   ) 

get the last memory ID used

Returns:
psMemId the last memory ID used

bool psMemGetPersistent ( psPtr  ptr  ) 

Get the memory's persistent flag.

Checks if a memory block has been marked as persistent by p_psMemSetPresistent.

Memory marked as persistent is excluded from memory leak checks.

Returns:
bool true if memory is marked persistent, otherwise false.
Parameters:
ptr  the memory block to check.

psReferenceCount psMemGetRefCounter ( const psPtr  ptr  ) 

Return reference counter.

Returns:
psReferenceCount
Parameters:
ptr  Pointer to get refCounter for

bool psMemGetThreadSafety ( void   ) 

Get the current state of thread safety and mutex locking in the memory management.

psMemGetThreadSafety shall return the current state of thread safety in the memory management system.

Returns:
bool: The current state of thread safety.

psPtr psMemIncrRefCounter ( const psPtr  ptr  ) 

Increment reference counter and return the pointer.

Returns:
psPtr
Parameters:
ptr  Pointer to increment refCounter, and return

void psMemSetDeallocator ( psPtr  ptr,
psFreeFunc  freeFunc 
)

Set the deallocator routine.

A deallocator routine can optionally be assigned to a memory block to ensure that associated memory blocks also get freed, e.g., memory buffers referenced within a struct.

Parameters:
ptr  the memory block to operate on
freeFunc  the function to be executed at deallocation

void psMemSetPersistent ( psPtr  ptr,
bool  value 
)

Set the memory as persistent so that it is ignored when detecting memory leaks.

Used to mark a memory block as persistent data within the library, i.e., non user-level data used to hold psLib's state or cache data. Such examples of this class of memory is psTrace's trace-levels and dynamic error codes.

Memory marked as persistent is excluded from memory leak checks.

Parameters:
ptr  the memory block to operate on
value  true if memory is persistent, otherwise false

bool psMemSetThreadSafety ( bool  safe  ) 

Activate or Deactivate thread safety and mutex locking in the memory management.

psMemThreadSafety shall turn on thread safety in the memory management functions if safe is true, and deactivate all mutex locking in the memory management functions if safe is false. The function shall return the previous value of the thread safety. Note that the default behaviour of the library shall be for the locking to be performed.

Returns:
bool: The previous value of the thread safety.
Parameters:
safe  boolean for turning on/off thread safety

size_t psMemStats ( const bool  print,
size_t *  allocated,
size_t *  persistent 
)

return statistics on memory usage persistent memory that's currently allocated

Returns:
the total amount of memory owned by psLib; if non-NULL also provide a breakdown into allocated and allocated-and-persistent
Parameters:
print  print details as they're found?
allocated  memory that's currently allocated (but not persistent)

psPtr psRealloc ( psPtr  ptr,
size_t  size 
)

Memory re-allocation.

This operates much like realloc(), but is guaranteed to return a non-NULL value.

Returns:
psPtr pointer to resized buffer. This will not be NULL.
See also:
psAlloc, psFree
Parameters:
ptr  Pointer to re-allocate
size  Size required


Generated on Fri Feb 2 22:25:49 2007 for pslib by  doxygen 1.5.1