-
Notifications
You must be signed in to change notification settings - Fork 0
/
memsafe_boii.c
524 lines (470 loc) · 13.4 KB
/
memsafe_boii.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "memsafe_boii.h"
/**
* This is the allocation record, which will be stored
* in a linked list of allocation records.
*/
typedef struct memsafe__boii__allocation
{
void* mem_address;
size_t mem_size;
char* func_call_file;
size_t func_call_line;
char* func_name;
struct memsafe__boii__allocation* next;
} memsafe__boii__allocation;
/**
* This is the linked list for the allocation records.
*/
typedef struct memsafe__boii__allocation_list
{
memsafe__boii__allocation* head;
memsafe__boii__allocation* tail;
} memsafe__boii__allocation_list;
/**
* Creates/allocates memory for a new allocation record.
*
* Returns `NULL` if unable to create record.
*/
static memsafe__boii__allocation*
memsafe__boii__allocation_new(void* mem_address,
size_t mem_size,
const char* func_call_file,
size_t func_call_line,
const char* func_name);
/**
* Frees/deallocates memory for the allocation record.
*/
static void memsafe__boii__allocation_free(memsafe__boii__allocation* record);
/**
* Creates/allocates memory for a new allocation record list.
*
* Returns `NULL` if unable to create new allocation record list.
*/
static memsafe__boii__allocation_list* memsafe__boii__allocation_list_new();
/**
* Adds the new allocation record to the record list.
*/
static void
memsafe__boii__allocation_list_add_record(memsafe__boii__allocation_list* list,
memsafe__boii__allocation* record);
/**
* Removes the allocation record for the address pointed by the entity.
*/
static void memsafe__boii__allocation_list_remove_record(
memsafe__boii__allocation_list* list, void* entity);
/**
* Frees/deallocates memory for allocation record list.
*/
static void
memsafe__boii__allocation_list_free(memsafe__boii__allocation_list* list);
/**
* Actual variable which holds the records.
*/
static memsafe__boii__allocation_list memory_allocations_record_list =
(memsafe__boii__allocation_list){.head = NULL, .tail = NULL};
static memsafe__boii__allocation*
memsafe__boii__allocation_new(void* mem_address,
size_t mem_size,
const char* func_call_file,
size_t func_call_line,
const char* func_name)
{
if(!mem_address)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Tyring to allocate new reacord with NULL memory "
"address!\n");
#endif
return NULL;
}
if(mem_size < 0)
{
// some idiot is allocating negative memory
return NULL;
}
memsafe__boii__allocation* record =
(memsafe__boii__allocation*)calloc(1, sizeof(memsafe__boii__allocation));
if(!record)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Unable to allocate memory for record!\n");
#endif
return NULL;
}
record->mem_address = mem_address;
record->mem_size = mem_size;
record->func_call_file =
(char*)calloc(strlen(func_call_file) + 1, sizeof(char));
strcpy(record->func_call_file, func_call_file);
record->func_call_line = func_call_line;
record->func_name = (char*)calloc(strlen(func_name) + 1, sizeof(char));
strcpy(record->func_name, func_name);
return record;
}
static void memsafe__boii__allocation_free(memsafe__boii__allocation* record)
{
if(!record)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Trying to free record pointing to NULL!\n");
#endif
return;
}
// freeing function name, filename strings before
// as these strings are dynamically allocated.
free(record->func_call_file);
free(record->func_name);
free(record);
}
static memsafe__boii__allocation_list* memsafe__boii__allocation_list_new()
{
memsafe__boii__allocation_list* list =
(memsafe__boii__allocation_list*)calloc(
1, sizeof(memsafe__boii__allocation_list));
if(!list)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Unable to allocate memory for new allocations "
"record list!\n");
#endif
return NULL;
}
list->head = NULL;
list->tail = NULL;
return list;
}
static void
memsafe__boii__allocation_list_add_record(memsafe__boii__allocation_list* list,
memsafe__boii__allocation* record)
{
if(!list)
{
#ifdef DEBUG
printf(
"MemsafeBoii__Log: Trying to add record to a NULL pointing allocations "
"record list!\n");
#endif
return;
}
if(!record)
{
#ifdef DEBUG
printf(
"MemsafeBoii__Log: Trying to add a NULL pointing record to allocations "
"record list!\n");
#endif
return;
}
if(list->head == NULL)
{
list->head = record;
list->tail = record;
return;
}
list->tail->next = record;
list->tail = record;
}
static void memsafe__boii__allocation_list_remove_record(
memsafe__boii__allocation_list* list, void* entity)
{
if(list == NULL)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Trying to remove record from a NULL pointing "
"allocations record "
"list!\n");
#endif
return;
}
if(entity == NULL)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Trying to remove a record of NULL pointing "
"entity!\n");
#endif
return;
}
if((*list).head == NULL)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Trying to remove a record of an entity from an "
"empty allocations "
"record list!\n");
#endif
return;
}
// finding the entity's allocation record in the list
memsafe__boii__allocation* entity_record_allocation_node = (*list).head;
while(entity_record_allocation_node != NULL)
{
if(entity_record_allocation_node->mem_address == entity)
{
break;
}
entity_record_allocation_node = entity_record_allocation_node->next;
}
// freeing the entity memory
free(entity);
if((*list).head == (*list).tail)
{
// only one record in the list
memsafe__boii__allocation_free((*list).head);
(*list).head = NULL;
(*list).tail = NULL;
return;
}
// removing entity's allocation record from list.
// deleting node in a linked list, using a pointer to the node
if(entity_record_allocation_node == (*list).tail)
{
// we need to traverse the list from head to delete the tail node
memsafe__boii__allocation* temp = (*list).head;
while(temp->next != entity_record_allocation_node)
{
temp = temp->next;
}
temp->next = NULL;
free(entity_record_allocation_node);
}
else
{
// we could just copy values of next node into current node
// then free memory of next node
memsafe__boii__allocation* next_record =
entity_record_allocation_node->next;
entity_record_allocation_node->mem_address = next_record->mem_address;
entity_record_allocation_node->mem_size = next_record->mem_size;
entity_record_allocation_node->func_call_line = next_record->func_call_line;
entity_record_allocation_node->next = next_record->next;
// we need to free function name, filename strings in entity's record
// as they are dynamically allocated, if we just replace the pointer with
// next record's pointer, we would leave the memory un-freed
free(entity_record_allocation_node->func_name);
free(entity_record_allocation_node->func_call_file);
entity_record_allocation_node->func_name = next_record->func_name;
entity_record_allocation_node->func_call_file = next_record->func_call_file;
// freeing memory of next record
free(next_record);
}
}
static void
memsafe__boii__allocation_list_free(memsafe__boii__allocation_list* list)
{
if(list == NULL)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Trying to free a NULL pointing record list!\n");
#endif
return;
}
if((*list).head == NULL)
{
// no records in the list
// free(list);
return;
}
if((*list).head == (*list).tail)
{
// only one record in the list
memsafe__boii__allocation_free((*list).head);
(*list).head = NULL;
(*list).tail = NULL;
// free(list);
return;
}
// freeing each record by traversing the list
memsafe__boii__allocation* current_record = (*list).head->next;
memsafe__boii__allocation* past_record = (*list).head;
while(current_record != NULL)
{
memsafe__boii__allocation_free(past_record);
past_record = current_record;
current_record = current_record->next;
}
memsafe__boii__allocation_free(past_record);
// finally freeing list, after all records are freed
// No need to free the list, as it is not dynamically allocated
// it is a static variable defined in memsafe_boii.h
// free(list);
}
void* memsafe__boii__allocate(size_t mem_size,
const char* func_call_file,
size_t func_call_line,
const char* func_name)
{
void* mem_ptr = calloc(1, mem_size);
if(!mem_ptr)
{
// out of memory
#ifdef DEBUG
printf("MemsafeBoii__Log: Unable to allocate memory of size: %zu\n",
mem_size);
#endif
return NULL;
}
if(mem_size < 0)
{
// some idiot is allocating negative memory
return NULL;
}
#ifdef DEBUG
printf("MemsafeBoii__Log: Allocation:\n memory_size: %zu\n function_name: "
"%s\n line_number: "
"%zu\n file: %s\n",
mem_size,
func_name,
func_call_line,
func_call_file);
#endif
// Record allocation
memsafe__boii__allocation* record = memsafe__boii__allocation_new(
mem_ptr, mem_size, func_call_file, func_call_line, func_name);
if(!record)
{
// unable to allocate memory for record.
free(mem_ptr);
return NULL;
}
memsafe__boii__allocation_list_add_record(&memory_allocations_record_list,
record);
return mem_ptr;
}
void* memsafe__boii__reallocate(void* entity,
size_t new_mem_size,
const char* func_call_file,
size_t func_call_line,
const char* func_name)
{
if(!entity)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Attempt to reallocate memory for a NULL pointed "
"memory\n");
#endif
return NULL;
}
if(memory_allocations_record_list.head == NULL)
{
// no records
return NULL;
}
if(new_mem_size < 0)
{
// some idiot is allocating negative memory
//
// Cleanup of the old memory is left
// to be done by cleanup function
// as it would be costly to iterate the
// record list every time some idiot does this
//
// Cleanup function iterate record list only once
return NULL;
}
// searching for the allocation record
memsafe__boii__allocation* record = memory_allocations_record_list.head;
while(record)
{
if(record->mem_address == entity)
{
break;
}
record = record->next;
}
if(!record)
{
// allocation record not founc
return NULL;
}
entity = realloc(entity, new_mem_size);
if(!entity)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Unable to reallocate memory with size: %zu\n",
new_mem_size);
#endif
return NULL;
}
#ifdef DEBUG
printf(
"MemsafeBoii__Log: Re-allocation:\n memory_size: %zu\n function_name: "
"%s\n line_number: "
"%zu\n file: %s\n",
new_mem_size,
func_name,
func_call_line,
func_call_file);
#endif
// Update allocation record
record->mem_address = entity;
record->mem_size = new_mem_size;
record->func_name =
(char*)realloc(record->func_name, sizeof(char) * (strlen(func_name) + 1));
strcpy(record->func_name, func_name);
record->func_call_line = func_call_line;
record->func_call_file = (char*)realloc(
record->func_call_file, sizeof(char) * (strlen(func_call_file) + 1));
strcpy(record->func_call_file, func_call_file);
return entity;
}
void memsafe__boii__deallocate(void* entity,
const char* func_call_file,
size_t func_call_line,
const char* func_name)
{
if(entity == NULL)
{
#ifdef DEBUG
printf("MemsafeBoii__Log: Trying to free NULL pointed memory:\n address: "
"%p\n "
"function_name: %s\n line_number: %zu\n file: %s\n",
entity,
func_name,
func_call_line,
func_call_file);
#endif
return;
}
#ifdef DEBUG
printf(
"MemsafeBoii__Log: Deallocation:\n address: %p\n function_name: %s\n "
"line_number: "
"%zu\n file: %s\n",
entity,
func_name,
func_call_line,
func_call_file);
#endif
// Remove allocation
memsafe__boii__allocation_list_remove_record(&memory_allocations_record_list,
entity);
}
void memsafe__boii__clean(const char* func_call_file,
size_t func_call_line,
const char* func_name)
{
memsafe__boii__allocation* temp = memory_allocations_record_list.head;
size_t cleanup_count = 0;
while(temp != NULL)
{
cleanup_count++;
#ifdef DEBUG
printf(
"MemsafeBoii__Log: Cleanup (deallocating un-freed memory):\n address: "
"%p\n memory_size: %zu\n function_name: %s\n line_number: "
"%zu\n file: %s\n",
temp->mem_address,
temp->mem_size,
func_name,
func_call_line,
func_call_file);
#endif
free(temp->mem_address);
temp->mem_address = NULL;
temp = temp->next;
}
memsafe__boii__allocation_list_free(&memory_allocations_record_list);
printf("\n * MemsafeBoii * Total cleanups done: %zu\n", cleanup_count);
}