-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_simple_pow2_pool.cc
126 lines (107 loc) · 2.89 KB
/
t_simple_pow2_pool.cc
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
// Copyright (C) 2018 XMM SWAP LTD
#include "simple_pow2_pool.h"
#include <stdlib.h>
static char s_pool[4096];
int main(int argc, char *argv[])
{
size_t counter = argc > 1 ? strtod(argv[1], nullptr) : 1e8;
size_t seed = argc > 2 ? strtod(argv[2], nullptr) : counter;
simple_pow2_pool<3,6,5> pool(s_pool, sizeof(s_pool));
size_t constexpr num_raw_objects = 31; // up to 32 size_t[8]
void *raw_objects[num_raw_objects] {};
unsigned short raw_sizes[num_raw_objects] {};
size_t values[num_raw_objects];
srandom(seed);
for (size_t i = 0; i < counter; i++) {
size_t r = random();
size_t n = r % num_raw_objects;
auto *ptr = static_cast<size_t *>(raw_objects[n]);
assert((ptr == nullptr) == (raw_sizes[n] == 0));
pool.prefetch_next_alloc();
pool.prefetch_next_alloc<size_t[1]>();
pool.prefetch_next_alloc_deep(2);
pool.prefetch_next_alloc_deep<size_t[4]>(3);
if (ptr) {
assert(ptr[0] < i);
assert(ptr[0] == values[n]);
switch (raw_sizes[n]) {
case 1:
pool.dealloc<size_t[1]>(ptr);
break;
case 2:
pool.dealloc<size_t[2]>(ptr);
break;
case 4:
pool.dealloc<size_t[4]>(ptr);
break;
case 8:
pool.dealloc<size_t[8]>(ptr);
break;
default:
assert(false);
}
raw_objects[n] = nullptr;
raw_sizes[n] = 0;
} else {
switch ((r >> 19) & 3) {
case 0:
raw_objects[n] = pool.alloc<size_t[1]>();
raw_sizes[n] = 1;
break;
case 1:
raw_objects[n] = pool.alloc<size_t[2]>();
raw_sizes[n] = 2;
break;
case 2:
raw_objects[n] = pool.alloc<size_t[4]>();
raw_sizes[n] = 4;
break;
case 3:
raw_objects[n] = pool.alloc<size_t[8]>();
raw_sizes[n] = 8;
break;
}
ptr = static_cast<size_t *>(raw_objects[n]);
ptr[0] = i;
values[n] = i;
}
}
// Deallocate all.
for (size_t i = 0; i < num_raw_objects; i++) {
switch (raw_sizes[i]) {
case 0:
assert(raw_objects[i] == nullptr);
break;
case 1:
pool.dealloc<size_t[1]>(raw_objects[i]);
break;
case 2:
pool.dealloc<size_t[2]>(raw_objects[i]);
break;
case 4:
pool.dealloc<size_t[4]>(raw_objects[i]);
break;
case 8:
pool.dealloc<size_t[8]>(raw_objects[i]);
break;
default:
assert(false);
}
}
for (size_t n = 0; n < num_raw_objects; n++)
raw_objects[n] = pool.alloc<size_t[1]>();
for (size_t n = 0; n < num_raw_objects; n++)
pool.dealloc<size_t[1]>(raw_objects[n]);
for (size_t n = 0; n < num_raw_objects; n++)
raw_objects[n] = pool.alloc<size_t[2]>();
for (size_t n = 0; n < num_raw_objects; n++)
pool.dealloc<size_t[2]>(raw_objects[n]);
for (size_t n = 0; n < num_raw_objects; n++)
raw_objects[n] = pool.alloc<size_t[4]>();
for (size_t n = 0; n < num_raw_objects; n++)
pool.dealloc<size_t[4]>(raw_objects[n]);
for (size_t n = 0; n < num_raw_objects; n++)
raw_objects[n] = pool.alloc<size_t[8]>();
for (size_t n = 0; n < num_raw_objects; n++)
pool.dealloc<size_t[8]>(raw_objects[n]);
}