mkernel 0.0.2
Micro-kernel framework, everything as a module
memtrack.c File Reference

Memory block metadata tracking implementation. More...

#include "memtrack.h"
#include <string.h>
#include <stdio.h>
Include dependency graph for memtrack.c:

Go to the source code of this file.

Functions

void memtrack_reset ()
 Memory block metadata list reset. More...
 

Variables

unsigned int(* memtrack_addblock )(const void *p_Ptr, const size_t p_Size, const char *p_File, const int p_Line, const char *p_CompilDate, const char *p_CompilTime, const char *p_Function)
 Functor to register an allocated memory block metadata. More...
 
unsigned int(* memtrack_delblock )(const void *p_Ptr, const char *p_File, const int p_Line, const char *p_CompilDate, const char *p_CompilTime, const char *p_Function)
 Functor to unregister an allocated memory block metadata. More...
 
uint64_t(* memtrack_dumpblocks )() = memtrack_dumpblocks_preinit
 Functor to list allocated memory blocks metadata. More...
 
uint64_t(* memtrack_getallocatedblocks )() = memtrack_getallocatedblocks_preinit
 Functor to get the number of allocated blocks. More...
 
uint64_t(* memtrack_getallocatedRAM )() = memtrack_getallocatedRAM_preinit
 Functor to get the total RAM size allocated. More...
 
size_t(* memtrack_getblocksize )(const void *p_Ptr) = memtrack_getblocksize_preinit
 Functor to get size of a specific memory block. More...
 

Detailed Description

Memory block metadata tracking implementation.

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 memtrack.c.

Function Documentation

◆ memtrack_reset()

void memtrack_reset ( )

Memory block metadata list reset.

This function is not defined in the header file because it should never be used. It is exported because it is used by the unit tests. NEVER USE IT, it will crash your code.

Definition at line 99 of file memtrack.c.

Variable Documentation

◆ memtrack_addblock

unsigned int(* memtrack_addblock) (const void *p_Ptr, const size_t p_Size, const char *p_File, const int p_Line, const char *p_CompilDate, const char *p_CompilTime, const char *p_Function) ( const void *  p_Ptr,
const size_t  p_Size,
const char *  p_File,
const int  p_Line,
const char *  p_CompilDate,
const char *  p_CompilTime,
const char *  p_Function 
)
Initial value:
=
memtrack_addblock_preinit

Functor to register an allocated memory block metadata.

Create and adds a memory block metadata record in the tracking system to detect memory leaks. It performs some basic sanity checks. The filename, compilation date and compilation time can not be null or empty, the line number can not be 0, the memory pointer to store can not be NULL or already registered and its size needs to be greater than 0.

The function is called from memdbg.c functions used in the application code thru memory.h macros. The macro automatically fills the filename, line number, compilation date and time, and function name (using GCC's non-ansi __func_ extension).

This functor is used to implement a lazy initialization. It initialy reference a temporary function to trigger memtrack initialization before calling the actual function. Then, it references the actual function directly to avoid any useless tests.

Parameters
[in]p_PtrAllocated memory block pointer
[in]p_SizeAllocated memory block size
[in]p_File: Source file
[in]p_Line: Source line number
[in]p_CompilDate: File compilation date
[in]p_CompilTime: File compilation time
[in]p_Function: Source function name
Returns
Registration status
Return values
0if succeeded,
1if not possible.

Definition at line 593 of file memtrack.c.

◆ memtrack_delblock

unsigned int(* memtrack_delblock) (const void *p_Ptr, const char *p_File, const int p_Line, const char *p_CompilDate, const char *p_CompilTime, const char *p_Function) ( const void *  p_Ptr,
const char *  p_File,
const int  p_Line,
const char *  p_CompilDate,
const char *  p_CompilTime,
const char *  p_Function 
)
Initial value:
=
memtrack_delblock_preinit

Functor to unregister an allocated memory block metadata.

Find and delete a memory block metadata record in the tracking system. It performs some basic sanity checks. The filename, compilation date and compilation time can not be null or empty, the line number can not be 0, the memory pointer to remove can not be NULL, it needs to already be registered.

The function is called from memdbg.c functions used in the application code thru memory.h macros. The macro automatically fills the filename, line number, compilation date and time, and function name (using GCC's non-ansi __func_ extension).

This functor is used to implement a lazy initialization. It initialy reference a temporary function to trigger memtrack initialization before calling the actual function. Then, it references the actual function directly to avoid any useless tests.

Parameters
[in]p_PtrAllocated memory block pointer
[in]p_File: Source file
[in]p_Line: Source line number
[in]p_CompilDate: File compilation date
[in]p_CompilTime: File compilation time
[in]p_Function: Source function name
Returns
Registration status
Return values
0if succeeded,
!0if not possible.

Definition at line 603 of file memtrack.c.

◆ memtrack_dumpblocks

uint64_t(* memtrack_dumpblocks) () ( ) = memtrack_dumpblocks_preinit

Functor to list allocated memory blocks metadata.

Dumps all the metadata of the registered memory blocks to stderr.

This functor is used to implement a lazy initialization. It initialy reference a temporary function to trigger memtrack initialization before calling the actual function. Then, it references the actual function directly to avoid any useless tests.

Returns
Number of registered blocks (counted)
Return values
0if succeeded,
!0if not possible.

Definition at line 612 of file memtrack.c.

◆ memtrack_getallocatedblocks

uint64_t(* memtrack_getallocatedblocks) () ( ) = memtrack_getallocatedblocks_preinit

Functor to get the number of allocated blocks.

This function returns the value of the internal counter of allocated memory blocks metadata.

This functor is used to implement a lazy initialization. It initialy reference a temporary function to trigger memtrack initialization before calling the actual function. Then, it references the actual function directly to avoid any useless tests.

Returns
Number of registered blocks

Definition at line 615 of file memtrack.c.

◆ memtrack_getallocatedRAM

uint64_t(* memtrack_getallocatedRAM) () ( ) = memtrack_getallocatedRAM_preinit

Functor to get the total RAM size allocated.

This function returns the internal summ of all the allocated memory blocks which are registered.

This functor is used to implement a lazy initialization. It initialy reference a temporary function to trigger memtrack initialization before calling the actual function. Then, it references the actual function directly to avoid any useless tests.

Returns
Total RAM size in bytes

Definition at line 618 of file memtrack.c.

◆ memtrack_getblocksize

size_t(* memtrack_getblocksize) (const void *p_Ptr) ( const void *  p_Ptr) = memtrack_getblocksize_preinit

Functor to get size of a specific memory block.

The function will search in the list for the specified pointer. If the pointer is not found, it will return 0, which is discriminent as the memtracker does not allow to track a zero sized block. The memtracker does not allow neither to track a NULL pointer, thus NULL will return 0. Otherwise, the function will return the memory block size.

This functor is used to implement a lazy initialization. It initialy reference a temporary function to trigger memtrack initialization before calling the actual function. Then, it references the actual function directly to avoid any useless tests.

Parameters
[in]p_PtrAllocated and tracked memory block pointer
Returns
Memory block size in bytes

Definition at line 621 of file memtrack.c.