-
Notifications
You must be signed in to change notification settings - Fork 23
/
cust_malloc.h
49 lines (38 loc) · 1.15 KB
/
cust_malloc.h
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
#ifndef __CUST_MALLOC
#define __CUST_MALLOC
typedef struct {
char file[128];
int line;
size_t size;
} malloc_info;
const int _offset = sizeof(malloc_info);
static int allocatedMemory = 0;
void* my_malloc(size_t size, const char* file, int line) {
void* mem = malloc(size + _offset);
if(mem) {
malloc_info* info = (malloc_info*)mem;
strcpy(info->file, file);
info->line = line;
info->size = size;
printf("Allocating %u bytes at 0x%x in %s:%d\n", size, mem, file, line);
allocatedMemory += size;
return (void*)((char*)mem + _offset);
} else {
printf("Allocation of %u bytes failed in %s:%d\n", size, file, line);
}
return NULL;
}
void my_free(void* ptr, const char* file, int line) {
malloc_info* info = (malloc_info*)((char*)ptr - _offset);
printf("Deallocating %u bytes at 0x%x (allocated at %s:%d) in %s:%d\n", info->size, info, info->file, info->line, file, line);
allocatedMemory -= info->size;
free(info);
}
#if MEM_DEBUG
#define cust_malloc(size) my_malloc(size, __FILE__, __LINE__)
#define cust_free(ptr) my_free(ptr, __FILE__, __LINE__)
#else
#define cust_malloc(size) malloc(size)
#define cust_free(ptr) free(ptr)
#endif
#endif