mkernel 0.0.2
Micro-kernel framework, everything as a module
memdbg.h File Reference

Memory leak tracker header. More...

#include "memtrack.h"
Include dependency graph for memdbg.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void * dbg_malloc (const size_t Size, const char *File, const int Line, const char *CompilDate, const char *CompilTime, const char *Function)
 Malloc compatible standard allocation. More...
 
void dbg_free (void *Ptr, const char *File, const int Line, const char *CompilDate, const char *CompilTime, const char *Function)
 Free compatible standard memory release. More...
 
void * dbg_calloc (const size_t NMemb, const size_t Size, const char *File, const int Line, const char *CompilDate, const char *CompilTime, const char *Function)
 Allocate a table of item from the size of each and number. More...
 
void * dbg_realloc (void *Ptr, const size_t Size, const char *File, const int Line, const char *CompilDate, const char *CompilTime, const char *Function)
 Resize an already allocated and tracked block. More...
 
char * dbg_strdup (const char *Ptr, const char *File, const int Line, const char *CompilDate, const char *CompilTime, const char *Function)
 String duplication with allocation. More...
 
int dbg_asprintf (char **p_Ptr, const char *p_Format, const char *File, const int Line, const char *CompilDate, const char *CompilTime, const char *Function,...)
 Build a formatted string with allocation. More...
 

Detailed Description

Memory leak tracker header.

Date
25/09/2006
Author
François Cerbelle (Fanfan), franc.nosp@m.ois@.nosp@m.cerbe.nosp@m.lle..nosp@m.net

Originally inspired by "L'art du code", Steve Maguire, Microsoft Press

Definition in file memdbg.h.

Function Documentation

◆ dbg_asprintf()

int dbg_asprintf ( char **  p_Ptr,
const char *  p_Format,
const char *  File,
const int  Line,
const char *  CompilDate,
const char *  CompilTime,
const char *  Function,
  ... 
)

Build a formatted string with allocation.

Parameters
[in,out]p_Ptr: Pointer on the to be built string
[in]p_Format: Pointer on the format string
[in]File: Source file
[in]Line: Source line number
[in]CompilDate: File compilation date
[in]CompilTime: File compilation time
[in]Function: Source function name
[in]...: Values referenced by the format string
Returns
Status of the formatting
Return values
>=0number of chars in the output string (as strdup)
-1in case of error (*p_Ptr contents is undefined)
Todo:
Implement a vasprintf wrapping function to catch allocation and use it here

Definition at line 196 of file memdbg.c.

◆ dbg_calloc()

void * dbg_calloc ( const size_t  NMemb,
const size_t  Size,
const char *  File,
const int  Line,
const char *  CompilDate,
const char *  CompilTime,
const char *  Function 
)

Allocate a table of item from the size of each and number.

Uses malloc to allocate and track the memory bloc. If allocation and tracking succeed, fill the memory block with zeros.

Parameters
[in]NMemb: Item number in the table
[in]Size: Item size in bytes
[in]File: Source file
[in]Line: Source line number
[in]CompilDate: File compilation date
[in]CompilTime: File compilation time
[in]Function: Source function name
Returns
Allocated block address
Return values
Addressif succeeded,
NULLif not possible.

Definition at line 76 of file memdbg.c.

Here is the call graph for this function:

◆ dbg_free()

void dbg_free ( void *  Ptr,
const char *  File,
const int  Line,
const char *  CompilDate,
const char *  CompilTime,
const char *  Function 
)

Free compatible standard memory release.

If the Ptr value is not NULL, try to untrack it. If it can not be found in the tracked list, because it was not tracked, it was allocated by a non monitored function, the function abort the process for investigation (missing a monitoring macro/function in the tracker, bug in the tracker, allocation from an external non-instrumented library). If the Ptr value was tracked, found and removed from the list successfully, forward it to free for actual free.

Parameters
[in]Ptr: Pointer on the memory to free
[in]File: Source file
[in]Line: Source line number
[in]CompilDate: File compilation date
[in]CompilTime: File compilation time
[in]Function: Source function name

Definition at line 52 of file memdbg.c.

Here is the caller graph for this function:

◆ dbg_malloc()

void * dbg_malloc ( const size_t  Size,
const char *  File,
const int  Line,
const char *  CompilDate,
const char *  CompilTime,
const char *  Function 
)

Malloc compatible standard allocation.

Parameters
[in]Size: Requested size in bytes
[in]File: Source file
[in]Line: Source line number
[in]CompilDate: File compilation date
[in]CompilTime: File compilation time
[in]Function: Source function name
Returns
Allocated block address
Return values
Addressif succeeded,
NULLif not possible.

Definition at line 25 of file memdbg.c.

Here is the caller graph for this function:

◆ dbg_realloc()

void * dbg_realloc ( void *  Ptr,
const size_t  Size,
const char *  File,
const int  Line,
const char *  CompilDate,
const char *  CompilTime,
const char *  Function 
)

Resize an already allocated and tracked block.

This function always uses malloc to allocate a new block and force an address change, and a data loss in case of shrink, which is the worst case scenario for realloc.

As for realloc, a NULL source pointer makes realloc act as malloc, and a new size set to 0 acts as free. The input pointer is left untouched but should not be used or freed anymore. It is always different than the return value. The input values referenced by the input pointer are not wiped.

This implementation can not be used if realloc is used to reduce a huge bloc in order to manage an OOM situation. Real realloc can succeed by actually downsizing the same memory block, inplace, but this implementation will fail because it first allocate a new block.

Parameters
[in]Ptr: Pointer on the memory to resize
[in]Size: New size in bytes
[in]File: Source file
[in]Line: Source line number
[in]CompilDate: File compilation date
[in]CompilTime: File compilation time
[in]Function: Source function name
Returns
Resized block address
Return values
Addressif succeeded,
NULLif not possible.

< Existing bloc size

< New block

Definition at line 102 of file memdbg.c.

Here is the call graph for this function:

◆ dbg_strdup()

char * dbg_strdup ( const char *  Ptr,
const char *  File,
const int  Line,
const char *  CompilDate,
const char *  CompilTime,
const char *  Function 
)

String duplication with allocation.

Parameters
[in]Ptr: Pointer on the string to copy
[in]File: Source file
[in]Line: Source line number
[in]CompilDate: File compilation date
[in]CompilTime: File compilation time
[in]Function: Source function name
Returns
Copied string address
Return values
Addressif succeeded,
NULLif not possible.

Copy address

Definition at line 163 of file memdbg.c.