-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
malloc_dbg.c
289 lines (260 loc) · 8.48 KB
/
malloc_dbg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* @file
* @brief This file contains malloc_dbg, calloc_dbg, free_dbg and printLeaks implementations.
* @author [tinouduart33](https://github.com/tinouduart33)
*/
#include <stdlib.h> /// For the malloc, calloc and free functions.
#include <stdio.h> /// For IO operations (printf).
#include <string.h> /// For the memcmp function.
#include "malloc_dbg.h" /// Header file which contains the prototypes of malloc_dbg, calloc_dbg and fre_dbg.
/* We must undef these macros in order to use the real malloc / calloc and free functions */
#undef malloc
#undef calloc
#undef free
/**
* @brief Structure used to save an allocated pointer
*/
typedef struct MEMORY_INFORMATION
{
void* ptr; ///< Pointer returned by malloc / calloc
const char* fileName; ///< File in which malloc or calloc has been called
const char* functionName; ///< Function in which malloc or calloc has been called
size_t bytes; ///< Number of bytes allocated
int line; ///< Line number (in file) corresponding to the malloc / calloc call
struct MEMORY_INFORMATION* next; ///< Next element in the list
struct MEMORY_INFORMATION* previous; ///< Previous element in the list
} mem_info;
/** We use a global variable for the list so we can use it at any time.
* */
mem_info* memoryInformation = NULL;
/** Another global variable. This one is used to know if we already call the atexit function.
* */
int atexitCalled = 0;
/**
* @brief addMemInfo function add a memory allocation in the memoryInfo list.
* @details This function creates a new element and add it on top of the list
* @param memoryInfo Pointer to the doubly linked list used to store all of the allocations
* @param ptrToreturn Pointer returned by malloc or calloc
* @param bytes Size in bytes of the allocation
* @param line Line where the allocation has been called
* @param filename File where the allocation has been called
* @param functionName Name of the function where the allocation has been called
* @returns mem_info pointer if it succeeds, NULL otherwise
*/
mem_info* addMemInfo(mem_info* memoryInfo, void* ptrToReturn, size_t bytes, int line, const char* filename, const char* functionName)
{
mem_info* newMemInfo = (mem_info*)malloc(sizeof(mem_info));
if (!newMemInfo)
{
return NULL;
}
newMemInfo->ptr = ptrToReturn;
newMemInfo->bytes = bytes;
newMemInfo->line = line;
newMemInfo->fileName = filename;
newMemInfo->functionName = functionName;
newMemInfo->next = memoryInfo;
newMemInfo->previous = NULL;
if (memoryInformation)
memoryInformation->previous = newMemInfo;
return newMemInfo;
}
/**
* @brief inList function is used to know if an element is already in the memoryInfo list.
* @details This function is used to know if an allocation in a specific file at a specific line already exists in the list.
* @param filename File in which malloc or calloc has been called
* @param line Line 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.
*/
int inList(const char* filename, int line)
{
mem_info* tmp = memoryInformation;
int counter = 0;
int len = strlen(filename);
while (tmp)
{
if (len == strlen(tmp->fileName))
{
if (!memcmp(filename, tmp->fileName, len) && tmp->line == line)
{
return counter;
}
}
tmp = tmp->next;
counter++;
}
return -1;
}
/**
* @brief editInfo function is used to edit an element in the memoryInfo list.
* @details This function is used to edit the number of bytes allocated at a specific line.
* @param elemPos Position of an element in the doubly linked list memoryInfo
* @param bytes Size of the allocation in bytes
* @returns Nothing.
*/
void editInfo(int elemPos, size_t bytes)
{
int counter = 0;
mem_info* tmp = memoryInformation;
while (counter != elemPos)
{
tmp = tmp->next;
counter++;
}
tmp->bytes += bytes;
}
/**
* @brief malloc_dbg function is a wrapper around the malloc function.
* @details 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.
* @param bytes Size of the allocation in bytes
* @param filename Caller file
* @param functionName Caller function
* @returns Pointer returned by malloc if it's valid, NULL otherwhise.
*/
void* malloc_dbg(size_t bytes, int line, const char* filename, const char* functionName)
{
void* ptrToReturn = malloc(bytes);
int pos = 0;
if (!ptrToReturn)
{
return NULL;
}
// We must check atexitCalled value to know if we already called the function
if (!atexitCalled)
{
atexit(printLeaks); // Used to call printLeaks when the program exit
atexitCalled = 1;
}
pos = inList(filename, line);
if (pos == -1)
{
// Add a new element in the mem_info list
memoryInformation = addMemInfo(memoryInformation, ptrToReturn, bytes, line, filename, functionName);
if (!memoryInformation)
{
free(ptrToReturn);
return NULL;
}
}
else
{
editInfo(pos, bytes);
}
return ptrToReturn;
}
/**
* @brief calloc_dbg function is a wrapper around the calloc function.
* @details 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.
* @param elementCount number of element to allocate
* @param elementSize Size of each element
* @param line Line number in the caller file
* @param filename Caller file
* @param functionName Caller function
* @returns Pointer returned by calloc if it's valid, NULL otherwhise.
*/
void* calloc_dbg(size_t elementCount, size_t elementSize, int line, const char* filename, const char* functionName)
{
void* ptrToReturn = calloc(elementCount, elementSize);
if (!ptrToReturn)
{
return NULL;
}
// We must check atexitCalled value to know if we already called the function
if (!atexitCalled)
{
atexit(printLeaks); // Used to call printLeaks when the program exit
atexitCalled = 1;
}
// Add a new element in the mem_info list
memoryInformation = addMemInfo(memoryInformation, ptrToReturn, elementCount * elementSize, line, filename, functionName);
if (!memoryInformation)
{
free(ptrToReturn);
return NULL;
}
return ptrToReturn;
}
/**
* @brief free_dbg function is used to free the memory allocated to a pointer.
* @details 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.
* @param ptrToFree Pointer that must be freed
* @returns Nothing.
*/
void free_dbg(void* ptrToFree)
{
mem_info* tmp = memoryInformation;
mem_info* toFree = NULL;
mem_info* previous = NULL;
// Check if the head contains the pointer to free
if (tmp->ptr == ptrToFree)
{
toFree = tmp;
memoryInformation = tmp->next;
free(toFree->ptr);
free(toFree);
if (memoryInformation)
{
memoryInformation->previous = NULL;
}
return;
}
// We can loop through the list without any problems, the head is not the pointer
while (tmp)
{
if (tmp->ptr == ptrToFree) // If we found the pointer that must be freed
{
toFree = tmp;
tmp = tmp->next;
previous = toFree->previous;
if (previous)
{
previous->next = tmp;
}
if (tmp)
{
tmp->previous = previous;
}
free(toFree->ptr);
if (toFree == memoryInformation)
{
memoryInformation = NULL;
}
free(toFree);
return;
}
tmp = tmp->next;
}
}
/**
* @brief printLeaks function is used to print all the memory leaks.
* @details 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.
*/
void printLeaks()
{
mem_info* tmp = memoryInformation;
mem_info* previous = NULL;
size_t sum = 0;
int nbBlocks = 0;
if (tmp)
{
printf("Memory Leaks detected.\n");
}
while (tmp)
{
previous = tmp;
printf("\n%ld bytes lost\n", tmp->bytes);
printf("address : 0x%p in %s\t%s:%d\n", tmp->ptr, tmp->functionName, tmp->fileName, tmp->line);
printf("\n====================================\n");
sum += tmp->bytes;
tmp = tmp->next;
free(previous);
nbBlocks++;
}
printf("SUMMARY :\n%ld bytes lost in %d blocks\n", sum, nbBlocks);
}