Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
This file contains malloc_dbg, calloc_dbg, free_dbg and printLeaks implementations. More...
Data Structures | |
struct | MEMORY_INFORMATION |
For the malloc, calloc and free functions. More... | |
Typedefs | |
typedef struct MEMORY_INFORMATION | mem_info |
For the malloc, calloc and free functions. More... | |
Functions | |
mem_info * | addMemInfo (mem_info *memoryInfo, void *ptrToReturn, size_t bytes, int line, const char *filename, const char *functionName) |
addMemInfo function add a memory allocation in the memoryInfo list. More... | |
int | inList (const char *filename, int line) |
inList function is used to know if an element is already in the memoryInfo list. More... | |
void | editInfo (int elemPos, size_t bytes) |
editInfo function is used to edit an element in the memoryInfo list. More... | |
void * | malloc_dbg (size_t bytes, int line, const char *filename, const char *functionName) |
malloc_dbg function is a wrapper around the malloc function. More... | |
void * | calloc_dbg (size_t elementCount, size_t elementSize, int line, const char *filename, const char *functionName) |
calloc_dbg function is a wrapper around the calloc function. More... | |
void | free_dbg (void *ptrToFree) |
free_dbg function is used to free the memory allocated to a pointer. More... | |
void | printLeaks () |
printLeaks function is used to print all the memory leaks. More... | |
Variables | |
mem_info * | memoryInformation = NULL |
We use a global variable for the list so we can use it at any time. | |
int | atexitCalled = 0 |
Another global variable. More... | |
This file contains malloc_dbg, calloc_dbg, free_dbg and printLeaks implementations.
typedef struct MEMORY_INFORMATION mem_info |
For the malloc, calloc and free functions.
For IO operations (printf). For the memcmp function. Header file which contains the prototypes of malloc_dbg, calloc_dbg and fre_dbg.
Structure used to save an allocated pointer
mem_info * addMemInfo | ( | mem_info * | memoryInfo, |
void * | ptrToReturn, | ||
size_t | bytes, | ||
int | line, | ||
const char * | filename, | ||
const char * | functionName | ||
) |
addMemInfo function add a memory allocation in the memoryInfo list.
This function creates a new element and add it on top of the list
memoryInfo | Pointer to the doubly linked list used to store all of the allocations |
ptrToreturn | Pointer returned by malloc or calloc |
bytes | Size in bytes of the allocation |
line | Line where the allocation has been called |
filename | File where the allocation has been called |
functionName | Name of the function where the allocation has been called |
void * calloc_dbg | ( | size_t | elementCount, |
size_t | elementSize, | ||
int | line, | ||
const char * | filename, | ||
const char * | functionName | ||
) |
calloc_dbg function is a wrapper around the calloc function.
This function calls calloc and allocates the number of bytes passed in the parameters. If the allocation succeeds then it add the pointer returned by malloc in the mem_info list.
elementCount | number of element to allocate |
elementSize | Size of each element |
line | Line number in the caller file |
filename | Caller file |
functionName | Caller function |
void editInfo | ( | int | elemPos, |
size_t | bytes | ||
) |
editInfo function is used to edit an element in the memoryInfo list.
This function is used to edit the number of bytes allocated at a specific line.
elemPos | Position of an element in the doubly linked list memoryInfo |
bytes | Size of the allocation in bytes |
void free_dbg | ( | void * | ptrToFree | ) |
free_dbg function is used to free the memory allocated to a pointer.
This function free the memory pointed by the pointer passed in parameter. To free this pointer, we loop through the mem_info list and check if we find the pointer. Once it's found, the pointer is freed and the element is deleted from the list.
ptrToFree | Pointer that must be freed |
int inList | ( | const char * | filename, |
int | line | ||
) |
inList function is used to know if an element is already in the memoryInfo list.
This function is used to know if an allocation in a specific file at a specific line already exists in the list.
filename | File in which malloc or calloc has been called |
line | Line number in the file in which malloc or calloc has been called |
void * malloc_dbg | ( | size_t | bytes, |
int | line, | ||
const char * | filename, | ||
const char * | functionName | ||
) |
malloc_dbg function is a wrapper around the malloc function.
This function calls malloc and allocates the number of bytes passed in the parameters. If the allocation succeeds then it add the pointer returned by malloc in the mem_info list.
bytes | Size of the allocation in bytes |
filename | Caller file |
functionName | Caller function |
void printLeaks | ( | void | ) |
printLeaks function is used to print all the memory leaks.
This function is called when the program exits. It loop through the mem_info list and if it's not empty, it prints the memory leaks.
int atexitCalled = 0 |
Another global variable.
This one is used to know if we already call the atexit function.