-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathuint32s_index.c
More file actions
393 lines (372 loc) · 12.2 KB
/
uint32s_index.c
File metadata and controls
393 lines (372 loc) · 12.2 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* RetroArch - A frontend for libretro.
*
* RetroArch is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with RetroArch. If not, see <http://www.gnu.org/licenses/>.
**/
#ifdef HAVE_STATESTREAM
#include "uint32s_index.h"
#include <string.h>
#include <array/rhmap.h>
#include <array/rbuf.h>
#include "../../verbosity.h"
#define XXH_INLINE_ALL
#include <xxHash/xxhash.h>
#define HASHMAP_CAP 65536
#define uint32s_hash_bytes(bytes, len) XXH32(bytes,len,0)
uint32s_index_t *uint32s_index_new(size_t object_size,
uint8_t commit_interval, uint8_t commit_threshold)
{
uint32_t *zeros = (uint32_t*)calloc(object_size, sizeof(uint32_t));
uint32s_index_t *index = (uint32s_index_t *)malloc(sizeof(uint32s_index_t));
index->object_size = object_size;
index->index = NULL;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtautological-compare"
RHMAP_FIT(index->index, HASHMAP_CAP);
#pragma GCC diagnostic pop
index->objects = NULL;
index->counts = NULL;
index->hashes = NULL;
index->additions = NULL;
index->commit_interval = commit_interval;
index->commit_threshold = commit_threshold;
/* transfers ownership of zero buffer */
uint32s_index_insert_exact(index, 0, zeros, 0);
RBUF_CLEAR(index->additions); /* scrap first addition, we never want to delete 0s during rewind */
return index;
}
void uint32s_bucket_free(struct uint32s_bucket *bucket)
{
if (bucket->len > 3)
free(bucket->contents.vec.idxs);
}
bool uint32s_bucket_get(uint32s_index_t *index, struct uint32s_bucket *bucket, uint32_t *object, size_t size_bytes, uint32_t *out_idx)
{
uint32_t i;
uint32_t *coll = bucket->len < 4 ? bucket->contents.idxs : bucket->contents.vec.idxs;
for (i = 0; i < bucket->len; i++)
{
uint32_t idx = coll[i];
if (memcmp(index->objects[idx], object, size_bytes) == 0)
{
*out_idx = idx;
return true;
}
}
return false;
}
void uint32s_bucket_expand(struct uint32s_bucket *bucket, uint32_t idx)
{
if (bucket->len < 3)
bucket->contents.idxs[bucket->len] = idx;
else if (bucket->len == 3)
{
uint32_t *idxs = (uint32_t*)calloc(8, sizeof(uint32_t));
memcpy(idxs, bucket->contents.idxs, 3*sizeof(uint32_t));
bucket->contents.vec.cap = 8;
bucket->contents.vec.idxs = idxs;
bucket->contents.vec.idxs[bucket->len] = idx;
}
else if (bucket->len < bucket->contents.vec.cap)
bucket->contents.vec.idxs[bucket->len] = idx;
else /* bucket->len == bucket->contents.vec.cap */
{
bucket->contents.vec.cap *= 2;
bucket->contents.vec.idxs = (uint32_t*)realloc(bucket->contents.vec.idxs, bucket->contents.vec.cap * sizeof(uint32_t));
bucket->contents.vec.idxs[bucket->len] = idx;
}
bucket->len++;
}
bool uint32s_bucket_remove(struct uint32s_bucket *bucket, uint32_t idx)
{
int i;
bool small = bucket->len < 4;
uint32_t *coll = small ? bucket->contents.idxs : bucket->contents.vec.idxs;
if (idx == 0) /* never remove 0s pattern */
return false;
for (i = 0; i < (int)bucket->len; i++)
{
if (coll[i] == idx)
{
memmove((uint8_t*)(coll+i), (uint8_t*)(coll+i+1), (bucket->len-(i+1))*sizeof(uint32_t));
bucket->len--;
if (bucket->len == 3)
{
memcpy(bucket->contents.idxs, coll, 3*sizeof(uint32_t));
free(coll);
}
return true;
}
}
RARCH_ERR("[STATESTREAM] didn't find index %d during remove\n",idx);
return false;
}
uint32s_insert_result_t uint32s_index_insert(uint32s_index_t *index, uint32_t *object, uint64_t frame)
{
uint32_t idx;
uint32_t *copy;
struct uint32s_bucket *bucket;
uint32s_insert_result_t result;
size_t size_bytes = index->object_size * sizeof(uint32_t);
uint32_t hash = uint32s_hash_bytes((uint8_t *)object, size_bytes);
uint32_t additions_len = RBUF_LEN(index->additions);
result.index = 0;
result.is_new = false;
if (RHMAP_HAS(index->index, hash))
{
bucket = RHMAP_PTR(index->index, hash);
if (uint32s_bucket_get(index, bucket, object, size_bytes, &result.index))
{
if (index->objects[result.index])
{
result.is_new = false;
index->counts[result.index]++;
return result;
}
RARCH_LOG("[STATESTREAM] accessed collected index %d\n",result.index);
}
idx = RBUF_LEN(index->objects);
copy = (uint32_t*)malloc(size_bytes);
memcpy(copy, object, size_bytes);
RBUF_PUSH(index->objects, copy);
RBUF_PUSH(index->counts, 1);
RBUF_PUSH(index->hashes, hash);
result.index = idx;
result.is_new = true;
uint32s_bucket_expand(bucket, idx);
}
else
{
struct uint32s_bucket new_bucket;
idx = RBUF_LEN(index->objects);
copy = (uint32_t*)malloc(size_bytes);
memcpy(copy, object, size_bytes);
RBUF_PUSH(index->objects, copy);
RBUF_PUSH(index->counts, 1);
RBUF_PUSH(index->hashes, hash);
new_bucket.len = 1;
new_bucket.contents.idxs[0] = idx;
new_bucket.contents.idxs[1] = 0;
new_bucket.contents.idxs[2] = 0;
RHMAP_SET(index->index, hash, new_bucket);
result.index = idx;
result.is_new = true;
}
if (additions_len == 0 || index->additions[additions_len-1].frame_counter < frame)
{
struct uint32s_frame_addition addition;
addition.frame_counter = frame;
addition.first_index = result.index;
RBUF_PUSH(index->additions, addition);
}
return result;
}
bool uint32s_index_insert_exact(uint32s_index_t *index, uint32_t idx, uint32_t *object, uint64_t frame)
{
struct uint32s_bucket *bucket;
uint32_t hash;
size_t size_bytes;
uint32_t additions_len;
if (idx != RBUF_LEN(index->objects))
return false;
size_bytes = index->object_size * sizeof(uint32_t);
hash = uint32s_hash_bytes((uint8_t *)object, size_bytes);
additions_len = RBUF_LEN(index->additions);
if (RHMAP_HAS(index->index, hash))
{
uint32_t idx = 0;
bucket = RHMAP_PTR(index->index, hash);
uint32s_bucket_expand(bucket, idx);
}
else
{
struct uint32s_bucket new_bucket;
new_bucket.len = 1;
new_bucket.contents.idxs[0] = idx;
new_bucket.contents.idxs[1] = 0;
new_bucket.contents.idxs[2] = 0;
RHMAP_SET(index->index, hash, new_bucket);
}
/* RARCH_LOG("[STATESTREAM] insert index %d\n",idx); */
RBUF_PUSH(index->objects, object);
RBUF_PUSH(index->counts, 1);
RBUF_PUSH(index->hashes, hash);
if ( additions_len == 0
|| index->additions[additions_len-1].frame_counter < frame)
{
struct uint32s_frame_addition addition;
addition.frame_counter = frame;
addition.first_index = idx;
RBUF_PUSH(index->additions, addition);
}
return true;
}
void uint32s_index_commit(uint32s_index_t *index)
{
uint32_t i, interval=index->commit_interval,threshold=index->commit_threshold;
struct uint32s_frame_addition prev,cur;
uint32_t additions_len = RBUF_LEN(index->additions), limit;
if (additions_len < interval || interval == 0)
return;
prev = index->additions[additions_len-interval];
cur = index->additions[additions_len-(interval-1)];
limit = cur.first_index;
for (i = prev.first_index; i < limit; i++)
{
struct uint32s_bucket *bucket;
if (index->counts[i] >= threshold)
continue;
free(index->objects[i]);
index->objects[i] = NULL;
bucket = RHMAP_PTR(index->index, index->hashes[i]);
uint32s_bucket_remove(bucket, i);
if (bucket->len == 0)
{
uint32s_bucket_free(bucket);
if (!RHMAP_DEL(index->index, index->hashes[i]))
RARCH_ERR("[STATESTREAM] Trying to remove absent hash %x\n",index->hashes[i]);
}
}
}
void uint32s_index_bump_count(uint32s_index_t *index, uint32_t which)
{
if (which >= RBUF_LEN(index->counts))
return;
index->counts[which]++;
}
uint32_t *uint32s_index_get(uint32s_index_t *index, uint32_t which)
{
if (which >= RBUF_LEN(index->objects))
return NULL;
if (!index->objects[which])
{
int i;
RARCH_LOG("[STATESTREAM] accessed garbage collected block %d\n", which);
for (i = RBUF_LEN(index->additions); i != 0; i--)
{
if (which >= index->additions[i].first_index)
{
RARCH_LOG("[STATESTREAM] originally allocated on frame %ld\n", index->additions[i].frame_counter);
break;
}
}
return NULL;
}
return index->objects[which];
}
void uint32s_index_pop(uint32s_index_t *index)
{
uint32_t idx = RBUF_LEN(index->objects)-1;
uint32_t hash = index->hashes[idx];
struct uint32s_bucket *bucket = RHMAP_PTR(index->index, hash);
uint32_t old_len = bucket->len;
if (old_len == 0) {
RARCH_ERR("[STATESTREAM] trying to pop from empty bucket\n");
return;
}
RBUF_RESIZE(index->objects, idx);
RBUF_RESIZE(index->counts, idx);
RBUF_RESIZE(index->hashes, idx);
uint32s_bucket_remove(bucket, idx);
if (old_len - 1 == 0)
{
uint32s_bucket_free(bucket);
if (!RHMAP_DEL(index->index, hash))
RARCH_ERR("[STATESTREAM] Trying to remove absent hash %x\n",hash);
}
}
/* goes backwards from end of additions */
void uint32s_index_remove_after(uint32s_index_t *index, uint64_t frame)
{
int i;
for(i = RBUF_LEN(index->additions)-1; i >= 0; i--)
{
struct uint32s_frame_addition add = index->additions[i];
if (add.frame_counter <= frame)
break;
while(add.first_index < RBUF_LEN(index->objects))
uint32s_index_pop(index);
}
RBUF_RESIZE(index->additions, i+1);
}
/* removes all data from index */
void uint32s_index_clear(uint32s_index_t *index)
{
size_t i, cap;
uint32_t *zeros = index->objects[0];
for(i = 0, cap = RHMAP_CAP(index->index); i != cap; i++)
if (RHMAP_KEY(index->index, i))
uint32s_bucket_free(&index->index[i]);
RHMAP_CLEAR(index->index);
/* don't dealloc all-zeros pattern */
for(i = 1; i < RBUF_LEN(index->objects); i++)
free(index->objects[i]);
RBUF_CLEAR(index->objects);
RBUF_CLEAR(index->counts);
RBUF_CLEAR(index->hashes);
uint32s_index_insert_exact(index, 0, zeros, 0);
/* wipe additions */
RBUF_CLEAR(index->additions);
}
void uint32s_index_free(uint32s_index_t *index)
{
size_t i, cap;
if (!index)
return;
for(i = 0, cap = RHMAP_CAP(index->index); i != cap; i++)
if (RHMAP_KEY(index->index, i))
uint32s_bucket_free(&index->index[i]);
RHMAP_FREE(index->index);
for(i = 0; i < RBUF_LEN(index->objects); i++)
free(index->objects[i]);
RBUF_FREE(index->objects);
RBUF_FREE(index->counts);
RBUF_FREE(index->hashes);
RBUF_FREE(index->additions);
free(index);
}
uint32_t uint32s_index_count(uint32s_index_t *index)
{
if (!index || !index->objects)
return 0;
return RBUF_LEN(index->objects);
}
#if DEBUG
#define BIN_COUNT 1000
uint32_t bins[BIN_COUNT];
void uint32s_index_print_count_data(uint32s_index_t *index)
{
uint32_t i;
/* TODO/FIXME: Don't count or differently count NULL objects entries */
uint32_t max=1;
if (!index)
return;
for(i = 0; i < BIN_COUNT; i++)
bins[i] = 0;
for(i = 1; i < RBUF_LEN(index->counts); i++)
max = MAX(max, index->counts[i]);
max = max + 1;
for(i = 1; i < RBUF_LEN(index->counts); i++)
bins[(int)((((float)index->counts[i]) / (float)max)*BIN_COUNT)]++;
for(i = 0; i < BIN_COUNT; i++)
{
uint32_t bin_start = MAX(1,(i*(float)max/(float)BIN_COUNT));
uint32_t bin_end = ((i+1)*(float)max/(float)BIN_COUNT);
if (bins[i] == 0)
continue;
RARCH_DBG("%d--%d: %d\n", bin_start, bin_end, bins[i]);
}
}
#endif
#endif