-
Notifications
You must be signed in to change notification settings - Fork 0
/
smalloc.c
85 lines (73 loc) · 2.09 KB
/
smalloc.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
#include "smalloc.h"
/* memory alloc */
/* |prefx(size_t)| ********memory *******| padding for sizeof(long), we pretend it has| */
static size_t used_bytes;
static void incr_used_bytes(size_t size){
/* for memory align */
if(size & (sizeof(long)-1)) {
size += sizeof(long) - (size & (sizeof(long) -1));
}
atomic_add(&used_bytes, size);
}
static void decr_used_bytes(size_t size) {
/* for memory align */
if(size & (sizeof(long)-1)) {
size += sizeof(long) - (size & (sizeof(long) -1));
}
atomic_sub(&used_bytes, size);
}
size_t smalloc_size(void *p) {
size_t size = *((size_t *)((char*)p - MEMORY_PREFIX_SIZE));
if(size & (sizeof(long)-1)) {
size += sizeof(long) - (size & (sizeof(long) -1));
}
return size;
}
static void smalloc_oom(size_t size) {
fprintf(stderr, "Out of memory, alloc %d bytes error\n", size);
fflush(stderr);
abort();
}
void *smalloc(size_t size) {
AGENT_SMALLOC(size);
void *p = malloc(MEMORY_PREFIX_SIZE + size);
if (!p) {
smalloc_oom(size);
}
*((size_t *)p) = size;
incr_used_bytes(MEMORY_PREFIX_SIZE + size);
return (char *)p + MEMORY_PREFIX_SIZE;
}
void sfree(void *p) {
AGENT_SFREE();
if (p == NULL) return;
void *r = (char *)p - MEMORY_PREFIX_SIZE;
size_t size = *(size_t *)r;
decr_used_bytes(size + MEMORY_PREFIX_SIZE);
free(r);
}
void *srealloc(void *p, size_t size) {
if (p == NULL) return smalloc(size);
void *r = (char *)p - MEMORY_PREFIX_SIZE;
size_t old_size = *(size_t *)r;
void *n = realloc(r, size + MEMORY_PREFIX_SIZE);
if (!n) {
smalloc_oom(size);
}
*((size_t *)n) = size;
decr_used_bytes(old_size + MEMORY_PREFIX_SIZE);
incr_used_bytes(size + MEMORY_PREFIX_SIZE);
return (char *)n + MEMORY_PREFIX_SIZE;
}
char *sstrdup(const char *str) {
if(str == NULL) return NULL;
size_t size = strlen(str) + 1;
char *n = smalloc(size);
memcpy(n, str, size);
return n;
}
size_t dump_used_bytes() {
size_t res;
res = atomic_get(&used_bytes);
return res;
}