-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmalloc.c
82 lines (68 loc) · 1.95 KB
/
xmalloc.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
#include <stdlib.h>
#include <stdio.h>
#include "xmalloc.h"
#define MAX_MALLOC_NOT_FREE 255
struct xmalloc_s
{
char *fileName;
int line;
void *ptr;
size_t size;
} gMallocs[MAX_MALLOC_NOT_FREE] = {0};
void *__xmalloc(size_t size, char *file, int line)
{
int i;
void *mem = malloc(size);
if (mem == 0)
fprintf(stderr, "(xmalloc) Error : malloc error in file %s at line %d.\n", file, line);
for (i = 0; i < MAX_MALLOC_NOT_FREE; i++)
{
if (gMallocs[i].ptr == 0)
{
gMallocs[i].fileName = file;
gMallocs[i].line = line;
gMallocs[i].ptr = mem;
gMallocs[i].size = size;
break;
}
}
if (i == MAX_MALLOC_NOT_FREE)
fprintf(stderr, "(xmalloc) Warning : xmalloc is full.\n");
// else
// fprintf(stderr, "(xmalloc) Info: new malloc [%d][0x%p][%d][%s][%d]\n",
// i, gMallocs[i].ptr, gMallocs[i].size,
// gMallocs[i].fileName, gMallocs[i].line);
return mem;
}
void __xfree(void *mem, char *file, int line)
{
int i;
for (i = 0; i < MAX_MALLOC_NOT_FREE; i++)
{
if (gMallocs[i].ptr == mem)
{
gMallocs[i].ptr = 0;
break;
}
}
if (i == MAX_MALLOC_NOT_FREE)
fprintf(stderr, "(xmalloc) Error : free - can't find 0x%p in file %s at line %d.\n", mem, file, line);
// else
// fprintf(stderr, "(xmalloc) Info : free of ptr 0x%p [%d] in file %s at line %d.\n", mem, i, file, line);
free(mem);
}
void xmalloc_status()
{
int i;
fprintf(stderr, "(xmalloc) Status of current malloc:\n");
for (i = 0; i < MAX_MALLOC_NOT_FREE; i++)
{
if (gMallocs[i].ptr != 0)
{
fprintf(stderr, "[%d][0x%p][%d][%s][%d]\n",
i, gMallocs[i].ptr, gMallocs[i].size,
gMallocs[i].fileName, gMallocs[i].line);
}
}
fprintf(stderr, "(xmalloc) End of status.\n");
}