-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjmalloc2complex.c
303 lines (285 loc) · 6.37 KB
/
jmalloc2complex.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <sys/mman.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
struct jmalloc_block {
union {
char block[0];
struct jmalloc_block *next;
} u;
};
size_t arenaremain;
char *arena;
// 16, 32, 64, 128, 256, 512, 1024, 2048
struct jmalloc_block *blocks[8] = {};
static inline size_t topages(size_t limit)
{
long pagesz = sysconf(_SC_PAGE_SIZE);
size_t pages, actlimit;
if (pagesz <= 0)
{
abort();
}
pages = (limit + (pagesz-1)) / pagesz;
actlimit = pages * pagesz;
return actlimit;
}
const uint8_t lookup[129] = {
0,0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
};
__attribute__((noinline)) void *jmalloc1(size_t sz)
{
struct jmalloc_block **ls = NULL;
struct jmalloc_block *blk;
void *ret;
uint8_t lookupval;
if (unlikely(sz > 2048))
{
ret = mmap(NULL, topages(sz), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if (unlikely(ret == MAP_FAILED))
{
return NULL;
}
return ret;
}
lookupval = lookup[(sz+15)/16];
if (lookupval == 0 && sizeof(struct jmalloc_block) > 16)
{
lookupval = 1;
}
if (lookupval == 1 && sizeof(struct jmalloc_block) > 32)
{
lookupval = 2;
}
if (lookupval == 2 && sizeof(struct jmalloc_block) > 64)
{
lookupval = 3;
}
if (lookupval == 3 && sizeof(struct jmalloc_block) > 128)
{
lookupval = 4;
}
if (lookupval == 4 && sizeof(struct jmalloc_block) > 256)
{
lookupval = 5;
}
if (lookupval == 5 && sizeof(struct jmalloc_block) > 512)
{
lookupval = 6;
}
if (lookupval == 6 && sizeof(struct jmalloc_block) > 1024)
{
lookupval = 7;
}
if (sizeof(struct jmalloc_block) > 2048)
{
abort();
}
ls = &blocks[lookupval];
sz = 1<<(4+lookupval);
if (unlikely(!*ls))
{
if (unlikely(arenaremain < sz))
{
arenaremain = 32*1024*1024;
arena = mmap(NULL, arenaremain, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if (unlikely(arena == MAP_FAILED || arena == NULL))
{
abort();
}
}
if (unlikely(arenaremain < sz))
{
abort();
}
ret = arena;
arenaremain -= sz;
arena += sz;
return ret;
}
blk = *ls;
*ls = blk->u.next;
return blk;
}
__attribute__((noinline)) void *jmalloc2(size_t sz, size_t *actual_sz)
{
struct jmalloc_block **ls = NULL;
struct jmalloc_block *blk;
void *ret;
uint8_t lookupval;
if (unlikely(sz > 2048))
{
ret = mmap(NULL, topages(sz), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if (unlikely(ret == MAP_FAILED))
{
return NULL;
}
return ret;
}
lookupval = lookup[(sz+15)/16];
if (lookupval == 0 && sizeof(struct jmalloc_block) > 16)
{
lookupval = 1;
}
if (lookupval == 1 && sizeof(struct jmalloc_block) > 32)
{
lookupval = 2;
}
if (lookupval == 2 && sizeof(struct jmalloc_block) > 64)
{
lookupval = 3;
}
if (lookupval == 3 && sizeof(struct jmalloc_block) > 128)
{
lookupval = 4;
}
if (lookupval == 4 && sizeof(struct jmalloc_block) > 256)
{
lookupval = 5;
}
if (lookupval == 5 && sizeof(struct jmalloc_block) > 512)
{
lookupval = 6;
}
if (lookupval == 6 && sizeof(struct jmalloc_block) > 1024)
{
lookupval = 7;
}
if (sizeof(struct jmalloc_block) > 2048)
{
abort();
}
ls = &blocks[lookupval];
sz = 1<<(4+lookupval);
if (unlikely(!*ls))
{
if (unlikely(arenaremain < sz))
{
arenaremain = 32*1024*1024;
arena = mmap(NULL, arenaremain, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if (unlikely(arena == MAP_FAILED || arena == NULL))
{
abort();
}
}
if (unlikely(arenaremain < sz))
{
abort();
}
ret = arena;
arenaremain -= sz;
arena += sz;
*actual_sz = sz;
return ret;
}
blk = *ls;
*ls = blk->u.next;
*actual_sz = sz;
return blk;
}
__attribute__((noinline)) void jmfree(void *ptr, size_t sz)
{
struct jmalloc_block **ls = NULL;
struct jmalloc_block *blk = ptr;
uint8_t lookupval;
if (unlikely(sz > 2048))
{
munmap(ptr, topages(sz));
return;
}
lookupval = lookup[(sz+15)/16];
if (lookupval == 0 && sizeof(struct jmalloc_block) > 16)
{
lookupval = 1;
}
if (lookupval == 1 && sizeof(struct jmalloc_block) > 32)
{
lookupval = 2;
}
if (lookupval == 2 && sizeof(struct jmalloc_block) > 64)
{
lookupval = 3;
}
if (lookupval == 3 && sizeof(struct jmalloc_block) > 128)
{
lookupval = 4;
}
if (lookupval == 4 && sizeof(struct jmalloc_block) > 256)
{
lookupval = 5;
}
if (lookupval == 5 && sizeof(struct jmalloc_block) > 512)
{
lookupval = 6;
}
if (lookupval == 6 && sizeof(struct jmalloc_block) > 1024)
{
lookupval = 7;
}
if (sizeof(struct jmalloc_block) > 2048)
{
abort();
}
ls = &blocks[lookupval];
blk->u.next = *ls;
*ls = blk;
}
static inline void *jmalloc(size_t sz, size_t *actual_sz)
{
if (actual_sz)
{
return jmalloc2(sz, actual_sz);
}
return jmalloc1(sz);
}
static unsigned int g_seed;
static inline void fast_srand(int seed) {
g_seed = seed;
}
static inline int fast_rand_mod2_10(void) {
g_seed = (214013*g_seed+2531011);
return (g_seed>>22);
}
struct block {
void *ptr;
size_t sz;
};
struct block blks[1024] = {};
int main(int argc, char **argv)
{
size_t i, j;
for (i = 0; i < 100*1000*1000; i++)
{
size_t idx[10];
size_t sz;
idx[0] = fast_rand_mod2_10();
idx[1] = fast_rand_mod2_10();
idx[2] = fast_rand_mod2_10();
idx[3] = fast_rand_mod2_10();
idx[4] = fast_rand_mod2_10();
idx[5] = fast_rand_mod2_10();
idx[6] = fast_rand_mod2_10();
idx[7] = fast_rand_mod2_10();
idx[8] = fast_rand_mod2_10();
idx[9] = fast_rand_mod2_10();
sz = 1<<(4 + (fast_rand_mod2_10()>>8));
for (j = 0; j < 5; j++)
{
if (blks[idx[j]].ptr)
{
jmfree(blks[idx[j]].ptr, blks[idx[j]].sz);
}
blks[idx[j]].ptr = jmalloc(sz<<j, &blks[idx[j]].sz);
}
for (j = 5; j < 10; j++)
{
if (blks[idx[j]].ptr)
{
jmfree(blks[idx[j]].ptr, blks[idx[j]].sz);
}
blks[idx[j]].ptr = jmalloc(sz<<(j-5), &blks[idx[j]].sz);
}
}
}