-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter.c
More file actions
1280 lines (1107 loc) · 45.7 KB
/
splinter.c
File metadata and controls
1280 lines (1107 loc) · 45.7 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
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2025 Tim Post
* License: Apache 2 (MIT available upon request to timthepost@protonmail.com)
*
* @file splinter.c
* @brief Main implementation of the libsplinter shared memory key-value store.
*
* libsplinter provides a high-performance, lock-free, shared-memory key-value
* store and message bus. It is designed for efficient inter-process
* communication (IPC), particularly for building process communities around
* local Large Language Model (LLM) runtimes.
*
* See docs for more.
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "config.h"
#include "splinter.h"
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef SPLINTER_NUMA_AFFINITY
#include <numa.h>
#include <numaif.h>
#endif // SPLINTER_NUMA_AFFINITY
/** @brief Base pointer to the memory-mapped region. */
static void *g_base = NULL;
/** @brief Total size of the memory-mapped region. */
static size_t g_total_sz = 0;
/** @brief Pointer to the header within the mapped region. */
static struct splinter_header *H;
/** @brief Pointer to the array of slots within the mapped region. */
static struct splinter_slot *S;
/** @brief Pointer to the start of the value storage area. */
static uint8_t *VALUES;
/**
* @brief Computes the 64-bit FNV-1a hash of a string.
* @param s The null-terminated string to hash.
* @return The 64-bit hash value.
*/
static uint64_t fnv1a(const char *s) {
uint64_t h = 14695981039346656037ULL;
for (; *s; ++s) h = (h ^ (unsigned char)*s) * 1099511628211ULL;
return h;
}
/**
* @brief Calculates the initial slot index for a given hash.
* @param hash The hash of the key.
* @param slots The total number of slots in the store.
* @return The calculated slot index.
*/
static inline size_t slot_idx(uint64_t hash, uint32_t slots) {
return (size_t)(hash % slots);
}
/**
* @brief Adds a specified number of milliseconds to a timespec struct.
* @param ts Pointer to the timespec struct to modify.
* @param ms The number of milliseconds to add.
*/
static void add_ms(struct timespec *ts, uint64_t ms) {
ts->tv_nsec += (ms % 1000) * NS_PER_MS;
ts->tv_sec += ms / 1000;
if (ts->tv_nsec >= 1000000000L) {
ts->tv_nsec -= 1000000000L;
ts->tv_sec += 1;
}
}
/**
* @brief Internal helper to memory-map a file descriptor and set up global pointers.
* @param fd The file descriptor to map.
* @param size The size of the region to map.
* @return 0 on success, -1 on failure.
*/
static int map_fd(int fd, size_t size) {
g_total_sz = size;
g_base = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (g_base == MAP_FAILED) return -1;
H = (struct splinter_header *)g_base;
S = (struct splinter_slot *)(H + 1);
VALUES = (uint8_t *)(S + H->slots);
return 0;
}
/**
* @brief Creates and initializes a new splinter store.
*
* The store is created as a shared memory object (`/dev/shm/...`) unless the
* `SPLINTER_PERSISTENT` macro is defined, in which case it's a regular file.
* The function fails if the store already exists.
*
* @param name_or_path The name of the shared memory object or path to the file.
* @param slots The total number of key-value slots to allocate.
* @param max_value_sz The maximum size in bytes for any single value.
* @return 0 on success, -1 on failure.
*/
int splinter_create(const char *name_or_path, size_t slots, size_t max_value_sz) {
int fd;
if (slots <= 0 || max_value_sz <= 0) {
errno = ENOTSUP;
return -2;
}
#ifdef SPLINTER_PERSISTENT
fd = open(name_or_path, O_RDWR | O_CREAT, 0666);
#else
// O_EXCL ensures this fails if the object already exists.
fd = shm_open(name_or_path, O_RDWR | O_CREAT | O_EXCL, 0666);
#endif
if (fd < 0) return -1;
size_t region_sz = slots * max_value_sz;
size_t total_sz = sizeof(struct splinter_header) + slots * sizeof(struct splinter_slot) + region_sz;
if (ftruncate(fd, (off_t)total_sz) != 0) return -1;
if (map_fd(fd, total_sz) != 0) return -1;
// Initialize header
H->magic = SPLINTER_MAGIC;
H->version = SPLINTER_VER;
H->slots = (uint32_t)slots;
H->max_val_sz = (uint32_t)max_value_sz;
H->val_sz = total_sz;
atomic_store_explicit(&H->val_brk, 0, memory_order_relaxed);
atomic_store_explicit(&H->epoch, 1, memory_order_relaxed);
atomic_store_explicit(&H->core_flags, 0, memory_order_relaxed);
atomic_store_explicit(&H->user_flags, 0, memory_order_relaxed);
atomic_store_explicit(&H->parse_failures, 0, memory_order_relaxed);
atomic_store_explicit(&H->last_failure_epoch, 0, memory_order_relaxed);
// Initialize slots
size_t i;
for (i = 0; i < slots; ++i) {
atomic_fetch_or(&S[i].type_flag, SPL_SLOT_DEFAULT_TYPE);
atomic_store_explicit(&S[i].hash, 0, memory_order_relaxed);
atomic_store_explicit(&S[i].epoch, 0, memory_order_relaxed);
atomic_store_explicit(&S[i].ctime, 0, memory_order_relaxed);
atomic_store_explicit(&S[i].atime, 0, memory_order_relaxed);
atomic_store_explicit(&S[i].user_flag, 0, memory_order_relaxed);
atomic_store_explicit(&S[i].watcher_mask, 0, memory_order_relaxed);
// we don't want brand new slots getting pulsed due to garbage in the bloom
// auto_scrub doesn't fully solve for this alone (and is optional,) so we
// do it here at the cost of a small loop.
for (int b = 0; b < 64; b++) {
atomic_store_explicit(&H->bloom_watches[b], 0xFF, memory_order_relaxed);
}
S[i].val_off = (uint32_t)(i * max_value_sz);
atomic_store_explicit(&S[i].val_len, 0, memory_order_relaxed);
S[i].key[0] = '\0';
}
return 0;
}
/**
* @brief Opens an existing splinter store.
*
* The function fails if the store does not exist or if the header metadata
* (magic number, version) is invalid.
*
* @param name_or_path The name of the shared memory object or path to the file.
* @return 0 on success, -1 on failure.
*/
int splinter_open(const char *name_or_path) {
int fd;
#ifdef SPLINTER_PERSISTENT
fd = open(name_or_path, O_RDWR);
#else
fd = shm_open(name_or_path, O_RDWR, 0666);
#endif
if (fd < 0) return -1;
struct stat st;
if (fstat(fd, &st) != 0) return -1;
if (map_fd(fd, (size_t)st.st_size) != 0) return -1;
// Validate header
if (H->magic != SPLINTER_MAGIC || H->version != SPLINTER_VER) return -1;
return 0;
}
#ifdef SPLINTER_NUMA_AFFINITY
/**
* @brief Opens the Splinter bus and binds it to a specific NUMA node.
* This ensures all memory pages for the VALUES arena and slots
* stay local to the target socket's memory controller.
*/
void* splinter_open_numa(const char *name, int target_node) {
if (numa_available() < 0) return NULL; // No NUMA support
// Open as usual
int fd = shm_open(name, O_RDWR, 0666);
struct stat st;
fstat(fd, &st);
void *addr = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
// Prepare the nodemask for mbind
unsigned long mask = (1UL << target_node);
unsigned long maxnode = numa_max_node() + 1;
// Bind the memory region to the specific physical node
// MPOL_BIND: Forces allocation strictly on these nodes
// MPOL_MF_STRICT: Fail if pages are already elsewhere
if (mbind(addr, st.st_size, MPOL_BIND, &mask, maxnode, MPOL_MF_STRICT | MPOL_MF_MOVE) != 0) {
perror("mbind failed");
}
return addr;
}
#endif //SPLINTER_NUMA_AFFINITY
/**
* @brief Creates a new splinter store, or opens it if it already exists.
*
* Tries to create first, and on failure, tries to open.
*
* @param name_or_path The name of the shared memory object or path to the file.
* @param slots The total number of key-value slots if creating.
* @param max_value_sz The maximum value size in bytes if creating.
* @return 0 on success, -1 on failure.
*/
int splinter_create_or_open(const char *name_or_path, size_t slots, size_t max_value_sz) {
int ret = splinter_create(name_or_path, slots, max_value_sz);
return (ret == 0 ? ret : splinter_open(name_or_path));
}
/**
* @brief Opens an existing splinter store, or creates it if it does not exist.
*
* Tries to open first, and on failure, tries to create.
*
* @param name_or_path The name of the shared memory object or path to the file.
* @param slots The total number of key-value slots if creating.
* @param max_value_sz The maximum value size in bytes if creating.
* @return 0 on success, -1 on failure.
*/
int splinter_open_or_create(const char *name_or_path, size_t slots, size_t max_value_sz) {
int ret = splinter_open(name_or_path);
return (ret == 0 ? ret : splinter_create(name_or_path, slots, max_value_sz));
}
/**
* @brief Sets the auto scrub atomic feature flag of the current bus (0 or 1)
* @return -2 if the bus is unavailable, 0 otherwise.
*/
int splinter_set_av(unsigned int mode) {
if (!H) return -2;
if (mode == 1) {
splinter_config_set(H, SPL_SYS_AUTO_SCRUB);
return 0;
} else if (mode == 0) {
// Clear both flags simultaneously
atomic_fetch_and(&H->core_flags, ~(SPL_SYS_AUTO_SCRUB | SPL_SYS_HYBRID_SCRUB));
return 0;
}
// The only reason this fails is mode being unexpected.
errno = ENOTSUP;
return -1;
}
/**
* @brief Get the auto scrub atomic feature flag of the current bus, as int.
* @return -2 if the bus is unavailable, value of the (unsigned) flag otherwise.
*/
int splinter_get_av(void) {
if (!H) return -2;
return (int) splinter_config_test(H, SPL_SYS_AUTO_SCRUB);
}
/**
* @brief Just like splinter_set_av(), but also engages the hybrid bit atomically / simultaneously
* @return int
*/
int splinter_set_hybrid_av(void) {
if (!H) return -2;
// Set both bits simultaneously. This opens the gate AND
// selects the 64-byte mop in one atomic cycle.
atomic_fetch_or(&H->core_flags, SPL_SYS_AUTO_SCRUB | SPL_SYS_HYBRID_SCRUB); //
return 0;
}
/**
* @brief Check if the bus has hybrid scrub enabled
* @return int
*/
int splinter_get_hybrid_av(void) {
if (!H) return -2;
return (int) splinter_config_test(H, SPL_SYS_HYBRID_SCRUB);
}
/**
* @brief Performs a high-efficiency hygiene sweep.
*/
void splinter_purge(void) {
if (!H || !S || !VALUES) return;
for (uint32_t i = 0; i < H->slots; ++i) {
struct splinter_slot *slot = &S[i];
// 1. Snapshot the state to avoid 'squatting' on a busy slot
uint64_t e = atomic_load_explicit(&slot->epoch, memory_order_acquire);
if (e & 1ull) continue; // Strike was already in progress
// 2. Acquire the seqlock
if (!atomic_compare_exchange_strong(&slot->epoch, &e, e + 1)) continue;
uint32_t len = atomic_load_explicit(&slot->val_len, memory_order_relaxed);
uint8_t *dst = VALUES + slot->val_off;
// 3. The Sweep: If active, mop the tail; if empty, boil the slot.
if (atomic_load_explicit(&slot->hash, memory_order_acquire) == 0) {
memset(dst, 0, H->max_val_sz);
} else if (len < H->max_val_sz) {
// Only mop the 'dirty' remainder beyond current data
memset(dst + len, 0, H->max_val_sz - len);
}
// 4. Release and return to silence
atomic_fetch_add_explicit(&slot->epoch, 1, memory_order_release);
}
}
/**
* @brief Closes the splinter store and unmaps the shared memory region.
*/
void splinter_close(void) {
if (g_base) munmap(g_base, g_total_sz);
g_base = NULL; H = NULL; S = NULL; VALUES = NULL; g_total_sz = 0;
}
/**
* @brief "unsets" a key (delete).
*
* This function atomically marks the slot as free. With seqlock semantics,
* if the slot is observed in the middle of a write (odd epoch), it returns
* -1 with errno = EAGAIN so the caller can retry.
*
* @param key The null-terminated key string.
* @return length of value deleted on success,
* -1 if key not found,
* -2 if store or key are invalid,
* -1 with errno = EAGAIN if writer in progress.
*/
int splinter_unset(const char *key) {
if (!H || !key) return -2;
uint64_t h = fnv1a(key);
size_t idx = slot_idx(h, H->slots);
size_t i;
for (i = 0; i < H->slots; ++i) {
struct splinter_slot *slot = &S[(idx + i) % H->slots];
uint64_t slot_hash = atomic_load_explicit(&slot->hash, memory_order_acquire);
if (slot_hash == h && strncmp(slot->key, key, SPLINTER_KEY_MAX) == 0) {
uint64_t start_epoch = atomic_load_explicit(&slot->epoch, memory_order_acquire);
if (start_epoch & 1) {
// Writer in progress
errno = EAGAIN;
return -1;
}
int ret = (int)atomic_load_explicit(&slot->val_len, memory_order_acquire);
atomic_store_explicit(&slot->hash, 0, memory_order_release);
if (splinter_config_test(H, SPL_SYS_AUTO_SCRUB)) {
memset(VALUES + slot->val_off, 0, H->max_val_sz);
memset(slot->key, 0, SPLINTER_KEY_MAX);
} else {
slot->key[0] = '\0';
}
// we first zero out the type flag, then set it to the default.
atomic_store_explicit(&slot->type_flag, 0, memory_order_release);
atomic_fetch_or(&slot->type_flag, SPL_SLOT_DEFAULT_TYPE);
atomic_store_explicit(&slot->epoch, 0, memory_order_release);
atomic_store_explicit(&slot->val_len, 0, memory_order_release);
atomic_store_explicit(&slot->ctime, 0, memory_order_release);
atomic_store_explicit(&slot->atime, 0, memory_order_release);
atomic_store_explicit(&slot->user_flag, 0, memory_order_release);
atomic_store_explicit(&slot->watcher_mask, 0, memory_order_release);
atomic_store_explicit(&slot->bloom, 0, memory_order_release);
// Increment slot epoch to mark the change (leave even)
atomic_fetch_add_explicit(&slot->epoch, 2, memory_order_release);
return ret;
}
}
return -1; // didn't find it
}
/**
* @brief Sets or updates a key-value pair in the store.
*
* This function uses linear probing to resolve hash collisions. It searches for
* an empty slot or a slot with a matching key starting from the key's
* natural hash position. If the store is full, the operation will fail.
*
* @param key The null-terminated key string.
* @param val Pointer to the value data.
* @param len The length of the value data. Must not exceed `max_val_sz`.
* @return 0 on success, -1 on failure (e.g., store is full, len is too large).
*/
int splinter_set(const char *key, const void *val, size_t len) {
if (!H || !key) return -1;
if (len == 0 || len > H->max_val_sz) return -1;
uint64_t h = fnv1a(key);
size_t idx = slot_idx(h, H->slots);
const size_t arena_sz = (size_t)H->slots * (size_t)H->max_val_sz;
for (size_t i = 0; i < H->slots; ++i) {
struct splinter_slot *slot = &S[(idx + i) % H->slots];
uint64_t slot_hash = atomic_load_explicit(&slot->hash, memory_order_acquire);
if (slot_hash == 0 || (slot_hash == h && strncmp(slot->key, key, SPLINTER_KEY_MAX) == 0)) {
uint64_t e = atomic_load_explicit(&slot->epoch, memory_order_relaxed);
if (e & 1ull) continue;
if (!atomic_compare_exchange_weak_explicit(&slot->epoch, &e, e + 1,
memory_order_acq_rel, memory_order_relaxed)) {
continue;
}
if ((size_t)slot->val_off >= arena_sz || (size_t)slot->val_off + len > arena_sz) {
atomic_fetch_add_explicit(&slot->epoch, 1, memory_order_release);
return -1;
}
uint8_t *dst = (uint8_t *)VALUES + slot->val_off;
if (splinter_config_test(H, SPL_SYS_AUTO_SCRUB)) {
// Determine if we do a full scrub or a fast cache-line scrub
if (splinter_config_test(H, SPL_SYS_HYBRID_SCRUB)) {
// Round up to next 64-byte boundary: (len + 63) & ~63
size_t scrub_len = (len + 63) & ~63;
if (scrub_len > H->max_val_sz) scrub_len = H->max_val_sz;
memset(dst, 0, scrub_len);
} else {
// Full boil mode: I wish hotels could do this!
memset(dst, 0, H->max_val_sz);
}
}
memcpy(dst, val, len);
atomic_store_explicit(&slot->val_len, (uint32_t)len, memory_order_release);
// Update key and publish
slot->key[0] = '\0';
strncpy(slot->key, key, SPLINTER_KEY_MAX - 1);
slot->key[SPLINTER_KEY_MAX - 1] = '\0';
atomic_thread_fence(memory_order_release);
atomic_store_explicit(&slot->hash, h, memory_order_release);
atomic_fetch_add_explicit(&slot->epoch, 1, memory_order_release);
splinter_pulse_watchers(slot);
atomic_fetch_add_explicit(&H->epoch, 1, memory_order_relaxed);
return 0;
}
}
return -1;
}
/**
* @brief Retrieves the value associated with a key (seqlock aware).
*
* @param key The null-terminated key string.
* @param buf The buffer to copy the value data into. Can be NULL to query size.
* @param buf_sz The size of the provided buffer.
* @param out_sz Pointer to a size_t to store the value's actual length. Can be NULL.
* @return 0 on success, -1 on failure. On retry condition, returns -1 and sets
* errno = EAGAIN. If the buffer is too small, returns -1 and sets errno = EMSGSIZE.
*/
int splinter_get(const char *key, void *buf, size_t buf_sz, size_t *out_sz) {
if (!H || !key) return -1;
uint64_t h = fnv1a(key);
size_t idx = slot_idx(h, H->slots);
size_t i;
for (i = 0; i < H->slots; ++i) {
struct splinter_slot *slot = &S[(idx + i) % H->slots];
if (atomic_load_explicit(&slot->hash, memory_order_acquire) == h &&
strncmp(slot->key, key, SPLINTER_KEY_MAX) == 0) {
uint64_t start = atomic_load_explicit(&slot->epoch, memory_order_acquire);
if (start & 1) {
// writer in progress
errno = EAGAIN;
return -1;
}
atomic_thread_fence(memory_order_acquire);
/* load length atomically */
size_t len = (size_t)atomic_load_explicit(&slot->val_len, memory_order_acquire);
if (out_sz) *out_sz = len;
if (buf) {
if (buf_sz < len) {
errno = EMSGSIZE;
return -1;
}
memcpy(buf, VALUES + slot->val_off, len);
}
uint64_t end = atomic_load_explicit(&slot->epoch, memory_order_acquire);
if (start == end && !(end & 1)) {
// consistent snapshot
return 0;
}
// inconsistent snapshot, ask caller to retry
errno = EAGAIN;
return -1;
}
}
return -1; // Not found
}
/**
* @brief Lists all keys currently in the store.
*
* @param out_keys An array of `char*` to be filled with pointers to the keys
* within the shared memory. These pointers are only valid as
* long as the store is open.
* @param max_keys The maximum number of keys to write to `out_keys`.
* @param out_count Pointer to a size_t to store the number of keys found.
* @return 0 on success, -1 on failure.
*/
int splinter_list(char **out_keys, size_t max_keys, size_t *out_count) {
if (!H || !out_keys || !out_count) return -1;
size_t count = 0, i;
for (i = 0; i < H->slots && count < max_keys; ++i) {
// A non-zero hash and value length indicates a valid, active key.
if (atomic_load_explicit(&S[i].hash, memory_order_acquire) &&
atomic_load_explicit(&S[i].val_len, memory_order_acquire) > 0) {
out_keys[count++] = S[i].key;
}
}
*out_count = count;
return 0;
}
/**
* @brief Waits for a key's value to be changed (updated).
*
* This function provides a publish-subscribe mechanism. It blocks until the
* per-slot epoch for the given key is incremented by a `splinter_set` call.
*
* With seqlock semantics, if the slot is observed in the middle of a write
* (odd epoch), this call returns immediately with errno = EAGAIN so the
* caller can retry cleanly.
*
* @param key The key to monitor for changes.
* @param timeout_ms The maximum time to wait in milliseconds.
* @return 0 if the value changed, -1 on timeout, -1 with errno = EAGAIN
* if a write was observed in progress.
*/
int splinter_poll(const char *key, uint64_t timeout_ms) {
if (!H || !key) return -1;
uint64_t h = fnv1a(key);
size_t idx = slot_idx(h, H->slots);
struct splinter_slot *slot = NULL;
// Find the slot corresponding to the key
size_t i;
for (i = 0; i < H->slots; ++i) {
struct splinter_slot *s = &S[(idx + i) % H->slots];
if (atomic_load_explicit(&s->hash, memory_order_acquire) == h &&
strncmp(s->key, key, SPLINTER_KEY_MAX) == 0) {
slot = s;
break;
}
}
if (!slot) return -1; // Key does not exist.
uint64_t start_epoch = atomic_load_explicit(&slot->epoch, memory_order_acquire);
if (start_epoch & 1) {
// Writer in progress
errno = EAGAIN;
return -2;
}
struct timespec deadline;
clock_gettime(CLOCK_REALTIME, &deadline);
add_ms(&deadline, timeout_ms);
struct timespec sleep_ts = {0, 10 * NS_PER_MS}; // 10ms sleep
while (1) {
uint64_t cur_epoch = atomic_load_explicit(&slot->epoch, memory_order_acquire);
if (cur_epoch & 1) {
errno = EAGAIN;
return -2; // Writer still in progress
}
if (cur_epoch != start_epoch) {
return 0; // Value changed
}
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
if ((now.tv_sec > deadline.tv_sec) ||
(now.tv_sec == deadline.tv_sec && now.tv_nsec >= deadline.tv_nsec)) {
errno = ETIMEDOUT;
return -2;
}
nanosleep(&sleep_ts, NULL);
}
}
/**
* @brief Copy the current atomic Splinter header structure into a corresponding
* non-atomic client version.
* @param snapshot A splinter_header_snaphshot_t structure to receive the values.
* @return void
*/
int splinter_get_header_snapshot(splinter_header_snapshot_t *snapshot) {
if (!H) return -1;
snapshot->magic = H->magic;
snapshot->version = H->version;
snapshot->slots = H->slots;
snapshot->max_val_sz = H->max_val_sz;
snapshot->core_flags = atomic_load_explicit(&H->core_flags, memory_order_acquire);
snapshot->user_flags = atomic_load_explicit(&H->user_flags, memory_order_acquire);
snapshot->epoch = atomic_load_explicit(&H->epoch, memory_order_acquire);
snapshot->parse_failures = atomic_load_explicit(&H->parse_failures, memory_order_relaxed);
snapshot->last_failure_epoch = atomic_load_explicit(&H->last_failure_epoch, memory_order_relaxed);
return 0;
}
/**
* @brief Copy the current atomic Splinter slot header to a corresponding client
* structure.
* @param snapshot A splinter_slot_snaphshot_t structure to receive the values.
* @return -1 on failure, 0 on success.
*/
int splinter_get_slot_snapshot(const char *key, splinter_slot_snapshot_t *snapshot) {
if (!H || !key || !snapshot) return -1;
uint64_t h = fnv1a(key);
size_t idx = slot_idx(h, H->slots), i = 0;
for (i = 0; i < H->slots; ++i) {
struct splinter_slot *slot = &S[(idx + i) % H->slots];
if (atomic_load_explicit(&slot->hash, memory_order_acquire) == h &&
strncmp(slot->key, key, SPLINTER_KEY_MAX) == 0) {
uint64_t start = 0, end = 0;
do {
start = atomic_load_explicit(&slot->epoch, memory_order_acquire);
if (start & 1) {
// Writer active, spin briefly or return EAGAIN for the test to retry
continue;
}
snapshot->hash = h;
snapshot->epoch = start;
snapshot->val_off = slot->val_off;
snapshot->val_len = atomic_load_explicit(&slot->val_len, memory_order_relaxed);
snapshot->type_flag = atomic_load_explicit(&slot->type_flag, memory_order_acquire);
snapshot->user_flag = atomic_load_explicit(&slot->user_flag, memory_order_acquire);
snapshot->ctime = atomic_load_explicit(&slot->ctime, memory_order_acquire);
snapshot->atime = atomic_load_explicit(&slot->atime, memory_order_acquire);
strncpy(snapshot->key, slot->key, SPLINTER_KEY_MAX);
#ifdef SPLINTER_EMBEDDINGS
// Copy the large vector (the high-risk area for tearing)
memcpy(snapshot->embedding, slot->embedding, sizeof(float) * SPLINTER_EMBED_DIM);
#endif
atomic_thread_fence(memory_order_acquire);
end = atomic_load_explicit(&slot->epoch, memory_order_acquire);
} while (start != end); // Loop until we get a clean, non-torn read
return 0;
}
}
return -1;
}
#ifdef SPLINTER_EMBEDDINGS
int splinter_set_embedding(const char *key, const float *vec) {
if (!H || !key || !vec) return -1;
uint64_t h = fnv1a(key);
size_t idx = slot_idx(h, H->slots);
for (size_t i = 0; i < H->slots; ++i) {
struct splinter_slot *slot = &S[(idx + i) % H->slots];
if (atomic_load_explicit(&slot->hash, memory_order_acquire) == h &&
strncmp(slot->key, key, SPLINTER_KEY_MAX) == 0) {
uint64_t e = atomic_load_explicit(&slot->epoch, memory_order_relaxed);
if (e & 1ull) return -1; // Writer already active
uint64_t want = e + 1;
if (!atomic_compare_exchange_strong(&slot->epoch, &e, want)) return -1;
// Perform the copy into the slot
memcpy(slot->embedding, vec, sizeof(float) * SPLINTER_EMBED_DIM);
/* RELEASE FENCE: Ensures all bytes of the embedding are written
to memory before the epoch is set back to an even number. */
atomic_thread_fence(memory_order_release);
atomic_fetch_add_explicit(&slot->epoch, 1, memory_order_release);
// Global epoch update for bus tracking
atomic_fetch_add_explicit(&H->epoch, 1, memory_order_relaxed);
return 0;
}
}
return -1;
}
int splinter_get_embedding(const char *key, float *embedding_out) {
if (!H || !key || !embedding_out) return -1;
uint64_t h = fnv1a(key);
size_t idx = slot_idx(h, H->slots);
for (size_t i = 0; i < H->slots; ++i) {
struct splinter_slot *slot = &S[(idx + i) % H->slots];
if (atomic_load_explicit(&slot->hash, memory_order_acquire) == h &&
strncmp(slot->key, key, SPLINTER_KEY_MAX) == 0) {
uint64_t start = atomic_load_explicit(&slot->epoch, memory_order_acquire);
if (start & 1) { errno = EAGAIN; return -1; }
atomic_thread_fence(memory_order_acquire);
memcpy(embedding_out, slot->embedding, sizeof(float) * SPLINTER_EMBED_DIM);
uint64_t end = atomic_load_explicit(&slot->epoch, memory_order_acquire);
if (start == end) return 0;
errno = EAGAIN;
return -1;
}
}
return -1;
}
#endif // SPLINTER_EMBEDDINGS
/**
* @brief Set a bus configuration value
* @param hdr: a splinter bus header structure
* @param mask: bitmask to apply
*/
void splinter_config_set(struct splinter_header *hdr, uint8_t mask) {
atomic_fetch_or(&hdr->core_flags, mask);
}
/**
* @brief Clear a bus configuration value
* @param hdr: a splinter bus header structure
* @param mask: bitmask to clear
*/
void splinter_config_clear(struct splinter_header *hdr, uint8_t mask) {
atomic_fetch_and(&hdr->core_flags, ~mask);
}
/**
* @brief Test a bus configuration value
* @param hdr: a splinter bus header structure
* @param mask: bitmask to test
*/
int splinter_config_test(struct splinter_header *hdr, uint8_t mask) {
return (atomic_load(&hdr->core_flags) & mask) != 0;
}
/**
* @brief Snapshot a bus configuration
* @param hdr: a splinter bus header structure
*/
uint8_t splinter_config_snapshot(struct splinter_header *hdr) {
return atomic_load(&hdr->core_flags);
}
/**
* @brief Set a user slot flag
* @param slot Splinter slot structure
* @param mask bitmask to set
*/
void splinter_slot_usr_set(struct splinter_slot *slot, uint16_t mask) {
atomic_fetch_or(&slot->user_flag, mask);
}
/**
* @brief Clear a user slot flag
* @param slot Splinter slot structure
* @param mask bitmask to clear
*/
void splinter_slot_usr_clear(struct splinter_slot *slot, uint16_t mask) {
atomic_fetch_and(&slot->user_flag, ~mask);
}
/**
* @brief Test a user slot flag
* @param slot Splinter slot structure
* @param mask bitmask to test
*/
int splinter_slot_usr_test(struct splinter_slot *slot, uint16_t mask) {
return (atomic_load(&slot->user_flag) & mask) != 0;
}
/**
* @brief Get a user slot flag snapshot
* @param slot Splinter slot structure
*/
uint16_t splinter_slot_usr_snapshot(struct splinter_slot *slot) {
return atomic_load(&slot->user_flag);
}
/**
* @brief Name (declare intent to) a type fo a slot
* @param key Name of the key to change
* @param mask Splinter type bitmask to apply (e.g SPL_SLOT_TYPE_BIGUINT)
* @return -1 or on error (sets errno), 0 on success
*/
int splinter_set_named_type(const char *key, uint16_t mask) {
if (!H || !key) return -1;
uint64_t h = fnv1a(key);
size_t idx = slot_idx(h, H->slots);
for (size_t i = 0; i < H->slots; ++i) {
struct splinter_slot *slot = &S[(idx + i) % H->slots];
if (atomic_load_explicit(&slot->hash, memory_order_acquire) == h &&
strncmp(slot->key, key, SPLINTER_KEY_MAX) == 0) {
// 1. Writer Check & Lock
uint64_t e = atomic_load_explicit(&slot->epoch, memory_order_relaxed);
if (e & 1) { errno = EAGAIN; return -1; }
if (!atomic_compare_exchange_strong(&slot->epoch, &e, e + 1)) {
errno = EAGAIN; return -1;
}
atomic_thread_fence(memory_order_acquire);
// 2. Expansion Logic for BIGUINT
uint32_t current_len = atomic_load(&slot->val_len);
if ((mask & SPL_SLOT_TYPE_BIGUINT) && current_len < 8) {
uint32_t new_off = atomic_fetch_add(&H->val_brk, 8);
if (new_off + 8 > H->val_sz) {
atomic_fetch_add(&slot->epoch, 1);
errno = ENOMEM; return -1;
}
uint8_t *old_ptr = VALUES + slot->val_off;
uint64_t converted_val = 0;
// Check if it's a numeric string that needs parsing
if (current_len > 0 && old_ptr[0] >= '0' && old_ptr[0] <= '9') {
char tmp_buf[16] = {0};
memcpy(tmp_buf, old_ptr, (current_len < 15) ? current_len : 15);
converted_val = strtoull(tmp_buf, NULL, 0);
} else {
// Fallback: just move the raw bytes
memcpy(&converted_val, old_ptr, (current_len < 8) ? current_len : 8);
}
uint64_t *new_ptr = (uint64_t *)(VALUES + new_off);
*new_ptr = converted_val; // Now it's a real 64-bit integer
slot->val_off = new_off;
atomic_store_explicit(&slot->val_len, 8, memory_order_relaxed);
}
// 3. Apply Type and Unlock
atomic_store_explicit(&slot->type_flag, mask, memory_order_release);
atomic_fetch_add_explicit(&slot->epoch, 1, memory_order_release);
// Global epoch update
atomic_fetch_add(&H->epoch, 1);
return 0;
}
}
errno = ENOENT;
return -1;
}
/**
* @brief Update a slot's ctime / atime
* @param key Name of the key to change
* @param mode (SPL_TIME_CTIME or SPL_TIME_ATIME)
* @param epoch client-supplied timestamp
* @param offset value to subtract from epoch due to update-after-write
* @return -1/-2 or on error (sets errno), 0 on success
*/
int splinter_set_slot_time(const char *key, unsigned short mode, uint64_t epoch, size_t offset) {
if (!H || !key) return -1;
uint64_t h = fnv1a(key);
size_t idx = slot_idx(h, H->slots), i;
for (i = 0; i < H->slots; ++i) {
struct splinter_slot *slot = &S[(idx + i) % H->slots];
if (atomic_load_explicit(&slot->hash, memory_order_acquire) == h &&
strncmp(slot->key, key, SPLINTER_KEY_MAX) == 0) {
uint64_t start = atomic_load_explicit(&slot->epoch, memory_order_acquire);
if (start & 1) {
// writer in progress
errno = EAGAIN;
return -1;
}
atomic_thread_fence(memory_order_acquire);
switch (mode) {
case SPL_TIME_CTIME:
atomic_store_explicit(&slot->ctime, epoch - offset, memory_order_release);
return 0;
case SPL_TIME_ATIME:
atomic_store_explicit(&slot->atime, epoch - offset, memory_order_release);
return 0;
default:
errno = ENOTSUP;
return -2;
}
}
}
errno = ENOENT;
return -1;
}
/**
* @brief Bitwise & arithmetic ops on keys named as big unsigned
* @param key Name of the key to operate on
* @param op Operation you want to do
* @param mask What you want to do it with
* @return 0 on success, -1 / -2 on internal / caller errors respectively
*/
int splinter_integer_op(const char *key, splinter_integer_op_t op, const void *mask) {
if (!H || !key) return -2;
uint64_t h = fnv1a(key);
size_t idx = slot_idx(h, H->slots);
uint64_t m64 = 0;
// uint64_t m64 = *(const uint64_t *)mask;
if (mask) {
memcpy(&m64, mask, sizeof(uint64_t));
}
// proactive fence for weak-memory hardware (e.g. chromebook consumer grade)
atomic_thread_fence(memory_order_acquire);
for (size_t i = 0; i < H->slots; ++i) {
struct splinter_slot *slot = &S[(idx + i) % H->slots];
if (atomic_load_explicit(&slot->hash, memory_order_acquire) == h &&
strncmp(slot->key, key, SPLINTER_KEY_MAX) == 0) {
// We expect a named type biguint
uint8_t type = atomic_load_explicit(&slot->type_flag, memory_order_relaxed);
if (!(type & SPL_SLOT_TYPE_BIGUINT)) {
errno = EPROTOTYPE;
return -1;
}
uint64_t e = atomic_load_explicit(&slot->epoch, memory_order_relaxed);
if (e & 1ull) { errno = EAGAIN; return -1; }
if (!atomic_compare_exchange_strong_explicit(&slot->epoch, &e, e + 1,
memory_order_acquire,
memory_order_relaxed)) {
errno = EAGAIN;
return -1;
}
// fast-track for the 64 bit lane, smaller lengths would require
// different handling (possible future TODO if it's absolutely
// ever needed)
uint64_t *val = (uint64_t *)(VALUES + slot->val_off);
switch (op) {
case SPL_OP_OR: *val |= m64; break;
case SPL_OP_AND: *val &= m64; break;
case SPL_OP_XOR: *val ^= m64; break;
case SPL_OP_NOT: *val = ~(*val); break;
case SPL_OP_INC: *val += m64; break;
case SPL_OP_DEC: *val -= m64; break;
}
// now make visible
atomic_fetch_add_explicit(&slot->epoch, 1, memory_order_release);
atomic_fetch_add_explicit(&H->epoch, 1, memory_order_relaxed);