-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathets_multimap.c
More file actions
543 lines (445 loc) · 14.2 KB
/
Copy pathets_multimap.c
File metadata and controls
543 lines (445 loc) · 14.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
* This file is part of AtomVM.
*
* Copyright 2025 Mateusz Furga <mateusz.furga@swmansion.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/
#include <assert.h>
#include <stdint.h>
#include "globalcontext.h"
#include "term.h"
#include "term_hash.h"
#include "ets_multimap.h"
#define DYNARRAY_INITIAL_CAPACITY 8
#define DYNARRAY_GROWTH_FACTOR 2
static EtsMultimapEntry *entry_new(term tuple);
static void entry_delete(EtsMultimapEntry *entry, GlobalContext *global);
static EtsMultimapNode *node_new(EtsMultimapNode *next, EtsMultimapEntry *entries);
static void node_delete(EtsMultimapNode *node, GlobalContext *global);
static ets_result_t node_find(
EtsMultimap *multimap,
term key,
EtsMultimapNode **out_node,
GlobalContext *global);
static term node_key(EtsMultimap *multimap, EtsMultimapNode *node);
static void multimap_to_single(EtsMultimap *multimap, GlobalContext *global);
static void insert_revert(
EtsMultimap *multimap,
EtsMultimapEntry **entries,
size_t count,
GlobalContext *global);
static ets_result_t tuple_exists(
EtsMultimapNode *node,
term tuple,
bool *exists,
GlobalContext *global);
EtsMultimap *ets_multimap_new(ets_multimap_type_t type, size_t key_index)
{
EtsMultimap *multimap = malloc(sizeof(struct EtsMultimap));
if (IS_NULL_PTR(multimap)) {
return NULL;
}
multimap->type = type;
multimap->key_index = key_index;
for (size_t i = 0; i < ETS_MULTIMAP_NUM_BUCKETS; i++) {
multimap->buckets[i] = NULL;
}
return multimap;
}
void ets_multimap_delete(EtsMultimap *multimap, GlobalContext *global)
{
for (size_t i = 0; i < ETS_MULTIMAP_NUM_BUCKETS; i++) {
EtsMultimapNode *node = multimap->buckets[i];
while (node != NULL) {
EtsMultimapNode *next = node->next;
node_delete(node, global);
node = next;
}
}
free(multimap);
}
ets_result_t ets_multimap_lookup(
EtsMultimap *multimap,
term key,
term **tuples,
size_t *count,
GlobalContext *global)
{
assert(count != NULL);
*count = 0;
EtsMultimapNode *node;
ets_result_t result = node_find(multimap, key, &node, global);
if (UNLIKELY(result == EtsAllocationError)) {
return result;
}
if (node == NULL) {
return EtsOk;
}
assert(node->entries != NULL);
for (EtsMultimapEntry *iter = node->entries; iter != NULL; iter = iter->next) {
(*count)++;
}
if (tuples == NULL) {
// only return number of tuples found
return EtsOk;
}
*tuples = malloc(sizeof(term) * (*count));
if (IS_NULL_PTR(*tuples)) {
return EtsAllocationError;
}
size_t i = 0;
for (EtsMultimapEntry *iter = node->entries; iter != NULL; iter = iter->next, i++) {
assert(i < *count);
(*tuples)[i] = iter->tuple;
}
return EtsOk;
}
ets_result_t ets_multimap_insert(
EtsMultimap *multimap,
term *tuples,
size_t count,
GlobalContext *global)
{
if (tuples == NULL || count == 0) {
return EtsOk;
}
EtsMultimapEntry **entries = malloc(sizeof(EtsMultimapEntry *) * count);
if (IS_NULL_PTR(entries)) {
return EtsAllocationError;
}
for (size_t i = 0; i < count; i++) {
entries[i] = entry_new(tuples[i]);
if (IS_NULL_PTR(entries[i])) {
for (size_t j = 0; j < i; j++) {
entry_delete(entries[j], global);
}
free(entries);
return EtsAllocationError;
}
}
ets_result_t status = EtsOk;
for (size_t i = 0; i < count; i++) {
EtsMultimapEntry *entry = entries[i];
term key = term_get_tuple_element(entry->tuple, multimap->key_index);
EtsMultimapNode *node;
if (UNLIKELY(node_find(multimap, key, &node, global) == EtsAllocationError)) {
status = EtsAllocationError;
break;
}
if (node == NULL) {
EtsMultimapNode *new_node = node_new(NULL, entry);
if (IS_NULL_PTR(new_node)) {
status = EtsAllocationError;
break;
}
assert(new_node->entries != NULL);
uint32_t idx = term_hash(key, global) % ETS_MULTIMAP_NUM_BUCKETS;
new_node->next = multimap->buckets[idx];
multimap->buckets[idx] = new_node;
continue;
}
assert(node->entries != NULL);
if (multimap->type == EtsMultimapTypeSet) {
bool exists;
if (UNLIKELY(tuple_exists(node, entry->tuple, &exists, global) == EtsAllocationError)) {
status = EtsAllocationError;
break;
}
if (exists) {
entry_delete(entry, global);
entries[i] = NULL;
continue;
}
}
entry->next = node->entries;
node->entries = entry;
}
if (status != EtsOk) {
insert_revert(multimap, entries, count, global);
} else if (multimap->type == EtsMultimapTypeSingle) {
multimap_to_single(multimap, global);
}
free(entries);
return status;
}
ets_result_t ets_multimap_remove(
EtsMultimap *multimap,
term key,
GlobalContext *global)
{
EtsMultimapNode *node;
if (UNLIKELY(node_find(multimap, key, &node, global) == EtsAllocationError)) {
return EtsAllocationError;
}
if (node == NULL) {
return EtsOk;
}
assert(node->entries != NULL);
assert(term_compare(key, node_key(multimap, node), TermCompareExact, global) == TermEquals);
uint32_t idx = term_hash(key, global) % ETS_MULTIMAP_NUM_BUCKETS;
EtsMultimapNode *iter = multimap->buckets[idx];
EtsMultimapNode *prev = NULL;
while (iter) {
if (iter == node) {
if (prev == NULL) {
multimap->buckets[idx] = iter->next;
} else {
prev->next = iter->next;
}
break;
}
prev = iter;
iter = iter->next;
}
node_delete(node, global);
return EtsOk;
}
ets_result_t ets_multimap_remove_tuple(
EtsMultimap *multimap,
term tuple,
GlobalContext *global)
{
assert(term_is_tuple(tuple));
if (multimap->key_index >= (size_t) term_get_tuple_arity(tuple)) {
return EtsBadEntry;
}
term key = term_get_tuple_element(tuple, multimap->key_index);
EtsMultimapNode *node;
if (UNLIKELY(node_find(multimap, key, &node, global) == EtsAllocationError)) {
return EtsAllocationError;
}
if (node == NULL) {
return EtsOk;
}
assert(node->entries != NULL);
size_t capacity = DYNARRAY_INITIAL_CAPACITY;
size_t count = 0;
EtsMultimapEntry **to_remove = malloc(sizeof(EtsMultimapEntry *) * capacity);
if (IS_NULL_PTR(to_remove)) {
return EtsAllocationError;
}
for (EtsMultimapEntry *iter = node->entries; iter != NULL; iter = iter->next) {
TermCompareResult result = term_compare(tuple, iter->tuple, TermCompareExact, global);
if (UNLIKELY(result == TermCompareMemoryAllocFail)) {
free(to_remove);
return EtsAllocationError;
}
if (result == TermEquals) {
if (count >= capacity) {
capacity *= DYNARRAY_GROWTH_FACTOR;
EtsMultimapEntry **new_to_remove = realloc(to_remove, sizeof(EtsMultimapEntry *) * capacity);
if (IS_NULL_PTR(new_to_remove)) {
// GCC 12 is raising here a false positive warning, according to man realloc:
// "If realloc() fails, the original block is left untouched; it is not freed or moved."
#pragma GCC diagnostic push
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 12
#pragma GCC diagnostic ignored "-Wuse-after-free"
#endif
free(to_remove);
#pragma GCC diagnostic pop
return EtsAllocationError;
}
to_remove = new_to_remove;
}
to_remove[count++] = iter;
}
}
EtsMultimapEntry *prev = NULL;
for (EtsMultimapEntry *iter = node->entries; iter != NULL; iter = iter->next) {
bool removed = false;
for (size_t i = 0; i < count; i++) {
if (iter == to_remove[i]) {
if (prev == NULL) {
node->entries = iter->next;
} else {
prev->next = iter->next;
}
removed = true;
break;
}
}
if (!removed) {
prev = iter;
}
}
for (size_t i = 0; i < count; i++) {
entry_delete(to_remove[i], global);
}
if (node->entries == NULL) {
uint32_t idx = term_hash(key, global) % ETS_MULTIMAP_NUM_BUCKETS;
EtsMultimapNode *prev_node = NULL;
for (EtsMultimapNode *iter = multimap->buckets[idx]; iter != NULL; prev_node = iter, iter = iter->next) {
if (iter == node) {
if (prev_node == NULL) {
multimap->buckets[idx] = iter->next;
} else {
prev_node->next = iter->next;
}
break;
}
}
node_delete(node, global);
}
free(to_remove);
return EtsOk;
}
static ets_result_t node_find(
EtsMultimap *multimap,
term key,
EtsMultimapNode **out_node,
GlobalContext *global)
{
assert(out_node != NULL);
*out_node = NULL;
uint32_t idx = term_hash(key, global) % ETS_MULTIMAP_NUM_BUCKETS;
EtsMultimapNode *node = multimap->buckets[idx];
while (node) {
TermCompareResult result = term_compare(key, node_key(multimap, node), TermCompareExact, global);
if (UNLIKELY(result == TermCompareMemoryAllocFail)) {
return EtsAllocationError;
}
if (result == TermEquals) {
*out_node = node;
return EtsOk;
}
node = node->next;
}
return EtsOk;
}
// Revert a partial batch insert. Assumes that newly inserted items are always at the head.
static void insert_revert(
EtsMultimap *multimap,
EtsMultimapEntry **entries,
size_t count,
GlobalContext *global)
{
for (size_t i = 0; i < ETS_MULTIMAP_NUM_BUCKETS; i++) {
EtsMultimapNode *node = multimap->buckets[i];
while (node != NULL) {
EtsMultimapNode *next_node = node->next;
EtsMultimapEntry *entry = node->entries;
assert(entry != NULL);
while (entry != NULL) {
EtsMultimapEntry *next_entry = entry->next;
for (size_t j = 0; j < count; j++) {
if (entry == entries[j]) {
node->entries = next_entry;
}
}
entry = next_entry;
}
if (node->entries == NULL) {
multimap->buckets[i] = next_node;
node_delete(node, global);
}
node = next_node;
}
}
for (size_t i = 0; i < count; i++) {
if (entries[i] != NULL) {
entry_delete(entries[i], global);
}
}
}
static void multimap_to_single(EtsMultimap *multimap, GlobalContext *global)
{
for (size_t i = 0; i < ETS_MULTIMAP_NUM_BUCKETS; i++) {
for (EtsMultimapNode *node = multimap->buckets[i]; node != NULL; node = node->next) {
assert(node->entries != NULL);
EtsMultimapEntry *entry = node->entries->next;
while (entry != NULL) {
EtsMultimapEntry *next = entry->next;
entry_delete(entry, global);
entry = next;
}
node->entries->next = NULL;
}
}
}
static ets_result_t tuple_exists(
EtsMultimapNode *node,
term tuple,
bool *exists,
GlobalContext *global)
{
for (EtsMultimapEntry *iter = node->entries; iter != NULL; iter = iter->next) {
TermCompareResult result = term_compare(tuple, iter->tuple, TermCompareExact, global);
if (UNLIKELY(result == TermCompareMemoryAllocFail)) {
return EtsAllocationError;
}
if (result == TermEquals) {
*exists = true;
return EtsOk;
}
}
*exists = false;
return EtsOk;
}
static term node_key(EtsMultimap *multimap, EtsMultimapNode *node)
{
EtsMultimapEntry *entry = node->entries;
assert(entry != NULL);
return term_get_tuple_element(entry->tuple, multimap->key_index);
}
static EtsMultimapNode *node_new(EtsMultimapNode *next, EtsMultimapEntry *entries)
{
EtsMultimapNode *node = malloc(sizeof(EtsMultimapNode));
if (IS_NULL_PTR(node)) {
return NULL;
}
node->next = next;
node->entries = entries;
return node;
}
static EtsMultimapEntry *entry_new(term tuple)
{
EtsMultimapEntry *entry = malloc(sizeof(EtsMultimapEntry));
if (IS_NULL_PTR(entry)) {
return NULL;
}
Heap *heap = malloc(sizeof(Heap));
if (IS_NULL_PTR(heap)) {
free(entry);
return NULL;
}
size_t size = memory_estimate_usage(tuple);
if (UNLIKELY(memory_init_heap(heap, size) != MEMORY_GC_OK)) {
free(entry);
free(heap);
return NULL;
}
tuple = memory_copy_term_tree(heap, tuple);
entry->tuple = tuple;
entry->heap = heap;
entry->next = NULL;
return entry;
}
static void node_delete(EtsMultimapNode *node, GlobalContext *global)
{
EtsMultimapEntry *entry = node->entries;
while (entry != NULL) {
EtsMultimapEntry *next = entry->next;
entry_delete(entry, global);
entry = next;
}
free(node);
}
static void entry_delete(EtsMultimapEntry *entry, GlobalContext *global)
{
memory_destroy_heap(entry->heap, global);
free(entry->heap);
free(entry);
}