Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
malloc_dbg.c File Reference

This file contains malloc_dbg, calloc_dbg, free_dbg and printLeaks implementations. More...

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

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_infoaddMemInfo (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_infomemoryInformation = NULL
 We use a global variable for the list so we can use it at any time.
 
int atexitCalled = 0
 Another global variable. More...
 

Detailed Description

This file contains malloc_dbg, calloc_dbg, free_dbg and printLeaks implementations.

Author
tinouduart33

Typedef Documentation

◆ mem_info

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

Function Documentation

◆ addMemInfo()

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

Parameters
memoryInfoPointer to the doubly linked list used to store all of the allocations
ptrToreturnPointer returned by malloc or calloc
bytesSize in bytes of the allocation
lineLine where the allocation has been called
filenameFile where the allocation has been called
functionNameName of the function where the allocation has been called
Returns
mem_info pointer if it succeeds, NULL otherwise
52 {
53  mem_info* newMemInfo = (mem_info*)malloc(sizeof(mem_info));
54  if (!newMemInfo)
55  {
56  return NULL;
57  }
58 
59  newMemInfo->ptr = ptrToReturn;
60  newMemInfo->bytes = bytes;
61  newMemInfo->line = line;
62  newMemInfo->fileName = filename;
63  newMemInfo->functionName = functionName;
64  newMemInfo->next = memoryInfo;
65  newMemInfo->previous = NULL;
67  memoryInformation->previous = newMemInfo;
68 
69  return newMemInfo;
70 }
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 malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
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
const char * functionName
Function in which malloc or calloc has been called.
Definition: malloc_dbg.c:24
void * ptr
Pointer returned by malloc / calloc.
Definition: malloc_dbg.c:22
int line
Line number (in file) corresponding to the malloc / calloc call.
Definition: malloc_dbg.c:26
size_t bytes
Number of bytes allocated.
Definition: malloc_dbg.c:25
const char * fileName
File in which malloc or calloc has been called.
Definition: malloc_dbg.c:23
struct MEMORY_INFORMATION * next
Next element in the list.
Definition: malloc_dbg.c:27

◆ 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);
191  if (!memoryInformation)
192  {
193  free(ptrToReturn);
194  return NULL;
195  }
196 
197  return ptrToReturn;
198 }
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 * 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
#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:

◆ editInfo()

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.

Parameters
elemPosPosition of an element in the doubly linked list memoryInfo
bytesSize of the allocation in bytes
Returns
Nothing.
108 {
109  int counter = 0;
111 
112  while (counter != elemPos)
113  {
114  tmp = tmp->next;
115  counter++;
116  }
117  tmp->bytes += bytes;
118 }

◆ 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);
221  if (memoryInformation)
222  {
223  memoryInformation->previous = NULL;
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 }

◆ inList()

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.

Parameters
filenameFile in which malloc or calloc has been called
lineLine number in the file in which malloc or calloc has been called
Returns
Position of the element in the list if the element is found, -1 otherwise.
80 {
82  int counter = 0;
83  int len = strlen(filename);
84 
85  while (tmp)
86  {
87  if (len == strlen(tmp->fileName))
88  {
89  if (!memcmp(filename, tmp->fileName, len) && tmp->line == line)
90  {
91  return counter;
92  }
93  }
94  tmp = tmp->next;
95  counter++;
96  }
97  return -1;
98 }

◆ 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);
150  if (!memoryInformation)
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
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 }

Variable Documentation

◆ atexitCalled

int atexitCalled = 0

Another global variable.

This one is used to know if we already call the atexit function.