25static uint64_t NbBlocks;
26static uint64_t RAMSize;
29static unsigned int memtrack_init();
32static unsigned int memtrack_addblock_preinit(
const void *p_Ptr,
36 const char *p_CompilDate,
37 const char *p_CompilTime,
38 const char *p_Function);
41static unsigned int memtrack_delblock_preinit(
const void *p_Ptr,
44 const char *p_CompilDate,
45 const char *p_CompilTime,
46 const char *p_Function);
49static uint64_t memtrack_dumpblocks_preinit();
52static uint64_t memtrack_getallocatedblocks_preinit();
55static uint64_t memtrack_getallocatedRAM_preinit();
58static size_t memtrack_getblocksize_preinit(
const void *p_Ptr);
61static unsigned int memtrack_addblock_postinit(
const void *p_Ptr,
65 const char *p_CompilDate,
66 const char *p_CompilTime,
67 const char *p_Function);
70static unsigned int memtrack_delblock_postinit(
const void *p_Ptr,
73 const char *p_CompilDate,
74 const char *p_CompilTime,
75 const char *p_Function);
78static uint64_t memtrack_dumpblocks_postinit();
81static uint64_t memtrack_getallocatedblocks_postinit();
84static uint64_t memtrack_getallocatedRAM_postinit();
87static size_t memtrack_getblocksize_postinit(
const void *p_Ptr);
114 while (Tail!=Head->
Next) {
141static unsigned int memtrack_init()
147 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
190static unsigned int memtrack_addblock_preinit(
const void *p_Ptr,
194 const char *p_CompilDate,
195 const char *p_CompilTime,
196 const char *p_Function)
199 if (0!=memtrack_init()) {
200 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
208 p_CompilTime, p_Function);
222static unsigned int memtrack_delblock_preinit(
const void *p_Ptr,
225 const char *p_CompilDate,
226 const char *p_CompilTime,
227 const char *p_Function)
230 if (0!=memtrack_init()) {
231 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
239 p_CompilTime, p_Function);
253static uint64_t memtrack_dumpblocks_preinit()
256 if (0!=memtrack_init()) {
257 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
278static uint64_t memtrack_getallocatedblocks_preinit()
281 if (0!=memtrack_init()) {
282 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
303static uint64_t memtrack_getallocatedRAM_preinit()
306 if (0!=memtrack_init()) {
307 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
328static size_t memtrack_getblocksize_preinit(
const void *p_Ptr)
331 if (0!=memtrack_init()) {
332 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
350static unsigned int memtrack_addblock_postinit(
const void *p_Ptr,
354 const char *p_CompilDate,
355 const char *p_CompilTime,
356 const char *p_Function)
367 (
NULL==p_CompilDate)||
368 (0==p_CompilDate[0])||
369 (
NULL==p_CompilTime)||
370 (0==p_CompilTime[0])||
372 (0==p_Function[0])) {
373 fprintf(stderr,
"%s:%d Null or empty parameters\n",__FILE__, __LINE__);
383 while ((l_tmp->
Ptr != p_Ptr) && (l_tmp != Tail))
388 fprintf(stderr,
"%s:%d Memory bloc already registered\n",__FILE__, __LINE__);
401 l_tmp->
Ptr = (
void *)p_Ptr;
402 l_tmp->
Size = p_Size;
407 l_tmp->
Line = p_Line;
447static unsigned int memtrack_delblock_postinit(
const void *p_Ptr,
450 const char *p_CompilDate,
451 const char *p_CompilTime,
452 const char *p_Function)
463 while ((l_tmp->
Ptr != p_Ptr) && (l_tmp != Tail))
468 fprintf(stderr,
"%s:%d Block not found for deletion\n",__FILE__, __LINE__);
485 RAMSize -= l_tmp->
Size;
501static uint64_t memtrack_dumpblocks_postinit()
504 uint64_t l_NbBlocks = 0;;
506 if (Head->
Next != Tail) {
509 "+----------------------------------------------------------------------------------------------------------+\n");
510 fprintf(stderr,
"| %-104s |\n",
"Memory Tracker Report");
512 "+----------------------+----------------------+------+-----------------+----------+------------------------+\n");
514 "| %-20s | %-20s | %-4s | %-15s | %-8s | %-22s |\n",
515 "Function",
"File",
"Line",
"Address",
"Bytes",
518 "+----------------------+----------------------+------+-----------------+----------+------------------------+\n");
522 while (l_tmp != Tail) {
524 "| %-20s | %-20s | %4d | %15p | %8lu | %11s @ %8s |\n",
529 l_BlockSize += l_tmp->
Size;
533 "+----------------------+----------------------+------+-----------------+----------+------------------------+\n");
535 "| %9lu bytes in %6lu blocks. %70s |\n", (
unsigned long)l_BlockSize,
538 "+----------------------------------------------------------------------------------------------------------+\n");
552static uint64_t memtrack_getallocatedblocks_postinit()
565static uint64_t memtrack_getallocatedRAM_postinit()
578static size_t memtrack_getblocksize_postinit(
const void *p_Ptr)
582 while ((l_tmp->
Ptr != p_Ptr) && (l_tmp != Tail))
597 const char *p_CompilDate,
598 const char *p_CompilTime,
599 const char *p_Function) =
600 memtrack_addblock_preinit;
606 const char *p_CompilDate,
607 const char *p_CompilTime,
608 const char *p_Function) =
609 memtrack_delblock_preinit;
#define malloc(size)
Same syntaxt and same behavior than regular malloc function, with memory leaks tracking.
#define strdup(chaine)
Same syntaxt and same behavior than regular strdup function, with memory leaks tracking.
#define free(ptr)
Same syntaxt and same behavior than regular free function, with memory leaks tracking.
size_t(* memtrack_getblocksize)(const void *p_Ptr)
Functor to get size of a specific memory block.
void memtrack_reset()
Memory block metadata list reset.
uint64_t(* memtrack_getallocatedblocks)()
Functor to get the number of allocated blocks.
uint64_t(* memtrack_getallocatedRAM)()
Functor to get the total RAM size allocated.
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.
uint64_t(* memtrack_dumpblocks)()
Functor to list allocated memory blocks metadata.
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.
Memory block metadata tracking headers.
Memory block metadata list item.
size_t Size
Allocated memory block size.
struct MemBlock * Prev
Previous item pointer.
struct MemBlock * Next
Next item pointer.
char * CompilDate
Source file compilation date.
char * CompilTime
Source file compilation time.
char * File
Source file which asked the allocation.
char * Function
Fonction name which asked the allocation.
void * Ptr
Allocated memory block pointer.
int Line
Source line number ch asked the allocation.