19#define _XOPEN_SOURCE 500
26static uint64_t NbBlocks;
27static uint64_t RAMSize;
29static unsigned int memtrack_init();
30static unsigned int memtrack_addblock_preinit(
const void *p_Ptr,
34 const char *p_CompilDate,
35 const char *p_CompilTime,
36 const char *p_Function);
37static unsigned int memtrack_delblock_preinit(
const void *p_Ptr,
40 const char *p_CompilDate,
41 const char *p_CompilTime,
42 const char *p_Function);
43static uint64_t memtrack_dumpblocks_preinit();
44static uint64_t memtrack_getallocatedblocks_preinit();
45static uint64_t memtrack_getallocatedRAM_preinit();
46static size_t memtrack_getblocksize_preinit(
const void *p_Ptr);
47static unsigned int memtrack_addblock_postinit(
const void *p_Ptr,
51 const char *p_CompilDate,
52 const char *p_CompilTime,
53 const char *p_Function);
54static unsigned int memtrack_delblock_postinit(
const void *p_Ptr,
57 const char *p_CompilDate,
58 const char *p_CompilTime,
59 const char *p_Function);
60static uint64_t memtrack_dumpblocks_postinit();
61static uint64_t memtrack_getallocatedblocks_postinit();
62static uint64_t memtrack_getallocatedRAM_postinit();
63static size_t memtrack_getblocksize_postinit(
const void *p_Ptr);
89 while (Tail!=Head->
Next) {
90 memtrack_delblock_postinit(Head->
Next->
Ptr,NULL,0,NULL,NULL,NULL);
116static unsigned int memtrack_init() {
120 if ((NULL==Head)||(NULL==Tail)) {
121 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
129 Tail->
Ptr = Head->
Ptr = (
void *)NULL;
131 Tail->
File = Head->
File = (
char *)NULL;
164static unsigned int memtrack_addblock_preinit(
const void *p_Ptr,
168 const char *p_CompilDate,
169 const char *p_CompilTime,
170 const char *p_Function) {
172 if (0!=memtrack_init()) {
173 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
181 p_CompilTime, p_Function);
195static unsigned int memtrack_delblock_preinit(
const void *p_Ptr,
198 const char *p_CompilDate,
199 const char *p_CompilTime,
200 const char *p_Function) {
202 if (0!=memtrack_init()) {
203 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
211 p_CompilTime, p_Function);
225static uint64_t memtrack_dumpblocks_preinit() {
227 if (0!=memtrack_init()) {
228 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
249static uint64_t memtrack_getallocatedblocks_preinit() {
251 if (0!=memtrack_init()) {
252 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
273static uint64_t memtrack_getallocatedRAM_preinit() {
275 if (0!=memtrack_init()) {
276 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
297static size_t memtrack_getblocksize_preinit(
const void *p_Ptr) {
299 if (0!=memtrack_init()) {
300 fprintf(stderr,
"%s:%d Not enough memory to initialize memtracker\n",
318static unsigned int memtrack_addblock_postinit(
const void *p_Ptr,
322 const char *p_CompilDate,
323 const char *p_CompilTime,
324 const char *p_Function) {
334 (NULL==p_CompilDate)||
335 (0==p_CompilDate[0])||
336 (NULL==p_CompilTime)||
337 (0==p_CompilTime[0])||
339 (0==p_Function[0])) {
340 fprintf(stderr,
"%s:%d Null or empty parameters\n",__FILE__, __LINE__);
350 while ((l_tmp->
Ptr != p_Ptr) && (l_tmp != Tail))
355 fprintf(stderr,
"%s:%d Memory bloc already registered\n",__FILE__, __LINE__);
368 l_tmp->
Ptr = (
void *)p_Ptr;
369 l_tmp->
Size = p_Size;
370 if (NULL==(l_tmp->
File =
strdup(p_File?p_File:
""))) {
374 l_tmp->
Line = p_Line;
414static unsigned int memtrack_delblock_postinit(
const void *p_Ptr,
417 const char *p_CompilDate,
418 const char *p_CompilTime,
419 const char *p_Function) {
429 while ((l_tmp->
Ptr != p_Ptr) && (l_tmp != Tail))
434 fprintf(stderr,
"%s:%d Block not found for deletion\n",__FILE__, __LINE__);
451 RAMSize -= l_tmp->
Size;
467static uint64_t memtrack_dumpblocks_postinit() {
469 uint64_t l_NbBlocks = 0;;
471 if (Head->
Next != Tail) {
474 "+----------------------------------------------------------------------------------------------------------+\n");
475 fprintf(stderr,
"| %-104s |\n",
"Memory Tracker Report");
477 "+----------------------+----------------------+------+-----------------+----------+------------------------+\n");
479 "| %-20s | %-20s | %-4s | %-15s | %-8s | %-22s |\n",
480 "Function",
"File",
"Line",
"Address",
"Bytes",
483 "+----------------------+----------------------+------+-----------------+----------+------------------------+\n");
487 while (l_tmp != Tail) {
489 "| %-20s | %-20s | %4d | %15p | %8lu | %11s @ %8s |\n",
494 l_BlockSize += l_tmp->
Size;
498 "+----------------------+----------------------+------+-----------------+----------+------------------------+\n");
500 "| %9lu bytes in %6lu blocks. %70s |\n", (
unsigned long)l_BlockSize,
501 (
unsigned long)l_NbBlocks,
"");
503 "+----------------------------------------------------------------------------------------------------------+\n");
517static uint64_t memtrack_getallocatedblocks_postinit() {
529static uint64_t memtrack_getallocatedRAM_postinit() {
541static size_t memtrack_getblocksize_postinit(
const void *p_Ptr) {
544 while ((l_tmp->
Ptr != p_Ptr) && (l_tmp != Tail))
588 const char *p_CompilDate,
589 const char *p_CompilTime,
590 const char *p_Function) =
591 memtrack_addblock_preinit;
624 const char *p_CompilDate,
625 const char *p_CompilTime,
626 const char *p_Function) =
627 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.