-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmem.c
79 lines (54 loc) · 1.38 KB
/
mem.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
void *debug_malloc(size_t size, const char* file, int line) {
void *p = malloc(size);
if (p == NULL) {
return NULL;
}
char buff[256];
sprintf(buff, "%p.mem", p);
FILE *f = fopen(buff, "w");
fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line, size);
fclose(f);
return p;
}
void *debug_calloc(size_t count, size_t size, const char* file, int line) {
void *p = calloc(count, size);
if (p == NULL) {
return NULL;
}
char buff[256];
sprintf(buff, "%p.mem", p);
FILE *f = fopen(buff, "w");
fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line,
count * size);
fclose(f);
return p;
}
void *debug_realloc(void *ptr, size_t size, const char* file, int line) {
void *p = realloc(ptr, size);
if (p == NULL) {
return NULL;
}
char buff[256];
//Delete the old pointer record
sprintf(buff, "%p.mem", ptr);
if (unlink(buff) < 0) {
printf("Double free: %p File: %s Line: %d\n", ptr, file, line);
}
//Create the new pointer record
sprintf(buff, "%p.mem", p);
FILE *f = fopen(buff, "w");
fprintf(f, "File: %s\nLine: %d\nSize: %zu bytes\n", file, line, size);
fclose(f);
return p;
}
void debug_free(void *p, const char* file, int line) {
char buff[256];
sprintf(buff, "%p.mem", p);
if (unlink(buff) < 0) {
printf("Double free: %p File: %s Line: %d\n", p, file, line);
}
free(p);
}