include/sa/sa_arena.h

filter: Statementstmt 92.1% 106 / 115 Branchbran 65.0% 78 / 120 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.3% 184 / 235 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/rate.xs ↑ Summary include/sa/sa_cuckoo.h »
1 #ifndef SA_ARENA_H
2 #define SA_ARENA_H
3  
4 /* sa_arena.h - the handle, the bump allocator and the name registry.
5 * Perl-free.
6 *
7 * ---- the handle is process-local, and that is the point ---------------------
8 *
9 * Everything a process knows about a region that is NOT shared lives here:
10 * the base address it mapped at, the length, the name it opened. A consuming
11 * .so gets its own handle and shares the region, rather than getting its own
12 * copy of a static and quietly sharing nothing - which is the failure this
13 * design starts from.
14 *
15 * ---- allocation is a bump, and there is no free -----------------------------
16 *
17 * A sub-region is carved by advancing a high-water offset and lives as long as
18 * the region does. That is a deliberate limit rather than an omission.
19 *
20 * A free list in shared memory is corrupted permanently by one process dying
21 * between two stores, and unlike a torn record there is no way to detect it
22 * afterwards or bound the damage: the next allocation hands out memory somebody
23 * else is using. Nothing that wants an arena needs to release a sub-region - a
24 * bus takes one for its whole life, a rate limiter takes two tables, a bloom
25 * filter takes three. A caller who genuinely needs reuse can implement it
26 * INSIDE its own carved region, where getting it wrong costs one feature
27 * instead of the whole arena.
28 *
29 * ---- the registry is what makes attaching worth anything --------------------
30 *
31 * Without it, a second process has to be told an offset out of band, which
32 * means the two builds must agree on a layout neither can check. With it, a
33 * late arriver asks for "events" and is handed the offset and the length, or
34 * nothing.
35 */
36  
37 #include "sa/sa_map.h"
38  
39 #ifndef SA_REGION_FWD
40 #define SA_REGION_FWD
41 typedef struct sa_region sa_region;
42 #endif
43  
44 struct sa_region {
45 sa_map map;
46 sa_header *hdr; /* == map.base, cached */
47 uint64_t total;
48 int created; /* did THIS process create it */
49  
50 /* This process's peer slot, filled lazily on first use. `peer_pid` is
51 * what makes a fork child take a slot of its OWN rather than inheriting
52 * its parent's, which would attribute the child's records to the wrong
53 * process and make a crash unprovable. */
54 int peer_idx;
55 uint32_t peer_epoch;
56 uint64_t peer_pid;
57  
58 /* Why the last region lookup or carve on THIS handle came back empty.
59 *
60 * Diagnostic only, and on the handle rather than at file scope so two
61 * handles - or two ithreads - do not overwrite each other's answer. It
62 * exists because an empty list is not a reason: a smoker that refused two
63 * carves could not say whether the arena was full or a stripe was busy,
64 * and those want opposite responses from the caller. */
65 int last_err;
66  
67 /* The waker table, carved like any other sub-region and found by name, so
68 * a fork child resolves it without being told where it is. */
69 uint64_t wakers_off;
70 uint32_t wakers_max;
71 int waker_idx;
72 /* Looked for the waker table and found none. Cached, because sa_wake_poke
73 * runs on every publish and a registry walk per record on a ring that
74 * simply has no wakers was a measurable share of the publish. wake_init
75 * clears it. */
76 int wakers_probed;
77  
78 /* Whether the descriptors in the waker table are the ones THIS process
79 * holds, answered once with an fstat and cached here rather than in the
80 * mapping - it is a fact about this process, not about the region. See the
81 * identity comment in sa_wake.h. */
82 int wake_checked;
83 int wake_ok;
84 uint64_t wake_pid;
85 };
86  
87 /* ---- carving --------------------------------------------------------------- */
88  
89 /* Advance the high-water mark by `len` and return the offset, or 0 when the
90 * region is full. Offset 0 is never a valid carve, because the header lives
91 * there, so 0 doubles as the failure value.
92 *
93 * A CAS loop rather than a fetch-add: a fetch-add that overshoots has already
94 * consumed the space it could not use, so a region that refuses one oversized
95 * carve would go on refusing every small one after it. */
96 40779 static uint64_t sa_bump(sa_region *r, uint64_t len) {
97 #if !SA_HAVE_ATOMICS
98 (void)r; (void)len;
99 return 0;
100 #else
101 40779 sa_header *h = r->hdr;
102 uint64_t old, want;
103 for (;;) {
104 40857 old = sa_at_load64_acq(&h->brk);
105 40857 want = sa_align_up(old);
106 40857 ▶ if (want + len > h->total || want + len < want) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (want + len > h->total || want + len < want) return 0; 40856 1 40857
if (want + len > h->total || want + len < want) return 0; 0 40856 40856
107 40856 ▶ if (sa_at_cas64(&h->brk, old, want + len)) return want;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_cas64(&h->brk, old, want + len)) return want; 40778 78 40856
108 }
109 #endif
110 }
111  
112 /* ---- AN OFFSET IS NOT ENOUGH: A STRUCTURE HAS A LENGTH --------------------
113 *
114 * sa_ptr answers whether the START of something is inside the mapping, which
115 * is not the question a caller about to read a structure is asking. Every
116 * tenant used to bind with it and then check its own size against the registry
117 * entry's `len` - a number that comes OUT OF THE SHARED SEGMENT, so anybody
118 * who can write to the segment chooses it.
119 *
120 * Measured: a process that knew only the name rewrote one registry entry to
121 * `off = total - 64, len = 1GB`, and the next process to bind that ring wrote
122 * about twenty-one kilobytes past the end of its own mapping. SIGSEGV, and the
123 * POD at the time promised that "a reader will not follow a length past the
124 * end of a mapping". t/30-bounds.t is that PoC.
125 *
126 * This is the check that makes the promise true. The comparison is written as
127 * `off > total - len` rather than `off + len > total` BECAUSE THE ADDITION CAN
128 * WRAP: with a 64-bit len chosen by an attacker, `off + len` is small again and
129 * the guard passes the very case it exists to refuse.
130 *
131 * The lower bound keeps a carve off the header, the registry and the peer
132 * table, which is where a hostile entry would point to make a tenant's memset
133 * eat the arena's own bookkeeping. */
134 5399 static void *sa_ptr_len(sa_region *r, uint64_t off, uint64_t len) {
135 uint64_t floor;
136 5399 ▶ if (!r || !r->map.base || !r->hdr) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!r || !r->map.base || !r->hdr) return NULL; 5399 0 5399
if (!r || !r->map.base || !r->hdr) return NULL; 5399 0 5399
if (!r || !r->map.base || !r->hdr) return NULL; 0 5399 5399
137 5399 floor = r->hdr->peers_off
138 5399 + (uint64_t)r->hdr->peers_max * (uint64_t)sizeof(sa_peer);
139 5399 ▶ if (off < floor) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (off < floor) return NULL; 0 5399 5399
140 5399 ▶ if (len > r->total) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (len > r->total) return NULL; 1 5398 5399
141 5398 ▶ if (off > r->total - len) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (off > r->total - len) return NULL; 0 5398 5398
142 5398 return SA_AT(r->map.base, off);
143 }
144  
145 /* Does this registry entry describe bytes that are actually in the mapping?
146 *
147 * Checked where an entry is READ rather than in each of the seven tenants that
148 * use one, so a tenant added later cannot forget it. An entry that fails is
149 * treated as not live: a name that does not resolve, rather than a pointer
150 * into somebody else's memory. */
151 4495 static int sa_reg_ok(sa_region *r, const sa_reg *e) {
152 4495 ▶ return e && sa_ptr_len(r, e->off, e->len) != NULL;
Branch: 2 branches
BranchTrueFalseTotal
return e && sa_ptr_len(r, e->off, e->len) != NULL; 4495 0 4495
return e && sa_ptr_len(r, e->off, e->len) != NULL; 4494 1 4495
153 }
154  
155 /* The registry entry for a name, or NULL.
156 *
157 * Only entries whose `state` has been published are visible, so a carve that is
158 * still being filled in is invisible rather than half-readable. */
159 45324 static sa_reg *sa_find(sa_region *r, const char *name, size_t nlen) {
160 #if !SA_HAVE_ATOMICS
161 (void)r; (void)name; (void)nlen;
162 return NULL;
163 #else
164 45324 sa_header *h = r->hdr;
165 45324 sa_reg *regs = SA_REGS(r->map.base, h);
166 uint32_t used, i;
167  
168 45324 ▶ if (!name || !nlen || nlen >= SA_NAMELEN) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!name || !nlen || nlen >= SA_NAMELEN) return NULL; 45324 0 45324
if (!name || !nlen || nlen >= SA_NAMELEN) return NULL; 45324 0 45324
if (!name || !nlen || nlen >= SA_NAMELEN) return NULL; 0 45324 45324
169 45324 used = sa_at_load32_acq(&h->reg_used);
170 45324 ▶ if (used > h->reg_max) used = h->reg_max;
Branch: 1 branch
BranchTrueFalseTotal
if (used > h->reg_max) used = h->reg_max; 9 45315 45324
171  
172 88016597 ▶ for (i = 0; i < used; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < used; i++) { 87975767 40830 88016597
173 87975767 ▶ if (sa_at_load32_acq(&regs[i].state) != SA_R_LIVE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load32_acq(&regs[i].state) != SA_R_LIVE) continue; 2 87975765 87975767
174 /* memcmp, not strncmp: a name is `nlen` bytes and the reserved ones
175 * begin with a NUL, so strncmp called "\0leases" and "\0wakers" the
176 * same name and a region with a lease refused its wakers as a shape
177 * mismatch. */
178 87975765 ▶ if (memcmp(regs[i].name, name, nlen) == 0 && regs[i].name[nlen] == '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if (memcmp(regs[i].name, name, nlen) == 0 && regs[i].name[nlen] == '\0') { 4497 87971268 87975765
if (memcmp(regs[i].name, name, nlen) == 0 && regs[i].name[nlen] == '\0') { 4495 2 4497
179 /* AN ENTRY WHOSE EXTENT IS NOT IN THE MAPPING IS NOT AN ENTRY, so
180 * it is skipped exactly as a non-live one is - not treated as a
181 * fatal answer for the name.
182 *
183 * Returning NULL here instead looks safer and is worse twice over:
184 * the name becomes permanently unresolvable, AND sa_carve, finding
185 * nothing, carves a SECOND entry under the same name. One bad word
186 * in the registry would shadow a working region for the life of the
187 * arena. Skipping means the bad entry describes nothing anybody
188 * will dereference, and a good entry for that name still wins.
189 *
190 * It is counted, because a refused entry means either corruption or
191 * somebody writing to the segment who should not be, and a fallback
192 * that says nothing is how that goes unnoticed. */
193 4495 ▶ if (!sa_reg_ok(r, &regs[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_reg_ok(r, &regs[i])) { 1 4494 4495
194 1 sa_at_fetch_add32(&h->reg_refused, 1);
195 1 continue;
196 }
197 4494 return &regs[i];
198 }
199 }
200 40830 return NULL;
201 #endif
202 }
203  
204 /* Carve a named sub-region, or return the existing one with that name.
205 *
206 * Returning the existing one rather than refusing is what lets every process
207 * run the same setup code: whoever gets there first creates it, everybody else
208 * finds it, and no caller has to know which it was. The type and length are
209 * checked against what is already there, because two processes disagreeing
210 * about the shape of a region is worse than either of them failing.
211 *
212 * Writes *err and returns NULL on refusal. Never croaks: the reader is C.
213 */
214 /* A bounded acquire that SLEEPS between attempts instead of spinning through
215 * its whole budget in one go.
216 *
217 * sa_at_lock is a pure spin, which is right on a hot path where the holder is
218 * about to finish and a sleep would cost more than the wait. It is wrong where
219 * there are more contenders than there are cores: the budget burns while the
220 * process actually holding the lock is not scheduled at all, and the caller is
221 * refused for being unlucky rather than for anything being full.
222 *
223 * Found on a 32-bit perl under emulation, eight processes carving four
224 * thousand regions: FOUR were refused, every run, deterministically, while the
225 * same four thousand carved serially were all fine. That is not a full arena,
226 * it is a spin that never got a turn.
227 *
228 * Still bounded, and that is not negotiable: a process that died holding a
229 * stripe must not wedge everybody who comes after it. The budget is just spent
230 * as sleeps rather than as one long burn, so the holder gets a chance to run
231 * and hand it over.
232 *
233 * SIXTY-FOUR FIFTY-MICROSECOND SLEEPS WAS STILL A GUESS ABOUT THE SCHEDULER,
234 * and a loaded smoker beat it: FreeBSD 15 on perl 5.42.2 refused two carves in
235 * four thousand, with room in the arena for every one of them and the serial
236 * arm of the same test passing. Three milliseconds is an ordinary amount of
237 * time not to be scheduled on a box running a dozen builds.
238 *
239 * So the wait escalates instead: a spin first, because the usual holder really
240 * is about to finish, then sleeps doubling from 50us to a 20ms ceiling. That
241 * spends about two seconds over its rounds - the same budget sa_map_open
242 * already allows a half-written header (SA_CREATE_WAIT_US), and for the same
243 * reason. An ordinary preemption becomes invisible; a process that died
244 * holding the stripe still hands everybody else a refusal rather than a hang.
245 *
246 * The retries after the first are a single test-and-set, NOT another spin:
247 * hammering the lock byte from eight waiters is what keeps the holder from
248 * reaching its own unlock. See sa_at_trylock.
249 *
250 * For rare operations only - carving a region, publishing a block. Anything on
251 * a per-request path should use sa_at_lock and take the refusal. */
252 #define SA_LOCK_ROUNDS 110
253 #define SA_LOCK_STALL_US 50
254 #define SA_LOCK_STALL_MAX 20000
255  
256 40984 static int sa_lock_wait(volatile unsigned char *locks, uint64_t h) {
257 int round;
258 40984 uint32_t us = SA_LOCK_STALL_US;
259  
260 40984 ▶ if (sa_at_lock(locks, h)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_lock(locks, h)) return 1; 40982 2 40984
261 2 ▶ for (round = 0; round < SA_LOCK_ROUNDS; round++) {
Branch: 1 branch
BranchTrueFalseTotal
for (round = 0; round < SA_LOCK_ROUNDS; round++) { 2 0 2
262 2 sa_stall(us);
263 2 ▶ if (sa_at_trylock(locks, h)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_trylock(locks, h)) return 1; 2 0 2
264 0 ▶ if (us < SA_LOCK_STALL_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (us < SA_LOCK_STALL_MAX) { 0 0 0
265 0 us *= 2;
266 0 ▶ if (us > SA_LOCK_STALL_MAX) us = SA_LOCK_STALL_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (us > SA_LOCK_STALL_MAX) us = SA_LOCK_STALL_MAX; 0 0 0
267 }
268 }
269 0 return 0;
270 }
271  
272 40968 static sa_reg *sa_carve(sa_region *r, const char *name, size_t nlen,
273 uint64_t len, uint32_t type, int *err)
274 {
275 #if !SA_HAVE_ATOMICS
276 (void)r; (void)name; (void)nlen; (void)len; (void)type;
277 if (err) *err = SA_E_NOATOMICS;
278 return NULL;
279 #else
280 40968 sa_header *h = r->hdr;
281 sa_reg *regs, *e;
282 uint64_t off, hash;
283 uint32_t idx;
284  
285 40968 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 40968 0 40968
286 40968 ▶ if (!name || !nlen || nlen >= SA_NAMELEN) {
Branch: 3 branches
BranchTrueFalseTotal
if (!name || !nlen || nlen >= SA_NAMELEN) { 40968 0 40968
if (!name || !nlen || nlen >= SA_NAMELEN) { 40967 1 40968
if (!name || !nlen || nlen >= SA_NAMELEN) { 1 40966 40967
287 2 ▶ if (err) *err = SA_E_NAME;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_NAME; 2 0 2
288 2 return NULL;
289 }
290  
291 40966 hash = sa_at_fnv(name, nlen);
292 /* The lock covers only the name check and the claim, so two processes
293 * carving DIFFERENT names contend on a stripe at worst.
294 *
295 * WAITS RATHER THAN SPINS. Carving happens once per region at startup, so
296 * a few microseconds of sleep costs nothing and a refusal costs a worker
297 * its region. See sa_lock_wait: a pure spin refused four carves in four
298 * thousand, every run, on a machine with fewer cores than carvers. */
299 40966 ▶ if (!sa_lock_wait(h->locks, hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_lock_wait(h->locks, hash)) { 0 40966 40966
300 /* SA_E_BUSY, not SA_E_FULL: nothing here is full. */
301 0 ▶ if (err) *err = SA_E_BUSY;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_BUSY; 0 0 0
302 0 return NULL;
303 }
304  
305 40966 e = sa_find(r, name, nlen);
306 40966 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 177 40789 40966
307 /* SA_E_SHAPE, not SA_E_NAME. The name is fine - it is the type or the
308 * size that disagrees with what is already carved, and telling a caller
309 * its name is "too long or empty" when the name is neither sends it
310 * looking in the wrong place entirely. */
311 177 ▶ int bad = (e->type != type) || (len && e->len < len);
Branch: 3 branches
BranchTrueFalseTotal
int bad = (e->type != type) || (len && e->len < len); 176 1 177
int bad = (e->type != type) || (len && e->len < len); 116 60 176
int bad = (e->type != type) || (len && e->len < len); 21 95 116
312 177 sa_at_unlock(h->locks, hash);
313 177 ▶ if (bad) { if (err) *err = SA_E_SHAPE; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (bad) { if (err) *err = SA_E_SHAPE; return NULL; } 22 155 177
if (bad) { if (err) *err = SA_E_SHAPE; return NULL; } 22 0 22
314 155 return e;
315 }
316  
317 /* THE SLOT IS CLAIMED WITH ONE ATOMIC, NOT READ AND THEN WRITTEN.
318 *
319 * The stripe lock above is keyed on the NAME, so it serialises two carves
320 * of the same name and does nothing at all for two carves of different
321 * ones - and the registry append is a resource they share. Reading
322 * `reg_used` here and storing `idx + 1` twenty lines later left a window in
323 * which two processes took the same index, both filled it, and the loser's
324 * entry was gone with its arena space still spent. Both callers were told
325 * they had succeeded.
326 *
327 * Measured before it was fixed: eight processes carving 4000 names between
328 * them kept 3263 of them, with no refusal reported for the other 737.
329 * t/04-carve-race.t is that test.
330 *
331 * fetch-add cannot overshoot into somebody else's entry, so `reg_used` may
332 * pass `reg_max` when the registry is full and every reader already clamps
333 * it - `sa_find` and `sa_regions` both do. It is never decremented: undoing
334 * it would hand the same index to a caller that is still writing. */
335 40789 idx = sa_at_fetch_add32(&h->reg_used, 1);
336 40789 ▶ if (idx >= h->reg_max) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= h->reg_max) { 10 40779 40789
337 10 sa_at_unlock(h->locks, hash);
338 10 ▶ if (err) *err = SA_E_FULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_FULL; 10 0 10
339 10 return NULL;
340 }
341  
342 40779 off = sa_bump(r, len);
343 40779 ▶ if (!off) {
Branch: 1 branch
BranchTrueFalseTotal
if (!off) { 1 40778 40779
344 1 sa_at_unlock(h->locks, hash);
345 1 ▶ if (err) *err = SA_E_FULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_FULL; 1 0 1
346 1 return NULL;
347 }
348  
349 40778 regs = SA_REGS(r->map.base, h);
350 40778 e = &regs[idx];
351 40778 memset(e->name, 0, SA_NAMELEN);
352 40778 memcpy(e->name, name, nlen);
353 40778 e->off = off;
354 40778 e->len = len;
355 40778 e->type = type;
356 /* `state` is published LAST with a release store: the count already moved
357 * when the index was claimed, so a concurrent reader scanning this far
358 * finds an entry that is still EMPTY rather than one that is half filled.
359 * A scan that sees nothing is a miss it will retry; a scan that reads a
360 * half-filled entry is a wrong answer it cannot detect. */
361 40778 sa_at_store32_rel(&e->state, SA_R_LIVE);
362  
363 40778 sa_at_unlock(h->locks, hash);
364 40778 return e;
365 #endif
366 }
367  
368 /* An offset into this process's address space. The one place an offset becomes
369 * a pointer, and it is never stored anywhere shared. */
370 287 static void *sa_ptr(sa_region *r, uint64_t off) {
371 287 ▶ if (!r || !r->map.base || off >= r->total) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!r || !r->map.base || off >= r->total) return NULL; 287 0 287
if (!r || !r->map.base || off >= r->total) return NULL; 287 0 287
if (!r || !r->map.base || off >= r->total) return NULL; 1 286 287
372 286 return SA_AT(r->map.base, off);
373 }
374  
375  
376 /* ---- lifecycle -------------------------------------------------------------- */
377  
378 161 static sa_region *sa_region_open(const char *name, size_t nlen, uint64_t len,
379 uint32_t reg_max, int may_create, int *err)
380 {
381 sa_region *r;
382 char nbuf[SA_NAMELEN + 8];
383 161 const char *nm = NULL;
384 161 int rc, created = 0;
385  
386 161 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 161 0 161
387  
388 161 ▶ if (name && nlen) {
Branch: 2 branches
BranchTrueFalseTotal
if (name && nlen) { 12 149 161
if (name && nlen) { 12 0 12
389 /* A POSIX shared name must start with a slash and contain no others,
390 * and macOS caps the whole thing at 31 characters including the NUL -
391 * shorter than PATH_MAX by a long way and the reason a name that works
392 * on Linux can fail there. Keep the caller's name inside SA_NAMELEN and
393 * the slash is ours to add. */
394 12 ▶ if (nlen >= SA_NAMELEN || memchr(name, '/', nlen)) {
Branch: 2 branches
BranchTrueFalseTotal
if (nlen >= SA_NAMELEN || memchr(name, '/', nlen)) { 12 0 12
if (nlen >= SA_NAMELEN || memchr(name, '/', nlen)) { 0 12 12
395 0 ▶ if (err) *err = SA_E_NAME;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_NAME; 0 0 0
396 0 return NULL;
397 }
398 12 nbuf[0] = '/';
399 12 memcpy(nbuf + 1, name, nlen);
400 12 nbuf[nlen + 1] = '\0';
401 12 nm = nbuf;
402 }
403  
404 161 r = (sa_region *)calloc(1, sizeof(sa_region));
405 161 ▶ if (!r) { if (err) *err = SA_E_NOMEM; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!r) { if (err) *err = SA_E_NOMEM; return NULL; } 0 161 161
if (!r) { if (err) *err = SA_E_NOMEM; return NULL; } 0 0 0
406 161 r->peer_idx = -1;
407 161 r->waker_idx = -1;
408  
409 161 rc = sa_map_open(&r->map, nm, len, reg_max, &created, may_create);
410 161 ▶ if (rc != SA_E_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SA_E_OK) { 2 159 161
411 2 free(r);
412 2 ▶ if (err) *err = rc;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = rc; 2 0 2
413 2 return NULL;
414 }
415  
416 159 r->hdr = (sa_header *)r->map.base;
417 159 r->total = r->hdr->total;
418 159 r->created = created;
419 159 return r;
420 }
421  
422 159 static void sa_region_release(sa_region *r) {
423 159 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 159 159
424 159 sa_map_release(&r->map);
425 159 free(r);
426 }
427  
428 8 static int sa_region_destroy(const char *name, size_t nlen) {
429 char nbuf[SA_NAMELEN + 8];
430 8 ▶ if (!name || !nlen || nlen >= SA_NAMELEN || memchr(name, '/', nlen))
Branch: 4 branches
BranchTrueFalseTotal
if (!name || !nlen || nlen >= SA_NAMELEN || memchr(name, '/', nlen)) 8 0 8
if (!name || !nlen || nlen >= SA_NAMELEN || memchr(name, '/', nlen)) 8 0 8
if (!name || !nlen || nlen >= SA_NAMELEN || memchr(name, '/', nlen)) 8 0 8
if (!name || !nlen || nlen >= SA_NAMELEN || memchr(name, '/', nlen)) 0 8 8
431 0 return SA_E_NAME;
432 8 nbuf[0] = '/';
433 8 memcpy(nbuf + 1, name, nlen);
434 8 nbuf[nlen + 1] = '\0';
435 8 return sa_os_destroy(nbuf);
436 }
437  
438 #endif /* SA_ARENA_H */
« xs/rate.xs ↑ Summary include/sa/sa_cuckoo.h »