Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
malloc_dbg.h File Reference

Header file that contains macros used to replace malloc/calloc and free. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define malloc(bytes)   malloc_dbg(bytes, __LINE__, __FILE__, __FUNCTION__)
 This macro replace the standard malloc function with malloc_dbg.
 
#define calloc(elemCount, elemSize)   calloc_dbg(elemCount, elemSize, __LINE__, __FILE__, __FUNCTION__)
 This macro replace the standard calloc function with calloc_dbg.
 
#define free(ptr)   free_dbg(ptr)
 This macro replace the standard free function with free_dbg.
 

Functions

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 (void)
 printLeaks function is used to print all the memory leaks. More...
 

Detailed Description

Header file that contains macros used to replace malloc/calloc and free.

Macros malloc, calloc and free respectively calls malloc_dbg, calloc_dbg and free_dbg. malloc_dbg and calloc_dbg allocates memory using the "real" malloc and calloc and store the pointer returned (with additional informations) in a linked list. Thanks to this linked list, it is possible to check memory leaks.

Author
tinouduart33
See also
malloc_dbg.c

Function Documentation

◆ calloc_dbg()

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.

Parameters
elementCountnumber of element to allocate
elementSizeSize of each element
lineLine number in the caller file
filenameCaller file
functionNameCaller function
Returns
Pointer returned by calloc if it's valid, NULL otherwhise.
175{
176 void* ptrToReturn = calloc(elementCount, elementSize);
177 if (!ptrToReturn)
178 {
179 return NULL;
180 }
181
182 // We must check atexitCalled value to know if we already called the function
183 if (!atexitCalled)
184 {
185 atexit(printLeaks); // Used to call printLeaks when the program exit
186 atexitCalled = 1;
187 }
188
189 // Add a new element in the mem_info list
190 memoryInformation = addMemInfo(memoryInformation, ptrToReturn, elementCount * elementSize, line, filename, functionName);
192 {
193 free(ptrToReturn);
194 return NULL;
195 }
196
197 return ptrToReturn;
198}
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.
Definition: malloc_dbg.c:51
void printLeaks()
printLeaks function is used to print all the memory leaks.
Definition: malloc_dbg.c:264
int atexitCalled
Another global variable.
Definition: malloc_dbg.c:37
mem_info * memoryInformation
We use a global variable for the list so we can use it at any time.
Definition: malloc_dbg.c:33
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition: malloc_dbg.h:22
Here is the call graph for this function:

◆ free_dbg()

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.

Parameters
ptrToFreePointer that must be freed
Returns
Nothing.
209{
211 mem_info* toFree = NULL;
212 mem_info* previous = NULL;
213
214 // Check if the head contains the pointer to free
215 if (tmp->ptr == ptrToFree)
216 {
217 toFree = tmp;
218 memoryInformation = tmp->next;
219 free(toFree->ptr);
220 free(toFree);
222 {
224 }
225 return;
226 }
227
228 // We can loop through the list without any problems, the head is not the pointer
229 while (tmp)
230 {
231 if (tmp->ptr == ptrToFree) // If we found the pointer that must be freed
232 {
233 toFree = tmp;
234 tmp = tmp->next;
235 previous = toFree->previous;
236
237 if (previous)
238 {
239 previous->next = tmp;
240 }
241 if (tmp)
242 {
243 tmp->previous = previous;
244 }
245
246 free(toFree->ptr);
247 if (toFree == memoryInformation)
248 {
249 memoryInformation = NULL;
250 }
251 free(toFree);
252 return;
253 }
254 tmp = tmp->next;
255 }
256}
For the malloc, calloc and free functions.
Definition: malloc_dbg.c:21
struct MEMORY_INFORMATION * previous
Previous element in the list.
Definition: malloc_dbg.c:28
void * ptr
Pointer returned by malloc / calloc.
Definition: malloc_dbg.c:22
struct MEMORY_INFORMATION * next
Next element in the list.
Definition: malloc_dbg.c:27
struct node * next
pointer to the node
Definition: bfs.c:24

◆ malloc_dbg()

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.

Parameters
bytesSize of the allocation in bytes
filenameCaller file
functionNameCaller function
Returns
Pointer returned by malloc if it's valid, NULL otherwhise.
130{
131 void* ptrToReturn = malloc(bytes);
132 int pos = 0;
133 if (!ptrToReturn)
134 {
135 return NULL;
136 }
137
138 // We must check atexitCalled value to know if we already called the function
139 if (!atexitCalled)
140 {
141 atexit(printLeaks); // Used to call printLeaks when the program exit
142 atexitCalled = 1;
143 }
144
145 pos = inList(filename, line);
146 if (pos == -1)
147 {
148 // Add a new element in the mem_info list
149 memoryInformation = addMemInfo(memoryInformation, ptrToReturn, bytes, line, filename, functionName);
151 {
152 free(ptrToReturn);
153 return NULL;
154 }
155 }
156 else
157 {
158 editInfo(pos, bytes);
159 }
160 return ptrToReturn;
161}
void editInfo(int elemPos, size_t bytes)
editInfo function is used to edit an element in the memoryInfo list.
Definition: malloc_dbg.c:107
int inList(const char *filename, int line)
inList function is used to know if an element is already in the memoryInfo list.
Definition: malloc_dbg.c:79
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
Here is the call graph for this function:

◆ printLeaks()

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.

Returns
Nothing.
265{
267 mem_info* previous = NULL;
268 size_t sum = 0;
269 int nbBlocks = 0;
270
271 if (tmp)
272 {
273 printf("Memory Leaks detected.\n");
274 }
275
276 while (tmp)
277 {
278 previous = tmp;
279 printf("\n%ld bytes lost\n", tmp->bytes);
280 printf("address : 0x%p in %s\t%s:%d\n", tmp->ptr, tmp->functionName, tmp->fileName, tmp->line);
281 printf("\n====================================\n");
282 sum += tmp->bytes;
283 tmp = tmp->next;
284 free(previous);
285 nbBlocks++;
286 }
287
288 printf("SUMMARY :\n%ld bytes lost in %d blocks\n", sum, nbBlocks);
289}