mksqlite  2.5
A MATLAB interface to SQLite
Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Attributes | List of all members
HeapCheck Class Reference

Helperclass for memory leak and access violation detection. More...

#include <heap_check.hpp>

Classes

struct  tagHeader
 Memory block header. More...
 

Public Member Functions

 HeapCheck ()
 Standard ctor.
 
 ~HeapCheck ()
 Destructor. More...
 
void Release ()
 Releasing unfreed memory. More...
 
void AddPtr (const tagHeader *ptr)
 Enqueues new memory block by header pointer ptr.
 
void RemovePtr (const tagHeader *ptr)
 Removes memory block, identified by ptr without freeing it. More...
 
void * New (size_t bytes, const char *file, const char *fcn, const char *notes, long nLine)
 Allocates a new block of memory with initialized header and footer. More...
 
void * Realloc (void *ptr_old, size_t bytes, const char *file, const char *fcn, const char *notes, long nLine)
 Reallocates a block of memory allocated with New() More...
 
void Free (void *ptr)
 Freeing space returned from New() or Realloc()
 
void UpdateNotes (void *ptr, const char *notes)
 Update "notes" field in memory block header.
 
void Walk (const char *text=NULL)
 Reporting walk through the linked memory list. More...
 

Static Public Member Functions

static size_t GetHeaderSize ()
 Returns the header size in bytes.
 
static int isPtrAligned (const void *ptr)
 Checks if header pointer ptr is well aligned. More...
 
static int VerifyPtr (const void *ptr)
 Checks if header pointer ptr is valid. More...
 
static void RenderDesc (const tagHeader *header, char *lpBuffer, size_t szBuffer)
 Formatted output of memory block information (from its header) More...
 

Private Types

typedef std::vector< const tagHeader * > vec_tagHeader
 Memory blocks linked list typedef.
 

Private Attributes

vec_tagHeader m_mem_blocks
 Linked list of memory blocks used in module scope.
 
bool flag_blocks_checked
 Flag will be set, when the block m_mem_blocks is released and checked.
 

Detailed Description

Helperclass for memory leak and access violation detection.

Definition at line 52 of file heap_check.hpp.


Class Documentation

◆ HeapCheck::tagFooter

struct HeapCheck::tagFooter

Memory block footer (end marker)

The footer only holds a pointer to the memory block header and is used for consistence check only.

Definition at line 82 of file heap_check.hpp.

+ Collaboration diagram for HeapCheck::tagFooter:
Class Members
tagHeader * lpHeader pointer to header of this memory block

◆ HeapCheck::tagHeader

struct HeapCheck::tagHeader

Memory block header.

Each memory block is surrounded by additional information: A preceding header stores who is responsible to this memory, some additional notes (comments) an a pointer to the end of the memory block (footer). By checking consitency of header and footer, one can test, if any close memory (write) accesses violation occures.

Definition at line 66 of file heap_check.hpp.

+ Collaboration diagram for HeapCheck::tagHeader:
Class Members
long lLineNumber line number or 0
const char * lpFilename filename or NULL
tagFooter * lpFooter pointer to footer, contiguous to memory block
const char * lpFunctionName function name
void * lpMem pointer to memory block (contiguous to this header space)
const char * lpNotes pointer to further notes or NULL

Constructor & Destructor Documentation

◆ ~HeapCheck()

HeapCheck::~HeapCheck ( )
inline

Destructor.

The destructor frees orphan memory space while reporting.

Definition at line 113 of file heap_check.hpp.

References Release().

Member Function Documentation

◆ isPtrAligned()

static int HeapCheck::isPtrAligned ( const void *  ptr)
inlinestatic

Checks if header pointer ptr is well aligned.

Headers are always aligned to HC_ALIGNMENT. Any other alignment inidcates an error.

Definition at line 170 of file heap_check.hpp.

References HC_ALIGNMENT.

Referenced by VerifyPtr().

+ Here is the caller graph for this function:

◆ New()

void* HeapCheck::New ( size_t  bytes,
const char *  file,
const char *  fcn,
const char *  notes,
long  nLine 
)
inline

Allocates a new block of memory with initialized header and footer.

