-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdumb_alloc.c
163 lines (132 loc) · 3.15 KB
/
dumb_alloc.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "dumb_alloc.h"
#include <stdint.h>
#include <string.h>
#include "align.h"
typedef struct {
size_t nmem;
} Preface;
typedef struct {
size_t m;
} Header;
static const size_t UNUSED_MASK = 1;
static
size_t
h_size_if_unused(Header h)
{
return (h.m & UNUSED_MASK) ? (h.m & ~UNUSED_MASK) : 0;
}
static
size_t
h_size(Header h)
{
return h.m & ~UNUSED_MASK;
}
static
void
h_mark_used(Header *h)
{
h->m &= ~UNUSED_MASK;
}
static
void
h_mark_unused(Header *h)
{
h->m |= UNUSED_MASK;
}
static
Header
h_create_used(size_t sz)
{
return (Header) {.m = sz};
}
bool
dumb_alloc_init(DumbAllocData d)
{
const size_t initnmem = ALIGN_TO_DUMB(sizeof(Preface));
if (!d.setnmem(d.userdata, 0, initnmem))
return false;
Preface p = {.nmem = initnmem};
memcpy(d.mem, &p, sizeof(p));
return true;
}
void *
dumb_malloc(DumbAllocData d, size_t n)
{
static const size_t HALF_ADDR_SPACE = (SIZE_MAX - ALIGN_TO_DUMB(sizeof(Header))) / 2;
if (!n || n > HALF_ADDR_SPACE)
return NULL;
n = ALIGN_TO_DUMB(n);
Preface p;
memcpy(&p, d.mem, sizeof(p));
char *last = ((char *) d.mem) + p.nmem;
char *cur = ((char *) d.mem) + ALIGN_TO_DUMB(sizeof(Preface));
while (cur != last) {
Header h;
memcpy(&h, cur, sizeof(h));
const size_t unused_sz = h_size_if_unused(h);
if (unused_sz >= n) {
h_mark_used(&h);
memcpy(cur, &h, sizeof(h));
return cur + ALIGN_TO_DUMB(sizeof(Header));
}
cur += ALIGN_TO_DUMB(sizeof(Header));
cur += h_size(h);
}
const size_t newnmem = p.nmem + ALIGN_TO_DUMB(sizeof(Header)) + n;
if (newnmem > HALF_ADDR_SPACE)
return NULL;
if (!d.setnmem(d.userdata, p.nmem, newnmem))
return NULL;
p.nmem = newnmem;
memcpy(d.mem, &p, sizeof(p));
Header h = h_create_used(n);
memcpy(last, &h, sizeof(h));
return last + ALIGN_TO_DUMB(sizeof(Header));
}
void
dumb_free(DumbAllocData d, void *block)
{
if (!block)
return;
void *hdr_addr = ((char *) block) - ALIGN_TO_DUMB(sizeof(Header));
Header h;
memcpy(&h, hdr_addr, sizeof(h));
const size_t block_size = h_size(h);
Preface p;
memcpy(&p, d.mem, sizeof(p));
if (
((char *) d.mem) + p.nmem ==
((char *) block) + block_size)
{
const size_t newnmem = p.nmem - block_size - ALIGN_TO_DUMB(sizeof(Header));
if (d.setnmem(d.userdata, p.nmem, newnmem)) {
p.nmem = newnmem;
memcpy(d.mem, &p, sizeof(p));
}
} else {
h_mark_unused(&h);
memcpy(hdr_addr, &h, sizeof(h));
}
}
void *
dumb_realloc(DumbAllocData d, void *block, size_t n)
{
if (!block)
return dumb_malloc(d, n);
if (!n) {
dumb_free(d, block);
return NULL;
}
void *hdr_addr = ((char *) block) - ALIGN_TO_DUMB(sizeof(Header));
Header h;
memcpy(&h, hdr_addr, sizeof(h));
const size_t block_size = h_size(h);
if (block_size >= n)
return block;
void *r = dumb_malloc(d, n);
if (!r)
return NULL;
memcpy(r, block, block_size);
dumb_free(d, block);
return r;
}