-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleakhelper.c
executable file
·56 lines (48 loc) · 1.31 KB
/
leakhelper.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
#include <stdlib.h>
typedef struct _ptr_t{
void* ptr;
} ptr_t;
static ptr_t* listing;
static int listing_ptrcount;
int init_listing(int ptrcount){
void* ptr = calloc(ptrcount, sizeof(ptr_t));
if (!ptr) return 1;
listing_ptrcount = ptrcount;
listing = ptr;
}
void* lcalloc(int size){
void* ptr = calloc(size, 1);
int free_element = 0;
while (free_element < listing_ptrcount && !listing[free_element].ptr) ++free_element;
listing[free_element].ptr = ptr;
return ptr;
}
void* lmalloc(int size){
void* ptr = malloc(size);
int free_element = 0;
while (free_element < listing_ptrcount && listing[free_element].ptr) ++free_element;
listing[free_element].ptr = ptr;
return ptr;
}
void* lrealloc(void* ptr, size_t new_size){
void* new = realloc(ptr, new_size);
if (!new) return new;
int index = 0;
while (free_element < listing_ptrcount && listing[index].ptr != ptr) ++index;
listing[index].ptr = new;
return new;
}
void lfree(void* ptr){
int offset = 0;
while (listing[offset].ptr != ptr && offset < listing_ptrcount) offset++;
listing[offset].ptr = 0;
free(ptr);
}
void free_all(){
for (int i = 0; i < listing_ptrcount; i++)
if (listing[i].ptr){
free(listing[i].ptr);
listing[i].ptr = 0;
}
free(listing);
}