-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrmap.cpp
More file actions
210 lines (161 loc) · 4.79 KB
/
Copy pathrmap.cpp
File metadata and controls
210 lines (161 loc) · 4.79 KB
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
#include "rmap.h"
#include "mylock.h"
#include "util.h"
#include <stdlib.h>
static uint32_t rmap_size(RMap* rmap) {
uint32_t size = __atomic_load_n(&rmap->size, __ATOMIC_RELAXED);
return min(size, rmap->alloc);
}
static RMapEntry* _rmap_search_binary(RMap* rmap,
const uint32_t id,
int u,
int o) {
if (u > o) {
return nullptr;
}
int index = (u + o) / 2;
RMapEntry* current_entry = rmap->list + index;
uint32_t current_id = __atomic_load_n(¤t_entry->id, __ATOMIC_RELAXED);
while (!current_id) {
if (current_entry == rmap->list) {
return nullptr;
}
current_entry--;
current_id = __atomic_load_n(¤t_entry->id, __ATOMIC_RELAXED);
}
if (current_id == id) {
return current_entry;
} else if (id < current_id) {
return _rmap_search_binary(rmap, id, u, index - 1);
} else {
return _rmap_search_binary(rmap, id, index + 1, o);
}
}
RMapEntry* rmap_search_binary(RMap* rmap, const uint32_t id) {
int size = rmap_size(rmap);
if (!size) {
return nullptr;
}
return _rmap_search_binary(rmap, id, 0, size - 1);
}
static RMapEntry* rmap_search_linear(RMap* rmap, const uint32_t id) {
int size = rmap_size(rmap);
for (int i = 0; i < size; i++) {
RMapEntry* current_entry = rmap->list + i;
uint32_t current_tid =
__atomic_load_n(¤t_entry->id, __ATOMIC_RELAXED);
if (current_tid == id) {
return current_entry;
}
}
return nullptr;
}
static RMapEntry* rmap_alloc_append(RMap* rmap, const uint32_t tid) {
uint32_t size = rmap_size(rmap);
Spinlock expected = 0;
if (size >= rmap->alloc) {
return nullptr;
}
while (1) {
uint32_t idx = __atomic_fetch_add(&rmap->size, 1, __ATOMIC_ACQUIRE);
RMapEntry* current_entry = rmap->list + idx;
if (idx >= rmap->alloc) {
return nullptr;
}
if (!__atomic_compare_exchange_n(¤t_entry->id, &expected, tid, 0,
__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
expected = 0;
continue;
}
return current_entry;
}
}
static RMapEntry* rmap_alloc_sparse(RMap* rmap, const uint32_t tid) {
int size = rmap_size(rmap);
Spinlock expected = 0;
for (int i = 0; i < size; i++) {
RMapEntry* current_entry = rmap->list + i;
if (!__atomic_compare_exchange_n(¤t_entry->id, &expected, tid, 0,
__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
expected = 0;
continue;
}
return current_entry;
}
return nullptr;
}
static RMapEntry* __rmap_get_noalloc(RMap* rmap, const uint32_t id) {
RMapEntry* entry;
if (!id) {
abort();
}
entry = rmap_search_binary(rmap, id);
if (entry) {
return entry;
}
entry = rmap_search_linear(rmap, id);
if (entry) {
return entry;
}
return nullptr;
}
RMapEntry* rmap_get_noalloc(RMap* rmap, const uint32_t id) {
RMapEntry* entry;
entry = __rmap_get_noalloc(rmap, id);
if (entry) {
assert(entry->id > 0);
return entry;
}
return nullptr;
}
RMapEntry* rmap_get(RMap* rmap, const uint32_t id) {
RMapEntry* entry;
entry = __rmap_get_noalloc(rmap, id);
if (entry) {
assert(entry->id > 0);
return entry;
}
entry = rmap_alloc_append(rmap, id);
if (entry) {
assert(entry->id > 0);
return entry;
}
entry = rmap_alloc_sparse(rmap, id);
if (entry) {
assert(entry->id > 0);
return entry;
}
return nullptr;
}
void _rmap_free(RMap* rmap, RMapEntry* entry) {
uint32_t size = __atomic_load_n(&rmap->size, __ATOMIC_ACQUIRE);
uint32_t actual_size = min(size, (uint32_t)rmap->alloc);
uint32_t idx = entry - rmap->list;
Spinlock expected = size;
void* data = entry->data;
WRITE_ONCE(entry->data, nullptr);
__asm volatile("" ::: "memory");
free(data);
if (idx == actual_size - 1) {
__atomic_compare_exchange_n(&rmap->size, &expected, actual_size - 1, 0,
__ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
}
__atomic_store_n(&entry->id, 0, __ATOMIC_RELEASE);
}
void rmap_free(RMap* rmap, const uint32_t id) {
RMapEntry* entry;
if (!id) {
abort();
}
entry = __rmap_get_noalloc(rmap, id);
if (entry) {
assert(entry->id > 0);
_rmap_free(rmap, entry);
}
}
RMap* rmap_alloc(uint32_t alloc) {
size_t size = sizeof(RMap) + alloc * sizeof(RMapEntry);
RMap* rmap = (RMap*)malloc(size);
rmap->alloc = alloc;
return rmap;
}