forked from feihengli/util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsal_malloc.c
117 lines (100 loc) · 2.74 KB
/
sal_malloc.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
#include "sal_malloc.h"
#include "sal_debug.h"
static unsigned int malloc_sum = 0;
typedef struct mem_s
{
void* this;
const char* func;
int line;
int size;
}mem_s;
static int mem_align(int bytes)
{
int ret = bytes;
if (bytes % 4 != 0)
{
ret = (bytes/4 + 1) * 4;
}
return ret;
}
static int mem_touch_file(mem_s* pMem)
{
int ret = -1;
char path[64];
ret = snprintf(path, sizeof(path), "/tmp/heap_%p_%s_%d_%d", pMem->this, pMem->func, pMem->line, pMem->size);
CHECK(ret > 0, -1, "error with %#x: %s\n", ret, strerror(errno));
int fd = creat(path, S_IWRITE|S_IREAD|S_IEXEC);
CHECK(fd > 0, -1, "error with %#x: %s\n", ret, strerror(errno));
close(fd);
return 0;
}
static int mem_rm_file(mem_s* pMem)
{
int ret = -1;
char path[64];
ret = snprintf(path, sizeof(path), "/tmp/heap_%p_%s_%d_%d", pMem->this, pMem->func, pMem->line, pMem->size);
CHECK(ret > 0, -1, "error with %#x: %s\n", ret, strerror(errno));
ret = remove(path);
CHECK(ret == 0, -1, "error with %#x: %s\n", ret, strerror(errno));
return 0;
}
void* mem_malloc1(int bytes, const char* func, int line)
{
if (bytes >= 0)
{
bytes += sizeof(mem_s);
int AlignSize = mem_align(bytes);
mem_s* ret = (mem_s*)malloc(AlignSize);
if (NULL != ret)
{
ret->this = ret;
ret->func = func;
ret->line = line;
ret->size = AlignSize;
mem_touch_file(ret);
malloc_sum += ret->size;
ret += 1;
//DBG("malloc %d ok, total malloc %d\n", AlignSize, malloc_sum);
return (void*)ret;
}
}
return NULL;
}
void* mem_realloc1(void* original, int new_bytes, const char* func, int line)
{
if (original && new_bytes > 0)
{
mem_s* tmp = (mem_s*)original;
tmp -= 1;
if (new_bytes == tmp->size)
{
DBG("original size[%d] is equal new_bytes[%d]\n", tmp->size, new_bytes);
return original;
}
void* new_addr = mem_malloc1(new_bytes, func, line);
if (new_addr)
{
int need_copy = (new_bytes > tmp->size) ? tmp->size : new_bytes;
memcpy(new_addr, original, need_copy);
mem_free1(original);
return new_addr;
}
}
return NULL;
}
void mem_free1(void* ptr)
{
if (ptr)
{
mem_s* tmp = (mem_s*)ptr;
tmp -= 1;
mem_rm_file(tmp);
malloc_sum -= tmp->size;
//DBG("free %d ok, total malloc %d\n", *tmp, malloc_sum);
free(tmp);
}
}
int mem_get_malloc_sum()
{
return malloc_sum;
}