-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConcurrentIndexMap.c
702 lines (571 loc) · 33 KB
/
ConcurrentIndexMap.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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
* Copyright (c) 2018, Stefan Johnson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define CC_QUICK_COMPILE
#include "ConcurrentIndexMap.h"
#include "MemoryAllocation.h"
#include "Assertion.h"
#include <stdatomic.h>
#include <string.h>
/*
CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE enforces standard compliant usage of atomic types. This however will
result in all operations requiring CAS-loops.
*/
#ifndef CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
#define CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE 0
#endif
#if !CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
#if !CC_HARDWARE_PTR_64 && !CC_HARDWARE_PTR_32
#undef CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
#define CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE 1
#endif
#endif
typedef struct {
CCConcurrentIndexMap indexMap;
_Atomic(size_t) count;
uint8_t buffer[];
} CCConcurrentIndexMapData;
typedef struct {
#if CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
#if CC_HARDWARE_PTR_64
uint32_t mutate;
uint32_t modify;
#else
uint16_t mutate;
uint16_t modify;
#endif
#else
#if CC_HARDWARE_PTR_64
_Atomic(uint32_t) mutate;
_Atomic(uint32_t) modify;
#else
_Atomic(uint16_t) mutate;
_Atomic(uint16_t) modify;
#endif
#endif
CCConcurrentIndexMapData *data;
} CCConcurrentIndexMapDataPointer;
#if !CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
#if CC_HARDWARE_PTR_64
_Static_assert(sizeof(_Atomic(CCConcurrentIndexMapDataPointer)) == 16, "Native types are not atomic, please set CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE");
#elif CC_HARDWARE_PTR_32
_Static_assert(sizeof(_Atomic(CCConcurrentIndexMapDataPointer)) == 8, "Native types are not atomic, please set CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE");
#endif
#endif
typedef struct {
size_t size;
_Bool (*initElement)(CCConcurrentIndexMap, void *, size_t, const void *);
void (*copyElement)(void *, size_t, const void *, size_t);
_Bool (*getElement)(CCConcurrentIndexMap, const void *, size_t, void *);
_Bool (*removeElement)(CCConcurrentIndexMap, const void *, size_t);
void (*destroyElement)(CCConcurrentIndexMap, const void *, size_t);
_Bool (*setElement)(CCConcurrentIndexMap, const void *, size_t, const void *, void *);
_Bool (*compareAndSwapElement)(CCConcurrentIndexMap, const void *, size_t, const void *, const void *);
_Bool (*appendElement)(CCConcurrentIndexMap, const void *, size_t *, size_t, const void *, void **);
} CCConcurrentIndexMapAtomicOperation;
typedef struct CCConcurrentIndexMapInfo {
CCAllocatorType allocator;
size_t size, chunkSize;
_Atomic(CCConcurrentIndexMapDataPointer) pointer;
CCConcurrentGarbageCollector gc;
} CCConcurrentIndexMapInfo;
#define CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE_SIZE(x) (x <= 8 ? (x <= 4 ? (x <= 2 ? (x <= 1 ? 1 : 2) : 4) : 8) : 16)
#define CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(x) \
typedef struct { uint8_t e[x - 1]; } CCConcurrentIndexMapAtomicElementType##x; \
typedef struct { CCConcurrentIndexMapAtomicElementType##x element; uint8_t set; uint8_t pad[CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE_SIZE(x) - x]; } CCConcurrentIndexMapAtomicType##x; \
_Static_assert(sizeof(CCConcurrentIndexMapAtomicType##x) == CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE_SIZE(x), "CCConcurrentIndexMapAtomicType"#x " should be padded to a power of 2 size"); \
static _Bool CCConcurrentIndexMapAtomicInitElement##x(CCConcurrentIndexMap IndexMap, void *Data, size_t Index, const void *Element) \
{ \
atomic_init(&((_Atomic(CCConcurrentIndexMapAtomicType##x)*)Data)[Index], Element ? ((CCConcurrentIndexMapAtomicType##x){ .set = TRUE, .element = *(CCConcurrentIndexMapAtomicElementType##x*)Element }) : ((CCConcurrentIndexMapAtomicType##x){ .set = FALSE })); \
return TRUE; \
} \
static void CCConcurrentIndexMapAtomicCopyElement##x(void *Data, size_t Index, const void *SrcData, size_t SrcIndex) \
{ \
CCConcurrentIndexMapAtomicType##x Value = atomic_load_explicit(&((_Atomic(CCConcurrentIndexMapAtomicType##x)*)SrcData)[SrcIndex], memory_order_relaxed); \
atomic_store_explicit(&((_Atomic(CCConcurrentIndexMapAtomicType##x)*)Data)[Index], Value, memory_order_relaxed); \
} \
static _Bool CCConcurrentIndexMapAtomicGetElement##x(CCConcurrentIndexMap IndexMap, const void *Data, size_t Index, void *Out) \
{ \
CCConcurrentIndexMapAtomicType##x Value = atomic_load_explicit(&((_Atomic(CCConcurrentIndexMapAtomicType##x)*)Data)[Index], memory_order_relaxed); \
if ((Value.set) && (Out)) *(CCConcurrentIndexMapAtomicElementType##x*)Out = Value.element; \
return Value.set; \
} \
static _Bool CCConcurrentIndexMapAtomicRemoveElement##x(CCConcurrentIndexMap IndexMap, const void *Data, size_t Index) \
{ \
CCConcurrentIndexMapAtomicType##x Value = atomic_exchange_explicit(&((_Atomic(CCConcurrentIndexMapAtomicType##x)*)Data)[Index], ((CCConcurrentIndexMapAtomicType##x){ .set = FALSE }), memory_order_relaxed); \
return Value.set; \
} \
static void CCConcurrentIndexMapAtomicDestroyElement##x(CCConcurrentIndexMap IndexMap, const void *Data, size_t Index) \
{ \
} \
static _Bool CCConcurrentIndexMapAtomicSetElement##x(CCConcurrentIndexMap IndexMap, const void *Data, size_t Index, const void *New, void *Out) \
{ \
CCConcurrentIndexMapAtomicType##x Value = atomic_load_explicit(&((_Atomic(CCConcurrentIndexMapAtomicType##x)*)Data)[Index], memory_order_relaxed); \
if (!Value.set) return FALSE; \
Value = atomic_exchange_explicit(&((_Atomic(CCConcurrentIndexMapAtomicType##x)*)Data)[Index], ((CCConcurrentIndexMapAtomicType##x){ .set = TRUE, .element = *(CCConcurrentIndexMapAtomicElementType##x*)New }), memory_order_relaxed); \
if ((Value.set) && (Out)) *(CCConcurrentIndexMapAtomicElementType##x*)Out = Value.element; \
return Value.set; \
} \
static _Bool CCConcurrentIndexMapAtomicCompareAndSwapElement##x(CCConcurrentIndexMap IndexMap, const void *Data, size_t Index, const void *New, const void *Match) \
{ \
return atomic_compare_exchange_strong_explicit(&((_Atomic(CCConcurrentIndexMapAtomicType##x)*)Data)[Index], &((CCConcurrentIndexMapAtomicType##x){ .set = TRUE, .element = *(CCConcurrentIndexMapAtomicElementType##x*)Match }), ((CCConcurrentIndexMapAtomicType##x){ .set = TRUE, .element = *(CCConcurrentIndexMapAtomicElementType##x*)New }), memory_order_relaxed, memory_order_relaxed); \
} \
static _Bool CCConcurrentIndexMapAtomicAppendElement##x(CCConcurrentIndexMap IndexMap, const void *Data, size_t *Index, size_t MaxCount, const void *New, void **State) \
{ \
for ( ; *Index < MaxCount; (*Index)++) \
{ \
if (atomic_compare_exchange_strong_explicit(&((_Atomic(CCConcurrentIndexMapAtomicType##x)*)Data)[*Index], &((CCConcurrentIndexMapAtomicType##x){ .set = FALSE }), ((CCConcurrentIndexMapAtomicType##x){ .set = TRUE, .element = *(CCConcurrentIndexMapAtomicElementType##x*)New }), memory_order_relaxed, memory_order_relaxed)) return TRUE; \
} \
return FALSE; \
}
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(1)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(2)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(3)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(4)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(5)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(6)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(7)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(8)
#if CC_HARDWARE_PTR_64
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(9)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(10)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(11)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(12)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(13)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(14)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(15)
CC_CONCURRENT_INDEX_MAP_ATOMIC_TYPE(16)
#endif
typedef struct {
void *ptr;
} CCConcurrentIndexMapAtomicTypePtr;
static _Bool CCConcurrentIndexMapAtomicInitElementPtr(CCConcurrentIndexMap IndexMap, void *Data, size_t Index, const void *New)
{
if (New)
{
void *Element = CCMalloc(IndexMap->allocator, IndexMap->size, NULL, CC_DEFAULT_ERROR_CALLBACK);
if (!Element) return FALSE;
memcpy(Element, New, IndexMap->size);
atomic_init(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[Index], ((CCConcurrentIndexMapAtomicTypePtr){ .ptr = Element }));
}
else atomic_init(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[Index], ((CCConcurrentIndexMapAtomicTypePtr){ .ptr = NULL }));
return TRUE;
}
static void CCConcurrentIndexMapAtomicCopyElementPtr(void *Data, size_t Index, const void *SrcData, size_t SrcIndex)
{
CCConcurrentIndexMapAtomicTypePtr Value = atomic_load_explicit(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)SrcData)[SrcIndex], memory_order_relaxed);
CCRetain(Value.ptr);
atomic_store_explicit(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[Index], Value, memory_order_relaxed);
}
static _Bool CCConcurrentIndexMapAtomicGetElementPtr(CCConcurrentIndexMap IndexMap, const void *Data, size_t Index, void *Out)
{
CCConcurrentIndexMapAtomicTypePtr Value = atomic_load_explicit(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[Index], memory_order_relaxed);
if ((Value.ptr) && (Out)) memcpy(Out, Value.ptr, IndexMap->size);
return Value.ptr;
}
static _Bool CCConcurrentIndexMapAtomicRemoveElementPtr(CCConcurrentIndexMap IndexMap, const void *Data, size_t Index)
{
CCConcurrentIndexMapAtomicTypePtr Value = atomic_exchange_explicit(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[Index], ((CCConcurrentIndexMapAtomicTypePtr){ .ptr = NULL }), memory_order_relaxed);
if (Value.ptr) CCConcurrentGarbageCollectorManage(IndexMap->gc, Value.ptr, CCFree);
return Value.ptr;
}
static void CCConcurrentIndexMapAtomicDestroyElementPtr(CCConcurrentIndexMap IndexMap, const void *Data, size_t Index)
{
CCConcurrentIndexMapAtomicTypePtr Value = atomic_load_explicit(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[Index], memory_order_relaxed);
if (Value.ptr) CCFree(Value.ptr);
}
static _Bool CCConcurrentIndexMapAtomicSetElementPtr(CCConcurrentIndexMap IndexMap, const void *Data, size_t Index, const void *New, void *Out)
{
CCConcurrentIndexMapAtomicTypePtr Value = atomic_load_explicit(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[Index], memory_order_relaxed);
if (!Value.ptr) return FALSE;
void *Element = CCMalloc(IndexMap->allocator, IndexMap->size, NULL, CC_DEFAULT_ERROR_CALLBACK);
if (!Element) return FALSE;
memcpy(Element, New, IndexMap->size);
Value = atomic_exchange_explicit(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[Index], ((CCConcurrentIndexMapAtomicTypePtr){ .ptr = Element }), memory_order_release);
if (Value.ptr)
{
if (Out) memcpy(Out, Value.ptr, IndexMap->size);
CCConcurrentGarbageCollectorManage(IndexMap->gc, Value.ptr, CCFree);
}
return Value.ptr;
}
static _Bool CCConcurrentIndexMapAtomicCompareAndSwapElementPtr(CCConcurrentIndexMap IndexMap, const void *Data, size_t Index, const void *New, const void *Match)
{
CCConcurrentIndexMapAtomicTypePtr Value = atomic_load_explicit(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[Index], memory_order_relaxed);
if (!Value.ptr) return FALSE;
void *Element = CCMalloc(IndexMap->allocator, IndexMap->size, NULL, CC_DEFAULT_ERROR_CALLBACK);
if (!Element) return FALSE;
memcpy(Element, New, IndexMap->size);
_Bool Success = FALSE;
for ( ; ; )
{
if (!memcmp(Value.ptr, Match, IndexMap->size))
{
Success = atomic_compare_exchange_strong_explicit(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[Index], &Value, ((CCConcurrentIndexMapAtomicTypePtr){ .ptr = Element }), memory_order_relaxed, memory_order_relaxed);
if (Success)
{
CCConcurrentGarbageCollectorManage(IndexMap->gc, Value.ptr, CCFree);
break;
}
}
else
{
CCFree(Element);
break;
}
}
return Success;
}
static _Bool CCConcurrentIndexMapAtomicAppendElementPtr(CCConcurrentIndexMap IndexMap, const void *Data, size_t *Index, size_t MaxCount, const void *New, void **State)
{
void *Element = *State;
if (!Element)
{
Element = CCMalloc(IndexMap->allocator, IndexMap->size, NULL, CC_DEFAULT_ERROR_CALLBACK);
if (!Element)
{
*Index = SIZE_MAX;
return FALSE;
}
memcpy(Element, New, IndexMap->size);
*State = Element;
}
for ( ; *Index < MaxCount; (*Index)++)
{
if (atomic_compare_exchange_strong_explicit(&((_Atomic(CCConcurrentIndexMapAtomicTypePtr)*)Data)[*Index], &((CCConcurrentIndexMapAtomicTypePtr){ .ptr = NULL}), ((CCConcurrentIndexMapAtomicTypePtr){ .ptr = Element }), memory_order_release, memory_order_relaxed)) return TRUE;
}
return FALSE;
}
#define CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(x) \
{ \
.size = sizeof(_Atomic(CCConcurrentIndexMapAtomicType##x)), \
.initElement = CCConcurrentIndexMapAtomicInitElement##x, \
.copyElement = CCConcurrentIndexMapAtomicCopyElement##x, \
.getElement = CCConcurrentIndexMapAtomicGetElement##x, \
.removeElement = CCConcurrentIndexMapAtomicRemoveElement##x, \
.destroyElement = CCConcurrentIndexMapAtomicDestroyElement##x, \
.setElement = CCConcurrentIndexMapAtomicSetElement##x, \
.compareAndSwapElement = CCConcurrentIndexMapAtomicCompareAndSwapElement##x, \
.appendElement = CCConcurrentIndexMapAtomicAppendElement##x \
}
static CCConcurrentIndexMapAtomicOperation CCConcurrentIndexMapAtomicOperations[] = {
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(1),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(2),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(3),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(4),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(5),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(6),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(7),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(8),
#if CC_HARDWARE_PTR_64
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(9),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(10),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(11),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(12),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(13),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(14),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(15),
CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(16)
#endif
};
static CCConcurrentIndexMapAtomicOperation CCConcurrentIndexMapAtomicPtrOperation = CC_CONCURRENT_INDEX_MAP_ATOMIC_OPERATION(Ptr);
static inline _Bool CCConcurrentIndexMapRequiresExternalStorage(size_t ElementSize)
{
return ElementSize >= (sizeof(CCConcurrentIndexMapAtomicOperations) / sizeof(typeof(*CCConcurrentIndexMapAtomicOperations)));
}
static inline const CCConcurrentIndexMapAtomicOperation *CCConcurrentIndexMapGetAtomicOperation(size_t ElementSize)
{
return CCConcurrentIndexMapRequiresExternalStorage(ElementSize) ? &CCConcurrentIndexMapAtomicPtrOperation : &CCConcurrentIndexMapAtomicOperations[ElementSize];
}
static inline size_t CCConcurrentIndexMapGetMaxCount(CCConcurrentIndexMap IndexMap, size_t Count)
{
return ((Count / IndexMap->chunkSize) + ((Count % IndexMap->chunkSize) || (!Count))) * IndexMap->chunkSize;
}
static void CCConcurrentIndexMapDestructor(CCConcurrentIndexMap IndexMap)
{
CCConcurrentIndexMapDataPointer Pointer = atomic_load(&IndexMap->pointer);
CCFree(Pointer.data);
CCConcurrentGarbageCollectorDestroy(IndexMap->gc);
}
static void CleanupElements(CCConcurrentIndexMapData *Data)
{
const CCConcurrentIndexMapAtomicOperation *Atomic = CCConcurrentIndexMapGetAtomicOperation(Data->indexMap->size);
for (size_t Loop = 0, MaxCount = CCConcurrentIndexMapGetMaxCount(Data->indexMap, atomic_load_explicit(&Data->count, memory_order_relaxed)); Loop < MaxCount; Loop++) Atomic->destroyElement(Data->indexMap, Data->buffer, Loop);
}
CCConcurrentIndexMap CCConcurrentIndexMapCreate(CCAllocatorType Allocator, size_t ElementSize, size_t ChunkSize, CCConcurrentGarbageCollector GC)
{
CCAssertLog(ChunkSize >= 1, "ChunkSize must be at least 1");
CCConcurrentIndexMap IndexMap = CCMalloc(Allocator, sizeof(CCConcurrentIndexMapInfo), NULL, CC_DEFAULT_ERROR_CALLBACK);
if (IndexMap)
{
const CCConcurrentIndexMapAtomicOperation *Atomic = CCConcurrentIndexMapGetAtomicOperation(ElementSize);
CCConcurrentIndexMapData *Data = CCMalloc(Allocator, sizeof(CCConcurrentIndexMapData) + (ChunkSize * Atomic->size), NULL, CC_DEFAULT_ERROR_CALLBACK);
Data->indexMap = IndexMap;
atomic_init(&Data->count, 0);
for (size_t Loop = 0; Loop < ChunkSize; Loop++) Atomic->initElement(IndexMap, Data->buffer, Loop, NULL);
CCMemorySetDestructor(Data, (CCMemoryDestructorCallback)CleanupElements);
*IndexMap = (CCConcurrentIndexMapInfo){
.allocator = Allocator,
.size = ElementSize,
.chunkSize = ChunkSize,
.pointer = ATOMIC_VAR_INIT(((CCConcurrentIndexMapDataPointer){
.data = Data,
#if CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
.mutate = 0,
.modify = 0
#else
.mutate = ATOMIC_VAR_INIT(0),
.modify = ATOMIC_VAR_INIT(0)
#endif
})),
.gc = GC
};
CCMemorySetDestructor(IndexMap, (CCMemoryDestructorCallback)CCConcurrentIndexMapDestructor);
}
return IndexMap;
}
void CCConcurrentIndexMapDestroy(CCConcurrentIndexMap IndexMap)
{
CCAssertLog(IndexMap, "IndexMap must not be null");
CCFree(IndexMap);
}
size_t CCConcurrentIndexMapGetCount(CCConcurrentIndexMap IndexMap)
{
CCAssertLog(IndexMap, "IndexMap must not be null");
CCConcurrentGarbageCollectorBegin(IndexMap->gc);
CCConcurrentIndexMapDataPointer Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
const size_t Count = atomic_load_explicit(&Pointer.data->count, memory_order_relaxed);
CCConcurrentGarbageCollectorEnd(IndexMap->gc);
return Count;
}
size_t CCConcurrentIndexMapGetElementSize(CCConcurrentIndexMap IndexMap)
{
CCAssertLog(IndexMap, "IndexMap must not be null");
return IndexMap->size;
}
_Bool CCConcurrentIndexMapGetElementAtIndex(CCConcurrentIndexMap IndexMap, size_t Index, void *Element)
{
CCAssertLog(IndexMap, "IndexMap must not be null");
CCConcurrentGarbageCollectorBegin(IndexMap->gc);
CCConcurrentIndexMapDataPointer Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
const size_t Count = atomic_load_explicit(&Pointer.data->count, memory_order_relaxed);
const size_t MaxCount = CCConcurrentIndexMapGetMaxCount(IndexMap, Count);
const _Bool Exists = Index < MaxCount ? CCConcurrentIndexMapGetAtomicOperation(IndexMap->size)->getElement(IndexMap, Pointer.data->buffer, Index, Element) : FALSE;
CCConcurrentGarbageCollectorEnd(IndexMap->gc);
return Exists;
}
_Bool CCConcurrentIndexMapReplaceElementAtIndex(CCConcurrentIndexMap IndexMap, size_t Index, const void *Element, void *ReplacedElement)
{
CCAssertLog(IndexMap, "IndexMap must not be null");
CCAssertLog(Element, "Element must not be null");
CCConcurrentGarbageCollectorBegin(IndexMap->gc);
#if CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
CCConcurrentIndexMapDataPointer Pointer;
do {
Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
} while (!atomic_compare_exchange_weak_explicit(&IndexMap->pointer, &Pointer, ((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify + 1, .mutate = Pointer.mutate, .data = Pointer.data }), memory_order_acquire, memory_order_relaxed));
#else
atomic_fetch_add_explicit(&((CCConcurrentIndexMapDataPointer*)&IndexMap->pointer)->modify, 1, memory_order_acquire);
CCConcurrentIndexMapDataPointer Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
#endif
const size_t Count = atomic_load_explicit(&Pointer.data->count, memory_order_relaxed);
const size_t MaxCount = CCConcurrentIndexMapGetMaxCount(IndexMap, Count);
const _Bool Exists = Index < MaxCount ? CCConcurrentIndexMapGetAtomicOperation(IndexMap->size)->setElement(IndexMap, Pointer.data->buffer, Index, Element, ReplacedElement) : FALSE;
#if CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
if (!atomic_compare_exchange_strong_explicit(&IndexMap->pointer, &((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify + 1, .mutate = Pointer.mutate, .data = Pointer.data }), ((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify, .mutate = Pointer.mutate + Exists, .data = Pointer.data }), memory_order_release, memory_order_relaxed))
{
do {
Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
} while (!atomic_compare_exchange_weak_explicit(&IndexMap->pointer, &Pointer, ((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify - 1, .mutate = Pointer.mutate + Exists, .data = Pointer.data }), memory_order_release, memory_order_relaxed));
}
#else
if (Exists) atomic_fetch_add_explicit(&((CCConcurrentIndexMapDataPointer*)&IndexMap->pointer)->mutate, 1, memory_order_relaxed);
atomic_fetch_sub_explicit(&((CCConcurrentIndexMapDataPointer*)&IndexMap->pointer)->modify, 1, memory_order_release);
#endif
CCConcurrentGarbageCollectorEnd(IndexMap->gc);
return Exists;
}
_Bool CCConcurrentIndexMapReplaceExactElementAtIndex(CCConcurrentIndexMap IndexMap, size_t Index, const void *Element, const void *Match)
{
CCAssertLog(IndexMap, "IndexMap must not be null");
CCAssertLog(Element, "Element must not be null");
CCAssertLog(Match, "Match must not be null");
CCConcurrentGarbageCollectorBegin(IndexMap->gc);
#if CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
CCConcurrentIndexMapDataPointer Pointer;
do {
Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
} while (!atomic_compare_exchange_weak_explicit(&IndexMap->pointer, &Pointer, ((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify + 1, .mutate = Pointer.mutate, .data = Pointer.data }), memory_order_acquire, memory_order_relaxed));
#else
atomic_fetch_add_explicit(&((CCConcurrentIndexMapDataPointer*)&IndexMap->pointer)->modify, 1, memory_order_acquire);
CCConcurrentIndexMapDataPointer Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
#endif
const size_t Count = atomic_load_explicit(&Pointer.data->count, memory_order_relaxed);
const size_t MaxCount = CCConcurrentIndexMapGetMaxCount(IndexMap, Count);
const _Bool Exists = Index < MaxCount ? CCConcurrentIndexMapGetAtomicOperation(IndexMap->size)->compareAndSwapElement(IndexMap, Pointer.data->buffer, Index, Element, Match) : FALSE;
#if CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
if (!atomic_compare_exchange_strong_explicit(&IndexMap->pointer, &((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify + 1, .mutate = Pointer.mutate, .data = Pointer.data }), ((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify, .mutate = Pointer.mutate + Exists, .data = Pointer.data }), memory_order_release, memory_order_relaxed))
{
do {
Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
} while (!atomic_compare_exchange_weak_explicit(&IndexMap->pointer, &Pointer, ((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify - 1, .mutate = Pointer.mutate + Exists, .data = Pointer.data }), memory_order_release, memory_order_relaxed));
}
#else
if (Exists) atomic_fetch_add_explicit(&((CCConcurrentIndexMapDataPointer*)&IndexMap->pointer)->mutate, 1, memory_order_relaxed);
atomic_fetch_sub_explicit(&((CCConcurrentIndexMapDataPointer*)&IndexMap->pointer)->modify, 1, memory_order_release);
#endif
CCConcurrentGarbageCollectorEnd(IndexMap->gc);
return Exists;
}
static CCConcurrentIndexMapData *CCConcurrentIndexMapResize(CCConcurrentIndexMap IndexMap, CCConcurrentIndexMapData *PrevData, size_t Count, size_t MaxCount, size_t SkipIndex, size_t ExtraIndex)
{
const CCConcurrentIndexMapAtomicOperation *Atomic = CCConcurrentIndexMapGetAtomicOperation(IndexMap->size);
CCConcurrentIndexMapData *Data = CCMalloc(IndexMap->allocator, sizeof(CCConcurrentIndexMapData) + (MaxCount * Atomic->size), NULL, CC_DEFAULT_ERROR_CALLBACK);
if (Data)
{
Data->indexMap = IndexMap;
atomic_init(&Data->count, Count + (SkipIndex == SIZE_MAX ? 1 : 0));
for (size_t Loop = 0; Loop < Count; Loop++) Atomic->copyElement(Data->buffer, (Loop < ExtraIndex ? Loop : Loop + 1), PrevData->buffer, (Loop < SkipIndex ? Loop : Loop + 1));
for (size_t Loop = (Count < ExtraIndex ? Count : Count + 1); Loop < MaxCount; Loop++) Atomic->initElement(IndexMap, Data->buffer, Loop, NULL);
CCMemorySetDestructor(Data, (CCMemoryDestructorCallback)CleanupElements);
}
return Data;
}
size_t CCConcurrentIndexMapAppendElement(CCConcurrentIndexMap IndexMap, const void *Element)
{
CCAssertLog(IndexMap, "IndexMap must not be null");
CCAssertLog(Element, "Element must not be null");
CCConcurrentGarbageCollectorBegin(IndexMap->gc);
const CCConcurrentIndexMapAtomicOperation *Atomic = CCConcurrentIndexMapGetAtomicOperation(IndexMap->size);
size_t Index;
void *State = NULL;
for ( ; ; )
{
#if CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
CCConcurrentIndexMapDataPointer Pointer;
do {
Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
} while (!atomic_compare_exchange_weak_explicit(&IndexMap->pointer, &Pointer, ((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify + 1, .mutate = Pointer.mutate, .data = Pointer.data }), memory_order_acquire, memory_order_relaxed));
#else
atomic_fetch_add_explicit(&((CCConcurrentIndexMapDataPointer*)&IndexMap->pointer)->modify, 1, memory_order_acquire);
CCConcurrentIndexMapDataPointer Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
#endif
Index = atomic_load_explicit(&Pointer.data->count, memory_order_relaxed);
const size_t MaxCount = CCConcurrentIndexMapGetMaxCount(IndexMap, Index);
const _Bool Success = Atomic->appendElement(IndexMap, Pointer.data->buffer, &Index, MaxCount, Element, &State);
if (Success)
{
atomic_fetch_add_explicit(&Pointer.data->count, 1, memory_order_relaxed);
#if !CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
atomic_fetch_add_explicit(&((CCConcurrentIndexMapDataPointer*)&IndexMap->pointer)->mutate, 1, memory_order_relaxed);
#endif
State = NULL;
}
#if CC_CONCURRENT_INDEX_MAP_STRICT_COMPLIANCE
if (!atomic_compare_exchange_strong_explicit(&IndexMap->pointer, &((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify + 1, .mutate = Pointer.mutate, .data = Pointer.data }), ((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify, .mutate = Pointer.mutate + Success, .data = Pointer.data }), memory_order_release, memory_order_relaxed))
{
do {
Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
} while (!atomic_compare_exchange_weak_explicit(&IndexMap->pointer, &Pointer, ((CCConcurrentIndexMapDataPointer){ .modify = Pointer.modify - 1, .mutate = Pointer.mutate + Success, .data = Pointer.data }), memory_order_release, memory_order_relaxed));
}
#else
atomic_fetch_sub_explicit(&((CCConcurrentIndexMapDataPointer*)&IndexMap->pointer)->modify, 1, memory_order_release);
#endif
if ((Success) || (CC_UNLIKELY(Index == SIZE_MAX))) break;
if (Index == atomic_load_explicit(&Pointer.data->count, memory_order_relaxed))
{
CCConcurrentIndexMapData *Data = CCConcurrentIndexMapResize(IndexMap, Pointer.data, Index, MaxCount + IndexMap->chunkSize, SIZE_MAX, SIZE_MAX);
if ((!Data) || (!Atomic->initElement(IndexMap, Data->buffer, Index, Element)))
{
if (Data)
{
for (size_t Loop = 0, Count = atomic_load_explicit(&Data->count, memory_order_relaxed); Loop < Count; Loop++) Atomic->removeElement(IndexMap, Data->buffer, Loop);
CCFree(Data);
}
Index = SIZE_MAX;
break;
}
else if (atomic_compare_exchange_strong_explicit(&IndexMap->pointer, &((CCConcurrentIndexMapDataPointer){ .modify = 0, .mutate = Pointer.mutate, .data = Pointer.data }), ((CCConcurrentIndexMapDataPointer){ .modify = 0, .mutate = Pointer.mutate + 1, .data = Data }), memory_order_release, memory_order_relaxed))
{
CCConcurrentGarbageCollectorManage(IndexMap->gc, Pointer.data, CCFree);
break;
}
else CCFree(Data);
}
}
CCConcurrentGarbageCollectorEnd(IndexMap->gc);
if (State) CCFree(State);
return Index;
}
_Bool CCConcurrentIndexMapRemoveElementAtIndex(CCConcurrentIndexMap IndexMap, size_t Index, void *RemovedElement)
{
CCAssertLog(IndexMap, "IndexMap must not be null");
CCConcurrentGarbageCollectorBegin(IndexMap->gc);
_Bool Removed = FALSE;
for (const CCConcurrentIndexMapAtomicOperation *Atomic = CCConcurrentIndexMapGetAtomicOperation(IndexMap->size); ; )
{
CCConcurrentIndexMapDataPointer Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
const size_t Count = atomic_load_explicit(&Pointer.data->count, memory_order_relaxed);
if ((Index >= Count) || (!Count)) break;
CCConcurrentIndexMapData *Data = CCConcurrentIndexMapResize(IndexMap, Pointer.data, Count - 1, CCConcurrentIndexMapGetMaxCount(IndexMap, Count - 1), Index, SIZE_MAX);
if (Data)
{
if (atomic_compare_exchange_strong_explicit(&IndexMap->pointer, &((CCConcurrentIndexMapDataPointer){ .modify = 0, .mutate = Pointer.mutate, .data = Pointer.data }), ((CCConcurrentIndexMapDataPointer){ .modify = 0, .mutate = Pointer.mutate + 1, .data = Data }), memory_order_release, memory_order_relaxed))
{
Atomic->getElement(IndexMap, Pointer.data->buffer, Index, RemovedElement);
CCConcurrentGarbageCollectorManage(IndexMap->gc, Pointer.data, CCFree);
Removed = TRUE;
break;
}
else CCFree(Data);
}
}
CCConcurrentGarbageCollectorEnd(IndexMap->gc);
return Removed;
}
_Bool CCConcurrentIndexMapInsertElementAtIndex(CCConcurrentIndexMap IndexMap, size_t Index, void *Element)
{
CCAssertLog(IndexMap, "IndexMap must not be null");
CCConcurrentGarbageCollectorBegin(IndexMap->gc);
_Bool Inserted = FALSE;
for (const CCConcurrentIndexMapAtomicOperation *Atomic = CCConcurrentIndexMapGetAtomicOperation(IndexMap->size); ; )
{
CCConcurrentIndexMapDataPointer Pointer = atomic_load_explicit(&IndexMap->pointer, memory_order_relaxed);
const size_t Count = atomic_load_explicit(&Pointer.data->count, memory_order_relaxed);
if ((Index >= Count) || (!Count)) break;
CCConcurrentIndexMapData *Data = CCConcurrentIndexMapResize(IndexMap, Pointer.data, Count, CCConcurrentIndexMapGetMaxCount(IndexMap, Count + 1), SIZE_MAX, Index);
if (Data)
{
if ((Atomic->initElement(IndexMap, Data->buffer, Index, Element)) && (atomic_compare_exchange_strong_explicit(&IndexMap->pointer, &((CCConcurrentIndexMapDataPointer){ .modify = 0, .mutate = Pointer.mutate, .data = Pointer.data }), ((CCConcurrentIndexMapDataPointer){ .modify = 0, .mutate = Pointer.mutate + 1, .data = Data }), memory_order_release, memory_order_relaxed)))
{
CCConcurrentGarbageCollectorManage(IndexMap->gc, Pointer.data, CCFree);
Inserted = TRUE;
break;
}
else CCFree(Data);
}
}
CCConcurrentGarbageCollectorEnd(IndexMap->gc);
return Inserted;
}