| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
/* |
|
2
|
|
|
|
|
|
|
* buf_generic.h — Macro-template for shared-memory typed buffers. |
|
3
|
|
|
|
|
|
|
* |
|
4
|
|
|
|
|
|
|
* Before including, define: |
|
5
|
|
|
|
|
|
|
* BUF_PREFIX — function prefix (e.g., buf_i64) |
|
6
|
|
|
|
|
|
|
* BUF_ELEM_TYPE — C element type (e.g., int64_t) |
|
7
|
|
|
|
|
|
|
* BUF_VARIANT_ID — unique integer for header validation |
|
8
|
|
|
|
|
|
|
* BUF_ELEM_SIZE — sizeof(BUF_ELEM_TYPE) or fixed string length |
|
9
|
|
|
|
|
|
|
* |
|
10
|
|
|
|
|
|
|
* Optional: |
|
11
|
|
|
|
|
|
|
* BUF_HAS_COUNTERS — generate incr/decr/cas (integer types only) |
|
12
|
|
|
|
|
|
|
* BUF_IS_FLOAT — element is float/double (affects SV conversion) |
|
13
|
|
|
|
|
|
|
* BUF_IS_FIXEDSTR — element is fixed-length char array |
|
14
|
|
|
|
|
|
|
*/ |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
/* ================================================================ |
|
17
|
|
|
|
|
|
|
* Part 1: Shared definitions (included once) |
|
18
|
|
|
|
|
|
|
* ================================================================ */ |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
#ifndef BUF_DEFS_H |
|
21
|
|
|
|
|
|
|
#define BUF_DEFS_H |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
#include |
|
24
|
|
|
|
|
|
|
#include |
|
25
|
|
|
|
|
|
|
#include |
|
26
|
|
|
|
|
|
|
#include |
|
27
|
|
|
|
|
|
|
#include |
|
28
|
|
|
|
|
|
|
#include |
|
29
|
|
|
|
|
|
|
#include |
|
30
|
|
|
|
|
|
|
#include |
|
31
|
|
|
|
|
|
|
#include |
|
32
|
|
|
|
|
|
|
#include |
|
33
|
|
|
|
|
|
|
#include |
|
34
|
|
|
|
|
|
|
#include |
|
35
|
|
|
|
|
|
|
#include |
|
36
|
|
|
|
|
|
|
#include |
|
37
|
|
|
|
|
|
|
#include |
|
38
|
|
|
|
|
|
|
#include |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
/* ---- Constants ---- */ |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
#define BUF_MAGIC 0x42554631U /* "BUF1" */ |
|
43
|
|
|
|
|
|
|
#define BUF_VERSION 2 /* v2: reader-slot table for dead-reader rwlock recovery */ |
|
44
|
|
|
|
|
|
|
#define BUF_ERR_BUFLEN 256 |
|
45
|
|
|
|
|
|
|
#define BUF_READER_SLOTS 1024 /* per-process reader-counter mirror */ |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
/* ---- Per-process reader-slot table (in shared memory) ---- |
|
48
|
|
|
|
|
|
|
* Mirrors each process's contribution to the global rwlock counters so a |
|
49
|
|
|
|
|
|
|
* dead reader's contribution can be reclaimed on writer-lock timeout. */ |
|
50
|
|
|
|
|
|
|
typedef struct { |
|
51
|
|
|
|
|
|
|
uint32_t pid; /* owning PID, 0 = free */ |
|
52
|
|
|
|
|
|
|
uint32_t subcount; /* this process's rwlock reader contribution */ |
|
53
|
|
|
|
|
|
|
uint32_t waiters_parked; /* this process's contribution to rwlock_waiters */ |
|
54
|
|
|
|
|
|
|
uint32_t writers_parked; /* this process's contribution to rwlock_writers_waiting */ |
|
55
|
|
|
|
|
|
|
} BufReaderSlot; |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
/* ---- Shared memory header (128 bytes, 2 cache lines, in mmap) ---- */ |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L |
|
60
|
|
|
|
|
|
|
#define BUF_STATIC_ASSERT(cond, msg) _Static_assert(cond, msg) |
|
61
|
|
|
|
|
|
|
#else |
|
62
|
|
|
|
|
|
|
#define BUF_STATIC_ASSERT(cond, msg) |
|
63
|
|
|
|
|
|
|
#endif |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
typedef struct { |
|
66
|
|
|
|
|
|
|
/* ---- Cache line 0 (0-63): immutable after create ---- */ |
|
67
|
|
|
|
|
|
|
uint32_t magic; /* 0 */ |
|
68
|
|
|
|
|
|
|
uint32_t version; /* 4 */ |
|
69
|
|
|
|
|
|
|
uint32_t variant_id; /* 8 */ |
|
70
|
|
|
|
|
|
|
uint32_t elem_size; /* 12 */ |
|
71
|
|
|
|
|
|
|
uint64_t capacity; /* 16: number of elements */ |
|
72
|
|
|
|
|
|
|
uint64_t total_size; /* 24: total mmap size */ |
|
73
|
|
|
|
|
|
|
uint64_t data_off; /* 32: offset to data array */ |
|
74
|
|
|
|
|
|
|
uint64_t reader_slots_off;/* 40: offset to BufReaderSlot[BUF_READER_SLOTS] */ |
|
75
|
|
|
|
|
|
|
uint8_t _reserved0[16]; /* 48-63 */ |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
/* ---- Cache line 1 (64-127): seqlock + rwlock + mutable state ---- */ |
|
78
|
|
|
|
|
|
|
uint32_t seq; /* 64: seqlock counter, odd = writer active */ |
|
79
|
|
|
|
|
|
|
uint32_t rwlock; /* 68: 0=unlocked, readers=1..0x7FFFFFFF, writer=0x80000000|pid */ |
|
80
|
|
|
|
|
|
|
uint32_t rwlock_waiters; /* 72: wake-target counter (readers+writers) */ |
|
81
|
|
|
|
|
|
|
uint32_t stat_recoveries; /* 76 */ |
|
82
|
|
|
|
|
|
|
uint32_t rwlock_writers_waiting; /* 80: reader yield signal (writers only) */ |
|
83
|
|
|
|
|
|
|
uint32_t _pad2; /* 84 */ |
|
84
|
|
|
|
|
|
|
uint64_t _reserved1[5]; /* 88-127 */ |
|
85
|
|
|
|
|
|
|
} BufHeader; |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
BUF_STATIC_ASSERT(sizeof(BufHeader) == 128, "BufHeader must be exactly 128 bytes (2 cache lines)"); |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
/* ---- Process-local handle ---- */ |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
typedef struct { |
|
92
|
|
|
|
|
|
|
BufHeader *hdr; |
|
93
|
|
|
|
|
|
|
void *data; /* pointer to element array in mmap */ |
|
94
|
|
|
|
|
|
|
BufReaderSlot *reader_slots; /* in mmap, BUF_READER_SLOTS entries */ |
|
95
|
|
|
|
|
|
|
size_t mmap_size; |
|
96
|
|
|
|
|
|
|
char *path; /* backing file path (strdup'd, NULL for anon) */ |
|
97
|
|
|
|
|
|
|
int fd; /* kept open for memfd, -1 otherwise */ |
|
98
|
|
|
|
|
|
|
int efd; /* eventfd for notifications, -1 if none */ |
|
99
|
|
|
|
|
|
|
uint32_t my_slot_idx; /* UINT32_MAX = unclaimed; per-process slot index */ |
|
100
|
|
|
|
|
|
|
uint32_t cached_pid; /* getpid() at claim time */ |
|
101
|
|
|
|
|
|
|
uint32_t cached_fork_gen; /* fork-generation at claim time */ |
|
102
|
|
|
|
|
|
|
uint8_t wr_locked; /* process-local: 1 if lock_wr is held */ |
|
103
|
|
|
|
|
|
|
uint8_t efd_owned; /* 1 if we created the eventfd (close on destroy) */ |
|
104
|
|
|
|
|
|
|
} BufHandle; |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
/* ---- Futex-based read-write lock ---- */ |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
#define BUF_RWLOCK_SPIN_LIMIT 32 |
|
109
|
|
|
|
|
|
|
#define BUF_LOCK_TIMEOUT_SEC 2 |
|
110
|
|
|
|
|
|
|
|
|
111
|
170
|
|
|
|
|
|
static inline void buf_spin_pause(void) { |
|
112
|
|
|
|
|
|
|
#if defined(__x86_64__) || defined(__i386__) |
|
113
|
170
|
|
|
|
|
|
__asm__ volatile("pause" ::: "memory"); |
|
114
|
|
|
|
|
|
|
#elif defined(__aarch64__) |
|
115
|
|
|
|
|
|
|
__asm__ volatile("yield" ::: "memory"); |
|
116
|
|
|
|
|
|
|
#else |
|
117
|
|
|
|
|
|
|
__asm__ volatile("" ::: "memory"); |
|
118
|
|
|
|
|
|
|
#endif |
|
119
|
170
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
#define BUF_RWLOCK_WRITER_BIT 0x80000000U |
|
122
|
|
|
|
|
|
|
#define BUF_RWLOCK_PID_MASK 0x7FFFFFFFU |
|
123
|
|
|
|
|
|
|
#define BUF_RWLOCK_WR(pid) (BUF_RWLOCK_WRITER_BIT | ((uint32_t)(pid) & BUF_RWLOCK_PID_MASK)) |
|
124
|
|
|
|
|
|
|
|
|
125
|
12
|
|
|
|
|
|
static inline int buf_pid_alive(uint32_t pid) { |
|
126
|
12
|
50
|
|
|
|
|
if (pid == 0) return 1; |
|
127
|
12
|
100
|
|
|
|
|
return !(kill((pid_t)pid, 0) == -1 && errno == ESRCH); |
|
|
|
50
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
/* ---- Per-process slot lifecycle (dead-reader recovery) ---- |
|
131
|
|
|
|
|
|
|
* Each process claims one BufReaderSlot lazily on first lock op so that |
|
132
|
|
|
|
|
|
|
* its contribution to the shared rwlock counter can be reclaimed by other |
|
133
|
|
|
|
|
|
|
* processes if it dies (SIGKILL'd worker no longer pins the counter). */ |
|
134
|
|
|
|
|
|
|
static uint32_t buf_fork_gen = 0; |
|
135
|
|
|
|
|
|
|
static pthread_once_t buf_atfork_once = PTHREAD_ONCE_INIT; |
|
136
|
2
|
|
|
|
|
|
static void buf_on_fork_child(void) { |
|
137
|
2
|
|
|
|
|
|
__atomic_add_fetch(&buf_fork_gen, 1, __ATOMIC_RELAXED); |
|
138
|
2
|
|
|
|
|
|
} |
|
139
|
11
|
|
|
|
|
|
static void buf_atfork_init(void) { |
|
140
|
11
|
|
|
|
|
|
pthread_atfork(NULL, NULL, buf_on_fork_child); |
|
141
|
11
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
557
|
|
|
|
|
|
static inline void buf_claim_reader_slot(BufHandle *h) { |
|
144
|
557
|
50
|
|
|
|
|
if (!h->reader_slots) return; |
|
145
|
557
|
|
|
|
|
|
pthread_once(&buf_atfork_once, buf_atfork_init); |
|
146
|
557
|
|
|
|
|
|
uint32_t cur_gen = __atomic_load_n(&buf_fork_gen, __ATOMIC_RELAXED); |
|
147
|
557
|
50
|
|
|
|
|
if (h->cached_fork_gen != cur_gen) { |
|
148
|
0
|
|
|
|
|
|
h->cached_fork_gen = cur_gen; |
|
149
|
0
|
|
|
|
|
|
h->my_slot_idx = UINT32_MAX; |
|
150
|
|
|
|
|
|
|
} |
|
151
|
557
|
100
|
|
|
|
|
if (h->my_slot_idx != UINT32_MAX) return; |
|
152
|
28
|
|
|
|
|
|
uint32_t now_pid = (uint32_t)getpid(); |
|
153
|
28
|
|
|
|
|
|
h->cached_pid = now_pid; |
|
154
|
28
|
|
|
|
|
|
uint32_t start = now_pid % BUF_READER_SLOTS; |
|
155
|
29
|
50
|
|
|
|
|
for (uint32_t i = 0; i < BUF_READER_SLOTS; i++) { |
|
156
|
29
|
|
|
|
|
|
uint32_t s = (start + i) % BUF_READER_SLOTS; |
|
157
|
29
|
|
|
|
|
|
uint32_t expected = 0; |
|
158
|
29
|
100
|
|
|
|
|
if (__atomic_compare_exchange_n(&h->reader_slots[s].pid, |
|
159
|
|
|
|
|
|
|
&expected, now_pid, 0, |
|
160
|
|
|
|
|
|
|
__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) { |
|
161
|
28
|
|
|
|
|
|
__atomic_store_n(&h->reader_slots[s].subcount, 0, __ATOMIC_RELAXED); |
|
162
|
28
|
|
|
|
|
|
__atomic_store_n(&h->reader_slots[s].waiters_parked, 0, __ATOMIC_RELAXED); |
|
163
|
28
|
|
|
|
|
|
__atomic_store_n(&h->reader_slots[s].writers_parked, 0, __ATOMIC_RELAXED); |
|
164
|
28
|
|
|
|
|
|
h->my_slot_idx = s; |
|
165
|
28
|
|
|
|
|
|
return; |
|
166
|
|
|
|
|
|
|
} |
|
167
|
|
|
|
|
|
|
} |
|
168
|
|
|
|
|
|
|
/* Slot table full — silently skip tracking; recovery falls back to |
|
169
|
|
|
|
|
|
|
* the slow per-op timeout drain. */ |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
/* Atomically subtract `sub` from a counter, capped at 0 (never underflows). */ |
|
173
|
2
|
|
|
|
|
|
static inline void buf_atomic_sub_cap(uint32_t *p, uint32_t sub) { |
|
174
|
2
|
50
|
|
|
|
|
if (!sub) return; |
|
175
|
2
|
|
|
|
|
|
uint32_t cur = __atomic_load_n(p, __ATOMIC_RELAXED); |
|
176
|
0
|
|
|
|
|
|
for (;;) { |
|
177
|
2
|
50
|
|
|
|
|
uint32_t want = (cur > sub) ? cur - sub : 0; |
|
178
|
2
|
50
|
|
|
|
|
if (__atomic_compare_exchange_n(p, &cur, want, |
|
179
|
|
|
|
|
|
|
1, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) |
|
180
|
2
|
|
|
|
|
|
return; |
|
181
|
|
|
|
|
|
|
} |
|
182
|
|
|
|
|
|
|
} |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
/* Try to claim a dead slot (CAS pid → 0) and drain its parked-waiter |
|
185
|
|
|
|
|
|
|
* contributions to the global counters. Returns 1 if drained, 0 if lost |
|
186
|
|
|
|
|
|
|
* the CAS race or had no contributions. ACQ_REL syncs us with the dead |
|
187
|
|
|
|
|
|
|
* process's RELAXED stores to mirror fields on weakly-ordered archs. */ |
|
188
|
5
|
|
|
|
|
|
static inline int buf_drain_dead_slot(BufHandle *h, uint32_t i, uint32_t pid) { |
|
189
|
5
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
190
|
5
|
|
|
|
|
|
uint32_t expected = pid; |
|
191
|
5
|
50
|
|
|
|
|
if (!__atomic_compare_exchange_n(&h->reader_slots[i].pid, &expected, 0, |
|
192
|
|
|
|
|
|
|
0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED)) |
|
193
|
0
|
|
|
|
|
|
return 0; |
|
194
|
5
|
|
|
|
|
|
uint32_t wp = __atomic_load_n(&h->reader_slots[i].waiters_parked, __ATOMIC_RELAXED); |
|
195
|
5
|
|
|
|
|
|
uint32_t writp = __atomic_load_n(&h->reader_slots[i].writers_parked, __ATOMIC_RELAXED); |
|
196
|
5
|
|
|
|
|
|
int drained = 0; |
|
197
|
5
|
100
|
|
|
|
|
if (wp) { buf_atomic_sub_cap(&hdr->rwlock_waiters, wp); drained = 1; } |
|
198
|
5
|
100
|
|
|
|
|
if (writp) { buf_atomic_sub_cap(&hdr->rwlock_writers_waiting, writp); drained = 1; } |
|
199
|
|
|
|
|
|
|
/* Don't zero slot fields — buf_claim_reader_slot zeros them on the |
|
200
|
|
|
|
|
|
|
* next claim; zeroing here can race a new claimant's increments. */ |
|
201
|
5
|
|
|
|
|
|
return drained; |
|
202
|
|
|
|
|
|
|
} |
|
203
|
|
|
|
|
|
|
|
|
204
|
2
|
|
|
|
|
|
static inline void buf_recover_dead_readers(BufHandle *h) { |
|
205
|
2
|
50
|
|
|
|
|
if (!h->reader_slots) return; |
|
206
|
2
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
207
|
2
|
|
|
|
|
|
int any_live_reader = 0; |
|
208
|
2
|
|
|
|
|
|
int found_dead_reader = 0; |
|
209
|
2
|
|
|
|
|
|
int any_recovery = 0; |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
/* Pass 1: scan; classify; immediate-wipe dead slots with sc==0 (no |
|
212
|
|
|
|
|
|
|
* rwlock contribution to lose). Defer wiping dead-with-sc>0 slots |
|
213
|
|
|
|
|
|
|
* until force-reset can fire — otherwise we'd lose the only record |
|
214
|
|
|
|
|
|
|
* of the orphan rwlock contribution while a live reader is present. */ |
|
215
|
2050
|
100
|
|
|
|
|
for (uint32_t i = 0; i < BUF_READER_SLOTS; i++) { |
|
216
|
2048
|
|
|
|
|
|
uint32_t pid = __atomic_load_n(&h->reader_slots[i].pid, __ATOMIC_ACQUIRE); |
|
217
|
2048
|
100
|
|
|
|
|
if (pid == 0) continue; |
|
218
|
7
|
|
|
|
|
|
uint32_t sc = __atomic_load_n(&h->reader_slots[i].subcount, __ATOMIC_RELAXED); |
|
219
|
7
|
100
|
|
|
|
|
if (buf_pid_alive(pid)) { |
|
220
|
2
|
100
|
|
|
|
|
if (sc > 0) any_live_reader = 1; |
|
221
|
2
|
|
|
|
|
|
continue; |
|
222
|
|
|
|
|
|
|
} |
|
223
|
5
|
100
|
|
|
|
|
if (sc > 0) { found_dead_reader = 1; continue; } |
|
224
|
1
|
50
|
|
|
|
|
if (buf_drain_dead_slot(h, i, pid)) any_recovery = 1; |
|
225
|
|
|
|
|
|
|
} |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
/* Pass 2: only if force-reset will fire. Issue the rwlock CAS first |
|
228
|
|
|
|
|
|
|
* to keep the race window with new readers narrow, then wipe the |
|
229
|
|
|
|
|
|
|
* deferred dead slots. */ |
|
230
|
2
|
100
|
|
|
|
|
if (found_dead_reader && !any_live_reader) { |
|
|
|
50
|
|
|
|
|
|
|
231
|
1
|
|
|
|
|
|
uint32_t cur = __atomic_load_n(&hdr->rwlock, __ATOMIC_RELAXED); |
|
232
|
1
|
50
|
|
|
|
|
if (cur > 0 && cur < BUF_RWLOCK_WRITER_BIT) { |
|
|
|
50
|
|
|
|
|
|
|
233
|
1
|
50
|
|
|
|
|
if (__atomic_compare_exchange_n(&hdr->rwlock, &cur, 0, |
|
234
|
|
|
|
|
|
|
0, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) { |
|
235
|
1
|
|
|
|
|
|
any_recovery = 1; |
|
236
|
1
|
50
|
|
|
|
|
if (__atomic_load_n(&hdr->rwlock_waiters, __ATOMIC_RELAXED) > 0) |
|
237
|
0
|
|
|
|
|
|
syscall(SYS_futex, &hdr->rwlock, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); |
|
238
|
|
|
|
|
|
|
} |
|
239
|
|
|
|
|
|
|
} |
|
240
|
1025
|
100
|
|
|
|
|
for (uint32_t i = 0; i < BUF_READER_SLOTS; i++) { |
|
241
|
1024
|
|
|
|
|
|
uint32_t pid = __atomic_load_n(&h->reader_slots[i].pid, __ATOMIC_ACQUIRE); |
|
242
|
1024
|
100
|
|
|
|
|
if (pid == 0) continue; |
|
243
|
5
|
100
|
|
|
|
|
if (buf_pid_alive(pid)) continue; |
|
244
|
4
|
50
|
|
|
|
|
if (buf_drain_dead_slot(h, i, pid)) any_recovery = 1; |
|
245
|
|
|
|
|
|
|
} |
|
246
|
|
|
|
|
|
|
} |
|
247
|
2
|
50
|
|
|
|
|
if (any_recovery) |
|
248
|
2
|
|
|
|
|
|
__atomic_add_fetch(&hdr->stat_recoveries, 1, __ATOMIC_RELAXED); |
|
249
|
|
|
|
|
|
|
} |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
/* Park/unpark helpers — keep global rwlock_waiters/writers_waiting and |
|
252
|
|
|
|
|
|
|
* per-slot mirror counters in sync so recovery can drain them. */ |
|
253
|
1
|
|
|
|
|
|
static inline void buf_park_reader(BufHandle *h) { |
|
254
|
1
|
|
|
|
|
|
__atomic_add_fetch(&h->hdr->rwlock_waiters, 1, __ATOMIC_RELAXED); |
|
255
|
1
|
50
|
|
|
|
|
if (h->my_slot_idx != UINT32_MAX) |
|
256
|
1
|
|
|
|
|
|
__atomic_add_fetch(&h->reader_slots[h->my_slot_idx].waiters_parked, 1, __ATOMIC_RELAXED); |
|
257
|
1
|
|
|
|
|
|
} |
|
258
|
1
|
|
|
|
|
|
static inline void buf_unpark_reader(BufHandle *h) { |
|
259
|
1
|
|
|
|
|
|
__atomic_sub_fetch(&h->hdr->rwlock_waiters, 1, __ATOMIC_RELAXED); |
|
260
|
1
|
50
|
|
|
|
|
if (h->my_slot_idx != UINT32_MAX) |
|
261
|
1
|
|
|
|
|
|
__atomic_sub_fetch(&h->reader_slots[h->my_slot_idx].waiters_parked, 1, __ATOMIC_RELAXED); |
|
262
|
1
|
|
|
|
|
|
} |
|
263
|
1
|
|
|
|
|
|
static inline void buf_park_writer(BufHandle *h) { |
|
264
|
1
|
|
|
|
|
|
__atomic_add_fetch(&h->hdr->rwlock_waiters, 1, __ATOMIC_RELAXED); |
|
265
|
1
|
|
|
|
|
|
__atomic_add_fetch(&h->hdr->rwlock_writers_waiting, 1, __ATOMIC_RELAXED); |
|
266
|
1
|
50
|
|
|
|
|
if (h->my_slot_idx != UINT32_MAX) { |
|
267
|
1
|
|
|
|
|
|
__atomic_add_fetch(&h->reader_slots[h->my_slot_idx].waiters_parked, 1, __ATOMIC_RELAXED); |
|
268
|
1
|
|
|
|
|
|
__atomic_add_fetch(&h->reader_slots[h->my_slot_idx].writers_parked, 1, __ATOMIC_RELAXED); |
|
269
|
|
|
|
|
|
|
} |
|
270
|
1
|
|
|
|
|
|
} |
|
271
|
1
|
|
|
|
|
|
static inline void buf_unpark_writer(BufHandle *h) { |
|
272
|
1
|
|
|
|
|
|
__atomic_sub_fetch(&h->hdr->rwlock_waiters, 1, __ATOMIC_RELAXED); |
|
273
|
1
|
|
|
|
|
|
__atomic_sub_fetch(&h->hdr->rwlock_writers_waiting, 1, __ATOMIC_RELAXED); |
|
274
|
1
|
50
|
|
|
|
|
if (h->my_slot_idx != UINT32_MAX) { |
|
275
|
1
|
|
|
|
|
|
__atomic_sub_fetch(&h->reader_slots[h->my_slot_idx].waiters_parked, 1, __ATOMIC_RELAXED); |
|
276
|
1
|
|
|
|
|
|
__atomic_sub_fetch(&h->reader_slots[h->my_slot_idx].writers_parked, 1, __ATOMIC_RELAXED); |
|
277
|
|
|
|
|
|
|
} |
|
278
|
1
|
|
|
|
|
|
} |
|
279
|
|
|
|
|
|
|
|
|
280
|
0
|
|
|
|
|
|
static inline void buf_recover_stale_lock(BufHeader *hdr, uint32_t observed_rwlock) { |
|
281
|
0
|
|
|
|
|
|
uint32_t mypid = BUF_RWLOCK_WR((uint32_t)getpid()); |
|
282
|
0
|
0
|
|
|
|
|
if (!__atomic_compare_exchange_n(&hdr->rwlock, &observed_rwlock, |
|
283
|
|
|
|
|
|
|
mypid, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) |
|
284
|
0
|
|
|
|
|
|
return; |
|
285
|
0
|
|
|
|
|
|
uint32_t seq = __atomic_load_n(&hdr->seq, __ATOMIC_ACQUIRE); |
|
286
|
0
|
0
|
|
|
|
|
if (seq & 1) |
|
287
|
0
|
|
|
|
|
|
__atomic_store_n(&hdr->seq, seq + 1, __ATOMIC_RELEASE); |
|
288
|
0
|
|
|
|
|
|
__atomic_add_fetch(&hdr->stat_recoveries, 1, __ATOMIC_RELAXED); |
|
289
|
0
|
|
|
|
|
|
__atomic_store_n(&hdr->rwlock, 0, __ATOMIC_RELEASE); |
|
290
|
0
|
0
|
|
|
|
|
if (__atomic_load_n(&hdr->rwlock_waiters, __ATOMIC_RELAXED) > 0) |
|
291
|
0
|
|
|
|
|
|
syscall(SYS_futex, &hdr->rwlock, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); |
|
292
|
|
|
|
|
|
|
} |
|
293
|
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
static const struct timespec buf_lock_timeout = { BUF_LOCK_TIMEOUT_SEC, 0 }; |
|
295
|
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
/* Recovery dispatcher: if a writer is dead, force-reset the lock word; |
|
297
|
|
|
|
|
|
|
* otherwise scan reader slots for dead readers and drain their stuck |
|
298
|
|
|
|
|
|
|
* contributions to the rwlock and waiter counters. Reload the lock |
|
299
|
|
|
|
|
|
|
* value here (rather than trusting a stale snapshot from the futex |
|
300
|
|
|
|
|
|
|
* caller) so that (a) a writer that died after our futex_wait started |
|
301
|
|
|
|
|
|
|
* is detected on the same timeout, and (b) phantom waiter/writers_waiting |
|
302
|
|
|
|
|
|
|
* contributions left by a dead parked writer are drained even when the |
|
303
|
|
|
|
|
|
|
* lock word itself is now 0. */ |
|
304
|
2
|
|
|
|
|
|
static inline void buf_recover_after_timeout(BufHandle *h) { |
|
305
|
2
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
306
|
2
|
|
|
|
|
|
uint32_t val = __atomic_load_n(&hdr->rwlock, __ATOMIC_RELAXED); |
|
307
|
2
|
50
|
|
|
|
|
if (val >= BUF_RWLOCK_WRITER_BIT) { |
|
308
|
0
|
|
|
|
|
|
uint32_t pid = val & BUF_RWLOCK_PID_MASK; |
|
309
|
0
|
0
|
|
|
|
|
if (!buf_pid_alive(pid)) |
|
310
|
0
|
|
|
|
|
|
buf_recover_stale_lock(hdr, val); |
|
311
|
|
|
|
|
|
|
} else { |
|
312
|
2
|
|
|
|
|
|
buf_recover_dead_readers(h); |
|
313
|
|
|
|
|
|
|
} |
|
314
|
2
|
|
|
|
|
|
} |
|
315
|
|
|
|
|
|
|
|
|
316
|
2
|
|
|
|
|
|
static inline void buf_rwlock_rdlock(BufHandle *h) { |
|
317
|
2
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
318
|
2
|
|
|
|
|
|
buf_claim_reader_slot(h); |
|
319
|
2
|
|
|
|
|
|
uint32_t *lock = &hdr->rwlock; |
|
320
|
2
|
|
|
|
|
|
uint32_t *writers_waiting = &hdr->rwlock_writers_waiting; |
|
321
|
|
|
|
|
|
|
/* Bump per-process subcount BEFORE attempting the rwlock CAS so a |
|
322
|
|
|
|
|
|
|
* concurrent recovery scan sees us as a live in-flight reader. */ |
|
323
|
2
|
50
|
|
|
|
|
if (h->my_slot_idx != UINT32_MAX) |
|
324
|
2
|
|
|
|
|
|
__atomic_add_fetch(&h->reader_slots[h->my_slot_idx].subcount, 1, __ATOMIC_RELAXED); |
|
325
|
35
|
|
|
|
|
|
for (int spin = 0; ; spin++) { |
|
326
|
35
|
|
|
|
|
|
uint32_t cur = __atomic_load_n(lock, __ATOMIC_RELAXED); |
|
327
|
35
|
50
|
|
|
|
|
if (cur > 0 && cur < BUF_RWLOCK_WRITER_BIT) { |
|
|
|
0
|
|
|
|
|
|
|
328
|
0
|
0
|
|
|
|
|
if (__atomic_compare_exchange_n(lock, &cur, cur + 1, |
|
329
|
|
|
|
|
|
|
1, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) |
|
330
|
2
|
|
|
|
|
|
return; |
|
331
|
35
|
50
|
|
|
|
|
} else if (cur == 0 && !__atomic_load_n(writers_waiting, __ATOMIC_RELAXED)) { |
|
|
|
100
|
|
|
|
|
|
|
332
|
2
|
50
|
|
|
|
|
if (__atomic_compare_exchange_n(lock, &cur, 1, |
|
333
|
|
|
|
|
|
|
1, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) |
|
334
|
2
|
|
|
|
|
|
return; |
|
335
|
|
|
|
|
|
|
} |
|
336
|
33
|
100
|
|
|
|
|
if (__builtin_expect(spin < BUF_RWLOCK_SPIN_LIMIT, 1)) { |
|
337
|
32
|
|
|
|
|
|
buf_spin_pause(); |
|
338
|
33
|
|
|
|
|
|
continue; |
|
339
|
|
|
|
|
|
|
} |
|
340
|
1
|
|
|
|
|
|
buf_park_reader(h); |
|
341
|
1
|
|
|
|
|
|
cur = __atomic_load_n(lock, __ATOMIC_RELAXED); |
|
342
|
1
|
50
|
|
|
|
|
if (cur >= BUF_RWLOCK_WRITER_BIT || cur == 0) { |
|
|
|
50
|
|
|
|
|
|
|
343
|
1
|
|
|
|
|
|
long rc = syscall(SYS_futex, lock, FUTEX_WAIT, cur, |
|
344
|
|
|
|
|
|
|
&buf_lock_timeout, NULL, 0); |
|
345
|
1
|
50
|
|
|
|
|
if (rc == -1 && errno == ETIMEDOUT) { |
|
|
|
50
|
|
|
|
|
|
|
346
|
1
|
|
|
|
|
|
buf_unpark_reader(h); |
|
347
|
1
|
|
|
|
|
|
buf_recover_after_timeout(h); |
|
348
|
1
|
|
|
|
|
|
spin = 0; |
|
349
|
1
|
|
|
|
|
|
continue; |
|
350
|
|
|
|
|
|
|
} |
|
351
|
|
|
|
|
|
|
} |
|
352
|
0
|
|
|
|
|
|
buf_unpark_reader(h); |
|
353
|
0
|
|
|
|
|
|
spin = 0; |
|
354
|
|
|
|
|
|
|
} |
|
355
|
|
|
|
|
|
|
} |
|
356
|
|
|
|
|
|
|
|
|
357
|
2
|
|
|
|
|
|
static inline void buf_rwlock_rdunlock(BufHandle *h) { |
|
358
|
|
|
|
|
|
|
/* Decrement rwlock BEFORE subcount: a concurrent recovery scan that |
|
359
|
|
|
|
|
|
|
* sees subcount > 0 with our (live) PID will (correctly) treat us as |
|
360
|
|
|
|
|
|
|
* an in-flight reader and skip force-reset. */ |
|
361
|
2
|
|
|
|
|
|
uint32_t prev = __atomic_sub_fetch(&h->hdr->rwlock, 1, __ATOMIC_RELEASE); |
|
362
|
2
|
50
|
|
|
|
|
if (h->my_slot_idx != UINT32_MAX) |
|
363
|
2
|
|
|
|
|
|
__atomic_sub_fetch(&h->reader_slots[h->my_slot_idx].subcount, 1, __ATOMIC_RELAXED); |
|
364
|
2
|
50
|
|
|
|
|
if (prev == 0 && __atomic_load_n(&h->hdr->rwlock_waiters, __ATOMIC_RELAXED) > 0) |
|
|
|
50
|
|
|
|
|
|
|
365
|
0
|
|
|
|
|
|
syscall(SYS_futex, &h->hdr->rwlock, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); |
|
366
|
2
|
|
|
|
|
|
} |
|
367
|
|
|
|
|
|
|
|
|
368
|
555
|
|
|
|
|
|
static inline void buf_rwlock_wrlock(BufHandle *h) { |
|
369
|
555
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
370
|
555
|
|
|
|
|
|
buf_claim_reader_slot(h); |
|
371
|
555
|
|
|
|
|
|
uint32_t *lock = &hdr->rwlock; |
|
372
|
555
|
|
|
|
|
|
uint32_t mypid = BUF_RWLOCK_WR((uint32_t)getpid()); |
|
373
|
694
|
|
|
|
|
|
for (int spin = 0; ; spin++) { |
|
374
|
694
|
|
|
|
|
|
uint32_t expected = 0; |
|
375
|
694
|
100
|
|
|
|
|
if (__atomic_compare_exchange_n(lock, &expected, mypid, |
|
376
|
|
|
|
|
|
|
1, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) |
|
377
|
555
|
|
|
|
|
|
return; |
|
378
|
139
|
100
|
|
|
|
|
if (__builtin_expect(spin < BUF_RWLOCK_SPIN_LIMIT, 1)) { |
|
379
|
138
|
|
|
|
|
|
buf_spin_pause(); |
|
380
|
139
|
|
|
|
|
|
continue; |
|
381
|
|
|
|
|
|
|
} |
|
382
|
1
|
|
|
|
|
|
buf_park_writer(h); |
|
383
|
1
|
|
|
|
|
|
uint32_t cur = __atomic_load_n(lock, __ATOMIC_RELAXED); |
|
384
|
1
|
50
|
|
|
|
|
if (cur != 0) { |
|
385
|
1
|
|
|
|
|
|
long rc = syscall(SYS_futex, lock, FUTEX_WAIT, cur, |
|
386
|
|
|
|
|
|
|
&buf_lock_timeout, NULL, 0); |
|
387
|
1
|
50
|
|
|
|
|
if (rc == -1 && errno == ETIMEDOUT) { |
|
|
|
50
|
|
|
|
|
|
|
388
|
1
|
|
|
|
|
|
buf_unpark_writer(h); |
|
389
|
1
|
|
|
|
|
|
buf_recover_after_timeout(h); |
|
390
|
1
|
|
|
|
|
|
spin = 0; |
|
391
|
1
|
|
|
|
|
|
continue; |
|
392
|
|
|
|
|
|
|
} |
|
393
|
|
|
|
|
|
|
} |
|
394
|
0
|
|
|
|
|
|
buf_unpark_writer(h); |
|
395
|
0
|
|
|
|
|
|
spin = 0; |
|
396
|
|
|
|
|
|
|
} |
|
397
|
|
|
|
|
|
|
} |
|
398
|
|
|
|
|
|
|
|
|
399
|
555
|
|
|
|
|
|
static inline void buf_rwlock_wrunlock(BufHandle *h) { |
|
400
|
555
|
|
|
|
|
|
__atomic_store_n(&h->hdr->rwlock, 0, __ATOMIC_RELEASE); |
|
401
|
555
|
100
|
|
|
|
|
if (__atomic_load_n(&h->hdr->rwlock_waiters, __ATOMIC_RELAXED) > 0) |
|
402
|
11
|
|
|
|
|
|
syscall(SYS_futex, &h->hdr->rwlock, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); |
|
403
|
555
|
|
|
|
|
|
} |
|
404
|
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
/* ---- Seqlock ---- */ |
|
406
|
|
|
|
|
|
|
|
|
407
|
34
|
|
|
|
|
|
static inline uint32_t buf_seqlock_read_begin(BufHeader *hdr) { |
|
408
|
34
|
|
|
|
|
|
int spin = 0; |
|
409
|
0
|
|
|
|
|
|
for (;;) { |
|
410
|
34
|
|
|
|
|
|
uint32_t s = __atomic_load_n(&hdr->seq, __ATOMIC_ACQUIRE); |
|
411
|
34
|
50
|
|
|
|
|
if (__builtin_expect((s & 1) == 0, 1)) return s; |
|
412
|
0
|
0
|
|
|
|
|
if (__builtin_expect(spin < 100000, 1)) { |
|
413
|
0
|
|
|
|
|
|
buf_spin_pause(); |
|
414
|
0
|
|
|
|
|
|
spin++; |
|
415
|
0
|
|
|
|
|
|
continue; |
|
416
|
|
|
|
|
|
|
} |
|
417
|
0
|
|
|
|
|
|
uint32_t val = __atomic_load_n(&hdr->rwlock, __ATOMIC_RELAXED); |
|
418
|
0
|
0
|
|
|
|
|
if (val >= BUF_RWLOCK_WRITER_BIT) { |
|
419
|
0
|
|
|
|
|
|
uint32_t pid = val & BUF_RWLOCK_PID_MASK; |
|
420
|
0
|
0
|
|
|
|
|
if (!buf_pid_alive(pid)) { |
|
421
|
0
|
|
|
|
|
|
buf_recover_stale_lock(hdr, val); |
|
422
|
0
|
|
|
|
|
|
spin = 0; |
|
423
|
0
|
|
|
|
|
|
continue; |
|
424
|
|
|
|
|
|
|
} |
|
425
|
|
|
|
|
|
|
} |
|
426
|
0
|
|
|
|
|
|
struct timespec ts = {0, 1000000}; |
|
427
|
0
|
|
|
|
|
|
nanosleep(&ts, NULL); |
|
428
|
0
|
|
|
|
|
|
spin = 0; |
|
429
|
|
|
|
|
|
|
} |
|
430
|
|
|
|
|
|
|
} |
|
431
|
|
|
|
|
|
|
|
|
432
|
34
|
|
|
|
|
|
static inline int buf_seqlock_read_retry(uint32_t *seq, uint32_t start) { |
|
433
|
34
|
|
|
|
|
|
return __atomic_load_n(seq, __ATOMIC_ACQUIRE) != start; |
|
434
|
|
|
|
|
|
|
} |
|
435
|
|
|
|
|
|
|
|
|
436
|
555
|
|
|
|
|
|
static inline void buf_seqlock_write_begin(uint32_t *seq) { |
|
437
|
555
|
|
|
|
|
|
__atomic_add_fetch(seq, 1, __ATOMIC_RELEASE); |
|
438
|
555
|
|
|
|
|
|
} |
|
439
|
|
|
|
|
|
|
|
|
440
|
555
|
|
|
|
|
|
static inline void buf_seqlock_write_end(uint32_t *seq) { |
|
441
|
555
|
|
|
|
|
|
__atomic_add_fetch(seq, 1, __ATOMIC_RELEASE); |
|
442
|
555
|
|
|
|
|
|
} |
|
443
|
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
/* ---- mmap create/open ---- */ |
|
445
|
|
|
|
|
|
|
|
|
446
|
46
|
|
|
|
|
|
static BufHandle *buf_create_map(const char *path, uint64_t capacity, |
|
447
|
|
|
|
|
|
|
uint32_t elem_size, uint32_t variant_id, |
|
448
|
|
|
|
|
|
|
char *errbuf) { |
|
449
|
46
|
|
|
|
|
|
errbuf[0] = '\0'; |
|
450
|
46
|
|
|
|
|
|
int created = 0; |
|
451
|
46
|
|
|
|
|
|
int fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0666); |
|
452
|
46
|
100
|
|
|
|
|
if (fd >= 0) { |
|
453
|
39
|
|
|
|
|
|
created = 1; |
|
454
|
7
|
50
|
|
|
|
|
} else if (errno == EEXIST) { |
|
455
|
7
|
|
|
|
|
|
fd = open(path, O_RDWR); |
|
456
|
|
|
|
|
|
|
} |
|
457
|
46
|
50
|
|
|
|
|
if (fd < 0) { |
|
458
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "open(%s): %s", path, strerror(errno)); |
|
459
|
0
|
|
|
|
|
|
return NULL; |
|
460
|
|
|
|
|
|
|
} |
|
461
|
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
/* Lock file for init race prevention */ |
|
463
|
46
|
50
|
|
|
|
|
if (flock(fd, LOCK_EX) < 0) { |
|
464
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "flock(%s): %s", path, strerror(errno)); |
|
465
|
0
|
|
|
|
|
|
close(fd); |
|
466
|
0
|
|
|
|
|
|
return NULL; |
|
467
|
|
|
|
|
|
|
} |
|
468
|
|
|
|
|
|
|
|
|
469
|
46
|
|
|
|
|
|
uint64_t reader_slots_off = sizeof(BufHeader); /* 128 */ |
|
470
|
46
|
|
|
|
|
|
uint64_t reader_slots_size = (uint64_t)BUF_READER_SLOTS * sizeof(BufReaderSlot); |
|
471
|
46
|
|
|
|
|
|
uint64_t data_off = reader_slots_off + reader_slots_size; /* cache-aligned */ |
|
472
|
46
|
50
|
|
|
|
|
if (elem_size > 0 && capacity > (UINT64_MAX - data_off) / elem_size) { |
|
|
|
50
|
|
|
|
|
|
|
473
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "buffer size overflow"); |
|
474
|
0
|
|
|
|
|
|
flock(fd, LOCK_UN); |
|
475
|
0
|
|
|
|
|
|
close(fd); |
|
476
|
0
|
|
|
|
|
|
return NULL; |
|
477
|
|
|
|
|
|
|
} |
|
478
|
46
|
|
|
|
|
|
uint64_t total_size = data_off + capacity * elem_size; |
|
479
|
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
struct stat st; |
|
481
|
46
|
50
|
|
|
|
|
if (fstat(fd, &st) < 0) { |
|
482
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "fstat(%s): %s", path, strerror(errno)); |
|
483
|
0
|
|
|
|
|
|
flock(fd, LOCK_UN); |
|
484
|
0
|
|
|
|
|
|
close(fd); |
|
485
|
0
|
|
|
|
|
|
return NULL; |
|
486
|
|
|
|
|
|
|
} |
|
487
|
|
|
|
|
|
|
|
|
488
|
46
|
100
|
|
|
|
|
if (!created && st.st_size > 0 && (uint64_t)st.st_size < sizeof(BufHeader)) { |
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
489
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "%s: file too small (%lld)", path, (long long)st.st_size); |
|
490
|
0
|
|
|
|
|
|
flock(fd, LOCK_UN); close(fd); return NULL; |
|
491
|
|
|
|
|
|
|
} |
|
492
|
46
|
100
|
|
|
|
|
if (created || st.st_size == 0) { |
|
|
|
50
|
|
|
|
|
|
|
493
|
39
|
50
|
|
|
|
|
if (ftruncate(fd, (off_t)total_size) < 0) { |
|
494
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "ftruncate(%s): %s", path, strerror(errno)); |
|
495
|
0
|
|
|
|
|
|
flock(fd, LOCK_UN); |
|
496
|
0
|
|
|
|
|
|
close(fd); |
|
497
|
0
|
|
|
|
|
|
return NULL; |
|
498
|
|
|
|
|
|
|
} |
|
499
|
|
|
|
|
|
|
} |
|
500
|
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
/* Re-stat after possible truncate */ |
|
502
|
46
|
50
|
|
|
|
|
if (fstat(fd, &st) < 0) { |
|
503
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "fstat(%s): %s", path, strerror(errno)); |
|
504
|
0
|
|
|
|
|
|
flock(fd, LOCK_UN); |
|
505
|
0
|
|
|
|
|
|
close(fd); |
|
506
|
0
|
|
|
|
|
|
return NULL; |
|
507
|
|
|
|
|
|
|
} |
|
508
|
|
|
|
|
|
|
|
|
509
|
46
|
|
|
|
|
|
void *base = mmap(NULL, (size_t)st.st_size, PROT_READ | PROT_WRITE, |
|
510
|
|
|
|
|
|
|
MAP_SHARED, fd, 0); |
|
511
|
46
|
50
|
|
|
|
|
if (base == MAP_FAILED) { |
|
512
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "mmap(%s): %s", path, strerror(errno)); |
|
513
|
0
|
|
|
|
|
|
flock(fd, LOCK_UN); |
|
514
|
0
|
|
|
|
|
|
close(fd); |
|
515
|
0
|
|
|
|
|
|
return NULL; |
|
516
|
|
|
|
|
|
|
} |
|
517
|
|
|
|
|
|
|
|
|
518
|
46
|
|
|
|
|
|
BufHeader *hdr = (BufHeader *)base; |
|
519
|
|
|
|
|
|
|
|
|
520
|
46
|
100
|
|
|
|
|
if (created || hdr->magic == 0) { |
|
|
|
50
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
/* Initialize header */ |
|
522
|
39
|
|
|
|
|
|
memset(hdr, 0, sizeof(BufHeader)); |
|
523
|
39
|
|
|
|
|
|
hdr->magic = BUF_MAGIC; |
|
524
|
39
|
|
|
|
|
|
hdr->version = BUF_VERSION; |
|
525
|
39
|
|
|
|
|
|
hdr->variant_id = variant_id; |
|
526
|
39
|
|
|
|
|
|
hdr->elem_size = elem_size; |
|
527
|
39
|
|
|
|
|
|
hdr->capacity = capacity; |
|
528
|
39
|
|
|
|
|
|
hdr->total_size = (uint64_t)st.st_size; |
|
529
|
39
|
|
|
|
|
|
hdr->data_off = data_off; |
|
530
|
39
|
|
|
|
|
|
hdr->reader_slots_off = reader_slots_off; |
|
531
|
|
|
|
|
|
|
/* Zero reader_slots + data area */ |
|
532
|
39
|
|
|
|
|
|
memset((char *)base + reader_slots_off, 0, |
|
533
|
39
|
|
|
|
|
|
(size_t)(reader_slots_size + capacity * elem_size)); |
|
534
|
39
|
|
|
|
|
|
__atomic_thread_fence(__ATOMIC_RELEASE); |
|
535
|
|
|
|
|
|
|
} else { |
|
536
|
|
|
|
|
|
|
/* Validate existing header */ |
|
537
|
7
|
100
|
|
|
|
|
if (hdr->magic != BUF_MAGIC) { |
|
538
|
1
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "%s: bad magic (0x%08x)", path, hdr->magic); |
|
539
|
1
|
|
|
|
|
|
goto fail; |
|
540
|
|
|
|
|
|
|
} |
|
541
|
6
|
50
|
|
|
|
|
if (hdr->version != BUF_VERSION) { |
|
542
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "%s: version mismatch (%u != %u)", |
|
543
|
|
|
|
|
|
|
path, hdr->version, BUF_VERSION); |
|
544
|
0
|
|
|
|
|
|
goto fail; |
|
545
|
|
|
|
|
|
|
} |
|
546
|
6
|
100
|
|
|
|
|
if (hdr->variant_id != variant_id) { |
|
547
|
1
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "%s: variant mismatch (%u != %u)", |
|
548
|
|
|
|
|
|
|
path, hdr->variant_id, variant_id); |
|
549
|
1
|
|
|
|
|
|
goto fail; |
|
550
|
|
|
|
|
|
|
} |
|
551
|
5
|
50
|
|
|
|
|
if (hdr->elem_size != elem_size) { |
|
552
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "%s: elem_size mismatch (%u != %u)", |
|
553
|
|
|
|
|
|
|
path, hdr->elem_size, elem_size); |
|
554
|
0
|
|
|
|
|
|
goto fail; |
|
555
|
|
|
|
|
|
|
} |
|
556
|
5
|
50
|
|
|
|
|
if (hdr->elem_size == 0 || |
|
557
|
5
|
50
|
|
|
|
|
hdr->reader_slots_off < sizeof(BufHeader) || |
|
558
|
5
|
50
|
|
|
|
|
hdr->reader_slots_off + reader_slots_size > (uint64_t)st.st_size || |
|
559
|
5
|
50
|
|
|
|
|
hdr->data_off < hdr->reader_slots_off + reader_slots_size || |
|
560
|
5
|
50
|
|
|
|
|
hdr->data_off >= (uint64_t)st.st_size || |
|
561
|
5
|
50
|
|
|
|
|
hdr->capacity > ((uint64_t)st.st_size - hdr->data_off) / hdr->elem_size) { |
|
562
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "%s: corrupt header (data doesn't fit in file)", path); |
|
563
|
0
|
|
|
|
|
|
goto fail; |
|
564
|
|
|
|
|
|
|
} |
|
565
|
|
|
|
|
|
|
} |
|
566
|
|
|
|
|
|
|
|
|
567
|
44
|
|
|
|
|
|
flock(fd, LOCK_UN); |
|
568
|
44
|
|
|
|
|
|
close(fd); |
|
569
|
|
|
|
|
|
|
|
|
570
|
44
|
|
|
|
|
|
BufHandle *h = (BufHandle *)calloc(1, sizeof(BufHandle)); |
|
571
|
44
|
50
|
|
|
|
|
if (!h) { |
|
572
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "calloc: out of memory"); |
|
573
|
0
|
|
|
|
|
|
munmap(base, (size_t)st.st_size); |
|
574
|
0
|
|
|
|
|
|
return NULL; |
|
575
|
|
|
|
|
|
|
} |
|
576
|
44
|
|
|
|
|
|
h->hdr = hdr; |
|
577
|
44
|
|
|
|
|
|
h->data = (char *)base + hdr->data_off; |
|
578
|
44
|
|
|
|
|
|
h->reader_slots = (BufReaderSlot *)((char *)base + hdr->reader_slots_off); |
|
579
|
44
|
|
|
|
|
|
h->my_slot_idx = UINT32_MAX; |
|
580
|
44
|
|
|
|
|
|
h->mmap_size = (size_t)st.st_size; |
|
581
|
44
|
|
|
|
|
|
h->path = strdup(path); |
|
582
|
44
|
|
|
|
|
|
h->fd = -1; |
|
583
|
44
|
|
|
|
|
|
h->efd = -1; |
|
584
|
44
|
|
|
|
|
|
return h; |
|
585
|
|
|
|
|
|
|
|
|
586
|
2
|
|
|
|
|
|
fail: |
|
587
|
2
|
|
|
|
|
|
munmap(base, (size_t)st.st_size); |
|
588
|
2
|
|
|
|
|
|
flock(fd, LOCK_UN); |
|
589
|
2
|
|
|
|
|
|
close(fd); |
|
590
|
2
|
|
|
|
|
|
return NULL; |
|
591
|
|
|
|
|
|
|
} |
|
592
|
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
/* ---- Anonymous mmap (no file, fork-only sharing) ---- */ |
|
594
|
|
|
|
|
|
|
|
|
595
|
40
|
|
|
|
|
|
static BufHandle *buf_create_anon(uint64_t capacity, uint32_t elem_size, |
|
596
|
|
|
|
|
|
|
uint32_t variant_id, char *errbuf) { |
|
597
|
40
|
|
|
|
|
|
errbuf[0] = '\0'; |
|
598
|
40
|
|
|
|
|
|
uint64_t reader_slots_off = sizeof(BufHeader); |
|
599
|
40
|
|
|
|
|
|
uint64_t reader_slots_size = (uint64_t)BUF_READER_SLOTS * sizeof(BufReaderSlot); |
|
600
|
40
|
|
|
|
|
|
uint64_t data_off = reader_slots_off + reader_slots_size; |
|
601
|
40
|
50
|
|
|
|
|
if (elem_size > 0 && capacity > (UINT64_MAX - data_off) / elem_size) { |
|
|
|
50
|
|
|
|
|
|
|
602
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "buffer size overflow"); |
|
603
|
0
|
|
|
|
|
|
return NULL; |
|
604
|
|
|
|
|
|
|
} |
|
605
|
40
|
|
|
|
|
|
uint64_t total_size = data_off + capacity * elem_size; |
|
606
|
|
|
|
|
|
|
|
|
607
|
40
|
|
|
|
|
|
void *base = mmap(NULL, (size_t)total_size, PROT_READ | PROT_WRITE, |
|
608
|
|
|
|
|
|
|
MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
|
609
|
40
|
50
|
|
|
|
|
if (base == MAP_FAILED) { |
|
610
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "mmap(anon): %s", strerror(errno)); |
|
611
|
0
|
|
|
|
|
|
return NULL; |
|
612
|
|
|
|
|
|
|
} |
|
613
|
|
|
|
|
|
|
|
|
614
|
40
|
|
|
|
|
|
BufHeader *hdr = (BufHeader *)base; |
|
615
|
40
|
|
|
|
|
|
memset(hdr, 0, sizeof(BufHeader)); |
|
616
|
40
|
|
|
|
|
|
hdr->magic = BUF_MAGIC; |
|
617
|
40
|
|
|
|
|
|
hdr->version = BUF_VERSION; |
|
618
|
40
|
|
|
|
|
|
hdr->variant_id = variant_id; |
|
619
|
40
|
|
|
|
|
|
hdr->elem_size = elem_size; |
|
620
|
40
|
|
|
|
|
|
hdr->capacity = capacity; |
|
621
|
40
|
|
|
|
|
|
hdr->total_size = total_size; |
|
622
|
40
|
|
|
|
|
|
hdr->data_off = data_off; |
|
623
|
40
|
|
|
|
|
|
hdr->reader_slots_off = reader_slots_off; |
|
624
|
|
|
|
|
|
|
/* MAP_ANONYMOUS already zero-fills reader_slots and data. */ |
|
625
|
|
|
|
|
|
|
|
|
626
|
40
|
|
|
|
|
|
BufHandle *h = (BufHandle *)calloc(1, sizeof(BufHandle)); |
|
627
|
40
|
50
|
|
|
|
|
if (!h) { |
|
628
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "calloc: out of memory"); |
|
629
|
0
|
|
|
|
|
|
munmap(base, (size_t)total_size); |
|
630
|
0
|
|
|
|
|
|
return NULL; |
|
631
|
|
|
|
|
|
|
} |
|
632
|
40
|
|
|
|
|
|
h->hdr = hdr; |
|
633
|
40
|
|
|
|
|
|
h->data = (char *)base + data_off; |
|
634
|
40
|
|
|
|
|
|
h->reader_slots = (BufReaderSlot *)((char *)base + reader_slots_off); |
|
635
|
40
|
|
|
|
|
|
h->my_slot_idx = UINT32_MAX; |
|
636
|
40
|
|
|
|
|
|
h->mmap_size = (size_t)total_size; |
|
637
|
40
|
|
|
|
|
|
h->path = NULL; |
|
638
|
40
|
|
|
|
|
|
h->fd = -1; |
|
639
|
40
|
|
|
|
|
|
h->efd = -1; |
|
640
|
40
|
|
|
|
|
|
return h; |
|
641
|
|
|
|
|
|
|
} |
|
642
|
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
/* ---- memfd (named anonymous, shareable via fd passing) ---- */ |
|
644
|
|
|
|
|
|
|
|
|
645
|
9
|
|
|
|
|
|
static BufHandle *buf_create_memfd(const char *name, uint64_t capacity, |
|
646
|
|
|
|
|
|
|
uint32_t elem_size, uint32_t variant_id, |
|
647
|
|
|
|
|
|
|
char *errbuf) { |
|
648
|
9
|
|
|
|
|
|
errbuf[0] = '\0'; |
|
649
|
9
|
|
|
|
|
|
uint64_t reader_slots_off = sizeof(BufHeader); |
|
650
|
9
|
|
|
|
|
|
uint64_t reader_slots_size = (uint64_t)BUF_READER_SLOTS * sizeof(BufReaderSlot); |
|
651
|
9
|
|
|
|
|
|
uint64_t data_off = reader_slots_off + reader_slots_size; |
|
652
|
9
|
50
|
|
|
|
|
if (elem_size > 0 && capacity > (UINT64_MAX - data_off) / elem_size) { |
|
|
|
50
|
|
|
|
|
|
|
653
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "buffer size overflow"); |
|
654
|
0
|
|
|
|
|
|
return NULL; |
|
655
|
|
|
|
|
|
|
} |
|
656
|
9
|
|
|
|
|
|
uint64_t total_size = data_off + capacity * elem_size; |
|
657
|
|
|
|
|
|
|
|
|
658
|
9
|
|
|
|
|
|
int fd = (int)syscall(SYS_memfd_create, name, MFD_CLOEXEC | MFD_ALLOW_SEALING); |
|
659
|
9
|
50
|
|
|
|
|
if (fd < 0) { |
|
660
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "memfd_create: %s", strerror(errno)); |
|
661
|
0
|
|
|
|
|
|
return NULL; |
|
662
|
|
|
|
|
|
|
} |
|
663
|
9
|
50
|
|
|
|
|
if (ftruncate(fd, (off_t)total_size) < 0) { |
|
664
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "ftruncate(memfd): %s", strerror(errno)); |
|
665
|
0
|
|
|
|
|
|
close(fd); |
|
666
|
0
|
|
|
|
|
|
return NULL; |
|
667
|
|
|
|
|
|
|
} |
|
668
|
9
|
|
|
|
|
|
(void)fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW); |
|
669
|
|
|
|
|
|
|
|
|
670
|
9
|
|
|
|
|
|
void *base = mmap(NULL, (size_t)total_size, PROT_READ | PROT_WRITE, |
|
671
|
|
|
|
|
|
|
MAP_SHARED, fd, 0); |
|
672
|
9
|
50
|
|
|
|
|
if (base == MAP_FAILED) { |
|
673
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "mmap(memfd): %s", strerror(errno)); |
|
674
|
0
|
|
|
|
|
|
close(fd); |
|
675
|
0
|
|
|
|
|
|
return NULL; |
|
676
|
|
|
|
|
|
|
} |
|
677
|
|
|
|
|
|
|
|
|
678
|
9
|
|
|
|
|
|
BufHeader *hdr = (BufHeader *)base; |
|
679
|
9
|
|
|
|
|
|
memset(hdr, 0, sizeof(BufHeader)); |
|
680
|
9
|
|
|
|
|
|
hdr->magic = BUF_MAGIC; |
|
681
|
9
|
|
|
|
|
|
hdr->version = BUF_VERSION; |
|
682
|
9
|
|
|
|
|
|
hdr->variant_id = variant_id; |
|
683
|
9
|
|
|
|
|
|
hdr->elem_size = elem_size; |
|
684
|
9
|
|
|
|
|
|
hdr->capacity = capacity; |
|
685
|
9
|
|
|
|
|
|
hdr->total_size = total_size; |
|
686
|
9
|
|
|
|
|
|
hdr->data_off = data_off; |
|
687
|
9
|
|
|
|
|
|
hdr->reader_slots_off = reader_slots_off; |
|
688
|
|
|
|
|
|
|
|
|
689
|
9
|
|
|
|
|
|
BufHandle *h = (BufHandle *)calloc(1, sizeof(BufHandle)); |
|
690
|
9
|
50
|
|
|
|
|
if (!h) { |
|
691
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "calloc: out of memory"); |
|
692
|
0
|
|
|
|
|
|
munmap(base, (size_t)total_size); |
|
693
|
0
|
|
|
|
|
|
close(fd); |
|
694
|
0
|
|
|
|
|
|
return NULL; |
|
695
|
|
|
|
|
|
|
} |
|
696
|
9
|
|
|
|
|
|
h->hdr = hdr; |
|
697
|
9
|
|
|
|
|
|
h->data = (char *)base + data_off; |
|
698
|
9
|
|
|
|
|
|
h->reader_slots = (BufReaderSlot *)((char *)base + reader_slots_off); |
|
699
|
9
|
|
|
|
|
|
h->my_slot_idx = UINT32_MAX; |
|
700
|
9
|
|
|
|
|
|
h->mmap_size = (size_t)total_size; |
|
701
|
9
|
|
|
|
|
|
h->path = NULL; |
|
702
|
9
|
|
|
|
|
|
h->fd = fd; |
|
703
|
9
|
|
|
|
|
|
h->efd = -1; |
|
704
|
9
|
|
|
|
|
|
return h; |
|
705
|
|
|
|
|
|
|
} |
|
706
|
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
/* ---- Open from fd (received via SCM_RIGHTS or dup) ---- */ |
|
708
|
|
|
|
|
|
|
|
|
709
|
5
|
|
|
|
|
|
static BufHandle *buf_open_fd(int fd, uint32_t elem_size, uint32_t variant_id, |
|
710
|
|
|
|
|
|
|
char *errbuf) { |
|
711
|
5
|
|
|
|
|
|
errbuf[0] = '\0'; |
|
712
|
|
|
|
|
|
|
struct stat st; |
|
713
|
5
|
50
|
|
|
|
|
if (fstat(fd, &st) < 0) { |
|
714
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "fstat(fd=%d): %s", fd, strerror(errno)); |
|
715
|
0
|
|
|
|
|
|
return NULL; |
|
716
|
|
|
|
|
|
|
} |
|
717
|
5
|
50
|
|
|
|
|
if ((uint64_t)st.st_size < sizeof(BufHeader)) { |
|
718
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "fd=%d: file too small for header", fd); |
|
719
|
0
|
|
|
|
|
|
return NULL; |
|
720
|
|
|
|
|
|
|
} |
|
721
|
|
|
|
|
|
|
|
|
722
|
5
|
|
|
|
|
|
void *base = mmap(NULL, (size_t)st.st_size, PROT_READ | PROT_WRITE, |
|
723
|
|
|
|
|
|
|
MAP_SHARED, fd, 0); |
|
724
|
5
|
50
|
|
|
|
|
if (base == MAP_FAILED) { |
|
725
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "mmap(fd=%d): %s", fd, strerror(errno)); |
|
726
|
0
|
|
|
|
|
|
return NULL; |
|
727
|
|
|
|
|
|
|
} |
|
728
|
|
|
|
|
|
|
|
|
729
|
5
|
|
|
|
|
|
BufHeader *hdr = (BufHeader *)base; |
|
730
|
5
|
50
|
|
|
|
|
if (hdr->magic != BUF_MAGIC) { |
|
731
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "fd=%d: bad magic (0x%08x)", fd, hdr->magic); |
|
732
|
0
|
|
|
|
|
|
goto fail; |
|
733
|
|
|
|
|
|
|
} |
|
734
|
5
|
50
|
|
|
|
|
if (hdr->version != BUF_VERSION) { |
|
735
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "fd=%d: version mismatch (%u != %u)", |
|
736
|
|
|
|
|
|
|
fd, hdr->version, BUF_VERSION); |
|
737
|
0
|
|
|
|
|
|
goto fail; |
|
738
|
|
|
|
|
|
|
} |
|
739
|
5
|
100
|
|
|
|
|
if (hdr->variant_id != variant_id) { |
|
740
|
1
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "fd=%d: variant mismatch (%u != %u)", |
|
741
|
|
|
|
|
|
|
fd, hdr->variant_id, variant_id); |
|
742
|
1
|
|
|
|
|
|
goto fail; |
|
743
|
|
|
|
|
|
|
} |
|
744
|
4
|
50
|
|
|
|
|
if (hdr->elem_size != elem_size) { |
|
745
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "fd=%d: elem_size mismatch (%u != %u)", |
|
746
|
|
|
|
|
|
|
fd, hdr->elem_size, elem_size); |
|
747
|
0
|
|
|
|
|
|
goto fail; |
|
748
|
|
|
|
|
|
|
} |
|
749
|
4
|
|
|
|
|
|
uint64_t reader_slots_size = (uint64_t)BUF_READER_SLOTS * sizeof(BufReaderSlot); |
|
750
|
4
|
50
|
|
|
|
|
if (hdr->elem_size == 0 || |
|
751
|
4
|
50
|
|
|
|
|
hdr->reader_slots_off < sizeof(BufHeader) || |
|
752
|
4
|
50
|
|
|
|
|
hdr->reader_slots_off + reader_slots_size > (uint64_t)st.st_size || |
|
753
|
4
|
50
|
|
|
|
|
hdr->data_off < hdr->reader_slots_off + reader_slots_size || |
|
754
|
4
|
50
|
|
|
|
|
hdr->total_size != (uint64_t)st.st_size || |
|
755
|
4
|
50
|
|
|
|
|
hdr->capacity > ((uint64_t)st.st_size - hdr->data_off) / hdr->elem_size) { |
|
756
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "fd=%d: corrupt header", fd); |
|
757
|
0
|
|
|
|
|
|
goto fail; |
|
758
|
|
|
|
|
|
|
} |
|
759
|
|
|
|
|
|
|
|
|
760
|
|
|
|
|
|
|
{ |
|
761
|
4
|
|
|
|
|
|
int myfd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
|
762
|
4
|
50
|
|
|
|
|
if (myfd < 0) { |
|
763
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "fcntl(F_DUPFD_CLOEXEC, fd=%d): %s", |
|
764
|
0
|
|
|
|
|
|
fd, strerror(errno)); |
|
765
|
0
|
|
|
|
|
|
munmap(base, (size_t)st.st_size); |
|
766
|
0
|
|
|
|
|
|
return NULL; |
|
767
|
|
|
|
|
|
|
} |
|
768
|
4
|
|
|
|
|
|
BufHandle *h = (BufHandle *)calloc(1, sizeof(BufHandle)); |
|
769
|
4
|
50
|
|
|
|
|
if (!h) { |
|
770
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "calloc: out of memory"); |
|
771
|
0
|
|
|
|
|
|
close(myfd); |
|
772
|
0
|
|
|
|
|
|
munmap(base, (size_t)st.st_size); |
|
773
|
0
|
|
|
|
|
|
return NULL; |
|
774
|
|
|
|
|
|
|
} |
|
775
|
4
|
|
|
|
|
|
h->hdr = hdr; |
|
776
|
4
|
|
|
|
|
|
h->data = (char *)base + hdr->data_off; |
|
777
|
4
|
|
|
|
|
|
h->reader_slots = (BufReaderSlot *)((char *)base + hdr->reader_slots_off); |
|
778
|
4
|
|
|
|
|
|
h->my_slot_idx = UINT32_MAX; |
|
779
|
4
|
|
|
|
|
|
h->mmap_size = (size_t)st.st_size; |
|
780
|
4
|
|
|
|
|
|
h->path = NULL; |
|
781
|
4
|
|
|
|
|
|
h->fd = myfd; |
|
782
|
4
|
|
|
|
|
|
h->efd = -1; |
|
783
|
4
|
|
|
|
|
|
return h; |
|
784
|
|
|
|
|
|
|
} |
|
785
|
|
|
|
|
|
|
|
|
786
|
1
|
|
|
|
|
|
fail: |
|
787
|
1
|
|
|
|
|
|
munmap(base, (size_t)st.st_size); |
|
788
|
1
|
|
|
|
|
|
return NULL; |
|
789
|
|
|
|
|
|
|
} |
|
790
|
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
/* ---- msync ---- */ |
|
792
|
|
|
|
|
|
|
|
|
793
|
3
|
|
|
|
|
|
static inline int buf_msync(BufHandle *h) { |
|
794
|
3
|
50
|
|
|
|
|
if (!h || !h->hdr) return 0; |
|
|
|
50
|
|
|
|
|
|
|
795
|
3
|
|
|
|
|
|
return msync(h->hdr, h->mmap_size, MS_SYNC); |
|
796
|
|
|
|
|
|
|
} |
|
797
|
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
/* ---- Eventfd integration (opt-in notifications) ---- */ |
|
799
|
|
|
|
|
|
|
|
|
800
|
8
|
|
|
|
|
|
static int buf_create_eventfd(BufHandle *h) { |
|
801
|
8
|
100
|
|
|
|
|
if (h->efd >= 0) return h->efd; |
|
802
|
7
|
|
|
|
|
|
int efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); |
|
803
|
7
|
50
|
|
|
|
|
if (efd < 0) return -1; |
|
804
|
7
|
|
|
|
|
|
h->efd = efd; |
|
805
|
7
|
|
|
|
|
|
h->efd_owned = 1; |
|
806
|
7
|
|
|
|
|
|
return efd; |
|
807
|
|
|
|
|
|
|
} |
|
808
|
|
|
|
|
|
|
|
|
809
|
2
|
|
|
|
|
|
static void buf_attach_eventfd(BufHandle *h, int efd) { |
|
810
|
2
|
50
|
|
|
|
|
if (h->efd >= 0 && h->efd_owned) close(h->efd); |
|
|
|
0
|
|
|
|
|
|
|
811
|
2
|
|
|
|
|
|
h->efd = efd; |
|
812
|
2
|
|
|
|
|
|
h->efd_owned = 0; |
|
813
|
2
|
|
|
|
|
|
} |
|
814
|
|
|
|
|
|
|
|
|
815
|
7
|
|
|
|
|
|
static int buf_notify(BufHandle *h) { |
|
816
|
7
|
100
|
|
|
|
|
if (h->efd < 0) return 0; |
|
817
|
6
|
|
|
|
|
|
uint64_t val = 1; |
|
818
|
6
|
|
|
|
|
|
return write(h->efd, &val, sizeof(val)) == sizeof(val); |
|
819
|
|
|
|
|
|
|
} |
|
820
|
|
|
|
|
|
|
|
|
821
|
9
|
|
|
|
|
|
static int64_t buf_wait_notify(BufHandle *h) { |
|
822
|
9
|
100
|
|
|
|
|
if (h->efd < 0) return -1; |
|
823
|
8
|
|
|
|
|
|
uint64_t val = 0; |
|
824
|
8
|
100
|
|
|
|
|
if (read(h->efd, &val, sizeof(val)) != sizeof(val)) return -1; |
|
825
|
6
|
|
|
|
|
|
return (int64_t)val; |
|
826
|
|
|
|
|
|
|
} |
|
827
|
|
|
|
|
|
|
|
|
828
|
97
|
|
|
|
|
|
static void buf_close_map(BufHandle *h) { |
|
829
|
97
|
50
|
|
|
|
|
if (!h) return; |
|
830
|
|
|
|
|
|
|
/* Release reader slot — only if we still own it AND no fork has happened |
|
831
|
|
|
|
|
|
|
* since we claimed it. A forked child that inherits the handle but never |
|
832
|
|
|
|
|
|
|
* acquired the lock itself must NOT clear the parent's slot. */ |
|
833
|
97
|
50
|
|
|
|
|
if (h->reader_slots && h->my_slot_idx != UINT32_MAX && h->cached_pid && |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
834
|
28
|
100
|
|
|
|
|
h->cached_fork_gen == __atomic_load_n(&buf_fork_gen, __ATOMIC_RELAXED)) { |
|
835
|
27
|
|
|
|
|
|
uint32_t expected = h->cached_pid; |
|
836
|
|
|
|
|
|
|
/* CAS pid → 0; do NOT clear subcount/wp/writp — between the CAS and |
|
837
|
|
|
|
|
|
|
* a follow-up store, a new process could claim the slot, and our |
|
838
|
|
|
|
|
|
|
* store would clobber its state. buf_claim_reader_slot zeros all |
|
839
|
|
|
|
|
|
|
* mirror fields on every claim, so leaving stale values is safe. */ |
|
840
|
27
|
|
|
|
|
|
__atomic_compare_exchange_n(&h->reader_slots[h->my_slot_idx].pid, |
|
841
|
|
|
|
|
|
|
&expected, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED); |
|
842
|
|
|
|
|
|
|
} |
|
843
|
97
|
100
|
|
|
|
|
if (h->efd >= 0 && h->efd_owned) close(h->efd); |
|
|
|
100
|
|
|
|
|
|
|
844
|
97
|
50
|
|
|
|
|
if (h->hdr) munmap(h->hdr, h->mmap_size); |
|
845
|
97
|
100
|
|
|
|
|
if (h->fd >= 0) close(h->fd); |
|
846
|
97
|
100
|
|
|
|
|
if (h->path) free(h->path); |
|
847
|
97
|
|
|
|
|
|
free(h); |
|
848
|
|
|
|
|
|
|
} |
|
849
|
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
#endif /* BUF_DEFS_H */ |
|
851
|
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
|
|
853
|
|
|
|
|
|
|
/* ================================================================ |
|
854
|
|
|
|
|
|
|
* Part 2: Per-variant functions (instantiated per include) |
|
855
|
|
|
|
|
|
|
* ================================================================ */ |
|
856
|
|
|
|
|
|
|
|
|
857
|
|
|
|
|
|
|
#ifndef BUF_PREFIX |
|
858
|
|
|
|
|
|
|
#error "BUF_PREFIX must be defined before including buf_generic.h" |
|
859
|
|
|
|
|
|
|
#endif |
|
860
|
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
#define BUF_PASTE2(a, b) a##_##b |
|
862
|
|
|
|
|
|
|
#define BUF_PASTE(a, b) BUF_PASTE2(a, b) |
|
863
|
|
|
|
|
|
|
#define BUF_FN(name) BUF_PASTE(BUF_PREFIX, name) |
|
864
|
|
|
|
|
|
|
|
|
865
|
|
|
|
|
|
|
/* ---- Create ---- */ |
|
866
|
|
|
|
|
|
|
|
|
867
|
|
|
|
|
|
|
#ifdef BUF_IS_FIXEDSTR |
|
868
|
4
|
|
|
|
|
|
static BufHandle *BUF_FN(create)(const char *path, uint64_t capacity, |
|
869
|
|
|
|
|
|
|
uint32_t str_len, char *errbuf) { |
|
870
|
4
|
50
|
|
|
|
|
if (str_len == 0) { |
|
871
|
0
|
|
|
|
|
|
snprintf(errbuf, BUF_ERR_BUFLEN, "str_len must be > 0"); |
|
872
|
0
|
|
|
|
|
|
return NULL; |
|
873
|
|
|
|
|
|
|
} |
|
874
|
4
|
|
|
|
|
|
return buf_create_map(path, capacity, str_len, BUF_VARIANT_ID, errbuf); |
|
875
|
|
|
|
|
|
|
} |
|
876
|
|
|
|
|
|
|
#else |
|
877
|
42
|
|
|
|
|
|
static BufHandle *BUF_FN(create)(const char *path, uint64_t capacity, char *errbuf) { |
|
|
5
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
878
|
42
|
|
|
|
|
|
return buf_create_map(path, capacity, BUF_ELEM_SIZE, BUF_VARIANT_ID, errbuf); |
|
|
5
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
879
|
|
|
|
|
|
|
} |
|
880
|
|
|
|
|
|
|
#endif |
|
881
|
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
/* ---- Create anonymous ---- */ |
|
883
|
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
#ifdef BUF_IS_FIXEDSTR |
|
885
|
8
|
|
|
|
|
|
static BufHandle *BUF_FN(create_anon)(uint64_t capacity, uint32_t str_len, char *errbuf) { |
|
886
|
8
|
50
|
|
|
|
|
if (str_len == 0) { snprintf(errbuf, BUF_ERR_BUFLEN, "str_len must be > 0"); return NULL; } |
|
887
|
8
|
|
|
|
|
|
return buf_create_anon(capacity, str_len, BUF_VARIANT_ID, errbuf); |
|
888
|
|
|
|
|
|
|
} |
|
889
|
2
|
|
|
|
|
|
static BufHandle *BUF_FN(create_memfd)(const char *name, uint64_t capacity, uint32_t str_len, char *errbuf) { |
|
890
|
2
|
50
|
|
|
|
|
if (str_len == 0) { snprintf(errbuf, BUF_ERR_BUFLEN, "str_len must be > 0"); return NULL; } |
|
891
|
2
|
|
|
|
|
|
return buf_create_memfd(name, capacity, str_len, BUF_VARIANT_ID, errbuf); |
|
892
|
|
|
|
|
|
|
} |
|
893
|
1
|
|
|
|
|
|
static BufHandle *BUF_FN(open_fd)(int fd, uint32_t str_len, char *errbuf) { |
|
894
|
1
|
50
|
|
|
|
|
if (str_len == 0) { snprintf(errbuf, BUF_ERR_BUFLEN, "str_len must be > 0"); return NULL; } |
|
895
|
1
|
|
|
|
|
|
return buf_open_fd(fd, str_len, BUF_VARIANT_ID, errbuf); |
|
896
|
|
|
|
|
|
|
} |
|
897
|
|
|
|
|
|
|
#else |
|
898
|
32
|
|
|
|
|
|
static BufHandle *BUF_FN(create_anon)(uint64_t capacity, char *errbuf) { |
|
|
5
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
899
|
32
|
|
|
|
|
|
return buf_create_anon(capacity, BUF_ELEM_SIZE, BUF_VARIANT_ID, errbuf); |
|
|
5
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
} |
|
901
|
7
|
|
|
|
|
|
static BufHandle *BUF_FN(create_memfd)(const char *name, uint64_t capacity, char *errbuf) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
902
|
7
|
|
|
|
|
|
return buf_create_memfd(name, capacity, BUF_ELEM_SIZE, BUF_VARIANT_ID, errbuf); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
903
|
|
|
|
|
|
|
} |
|
904
|
4
|
|
|
|
|
|
static BufHandle *BUF_FN(open_fd)(int fd, char *errbuf) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
905
|
4
|
|
|
|
|
|
return buf_open_fd(fd, BUF_ELEM_SIZE, BUF_VARIANT_ID, errbuf); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
} |
|
907
|
|
|
|
|
|
|
#endif |
|
908
|
|
|
|
|
|
|
|
|
909
|
|
|
|
|
|
|
/* ---- Raw byte access (for packed binary interop) ---- */ |
|
910
|
|
|
|
|
|
|
|
|
911
|
9
|
|
|
|
|
|
static int BUF_FN(get_raw)(BufHandle *h, uint64_t byte_off, uint64_t nbytes, void *out) { |
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
912
|
9
|
|
|
|
|
|
uint64_t data_size = h->hdr->capacity * (uint64_t)h->hdr->elem_size; |
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
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0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
916
|
1
|
|
|
|
|
|
memcpy(out, data + byte_off, (size_t)nbytes); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
917
|
|
|
|
|
|
|
} else { |
|
918
|
|
|
|
|
|
|
uint32_t seq_start; |
|
919
|
|
|
|
|
|
|
do { |
|
920
|
6
|
|
|
|
|
|
seq_start = buf_seqlock_read_begin(h->hdr); |
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
921
|
6
|
|
|
|
|
|
memcpy(out, data + byte_off, (size_t)nbytes); |
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
922
|
6
|
50
|
|
|
|
|
} while (buf_seqlock_read_retry(&h->hdr->seq, seq_start)); |
|
|
1
|
0
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
1
|
0
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
4
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
} |
|
924
|
7
|
|
|
|
|
|
return 1; |
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
925
|
|
|
|
|
|
|
} |
|
926
|
|
|
|
|
|
|
|
|
927
|
8
|
|
|
|
|
|
static int BUF_FN(set_raw)(BufHandle *h, uint64_t byte_off, uint64_t nbytes, const void *in) { |
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
928
|
8
|
|
|
|
|
|
uint64_t data_size = h->hdr->capacity * (uint64_t)h->hdr->elem_size; |
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|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|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
932
|
4
|
50
|
|
|
|
|
if (!nested) { buf_rwlock_wrlock(h); buf_seqlock_write_begin(&h->hdr->seq); } |
|
|
1
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
3
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
933
|
4
|
|
|
|
|
|
memcpy(data + byte_off, in, (size_t)nbytes); |
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
934
|
4
|
50
|
|
|
|
|
if (!nested) { buf_seqlock_write_end(&h->hdr->seq); buf_rwlock_wrunlock(h); } |
|
|
1
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
3
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
935
|
4
|
|
|
|
|
|
return 1; |
|
|
1
|
|
|
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
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
943
|
7
|
50
|
|
|
|
|
if (!nested) { buf_rwlock_wrlock(h); buf_seqlock_write_begin(&hdr->seq); } |
|
|
1
|
0
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
1
|
0
|
|
|
|
|
|
|
|
0
|
100
|
|
|
|
|
|
|
|
5
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
944
|
7
|
|
|
|
|
|
memset(h->data, 0, (size_t)(hdr->capacity * hdr->elem_size)); |
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
945
|
7
|
50
|
|
|
|
|
if (!nested) { buf_seqlock_write_end(&hdr->seq); buf_rwlock_wrunlock(h); } |
|
|
1
|
0
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
1
|
0
|
|
|
|
|
|
|
|
0
|
100
|
|
|
|
|
|
|
|
5
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
946
|
7
|
|
|
|
|
|
} |
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
947
|
|
|
|
|
|
|
|
|
948
|
|
|
|
|
|
|
/* ---- Single-element atomic get (lock-free for numeric types) ---- */ |
|
949
|
|
|
|
|
|
|
|
|
950
|
|
|
|
|
|
|
#ifdef BUF_IS_FIXEDSTR |
|
951
|
|
|
|
|
|
|
|
|
952
|
22
|
|
|
|
|
|
static int BUF_FN(get)(BufHandle *h, uint64_t idx, char *out, uint32_t *out_len) { |
|
953
|
22
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
954
|
22
|
|
|
|
|
|
uint32_t esz = hdr->elem_size; |
|
955
|
22
|
50
|
|
|
|
|
if (idx >= hdr->capacity) return 0; |
|
956
|
22
|
|
|
|
|
|
char *data = (char *)h->data; |
|
957
|
22
|
100
|
|
|
|
|
if (h->wr_locked) { |
|
958
|
1
|
|
|
|
|
|
memcpy(out, data + idx * esz, esz); |
|
959
|
|
|
|
|
|
|
} else { |
|
960
|
|
|
|
|
|
|
uint32_t seq_start; |
|
961
|
|
|
|
|
|
|
do { |
|
962
|
21
|
|
|
|
|
|
seq_start = buf_seqlock_read_begin(hdr); |
|
963
|
21
|
|
|
|
|
|
memcpy(out, data + idx * esz, esz); |
|
964
|
21
|
50
|
|
|
|
|
} while (buf_seqlock_read_retry(&hdr->seq, seq_start)); |
|
965
|
|
|
|
|
|
|
} |
|
966
|
22
|
|
|
|
|
|
uint32_t len = esz; |
|
967
|
260
|
100
|
|
|
|
|
while (len > 0 && out[len - 1] == '\0') len--; |
|
|
|
100
|
|
|
|
|
|
|
968
|
22
|
|
|
|
|
|
*out_len = len; |
|
969
|
22
|
|
|
|
|
|
return 1; |
|
970
|
|
|
|
|
|
|
} |
|
971
|
|
|
|
|
|
|
|
|
972
|
521
|
|
|
|
|
|
static int BUF_FN(set)(BufHandle *h, uint64_t idx, const char *val, uint32_t len) { |
|
973
|
521
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
974
|
521
|
|
|
|
|
|
uint32_t esz = hdr->elem_size; |
|
975
|
521
|
50
|
|
|
|
|
if (idx >= hdr->capacity) return 0; |
|
976
|
521
|
|
|
|
|
|
char *data = (char *)h->data; |
|
977
|
521
|
|
|
|
|
|
int nested = h->wr_locked; |
|
978
|
521
|
100
|
|
|
|
|
if (!nested) { buf_rwlock_wrlock(h); buf_seqlock_write_begin(&hdr->seq); } |
|
979
|
521
|
|
|
|
|
|
memset(data + idx * esz, 0, esz); |
|
980
|
521
|
|
|
|
|
|
uint32_t copy_len = len < esz ? len : esz; |
|
981
|
521
|
|
|
|
|
|
memcpy(data + idx * esz, val, copy_len); |
|
982
|
521
|
100
|
|
|
|
|
if (!nested) { buf_seqlock_write_end(&hdr->seq); buf_rwlock_wrunlock(h); } |
|
983
|
521
|
|
|
|
|
|
return 1; |
|
984
|
|
|
|
|
|
|
} |
|
985
|
|
|
|
|
|
|
|
|
986
|
|
|
|
|
|
|
#elif defined(BUF_IS_FLOAT) |
|
987
|
|
|
|
|
|
|
|
|
988
|
|
|
|
|
|
|
/* Float/double: GCC __atomic builtins don't support FP types. |
|
989
|
|
|
|
|
|
|
* Use same-sized integer atomic load/store + memcpy for lock-free access. */ |
|
990
|
|
|
|
|
|
|
|
|
991
|
|
|
|
|
|
|
#if BUF_ELEM_SIZE == 4 |
|
992
|
|
|
|
|
|
|
typedef uint32_t BUF_PASTE(BUF_PREFIX, _uint_t); |
|
993
|
|
|
|
|
|
|
#elif BUF_ELEM_SIZE == 8 |
|
994
|
|
|
|
|
|
|
typedef uint64_t BUF_PASTE(BUF_PREFIX, _uint_t); |
|
995
|
|
|
|
|
|
|
#else |
|
996
|
|
|
|
|
|
|
#error "BUF_IS_FLOAT requires BUF_ELEM_SIZE of 4 or 8" |
|
997
|
|
|
|
|
|
|
#endif |
|
998
|
|
|
|
|
|
|
|
|
999
|
14
|
|
|
|
|
|
static int BUF_FN(get)(BufHandle *h, uint64_t idx, BUF_ELEM_TYPE *out) { |
|
|
9
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
1000
|
14
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
|
9
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
1001
|
14
|
50
|
|
|
|
|
if (idx >= hdr->capacity) return 0; |
|
|
9
|
50
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
1002
|
|
|
|
|
|
|
typedef BUF_PASTE(BUF_PREFIX, _uint_t) uint_t; |
|
1003
|
14
|
|
|
|
|
|
uint_t *idata = (uint_t *)h->data; |
|
|
9
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
1004
|
14
|
|
|
|
|
|
uint_t tmp = __atomic_load_n(&idata[idx], __ATOMIC_RELAXED); |
|
|
9
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
1005
|
14
|
|
|
|
|
|
memcpy(out, &tmp, sizeof(BUF_ELEM_TYPE)); |
|
|
9
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
1006
|
14
|
|
|
|
|
|
return 1; |
|
|
9
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
1007
|
|
|
|
|
|
|
} |
|
1008
|
|
|
|
|
|
|
|
|
1009
|
1016
|
|
|
|
|
|
static int BUF_FN(set)(BufHandle *h, uint64_t idx, BUF_ELEM_TYPE val) { |
|
|
1012
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
1010
|
1016
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
|
1012
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
1011
|
1016
|
50
|
|
|
|
|
if (idx >= hdr->capacity) return 0; |
|
|
1012
|
50
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
1012
|
|
|
|
|
|
|
typedef BUF_PASTE(BUF_PREFIX, _uint_t) uint_t; |
|
1013
|
1016
|
|
|
|
|
|
uint_t *idata = (uint_t *)h->data; |
|
|
1012
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
1014
|
|
|
|
|
|
|
uint_t tmp; |
|
1015
|
1016
|
|
|
|
|
|
memcpy(&tmp, &val, sizeof(BUF_ELEM_TYPE)); |
|
|
1012
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
1016
|
1016
|
|
|
|
|
|
__atomic_store_n(&idata[idx], tmp, __ATOMIC_RELAXED); |
|
|
1012
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
1017
|
1016
|
|
|
|
|
|
return 1; |
|
|
1012
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
1018
|
|
|
|
|
|
|
} |
|
1019
|
|
|
|
|
|
|
|
|
1020
|
|
|
|
|
|
|
#else /* integer types */ |
|
1021
|
|
|
|
|
|
|
|
|
1022
|
1227
|
|
|
|
|
|
static int BUF_FN(get)(BufHandle *h, uint64_t idx, BUF_ELEM_TYPE *out) { |
|
|
2
|
|
|
|
|
|
|
|
|
1204
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
1023
|
1227
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
|
2
|
|
|
|
|
|
|
|
|
1204
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
1024
|
1227
|
50
|
|
|
|
|
if (idx >= hdr->capacity) return 0; |
|
|
2
|
100
|
|
|
|
|
|
|
|
1204
|
50
|
|
|
|
|
|
|
|
10
|
50
|
|
|
|
|
|
|
|
2
|
50
|
|
|
|
|
|
|
|
2
|
50
|
|
|
|
|
|
|
|
2
|
50
|
|
|
|
|
|
|
|
2
|
50
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
1025
|
1224
|
|
|
|
|
|
BUF_ELEM_TYPE *data = (BUF_ELEM_TYPE *)h->data; |
|
|
2
|
|
|
|
|
|
|
|
|
1201
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
1026
|
1224
|
|
|
|
|
|
*out = __atomic_load_n(&data[idx], __ATOMIC_RELAXED); |
|
|
2
|
|
|
|
|
|
|
|
|
1201
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
1027
|
1224
|
|
|
|
|
|
return 1; |
|
|
2
|
|
|
|
|
|
|
|
|
1201
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
1028
|
|
|
|
|
|
|
} |
|
1029
|
|
|
|
|
|
|
|
|
1030
|
90
|
|
|
|
|
|
static int BUF_FN(set)(BufHandle *h, uint64_t idx, BUF_ELEM_TYPE val) { |
|
|
1
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
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|
1
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
1036
|
|
|
|
|
|
|
} |
|
1037
|
|
|
|
|
|
|
|
|
1038
|
|
|
|
|
|
|
#endif /* BUF_IS_FIXEDSTR */ |
|
1039
|
|
|
|
|
|
|
|
|
1040
|
|
|
|
|
|
|
/* ---- Bulk operations (seqlock-guarded) ---- */ |
|
1041
|
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
#ifdef BUF_IS_FIXEDSTR |
|
1043
|
|
|
|
|
|
|
|
|
1044
|
2
|
|
|
|
|
|
static int BUF_FN(get_slice)(BufHandle *h, uint64_t from, uint64_t count, |
|
1045
|
|
|
|
|
|
|
void *out) { |
|
1046
|
2
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
1047
|
2
|
|
|
|
|
|
uint32_t esz = hdr->elem_size; |
|
1048
|
2
|
50
|
|
|
|
|
if (count > hdr->capacity || from > hdr->capacity - count) return 0; |
|
|
|
50
|
|
|
|
|
|
|
1049
|
2
|
|
|
|
|
|
char *data = (char *)h->data; |
|
1050
|
2
|
100
|
|
|
|
|
if (h->wr_locked) { |
|
1051
|
1
|
|
|
|
|
|
memcpy(out, data + from * esz, count * esz); |
|
1052
|
|
|
|
|
|
|
} else { |
|
1053
|
|
|
|
|
|
|
uint32_t seq_start; |
|
1054
|
|
|
|
|
|
|
do { |
|
1055
|
1
|
|
|
|
|
|
seq_start = buf_seqlock_read_begin(hdr); |
|
1056
|
1
|
|
|
|
|
|
memcpy(out, data + from * esz, count * esz); |
|
1057
|
1
|
50
|
|
|
|
|
} while (buf_seqlock_read_retry(&hdr->seq, seq_start)); |
|
1058
|
|
|
|
|
|
|
} |
|
1059
|
2
|
|
|
|
|
|
return 1; |
|
1060
|
|
|
|
|
|
|
} |
|
1061
|
|
|
|
|
|
|
|
|
1062
|
1
|
|
|
|
|
|
static int BUF_FN(set_slice)(BufHandle *h, uint64_t from, uint64_t count, |
|
1063
|
|
|
|
|
|
|
const void *in) { |
|
1064
|
1
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
1065
|
1
|
|
|
|
|
|
uint32_t esz = hdr->elem_size; |
|
1066
|
1
|
50
|
|
|
|
|
if (count > hdr->capacity || from > hdr->capacity - count) return 0; |
|
|
|
50
|
|
|
|
|
|
|
1067
|
1
|
|
|
|
|
|
char *data = (char *)h->data; |
|
1068
|
1
|
|
|
|
|
|
int nested = h->wr_locked; |
|
1069
|
1
|
50
|
|
|
|
|
if (!nested) { buf_rwlock_wrlock(h); buf_seqlock_write_begin(&hdr->seq); } |
|
1070
|
1
|
|
|
|
|
|
memcpy(data + from * esz, in, count * esz); |
|
1071
|
1
|
50
|
|
|
|
|
if (!nested) { buf_seqlock_write_end(&hdr->seq); buf_rwlock_wrunlock(h); } |
|
1072
|
1
|
|
|
|
|
|
return 1; |
|
1073
|
|
|
|
|
|
|
} |
|
1074
|
|
|
|
|
|
|
|
|
1075
|
|
|
|
|
|
|
#else /* numeric */ |
|
1076
|
|
|
|
|
|
|
|
|
1077
|
9
|
|
|
|
|
|
static int BUF_FN(get_slice)(BufHandle *h, uint64_t from, uint64_t count, |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1078
|
|
|
|
|
|
|
BUF_ELEM_TYPE *out) { |
|
1079
|
9
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|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
1083
|
1
|
|
|
|
|
|
memcpy(out, &data[from], count * sizeof(BUF_ELEM_TYPE)); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1084
|
|
|
|
|
|
|
} else { |
|
1085
|
|
|
|
|
|
|
uint32_t seq_start; |
|
1086
|
|
|
|
|
|
|
do { |
|
1087
|
6
|
|
|
|
|
|
seq_start = buf_seqlock_read_begin(hdr); |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1088
|
6
|
|
|
|
|
|
memcpy(out, &data[from], count * sizeof(BUF_ELEM_TYPE)); |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1089
|
6
|
50
|
|
|
|
|
} while (buf_seqlock_read_retry(&hdr->seq, seq_start)); |
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
0
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
4
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1090
|
|
|
|
|
|
|
} |
|
1091
|
7
|
|
|
|
|
|
return 1; |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
} |
|
1093
|
|
|
|
|
|
|
|
|
1094
|
6
|
|
|
|
|
|
static int BUF_FN(set_slice)(BufHandle *h, uint64_t from, uint64_t count, |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1095
|
|
|
|
|
|
|
const BUF_ELEM_TYPE *in) { |
|
1096
|
6
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1097
|
6
|
50
|
|
|
|
|
if (count > hdr->capacity || from > hdr->capacity - count) return 0; |
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
0
|
50
|
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|
|
|
|
|
|
1110
|
|
|
|
|
|
|
#ifdef BUF_IS_FIXEDSTR |
|
1111
|
|
|
|
|
|
|
|
|
1112
|
2
|
|
|
|
|
|
static void BUF_FN(fill)(BufHandle *h, const char *val, uint32_t len) { |
|
1113
|
2
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
1114
|
2
|
|
|
|
|
|
uint32_t esz = hdr->elem_size; |
|
1115
|
2
|
|
|
|
|
|
char *data = (char *)h->data; |
|
1116
|
2
|
|
|
|
|
|
int nested = h->wr_locked; |
|
1117
|
2
|
50
|
|
|
|
|
if (!nested) { buf_rwlock_wrlock(h); buf_seqlock_write_begin(&hdr->seq); } |
|
1118
|
2
|
|
|
|
|
|
uint32_t copy_len = len < esz ? len : esz; |
|
1119
|
2
|
|
|
|
|
|
memset(data, 0, (size_t)hdr->capacity * esz); |
|
1120
|
27
|
100
|
|
|
|
|
for (uint64_t i = 0; i < hdr->capacity; i++) |
|
1121
|
25
|
|
|
|
|
|
memcpy(data + i * esz, val, copy_len); |
|
1122
|
2
|
50
|
|
|
|
|
if (!nested) { buf_seqlock_write_end(&hdr->seq); buf_rwlock_wrunlock(h); } |
|
1123
|
2
|
|
|
|
|
|
} |
|
1124
|
|
|
|
|
|
|
|
|
1125
|
|
|
|
|
|
|
#else |
|
1126
|
|
|
|
|
|
|
|
|
1127
|
11
|
|
|
|
|
|
static void BUF_FN(fill)(BufHandle *h, BUF_ELEM_TYPE val) { |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1128
|
11
|
|
|
|
|
|
BufHeader *hdr = h->hdr; |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|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ations (integer types only) ---- */ |
|
1140
|
|
|
|
|
|
|
|
|
1141
|
|
|
|
|
|
|
#ifdef BUF_HAS_COUNTERS |
|
1142
|
|
|
|
|
|
|
|
|
1143
|
4519
|
|
|
|
|
|
static BUF_ELEM_TYPE BUF_FN(incr)(BufHandle *h, uint64_t idx) { |
|
|
1
|
|
|
|
|
|
|
|
|
4510
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
1144
|
4519
|
50
|
|
|
|
|
if (idx >= h->hdr->capacity) return 0; |
|
|
1
|
50
|
|
|
|
|
|
|
|
4510
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
2
|
50
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
1145
|
4519
|
|
|
|
|
|
BUF_ELEM_TYPE *data = (BUF_ELEM_TYPE *)h->data; |
|
|
1
|
|
|
|
|
|
|
|
|
4510
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
1146
|
4519
|
|
|
|
|
|
return __atomic_add_fetch(&data[idx], 1, __ATOMIC_RELAXED); |
|
|
1
|
|
|
|
|
|
|
|
|
4510
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
1147
|
|
|
|
|
|
|
} |
|
1148
|
|
|
|
|
|
|
|
|
1149
|
13
|
|
|
|
|
|
static BUF_ELEM_TYPE BUF_FN(decr)(BufHandle *h, uint64_t idx) { |
|
|
1
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
1150
|
13
|
50
|
|
|
|
|
if (idx >= h->hdr->capacity) return 0; |
|
|
1
|
50
|
|
|
|
|
|
|
|
4
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
2
|
50
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
1151
|
13
|
|
|
|
|
|
BUF_ELEM_TYPE *data = (BUF_ELEM_TYPE *)h->data; |
|
|
1
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
1152
|
13
|
|
|
|
|
|
return __atomic_sub_fetch(&data[idx], 1, __ATOMIC_RELAXED); |
|
|
1
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
1153
|
|
|
|
|
|
|
} |
|
1154
|
|
|
|
|
|
|
|
|
1155
|
14
|
|
|
|
|
|
static BUF_ELEM_TYPE BUF_FN(add)(BufHandle *h, uint64_t idx, BUF_ELEM_TYPE delta) { |
|
|
1
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
1156
|
14
|
50
|
|
|
|
|
if (idx >= h->hdr->capacity) return 0; |
|
|
1
|
50
|
|
|
|
|
|
|
|
7
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
1157
|
14
|
|
|
|
|
|
BUF_ELEM_TYPE *data = (BUF_ELEM_TYPE *)h->data; |
|
|
1
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
1158
|
14
|
|
|
|
|
|
return __atomic_add_fetch(&data[idx], delta, __ATOMIC_RELAXED); |
|
|
1
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
1159
|
|
|
|
|
|
|
} |
|
1160
|
|
|
|
|
|
|
|
|
1161
|
1125
|
|
|
|
|
|
static int BUF_FN(cas)(BufHandle *h, uint64_t idx, |
|
|
1
|
|
|
|
|
|
|
|
|
1118
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
1162
|
|
|
|
|
|
|
BUF_ELEM_TYPE expected, BUF_ELEM_TYPE desired) { |
|
1163
|
1125
|
50
|
|
|
|
|
if (idx >= h->hdr->capacity) return 0; |
|
|
1
|
50
|
|
|
|
|
|
|
|
1118
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
50
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
1164
|
1125
|
|
|
|
|
|
BUF_ELEM_TYPE *data = (BUF_ELEM_TYPE *)h->data; |
|
|
1
|
|
|
|
|
|
|
|
|
1118
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
1165
|
1125
|
|
|
|
|
|
return __atomic_compare_exchange_n(&data[idx], &expected, desired, |
|
|
1
|
|
|
|
|
|
|
|
|
1118
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
1166
|
|
|
|
|
|
|
0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED); |
|
1167
|
|
|
|
|
|
|
} |
|
1168
|
|
|
|
|
|
|
|
|
1169
|
6
|
|
|
|
|
|
static BUF_ELEM_TYPE BUF_FN(cmpxchg)(BufHandle *h, uint64_t idx, |
|
|
0
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1170
|
|
|
|
|
|
|
BUF_ELEM_TYPE expected, BUF_ELEM_TYPE desired) { |
|
1171
|
6
|
0
|
|
|
|
|
if (idx >= h->hdr->capacity) return expected; |
|
|
0
|
50
|
|
|
|
|
|
|
|
6
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1172
|
6
|
|
|
|
|
|
BUF_ELEM_TYPE *data = (BUF_ELEM_TYPE *)h->data; |
|
|
0
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1173
|
6
|
|
|
|
|
|
__atomic_compare_exchange_n(&data[idx], &expected, desired, |
|
|
0
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1174
|
|
|
|
|
|
|
0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED); |
|
1175
|
6
|
|
|
|
|
|
return expected; /* on failure, expected is updated to the current value */ |
|
|
0
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1176
|
|
|
|
|
|
|
} |
|
1177
|
|
|
|
|
|
|
|
|
1178
|
4
|
|
|
|
|
|
static BUF_ELEM_TYPE BUF_FN(atomic_and)(BufHandle *h, uint64_t idx, BUF_ELEM_TYPE mask) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1179
|
4
|
0
|
|
|
|
|
if (idx >= h->hdr->capacity) return 0; |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
4
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1180
|
4
|
|
|
|
|
|
BUF_ELEM_TYPE *data = (BUF_ELEM_TYPE *)h->data; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1181
|
4
|
|
|
|
|
|
return __atomic_and_fetch(&data[idx], mask, __ATOMIC_RELAXED); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1182
|
|
|
|
|
|
|
} |
|
1183
|
|
|
|
|
|
|
|
|
1184
|
4
|
|
|
|
|
|
static BUF_ELEM_TYPE BUF_FN(atomic_or)(BufHandle *h, uint64_t idx, BUF_ELEM_TYPE mask) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1185
|
4
|
0
|
|
|
|
|
if (idx >= h->hdr->capacity) return 0; |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
4
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1186
|
4
|
|
|
|
|
|
BUF_ELEM_TYPE *data = (BUF_ELEM_TYPE *)h->data; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1187
|
4
|
|
|
|
|
|
return __atomic_or_fetch(&data[idx], mask, __ATOMIC_RELAXED); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1188
|
|
|
|
|
|
|
} |
|
1189
|
|
|
|
|
|
|
|
|
1190
|
2
|
|
|
|
|
|
static BUF_ELEM_TYPE BUF_FN(atomic_xor)(BufHandle *h, uint64_t idx, BUF_ELEM_TYPE mask) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1191
|
2
|
0
|
|
|
|
|
if (idx >= h->hdr->capacity) return 0; |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
2
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1192
|
2
|
|
|
|
|
|
BUF_ELEM_TYPE *data = (BUF_ELEM_TYPE *)h->data; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1193
|
2
|
|
|
|
|
|
return __atomic_xor_fetch(&data[idx], mask, __ATOMIC_RELAXED); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1194
|
|
|
|
|
|
|
} |
|
1195
|
|
|
|
|
|
|
|
|
1196
|
1003
|
|
|
|
|
|
static int BUF_FN(add_slice)(BufHandle *h, uint64_t from, uint64_t count, |
|
|
0
|
|
|
|
|
|
|
|
|
1002
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1197
|
|
|
|
|
|
|
const BUF_ELEM_TYPE *deltas) { |
|
1198
|
1003
|
0
|
|
|
|
|
if (count > h->hdr->capacity || from > h->hdr->capacity - count) return 0; |
|
|
0
|
0
|
|
|
|
|
|
|
|
1002
|
50
|
|
|
|
|
|
|
|
1
|
100
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
0
|
50
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
1199
|
1002
|
|
|
|
|
|
BUF_ELEM_TYPE *data = (BUF_ELEM_TYPE *)h->data; |
|
|
0
|
|
|
|
|
|
|
|
|
1001
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1200
|
4007
|
0
|
|
|
|
|
for (uint64_t i = 0; i < count; i++) |
|
|
0
|
100
|
|
|
|
|
|
|
|
4004
|
100
|
|
|
|
|
|
|
|
3
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1201
|
3005
|
|
|
|
|
|
__atomic_add_fetch(&data[from + i], deltas[i], __ATOMIC_RELAXED); |
|
|
0
|
|
|
|
|
|
|
|
|
3003
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1202
|
1002
|
|
|
|
|
|
return 1; |
|
|
0
|
|
|
|
|
|
|
|
|
1001
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1203
|
|
|
|
|
|
|
} |
|
1204
|
|
|
|
|
|
|
|
|
1205
|
|
|
|
|
|
|
#endif /* BUF_HAS_COUNTERS */ |
|
1206
|
|
|
|
|
|
|
|
|
1207
|
|
|
|
|
|
|
/* ---- Diagnostics ---- */ |
|
1208
|
|
|
|
|
|
|
|
|
1209
|
11
|
|
|
|
|
|
static inline uint64_t BUF_FN(capacity)(BufHandle *h) { |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1210
|
11
|
|
|
|
|
|
return h->hdr->capacity; |
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
1
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1211
|
|
|
|
|
|
|
} |
|
1212
|
|
|
|
|
|
|
|
|
1213
|
1
|
|
|
|
|
|
static inline uint64_t BUF_FN(mmap_size)(B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0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1219
|
|
|
|
|
|
|
} |
|
1220
|
|
|
|
|
|
|
|
|
1221
|
|
|
|
|
|
|
/* ---- Raw pointer access (for passing to external C/XS code) ---- */ |
|
1222
|
|
|
|
|
|
|
|
|
1223
|
2
|
|
|
|
|
|
static inline void *BUF_FN(ptr)(BufHandle *h) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1224
|
2
|
|
|
|
|
|
return h->data; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1225
|
|
|
|
|
|
|
} |
|
1226
|
|
|
|
|
|
|
|
|
1227
|
4
|
|
|
|
|
|
static inline void *BUF_FN(ptr_at)(BufHandle *h, uint64_t idx) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1228
|
4
|
0
|
|
|
|
|
if (idx >= h->hdr->capacity) return NULL; |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
100
|
|
|
|
|
|
|
|
4
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1229
|
3
|
|
|
|
|
|
return (char *)h->data + idx * h->hdr->elem_size; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1230
|
|
|
|
|
|
|
} |
|
1231
|
|
|
|
|
|
|
|
|
1232
|
|
|
|
|
|
|
/* ---- Explicit locking for batch operations ---- */ |
|
1233
|
|
|
|
|
|
|
|
|
1234
|
9
|
|
|
|
|
|
static inline void BUF_FN(lock_wr)(BufHandle *h) { |
|
|
2
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
1235
|
9
|
|
|
|
|
|
buf_rwlock_wrlock(h); |
|
|
2
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
7
|
|
|
|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BUF_VARIANT_ID |
|
1263
|
|
|
|
|
|
|
#ifdef BUF_HAS_COUNTERS |
|
1264
|
|
|
|
|
|
|
#undef BUF_HAS_COUNTERS |
|
1265
|
|
|
|
|
|
|
#endif |
|
1266
|
|
|
|
|
|
|
#ifdef BUF_IS_FLOAT |
|
1267
|
|
|
|
|
|
|
#undef BUF_IS_FLOAT |
|
1268
|
|
|
|
|
|
|
#endif |
|
1269
|
|
|
|
|
|
|
#ifdef BUF_IS_FIXEDSTR |
|
1270
|
|
|
|
|
|
|
#undef BUF_IS_FIXEDSTR |
|
1271
|
|
|
|
|
|
|
#endif |