Parameters
[in]bytesSize of memory needed
[in]fileSource filename with calling function
[in]fcnName of calling function
[in]notesAdditional (optional) notes as comment for reporting function
[in]nLineLine number of calling function
Returns
Pointer to callers memory block (not to header!) or NULL on fail

Definition at line 239 of file heap_check.hpp.

References AddPtr(), HC_DOALIGN, HeapCheck::tagHeader::lLineNumber, HeapCheck::tagHeader::lpFilename, HeapCheck::tagHeader::lpFooter, HeapCheck::tagHeader::lpFunctionName, HeapCheck::tagFooter::lpHeader, HeapCheck::tagHeader::lpMem, and HeapCheck::tagHeader::lpNotes.

◆ Realloc()

void* HeapCheck::Realloc ( void *  ptr_old,
size_t  bytes,
const char *  file,
const char *  fcn,
const char *  notes,
long  nLine 
)
inline

Reallocates a block of memory allocated with New()

Parameters
[in]ptr_oldPointer returned by New() or NULL
[in]bytesSize of memory needed
[in]fileSource filename with calling function
[in]fcnName replacement of calling function or NULL if not
[in]notesAdditional (optional) notes replacement as comment for reporting function or NULL if not
[in]nLineLine number replacement of calling functionor 0 if not
Returns
Pointer to callers memory block (not to header!) or NULL on fail

Definition at line 282 of file heap_check.hpp.

References AddPtr(), HC_DOALIGN, HeapCheck::tagHeader::lLineNumber, HeapCheck::tagHeader::lpFilename, HeapCheck::tagHeader::lpFooter, HeapCheck::tagHeader::lpFunctionName, HeapCheck::tagFooter::lpHeader, HeapCheck::tagHeader::lpMem, HeapCheck::tagHeader::lpNotes, MEM_REALLOC, RemovePtr(), and VerifyPtr().

◆ Release()

void HeapCheck::Release ( )
inline

Releasing unfreed memory.

Releasing memory, that is logged in the list of allocated memory blocks. Calling this function ensures that heap space is cleanly leaved. If any space is freed, a message will be displayed in MATLAB command window.

Definition at line 127 of file heap_check.hpp.

References MEM_FREE, PRINTF, and Walk().

Referenced by ~HeapCheck().

+ Here is the caller graph for this function:

◆ RemovePtr()

void HeapCheck::RemovePtr ( const tagHeader ptr)
inline

Removes memory block, identified by ptr without freeing it.

Todo:
SLOW (sorted list facilitates binary search)

Definition at line 215 of file heap_check.hpp.

Referenced by Free(), and Realloc().

+ Here is the caller graph for this function:

◆ RenderDesc()

static void HeapCheck::RenderDesc ( const tagHeader header,
char *  lpBuffer,
size_t  szBuffer 
)
inlinestatic

Formatted output of memory block information (from its header)

Parameters
[in]headerPointer to the header identifying the memory block
[in,out]lpBufferBuffer to hold the output (ASCII)
[in]szBufferSize of the buffer lpBuffer

Definition at line 371 of file heap_check.hpp.

References HeapCheck::tagHeader::lpMem.

Referenced by Walk().

+ Here is the caller graph for this function:

◆ VerifyPtr()

static int HeapCheck::VerifyPtr ( const void *  ptr)
inlinestatic

Checks if header pointer ptr is valid.

Check if ptr is well aligned and header and footer are consistent. If not, the was an access violation.

Definition at line 183 of file heap_check.hpp.

References HC_ASSERT, isPtrAligned(), HeapCheck::tagHeader::lpFooter, HeapCheck::tagFooter::lpHeader, and HeapCheck::tagHeader::lpMem.

Referenced by Free(), Realloc(), and UpdateNotes().

+ Here is the caller graph for this function:

◆ Walk()

void HeapCheck::Walk ( const char *  text = NULL)
inline

Reporting walk through the linked memory list.

Parameters
[in]textText which is additionally outputted to each memory block report, or NULL if not

Definition at line 407 of file heap_check.hpp.

References PRINTF, and RenderDesc().

Referenced by mexFunction(), and Release().

+ Here is the caller graph for this function:

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