lib/Hash/SharedMem.xs

filter: Statementstmt 88.2% 1513 / 1715 Branchbran 66.6% 636 / 954 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.5% 2149 / 2669 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Hash/SharedMem.pm ↑ Summary blib/lib/Hash/SharedMem/Handle.pm »
1 #define PERL_NO_GET_CONTEXT 1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "callchecker0.h"
6 #include <sys/mman.h>
7  
8 /* Perl compatibility */
9  
10 #define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
11 #define PERL_DECIMAL_VERSION \
12 PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
13 #define PERL_VERSION_GE(r,v,s) \
14 (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
15  
16 #if !PERL_VERSION_GE(5,7,2)
17 # undef dNOOP
18 # define dNOOP extern int Perl___notused_func(void)
19 #endif /* <5.7.2 */
20  
21 #ifndef cBOOL
22 # define cBOOL(x) ((bool)!!(x))
23 #endif /* !cBOOL */
24  
25 #ifndef EXPECT
26 # ifdef __GNUC__
27 # define EXPECT(e, v) __builtin_expect(e, v)
28 # else /* !__GNUC__ */
29 # define EXPECT(e, v) (e)
30 # endif /* !__GNUC__ */
31 #endif /* !EXPECT */
32  
33 #define likely(t) EXPECT(cBOOL(t), 1)
34 #define unlikely(t) EXPECT(cBOOL(t), 0)
35  
36 #ifndef __attribute__noreturn__
37 # ifdef __GNUC__
38 # define __attribute__noreturn__ __attribute__((noreturn))
39 # else /* !__GNUC__ */
40 # define __attribute__noreturn__ /**/
41 # endif /* !__GNUC__ */
42 #endif /* !__attribute__noreturn__ */
43  
44 #ifndef C_ARRAY_LENGTH
45 # define C_ARRAY_LENGTH(a) (sizeof(a)/sizeof(*(a)))
46 #endif /* !C_ARRAY_LENGTH */
47  
48 #ifndef PERL_STATIC_INLINE
49 # define PERL_STATIC_INLINE static
50 #endif /* !PERL_STATIC_INLINE */
51  
52 #ifndef PERL_UNUSED_VAR
53 # define PERL_UNUSED_VAR(x) ((void)x)
54 #endif /* !PERL_UNUSED_VAR */
55  
56 #ifndef PERL_UNUSED_ARG
57 # define PERL_UNUSED_ARG(x) PERL_UNUSED_VAR(x)
58 #endif /* !PERL_UNUSED_ARG */
59  
60 #ifndef STATIC_ASSERT_DECL
61 # ifdef STATIC_ASSERT_GLOBAL
62 # define STATIC_ASSERT_DECL STATIC_ASSERT_GLOBAL
63 # else /* !STATIC_ASSERT_GLOBAL */
64 # define STATIC_ASSERT_2(COND, SUFFIX) \
65 enum { STATIC_ASSERT_ENUM_##SUFFIX = 1/(cBOOL(COND)) }
66 # define STATIC_ASSERT_1(COND, SUFFIX) STATIC_ASSERT_2(COND, SUFFIX)
67 # define STATIC_ASSERT_DECL(COND) STATIC_ASSERT_1(COND, __LINE__)
68 # endif /* !STATIC_ASSERT_GLOBAL */
69 #endif /* !STATIC_ASSERT_DECL */
70  
71 #ifndef DPTR2FPTR
72 # define DPTR2FPTR(t,x) ((t)(UV)(x))
73 #endif /* !DPTR2FPTR */
74  
75 #ifndef FPTR2DPTR
76 # define FPTR2DPTR(t,x) ((t)(UV)(x))
77 #endif /* !FPTR2DPTR */
78  
79 #ifndef Newx
80 # define Newx(v,n,t) New(0,v,n,t)
81 #endif /* !Newx */
82  
83 #ifndef Newxz
84 # define Newxz(v,n,t) Newz(0,v,n,t)
85 #endif /* !Newxz */
86  
87 #ifndef newSVpv_share
88 # ifdef newSVpvn_share
89 # define newSVpv_share(pv, hash) THX_newSVpv_share(aTHX_ pv, hash)
90 PERL_STATIC_INLINE SV *THX_newSVpv_share(pTHX_ char const *pv, U32 hash)
91 {
92 return newSVpvn_share(pv, strlen(pv), hash);
93 }
94 # else /* !newSVpvn_share */
95 # define newSVpv_share(pv, hash) newSVpv(pv, 0)
96 # define SvSHARED_HASH(sv) 0
97 # endif /* !newSVpvn_share */
98 #endif /* !newSVpv_share */
99  
100 #ifndef SvSHARED_HASH
101 # define SvSHARED_HASH(sv) SvUVX(sv)
102 #endif /* !SvSHARED_HASH */
103  
104 #ifndef SvREFCNT_inc_NN
105 # define SvREFCNT_inc_NN SvREFCNT_inc
106 #endif /* !SvREFCNT_inc_NN */
107  
108 #ifndef SvREFCNT_inc_simple
109 # define SvREFCNT_inc_simple SvREFCNT_inc
110 #endif /* !SvREFCNT_inc_simple */
111  
112 #ifndef SvREFCNT_inc_simple_NN
113 # define SvREFCNT_inc_simple_NN SvREFCNT_inc_NN
114 #endif /* !SvREFCNT_inc_simple_NN */
115  
116 #ifndef SvREFCNT_inc_void
117 # define SvREFCNT_inc_void(sv) ((void) SvREFCNT_inc(sv))
118 #endif /* !SvREFCNT_inc_void */
119  
120 #ifndef SvREFCNT_inc_void_NN
121 # define SvREFCNT_inc_void_NN(sv) ((void) SvREFCNT_inc_NN(sv))
122 #endif /* !SvREFCNT_inc_void_NN */
123  
124 #ifndef SvREFCNT_inc_simple_void
125 # define SvREFCNT_inc_simple_void(sv) ((void) SvREFCNT_inc_simple(sv))
126 #endif /* !SvREFCNT_inc_simple_void */
127  
128 #ifndef SvREFCNT_inc_simple_void_NN
129 # define SvREFCNT_inc_simple_void_NN(sv) ((void) SvREFCNT_inc_simple_NN(sv))
130 #endif /* !SvREFCNT_inc_simple_void_NN */
131  
132 #ifndef SvREFCNT_dec_NN
133 # define SvREFCNT_dec_NN SvREFCNT_dec
134 #endif /* !SvREFCNT_dec_NN */
135  
136 #ifndef SvUV_set
137 # define SvUV_set(sv, uv) (SvUVX(sv) = (uv))
138 #endif /* !SvUV_set */
139  
140 #ifndef CvPROTO
141 # define CvPROTO(cv) SvPVX((SV*)(cv))
142 # define CvPROTOLEN(cv) SvCUR((SV*)(cv))
143 #endif /* !CvPROTO */
144  
145 #if PERL_VERSION_GE(5,7,3)
146 # define PERL_UNUSED_THX() NOOP
147 #else /* <5.7.3 */
148 # define PERL_UNUSED_THX() ((void)(aTHX+0))
149 #endif /* <5.7.3 */
150  
151 #ifndef SvMAGIC_set
152 # define SvMAGIC_set(sv, mg) (SvMAGIC(sv) = (mg))
153 #endif /* !SvMAGIC_set */
154  
155 #ifndef PERL_MAGIC_ext
156 # define PERL_MAGIC_ext '~'
157 #endif /* !PERL_MAGIC_ext */
158  
159 #ifndef sv_magicext
160 # define sv_magicext(sv, obj, type, vtbl, name, namlen) \
161 THX_sv_magicext(aTHX_ sv, obj, type, vtbl, name, namlen)
162 static MAGIC *THX_sv_magicext(pTHX_ SV *sv, SV *obj, int type,
163 MGVTBL const *vtbl, char const *name, I32 namlen)
164 {
165 MAGIC *mg;
166 PERL_UNUSED_ARG(namlen);
167 Newxz(mg, 1, MAGIC);
168 mg->mg_virtual = (MGVTBL*)vtbl;
169 mg->mg_type = type;
170 mg->mg_obj = obj;
171 if(likely(obj && obj != sv)) {
172 SvREFCNT_inc_simple_void_NN(obj);
173 mg->mg_flags |= MGf_REFCOUNTED;
174 }
175 mg->mg_ptr = (char*)name;
176 (void) SvUPGRADE(sv, SVt_PVMG);
177 mg->mg_moremagic = SvMAGIC(sv);
178 SvMAGIC_set(sv, mg);
179 SvMAGICAL_off(sv);
180 mg_magical(sv);
181 return mg;
182 }
183 #endif /* !sv_magicext */
184  
185 #ifndef sv_unmagicext
186 # define sv_unmagicext(sv, type, vtbl) THX_sv_unmagicext(aTHX_ sv, type, vtbl)
187 PERL_STATIC_INLINE int THX_sv_unmagicext(pTHX_ SV *sv, int type,
188 MGVTBL const *vtbl)
189 {
190 MAGIC *mg, **mgp;
191 if(unlikely(SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))) return 0;
192 mgp = NULL;
193 for(mg = SvMAGIC(sv); mg; mg = unlikely(mgp) ? *mgp : SvMAGIC(sv)) {
194 if(likely(mg->mg_type == type && mg->mg_virtual == vtbl)) {
195 if(unlikely(mgp))
196 *mgp = mg->mg_moremagic;
197 else
198 SvMAGIC_set(sv, mg->mg_moremagic);
199 if(likely(vtbl->svt_free)) vtbl->svt_free(aTHX_ sv, mg);
200 if(unlikely(mg->mg_flags & MGf_REFCOUNTED))
201 SvREFCNT_dec(mg->mg_obj);
202 Safefree(mg);
203 } else {
204 mgp = &mg->mg_moremagic;
205 }
206 }
207 SvMAGICAL_off(sv);
208 mg_magical(sv);
209 return 0;
210 }
211 #endif /* !sv_unmagicext */
212  
213 #ifndef newSV_type
214 # define newSV_type(type) THX_newSV_type(aTHX_ type)
215 static SV *THX_newSV_type(pTHX_ svtype type)
216 {
217 SV *sv = newSV(0);
218 (void) SvUPGRADE(sv, type);
219 return sv;
220 }
221 #endif /* !newSV_type */
222  
223 #ifndef gv_stashpvs
224 # define gv_stashpvs(name, flags) gv_stashpvn(""name"", sizeof(name)-1, flags)
225 #endif /* !gv_stashpvs */
226  
227 #ifndef hv_fetchs
228 # define hv_fetchs(hv, keystr, lval) \
229 hv_fetch(hv, ""keystr"", sizeof(keystr)-1, lval)
230 #endif /* !hv_fetchs */
231  
232 #ifndef hv_stores
233 # define hv_stores(hv, keystr, val) \
234 hv_store(hv, ""keystr"", sizeof(keystr)-1, val, 0)
235 #endif /* !hv_stores */
236  
237 #if defined(USE_ITHREADS) && !defined(sv_dup_inc)
238 # define sv_dup_inc(sv, param) SvREFCNT_inc(sv_dup(sv, param))
239 #endif /* USE_ITHREADS && !sv_dup_inc */
240  
241 #if !PERL_VERSION_GE(5,9,3)
242 typedef OP *(*Perl_ppaddr_t)(pTHX);
243 #endif /* <5.9.3 */
244  
245 #ifndef SvPV_nomg
246 # define SvPV_nomg(sv, len) \
247 (unlikely(SvGMAGICAL(sv)) ? THX_SvPV_nomg_magical(aTHX_ sv, &(len)) : \
248 SvPV(sv, len))
249 struct remagic {
250 SV *sv;
251 MAGIC *mg;
252 U32 flags;
253 };
254 static void THX_remagic_cleanup(pTHX_ void *remagic_v)
255 {
256 struct remagic remagic = *(struct remagic *)remagic_v;
257 Safefree(remagic_v);
258 if(unlikely(remagic.sv)) {
259 SvMAGIC(remagic.sv) = remagic.mg;
260 SvFLAGS(remagic.sv) |= remagic.flags;
261 SvREFCNT_dec_NN(remagic.sv);
262 }
263 }
264 static char *THX_SvPV_nomg_magical(pTHX_ SV *sv, STRLEN *len_p)
265 {
266 char *pv;
267 struct remagic *remagic;
268 Newx(remagic, 1, struct remagic);
269 remagic->sv = sv;
270 remagic->mg = SvMAGIC(sv);
271 remagic->flags = SvMAGICAL(sv);
272 SAVEDESTRUCTOR_X(THX_remagic_cleanup, remagic);
273 SvREFCNT_inc_simple_void_NN(sv);
274 SvMAGIC(sv) = NULL;
275 pv = SvPV(sv, *len_p);
276 SvMAGIC(sv) = remagic->mg;
277 SvFLAGS(sv) |= remagic->flags;
278 SvREFCNT_dec_NN(sv);
279 remagic->sv = NULL;
280 return pv;
281 }
282 #endif /* !SvPV_nomg */
283  
284 #ifndef START_MY_CXT
285 # ifdef PERL_IMPLICIT_CONTEXT
286 # define START_MY_CXT
287 # define dMY_CXT_SV \
288 SV *my_cxt_sv = *hv_fetch(PL_modglobal, \
289 MY_CXT_KEY, sizeof(MY_CXT_KEY)-1, 1)
290 # define dMY_CXT \
291 dMY_CXT_SV; my_cxt_t *my_cxtp = INT2PTR(my_cxt_t*, SvUV(my_cxt_sv))
292 # define MY_CXT_INIT \
293 dMY_CXT_SV; \
294 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
295 Zero(my_cxtp, 1, my_cxt_t); \
296 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
297 # define MY_CXT (*my_cxtp)
298 # define pMY_CXT my_cxt_t *my_cxtp
299 # define pMY_CXT_ pMY_CXT,
300 # define _pMY_CXT ,pMY_CXT
301 # define aMY_CXT my_cxtp
302 # define aMY_CXT_ aMY_CXT,
303 # define _aMY_CXT ,aMY_CXT
304 # else /* !PERL_IMPLICIT_CONTEXT */
305 # define START_MY_CXT static my_cxt_t my_cxt;
306 # define dMY_CXT dNOOP
307 # define MY_CXT_INIT NOOP
308 # define MY_CXT my_cxt
309 # define pMY_CXT void
310 # define pMY_CXT_ /**/
311 # define _pMY_CXT /**/
312 # define aMY_CXT /**/
313 # define aMY_CXT_ /**/
314 # define _aMY_CXT /**/
315 # endif /* !PERL_IMPLICIT_CONTEXT */
316 #endif /* !START_MY_CXT */
317  
318 #ifndef MY_CXT_CLONE
319 # ifdef PERL_IMPLICIT_CONTEXT
320 # define MY_CXT_CLONE \
321 dMY_CXT_SV; \
322 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
323 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
324 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
325 # else /* !PERL_IMPLICIT_CONTEXT */
326 # define MY_CXT_CLONE NOOP
327 # endif /* !PERL_IMPLICIT_CONTEXT */
328 #endif /* !MY_CXT_CLONE */
329  
330 #if PERL_VERSION_GE(5,19,4)
331 typedef SSize_t tmps_ix_t;
332 #else /* <5.19.4 */
333 typedef I32 tmps_ix_t;
334 #endif /* <5.19.4 */
335  
336 #if PERL_VERSION_GE(5,19,4)
337 typedef SSize_t array_ix_t;
338 #else /* <5.19.4 */
339 typedef I32 array_ix_t;
340 #endif /* <5.19.4 */
341  
342 #ifdef newSVpvn_flags
343 # define newSVpvn_mortal(pv, len) newSVpvn_flags(pv, len, SVs_TEMP)
344 #else /* !newSVpvn_flags */
345 # define newSVpvn_mortal(pv, len) sv_2mortal(newSVpvn(pv, len))
346 #endif /* !newSVpvn_flags */
347  
348 #ifndef my_strerror
349 # define my_strerror Strerror
350 #endif /* !my_strerror */
351  
352 #ifndef OpMORESIB_set
353 # define OpMORESIB_set(o, sib) ((o)->op_sibling = (sib))
354 # define OpLASTSIB_set(o, parent) ((o)->op_sibling = NULL)
355 # define OpMAYBESIB_set(o, sib, parent) ((o)->op_sibling = (sib))
356 #endif /* !OpMORESIB_set */
357 #ifndef OpSIBLING
358 # define OpHAS_SIBLING(o) (cBOOL((o)->op_sibling))
359 # define OpSIBLING(o) (0 + (o)->op_sibling)
360 #endif /* !OpSIBLING */
361  
362 /* Perl additions */
363  
364 typedef U64TYPE U64;
365  
366 #define sv_is_glob(sv) (SvTYPE(sv) == SVt_PVGV)
367  
368 #if PERL_VERSION_GE(5,11,0)
369 # define sv_is_regexp(sv) (SvTYPE(sv) == SVt_REGEXP)
370 #else /* <5.11.0 */
371 # define sv_is_regexp(sv) 0
372 #endif /* <5.11.0 */
373  
374 #define sv_is_undef(sv) (!sv_is_glob(sv) && !sv_is_regexp(sv) && !SvOK(sv))
375  
376 #define sv_is_string(sv) \
377 (!sv_is_glob(sv) && !sv_is_regexp(sv) && \
378 (SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK)))
379  
380 /* we need mg_dup to be invoked *after* duplicating a scalar's PV */
381 #define QCORE_MG_DUP_WORKS \
382 ((PERL_VERSION_GE(5,8,9) && !PERL_VERSION_GE(5,9,0)) || \
383 PERL_VERSION_GE(5,9,3))
384  
385 #ifndef QWITH_DUP
386 # if QCORE_MG_DUP_WORKS && defined(USE_ITHREADS)
387 # define QWITH_DUP 1
388 # else /* !(QCORE_MG_DUP_WORKS && USE_ITHREADS) */
389 # define QWITH_DUP 0
390 # endif /* !(QCORE_MG_DUP_WORKS && USE_ITHREADS) */
391 #endif /* !QWITH_DUP */
392  
393 #define newRV_ro_mortal_noinc(r) THX_newRV_ro_mortal_noinc(aTHX_ r)
394 1690 PERL_STATIC_INLINE SV *THX_newRV_ro_mortal_noinc(pTHX_ SV *referent)
395 {
396 1690 SV *rv = newRV_noinc(referent);
397 1690 SvREADONLY_on(rv);
398 1690 return sv_2mortal(rv);
399 }
400  
401 #define newRV_ro_mortal_inc(r) THX_newRV_ro_mortal_inc(aTHX_ r)
402 42 PERL_STATIC_INLINE SV *THX_newRV_ro_mortal_inc(pTHX_ SV *referent)
403 {
404 42 SV *rv = newRV_inc(referent);
405 42 SvREADONLY_on(rv);
406 42 return sv_2mortal(rv);
407 }
408  
409 /* system call compatibility */
410  
411 #ifndef MAP_FAILED
412 # define MAP_FAILED ((void*)-1)
413 #endif /* !MAP_FAILED */
414  
415 #ifndef FD_CLOEXEC
416 # define FD_CLOEXEC 1
417 #endif /* !FD_CLOEXEC */
418  
419 /* byte definition */
420  
421 #define BYTE_NBIT 8
422  
423 typedef U8 byte;
424 #define BYTE_MAX 0xff
425  
426 /* word definition */
427  
428 #define WORD_SZ_LOG2 3
429 #define WORD_SZ (1<<WORD_SZ_LOG2)
430 #define WORD_ALIGN_BITS (WORD_SZ-1)
431 #define IS_WORD_ALIGNED(v) (!((v) & WORD_ALIGN_BITS))
432 #define WORD_ALIGN(v) (((v) + WORD_ALIGN_BITS) & ~WORD_ALIGN_BITS)
433  
434 #define WORD_NBIT (BYTE_NBIT << WORD_SZ_LOG2)
435  
436 typedef U64 word;
437 #define WORD_C UINT64_C
438 STATIC_ASSERT_DECL(sizeof(word) == (1<<WORD_SZ_LOG2));
439 #define WORD_MAX WORD_C(0xffffffffffffffff)
440  
441 /*
442 * memory dereferencing
443 *
444 * BYTE_AT() and WORD_AT() are syntactic sugar for working with offsets
445 * into memory maps.
446 *
447 * word_cset() is an atomic compare-and-set operator on words, ordered
448 * relative to other memory operations. There's no standard C to
449 * implement this. Currently the only implementation used is the
450 * Intel-specified compiler built-in __sync_bool_compare_and_swap(),
451 * as implemented by gcc and other compilers.
452 *
453 * word_get() is intended to provide matching atomic reading of a mutable
454 * word, ordered relative to other memory operations. However, the C
455 * language (even with gcc extensions) doesn't actually provide a way
456 * to guarantee that a read is atomic. This implementation, using a
457 * "volatile" qualifier, ensures that the operation is a memory barrier,
458 * but doesn't guarantee atomicity. We must hope that the compiler uses
459 * a native atomic read instruction.
460 */
461  
462 #define BYTE_AT(base, offset) (*(byte*)(((char*)(base))+(offset)))
463 #define WORD_AT(base, offset) (*(word*)(((char*)(base))+(offset)))
464  
465 180502499 PERL_STATIC_INLINE word word_get(word const *ptr)
466 {
467 180502499 return *(word const volatile *)ptr;
468 }
469  
470 #define word_cset __sync_bool_compare_and_swap
471  
472 /*
473 * thread-safe reference counting
474 *
475 * This reference counting is used when sharing objects between threads
476 * due to spawning a thread requiring duplication of all objects the
477 * parent thread can reference. It's used for directory references and
478 * memory mappings.
479 *
480 * The value that's physically stored for the reference count is 1
481 * less than the actual number of live references. The word should be
482 * initialised to all-bits-zero (representing having a single reference).
483 * If the reference count word grows to all-bits-one, rather than wrap
484 * it will stick, preferring to leak the object rather than prematurely
485 * free it.
486 */
487  
488 #if QWITH_DUP
489  
490 PERL_STATIC_INLINE void threadrc_inc(word *rcp)
491 {
492 while(1) {
493 word rc = word_get(rcp);
494 if(unlikely(rc == ~(word)0)) break;
495 if(likely(word_cset(rcp, rc, rc+1))) break;
496 }
497 }
498  
499 PERL_STATIC_INLINE bool threadrc_dec(word *rcp)
500 {
501 while(1) {
502 word rc = word_get(rcp);
503 if(likely(rc == 0)) return 0;
504 if(unlikely(rc == ~(word)0)) return 1;
505 if(likely(word_cset(rcp, rc, rc-1))) return 1;
506 }
507 }
508  
509 #endif /* QWITH_DUP */
510  
511 /*
512 * opening with close-on-exec flag
513 *
514 * When we open file descriptors, we always want the close-on-exec flag
515 * set. Ideally we'd use the thread-safe (and convenient) O_CLOEXEC and
516 * F_DUPFD_CLOEXEC, but they're not available everywhere. So the wrappers
517 * open_cloexec(), openat_cloexec(), and dup_cloexec() encapsulate the
518 * job of setting the close-on-exec flag in the best manner possible.
519 *
520 * Even if the headers define O_CLOEXEC/F_DUPFD_CLOEXEC, it might not
521 * actually be implemented in the kernel at runtime. So it is necessary
522 * to experiment at runtime to see how to actually get the close-on-exec
523 * flag set. The experiment is run on the first attempts at opening.
524 * Experimentation is performed separately for open(2), openat(2),
525 * and fcntl(2)/F_DUPFD_CLOEXEC. In any sensible system open(2) and
526 * openat(2) will have identical treatment of the flags, but it's unwise
527 * to rely on sensibleness. We do rely on each syscall being consistent
528 * over time, within the scope of a single program run.
529 *
530 * Kernels that don't support O_CLOEXEC can't be relied upon to object
531 * if it's supplied. Linux, for example, ignores open(2) flags that
532 * it doesn't know about. So the experiment must check the actual flag
533 * state if open(2) appears to work. Anticipating that some other kernel
534 * will check the flags, EINVAL is also accepted as an indicator that
535 * O_CLOEXEC isn't valid. Once a definitive experimental result has been
536 * obtained, the system switches to one of three concrete strategies:
537 * use O_CLOEXEC only (if it worked), use F_SETFD only (if O_CLOEXEC was
538 * rejected or didn't work), or use both (if O_CLOEXEC was accepted and
539 * F_GETFD failed).
540 */
541  
542 enum {
543 CLOEXEC_EXPERIMENT,
544 CLOEXEC_AT_OPEN,
545 CLOEXEC_AFTER_OPEN,
546 CLOEXEC_AT_AND_AFTER_OPEN
547 };
548  
549 55 static int cloexec_determine_strategy(int fd)
550 {
551 55 int fdflags = fcntl(fd, F_GETFD);
552 55 ▶ if(unlikely(fdflags == -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(fdflags == -1)) { 0 55 55
553 0 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
554 0 return CLOEXEC_AT_AND_AFTER_OPEN;
555 55 ▶ } else if(likely(fdflags & FD_CLOEXEC)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(likely(fdflags & FD_CLOEXEC)) { 55 0 55
556 55 return CLOEXEC_AT_OPEN;
557 } else {
558 0 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
559 0 return CLOEXEC_AFTER_OPEN;
560 }
561 }
562  
563 #define DO_EXPERIMENTING_CLOEXEC(OPEN_WITH) \
564 do { \
565 static int strategy = CLOEXEC_EXPERIMENT; \
566 switch(strategy) { \
567 case CLOEXEC_EXPERIMENT: default: { \
568 int fd = OPEN_WITH; \
569 if(unlikely(fd == -1)) { \
570 if(unlikely(errno == EINVAL)) { \
571 strategy = CLOEXEC_AFTER_OPEN; \
572 goto after_open; \
573 } \
574 return -1; \
575 } \
576 strategy = cloexec_determine_strategy(fd); \
577 return fd; \
578 } \
579 case CLOEXEC_AT_OPEN: { \
580 return OPEN_WITH; \
581 } \
582 case CLOEXEC_AFTER_OPEN: after_open: break; \
583 case CLOEXEC_AT_AND_AFTER_OPEN: { \
584 int fd = OPEN_WITH; \
585 if(likely(fd != -1)) \
586 (void) fcntl(fd, F_SETFD, FD_CLOEXEC); \
587 return fd; \
588 } \
589 } \
590 } while(0)
591  
592 #define DO_CLOEXEC_AFTER_OPEN(OPEN_WITHOUT) \
593 do { \
594 int fd = OPEN_WITHOUT; \
595 if(likely(fd != -1)) (void) fcntl(fd, F_SETFD, FD_CLOEXEC); \
596 return fd; \
597 } while(0)
598  
599 310 static int open_cloexec(char const *path, int flags, mode_t mode)
600 {
601 #ifdef O_CLOEXEC
602 310 ▶ DO_EXPERIMENTING_CLOEXEC(open(path, flags | O_CLOEXEC, mode));
Branch: 3 branches
BranchTrueFalseTotal
DO_EXPERIMENTING_CLOEXEC(open(path, flags | O_CLOEXEC, mode)); 29 28 57
DO_EXPERIMENTING_CLOEXEC(open(path, flags | O_CLOEXEC, mode)); 0 29 29
DO_EXPERIMENTING_CLOEXEC(open(path, flags | O_CLOEXEC, mode)); 0 0 0
603 #endif /* O_CLOEXEC */
604 0 ▶ DO_CLOEXEC_AFTER_OPEN(open(path, flags, mode));
Branch: 1 branch
BranchTrueFalseTotal
DO_CLOEXEC_AFTER_OPEN(open(path, flags, mode)); 0 0 0
605 }
606  
607 #if QHAVE_OPENAT && QHAVE_FSTATAT && QHAVE_LINKAT && QHAVE_UNLINKAT && \
608 QHAVE_FDOPENDIR
609 2077 static int openat_cloexec(int dirfd, char const *path, int flags, mode_t mode)
610 {
611 # ifdef O_CLOEXEC
612 2077 ▶ DO_EXPERIMENTING_CLOEXEC(openat(dirfd, path, flags | O_CLOEXEC, mode));
Branch: 3 branches
BranchTrueFalseTotal
DO_EXPERIMENTING_CLOEXEC(openat(dirfd, path, flags | O_CLOEXEC, mode)); 31 27 58
DO_EXPERIMENTING_CLOEXEC(openat(dirfd, path, flags | O_CLOEXEC, mode)); 0 31 31
DO_EXPERIMENTING_CLOEXEC(openat(dirfd, path, flags | O_CLOEXEC, mode)); 0 0 0
613 # endif /* O_CLOEXEC */
614 0 ▶ DO_CLOEXEC_AFTER_OPEN(openat(dirfd, path, flags, mode));
Branch: 1 branch
BranchTrueFalseTotal
DO_CLOEXEC_AFTER_OPEN(openat(dirfd, path, flags, mode)); 0 0 0
615 }
616 #endif
617  
618 #if QHAVE_OPENAT && QHAVE_FSTATAT && QHAVE_LINKAT && QHAVE_UNLINKAT && \
619 QHAVE_FDOPENDIR && !QWITH_DUP
620 0 PERL_STATIC_INLINE int dup_cloexec(int oldfd)
621 {
622 # ifdef F_DUPFD_CLOEXEC
623 0 ▶ DO_EXPERIMENTING_CLOEXEC(fcntl(oldfd, F_DUPFD_CLOEXEC, 0));
Branch: 3 branches
BranchTrueFalseTotal
DO_EXPERIMENTING_CLOEXEC(fcntl(oldfd, F_DUPFD_CLOEXEC, 0)); 0 0 0
DO_EXPERIMENTING_CLOEXEC(fcntl(oldfd, F_DUPFD_CLOEXEC, 0)); 0 0 0
DO_EXPERIMENTING_CLOEXEC(fcntl(oldfd, F_DUPFD_CLOEXEC, 0)); 0 0 0
624 # endif /* F_DUPFD_CLOEXEC */
625 0 ▶ DO_CLOEXEC_AFTER_OPEN(dup(oldfd));
Branch: 1 branch
BranchTrueFalseTotal
DO_CLOEXEC_AFTER_OPEN(dup(oldfd)); 0 0 0
626 }
627 #endif
628  
629 /*
630 * file operations relative to referenced directory
631 *
632 * A directory is preferably referenced by a file descriptor.
633 * File operations relative to it are performed by the system calls
634 * openat(2), fstatat(2), linkat(2), and unlinkat(2). This system
635 * means that the directory reference remains valid if the directory
636 * is renamed, and means that there is a minimum of name resolution.
637 * However, these system calls aren't available very widely, so an
638 * alternative mechanism is required.
639 *
640 * The alternative is that an absolute pathname is stored, along with
641 * the device number and inode of the directory. File operations are
642 * performed by using full pathnames, immediately after checking that
643 * the stored directory pathname still refers to the correct directory.
644 * If the directory is moved, operations will start failing.
645 *
646 * Which system to use is in the general case determined at runtime,
647 * because even with calls to the modern system calls compiling, there's
648 * no guarantee that the running kernel is one that supports them.
649 * We therefore experiment, once per program run, to determine which
650 * system to use. The experiment is performed at initialiasation,
651 * before the first directory-referencing operation, and all operations
652 * therefore proceed with knowledge of which system is being used.
653 * If any of the necessary system calls aren't supported by the C library,
654 * such that calls to them don't compile, then instead of experimenting
655 * we statically use only the backup system.
656 *
657 * In threaded builds we need to duplicate directory references
658 * between threads. A pathname-based directory reference could be
659 * easily duplicated by copying the name structure. A descriptor-based
660 * directory reference is trickier. We could dup(2) the file descriptor,
661 * but that can fail, and the ability to handle errors during thread
662 * cloning is very limited. The file descriptor limit could be a problem
663 * in a program that uses a lot of threads, and for short-lived threads
664 * we'd rather avoid the system call overhead. So instead we share the
665 * file descriptor between threads. This is done by putting the file
666 * descriptor in a structure with a reference count, which is maintained
667 * using thread-safe atomic operators. In fact we use the same reference
668 * counting scheme with pathname-based directory references, to avoid
669 * having to allocate new memory upon duplication.
670 *
671 * The functions here provide a syscall-like interface. Errors are
672 * signalled in errno.
673 *
674 * To handle directory references that could be either a pointer or an
675 * immediate integer, dirref_t is an integer type that is both at least as
676 * large as int and at least large enough to store a pointer. Nasty casts
677 * are required for the pointer case. For efficiency of use, a null
678 * directory reference is always a zero value, and zeroing memory can be
679 * relied upon to yield a null reference. Where directory references are
680 * immediate integer file descriptors, fd 0 must therefore be avoided.
681 * In the unlikely event that opening a directory reference yields fd 0,
682 * it will be dup(2)ed to a different value. (Zero could instead have
683 * been reserved for nullity by incrementing the fd value for dirref_t,
684 * but that would make common operations using the directory reference
685 * slightly more expensive.)
686 */
687  
688 #if QHAVE_OPENAT && QHAVE_FSTATAT && QHAVE_LINKAT && QHAVE_UNLINKAT && \
689 QHAVE_FDOPENDIR
690 # define QMAY_DIRREF_BY_FD 1
691 #else
692 # define QMAY_DIRREF_BY_FD 0
693 #endif
694  
695 #if QMAY_DIRREF_BY_FD && QWITH_DUP
696 struct dirref_by_fd {
697 word rc; /* must be first in struct: see dirref_dup() */
698 int fd;
699 };
700 #endif /* QMAY_DIRREF_BY_FD && QWITH_DUP */
701  
702 struct dirref_by_name {
703 #if QWITH_DUP
704 word rc; /* must be first in struct: see dirref_dup() */
705 #endif /* QWITH_DUP */
706 ino_t ino;
707 dev_t dev;
708 char name[1]; /* struct hack */
709 };
710  
711 #if PTRSIZE <= INTSIZE
712 typedef unsigned dirref_t;
713 #elif PTRSIZE <= LONGSIZE
714 typedef unsigned long dirref_t;
715 #elif defined(HAS_LONG_LONG) && PTRSIZE <= LONGLONGSIZE
716 typedef unsigned long long dirref_t;
717 #else /* PTRSIZE > LONGLONGSIZE */
718 typedef UV dirref_t;
719 #endif /* PTRSIZE > LONGLONGSIZE */
720  
721 #if QMAY_DIRREF_BY_FD
722 # if QWITH_DUP
723 # define DIRREF_BYFD_FD(dr) (NUM2PTR(struct dirref_by_fd *, (dr)))->fd
724 # else /* !QWITH_DUP */
725 # define DIRREF_BYFD_FD(dr) ((int)(dr))
726 # endif /* !QWITH_DUP */
727 #endif /* QMAY_DIRREF_BY_FD */
728  
729 #if QMAY_DIRREF_BY_FD
730 enum {
731 DIRREF_EXPERIMENT,
732 DIRREF_BY_FD,
733 DIRREF_BY_NAME
734 };
735 static int dirref_strategy = DIRREF_EXPERIMENT;
736 #endif /* QMAY_DIRREF_BY_FD */
737  
738 31 PERL_STATIC_INLINE void dirref_ensure_strategy(void)
739 {
740 #if QMAY_DIRREF_BY_FD
741 # if AT_FDCWD == -1
742 # define QAT_BADFD (-2)
743 # else /* AT_FDCWD != -1 */
744 # define QAT_BADFD (-1)
745 # endif /* AT_FDCWD != -1 */
746 int res;
747 struct stat st;
748 31 ▶ if(unlikely(dirref_strategy != DIRREF_EXPERIMENT)) return;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(dirref_strategy != DIRREF_EXPERIMENT)) return; 0 31 31
749 31 res = openat_cloexec(QAT_BADFD, "", O_RDONLY, 0);
750 31 ▶ if(unlikely(res != -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(res != -1)) { 0 31 31
751 0 (void) close(res);
752 31 ▶ } else if(unlikely(errno == ENOSYS)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(unlikely(errno == ENOSYS)) { 0 31 31
753 0 by_name:
754 0 dirref_strategy = DIRREF_BY_NAME;
755 0 return;
756 }
757 31 res = fstatat(QAT_BADFD, "", &st, 0);
758 31 ▶ if(likely(res == -1) && unlikely(errno == ENOSYS)) goto by_name;
Branch: 2 branches
BranchTrueFalseTotal
if(likely(res == -1) && unlikely(errno == ENOSYS)) goto by_name; 31 0 31
if(likely(res == -1) && unlikely(errno == ENOSYS)) goto by_name; 0 31 31
759 31 res = linkat(QAT_BADFD, "", QAT_BADFD, "", 0);
760 31 ▶ if(likely(res == -1) && unlikely(errno == ENOSYS)) goto by_name;
Branch: 2 branches
BranchTrueFalseTotal
if(likely(res == -1) && unlikely(errno == ENOSYS)) goto by_name; 31 0 31
if(likely(res == -1) && unlikely(errno == ENOSYS)) goto by_name; 0 31 31
761 31 res = unlinkat(QAT_BADFD, "", 0);
762 31 ▶ if(likely(res == -1) && unlikely(errno == ENOSYS)) goto by_name;
Branch: 2 branches
BranchTrueFalseTotal
if(likely(res == -1) && unlikely(errno == ENOSYS)) goto by_name; 31 0 31
if(likely(res == -1) && unlikely(errno == ENOSYS)) goto by_name; 0 31 31
763 31 dirref_strategy = DIRREF_BY_FD;
764 #endif /* QMAY_DIRREF_BY_FD */
765 }
766  
767 33 PERL_STATIC_INLINE bool dirref_referential(void)
768 {
769 #if QMAY_DIRREF_BY_FD
770 33 return likely(dirref_strategy == DIRREF_BY_FD);
771 #else /* !QMAY_DIRREF_BY_FD */
772 return 0;
773 #endif /* !QMAY_DIRREF_BY_FD */
774 }
775  
776 #define dirref_null() ((dirref_t)0)
777  
778 1483 PERL_STATIC_INLINE bool dirref_is_null(dirref_t dirref)
779 {
780 1483 return dirref == dirref_null();
781 }
782  
783 0 static char *dirref_path_concat(char const *base, char const *rel)
784 {
785 0 size_t blen = strlen(base), rlen = strlen(rel);
786 0 size_t tlen = blen + rlen;
787 char *full;
788 0 ▶ if(unlikely(tlen < blen)) goto enomem;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(tlen < blen)) goto enomem; 0 0 0
789 0 tlen += 2;
790 0 ▶ if(unlikely(tlen < 2)) goto enomem;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(tlen < 2)) goto enomem; 0 0 0
791 0 full = malloc(tlen);
792 0 ▶ if(!likely(full)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(full)) { 0 0 0
793 0 enomem:
794 0 errno = ENOMEM;
795 0 return NULL;
796 }
797 0 (void) memcpy(full, base, blen);
798 0 ▶ if(unlikely(blen == 0) || likely(base[blen-1] != '/'))
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(blen == 0) || likely(base[blen-1] != '/')) 0 0 0
if(unlikely(blen == 0) || likely(base[blen-1] != '/')) 0 0 0
799 0 full[blen++] = '/';
800 0 (void) memcpy(full + blen, rel, rlen+1);
801 0 return full;
802 }
803  
804 310 static dirref_t dirref_open(char const *origname, struct stat *st)
805 {
806 #if QMAY_DIRREF_BY_FD
807 310 ▶ if(likely(dirref_strategy == DIRREF_BY_FD)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(dirref_strategy == DIRREF_BY_FD)) { 310 0 310
808 310 int fd = open_cloexec(origname, O_RDONLY, 0);
809 310 ▶ if(likely(fd != -1) && unlikely(fstat(fd, st) == -1)) {
Branch: 2 branches
BranchTrueFalseTotal
if(likely(fd != -1) && unlikely(fstat(fd, st) == -1)) { 221 89 310
if(likely(fd != -1) && unlikely(fstat(fd, st) == -1)) { 0 221 221
810 0 int er = errno;
811 0 (void) close(fd);
812 0 errno = er;
813 0 fd = -1;
814 }
815 # if QWITH_DUP
816 if(unlikely(fd == -1)) {
817 return dirref_null();
818 } else {
819 struct dirref_by_fd *byfd;
820 byfd = malloc(sizeof(struct dirref_by_fd));
821 if(!likely(byfd)) {
822 (void) close(fd);
823 errno = ENOMEM;
824 return dirref_null();
825 }
826 byfd->rc = 0;
827 byfd->fd = fd;
828 return NUM2PTR(dirref_t, byfd);
829 }
830 # else /* !QWITH_DUP */
831 310 ▶ if(unlikely(fd == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(fd == 0)) { 0 310 310
832 int er;
833 0 fd = dup_cloexec(fd);
834 0 er = errno;
835 0 (void) close(0);
836 0 errno = er;
837 }
838 310 ▶ return unlikely(fd == -1) ? dirref_null() : (dirref_t)fd;
Branch: 1 branch
BranchTrueFalseTotal
return unlikely(fd == -1) ? dirref_null() : (dirref_t)fd; 221 89 310
839 # endif /* !QWITH_DUP */
840 } else
841 #endif /* QMAY_DIRREF_BY_FD */
842 {
843 char *fullname;
844 size_t fullname_len, byname_len;
845 bool free_fullname;
846 struct dirref_by_name *byname;
847 #if QHAVE_REALPATH
848 long pmax;
849 #endif /* QHAVE_REALPATH */
850 0 ▶ if(unlikely(stat(origname, st) == -1)) return dirref_null();
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(stat(origname, st) == -1)) return dirref_null(); 0 0 0
851 #if QHAVE_REALPATH
852 # if QHAVE_PATHCONF && defined(_PC_PATH_MAX)
853 0 pmax = pathconf(".", _PC_PATH_MAX);
854 0 ▶ if(unlikely(pmax == -1))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(pmax == -1)) 0 0 0
855 # endif /* QHAVE_PATHCONF && _PC_PATH_MAX */
856 {
857 # ifdef PATH_MAX
858 0 pmax = PATH_MAX;
859 # else /* !PATH_MAX */
860 pmax = 4096;
861 # endif /* !PATH_MAX */
862 }
863 0 ▶ if(unlikely((long)(size_t)pmax != pmax ||
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely((long)(size_t)pmax != pmax || 0 0 0
864 ((size_t)pmax)+1 == 0))
865 0 goto enomem;
866 0 fullname = malloc(((size_t)pmax) + 1);
867 0 ▶ if(!likely(fullname)) goto enomem;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(fullname)) goto enomem; 0 0 0
868 0 ▶ if(!likely(realpath(origname, fullname))) {
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(realpath(origname, fullname))) { 0 0 0
869 0 int er = errno;
870 0 free(fullname);
871 0 errno = er;
872 0 return dirref_null();
873 }
874 0 free_fullname = 1;
875 #elif QHAVE_GETCWD
876 size_t origname_len = strlen(origname);
877 if(likely(origname[0] == '/')) {
878 fullname = (char*)origname;
879 fullname_len = origname_len;
880 free_fullname = 0;
881 } else {
882 size_t bufsz = 256;
883 char *cwd;
884 cwd = malloc(bufsz);
885 if(!likely(cwd)) goto enomem;
886 while(1) {
887 char *newbuf;
888 if(likely(getcwd(cwd, bufsz))) break;
889 if(unlikely(errno != ERANGE)) {
890 int er = errno;
891 free(cwd);
892 errno = er;
893 return dirref_null();
894 }
895 bufsz <<= 2;
896 if(!likely(bufsz)) goto enomem_free_cwd;
897 newbuf = realloc(cwd, bufsz);
898 if(!likely(newbuf)) {
899 enomem_free_cwd:
900 free(cwd);
901 goto enomem;
902 }
903 cwd = newbuf;
904 }
905 fullname = dirref_path_concat(cwd, origname);
906 free(cwd);
907 if(!likely(fullname)) goto enomem;
908 free_fullname = 1;
909 }
910 #else /* !QHAVE_REALPATH && !QHAVE_GETCWD */
911 #error neither realpath nor getcwd available
912 #endif /* !QHAVE_REALPATH && !QHAVE_GETCWD */
913 0 fullname_len = strlen(fullname);
914 0 byname_len = offsetof(struct dirref_by_name, name) + 1 +
915 fullname_len;
916 0 ▶ if(unlikely(byname_len < fullname_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(byname_len < fullname_len)) { 0 0 0
917 0 enomem_maybe_free_fullname:
918 0 ▶ if(free_fullname) free(fullname);
Branch: 1 branch
BranchTrueFalseTotal
if(free_fullname) free(fullname); 0 0 0
919 0 enomem:
920 0 errno = ENOMEM;
921 0 return dirref_null();
922 }
923 0 byname = malloc(byname_len);
924 0 ▶ if(!likely(byname)) goto enomem_maybe_free_fullname;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(byname)) goto enomem_maybe_free_fullname; 0 0 0
925 #if QWITH_DUP
926 byname->rc = 0;
927 #endif /* QWITH_DUP */
928 0 byname->dev = st->st_dev;
929 0 byname->ino = st->st_ino;
930 0 (void) memcpy(byname->name, fullname, fullname_len+1);
931 0 ▶ if(free_fullname) free(fullname);
Branch: 1 branch
BranchTrueFalseTotal
if(free_fullname) free(fullname); 0 0 0
932 0 return NUM2PTR(dirref_t, byname);
933 }
934 }
935  
936 #if QWITH_DUP
937 PERL_STATIC_INLINE dirref_t dirref_dup(dirref_t dirref)
938 {
939 if(unlikely(dirref_is_null(dirref))) return dirref;
940 /*
941 * This code doesn't look at whether the directory reference is
942 * by fd or by name. It relies on the reference count being
943 * in the same place in both structures, which is achieved by
944 * putting it at the beginning of both.
945 */
946 threadrc_inc(&(NUM2PTR(struct dirref_by_name *, dirref))->rc);
947 return dirref;
948 }
949 #endif /* QWITH_DUP */
950  
951 221 PERL_STATIC_INLINE void dirref_close(dirref_t dirref)
952 {
953 #if QWITH_DUP
954 /*
955 * Like dirref_dup(), this doesn't distinguish the types of
956 * directory reference when manipulating the reference count.
957 */
958 if(unlikely(threadrc_dec(
959 &(NUM2PTR(struct dirref_by_name *, dirref))->rc)))
960 return;
961 #endif /* QWITH_DUP */
962 #if QMAY_DIRREF_BY_FD
963 221 ▶ if(likely(dirref_strategy == DIRREF_BY_FD)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(dirref_strategy == DIRREF_BY_FD)) { 221 0 221
964 # if QWITH_DUP
965 struct dirref_by_fd *byfd =
966 NUM2PTR(struct dirref_by_fd *, dirref);
967 (void) close(byfd->fd);
968 free(byfd);
969 # else /* !QWITH_DUP */
970 221 (void) close((int)dirref);
971 # endif /* !QWITH_DUP */
972 } else
973 #endif /* QMAY_DIRREF_BY_FD */
974 {
975 0 free(NUM2PTR(struct dirref_by_name *, dirref));
976 }
977 221 }
978  
979 0 static bool dirref_byname_ok(struct dirref_by_name *byname)
980 {
981 struct stat st;
982 0 ▶ if(unlikely(stat(byname->name, &st) == -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(stat(byname->name, &st) == -1)) { 0 0 0
983 0 ▶ if(likely(errno == ENOENT) || likely(errno == ENOTDIR))
Branch: 2 branches
BranchTrueFalseTotal
if(likely(errno == ENOENT) || likely(errno == ENOTDIR)) 0 0 0
if(likely(errno == ENOENT) || likely(errno == ENOTDIR)) 0 0 0
984 0 errno = EIO;
985 0 return 0;
986 0 ▶ } else if(likely(st.st_dev == byname->dev &&
Branch: 2 branches
BranchTrueFalseTotal
} else if(likely(st.st_dev == byname->dev && 0 0 0
} else if(likely(st.st_dev == byname->dev && 0 0 0
987 st.st_ino == byname->ino)) {
988 0 return 1;
989 } else {
990 0 errno = EIO;
991 0 return 0;
992 }
993 }
994  
995 1497 PERL_STATIC_INLINE DIR *dirref_dir_opendir(dirref_t dirref)
996 {
997 #if QMAY_DIRREF_BY_FD
998 1497 ▶ if(likely(dirref_strategy == DIRREF_BY_FD)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(dirref_strategy == DIRREF_BY_FD)) { 1497 0 1497
999 DIR *dirh;
1000 1497 int fd = openat_cloexec(DIRREF_BYFD_FD(dirref), ".",
1001 O_RDONLY, 0);
1002 1497 ▶ if(unlikely(fd == -1)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(fd == -1)) return NULL; 0 1497 1497
1003 1497 dirh = fdopendir(fd);
1004 1497 ▶ if(!likely(dirh)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(dirh)) { 0 1497 1497
1005 0 int er = errno;
1006 0 (void) close(fd);
1007 0 errno = er;
1008 }
1009 1497 return dirh;
1010 } else
1011 #endif /* QMAY_DIRREF_BY_FD */
1012 {
1013 0 struct dirref_by_name *byname =
1014 NUM2PTR(struct dirref_by_name *, dirref);
1015 0 ▶ if(!likely(dirref_byname_ok(byname))) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(dirref_byname_ok(byname))) return NULL; 0 0 0
1016 0 return opendir(byname->name);
1017 }
1018 }
1019  
1020 549 static int dirref_rel_open_cloexec(dirref_t dirref, char const *rel,
1021 int flags, mode_t mode)
1022 {
1023 #if QMAY_DIRREF_BY_FD
1024 549 ▶ if(likely(dirref_strategy == DIRREF_BY_FD)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(dirref_strategy == DIRREF_BY_FD)) { 549 0 549
1025 549 return openat_cloexec(DIRREF_BYFD_FD(dirref), rel, flags, mode);
1026 } else
1027 #endif /* QMAY_DIRREF_BY_FD */
1028 {
1029 0 struct dirref_by_name *byname =
1030 NUM2PTR(struct dirref_by_name *, dirref);
1031 char *path;
1032 int res, er;
1033 0 path = dirref_path_concat(byname->name, rel);
1034 0 ▶ if(!likely(path)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(path)) return -1; 0 0 0
1035 0 ▶ res = !likely(dirref_byname_ok(byname)) ? -1 :
Branch: 1 branch
BranchTrueFalseTotal
res = !likely(dirref_byname_ok(byname)) ? -1 : 0 0 0
1036 0 open_cloexec(path, flags, mode);
1037 0 er = errno;
1038 0 free(path);
1039 0 errno = er;
1040 0 return res;
1041 }
1042 }
1043  
1044 147 PERL_STATIC_INLINE int dirref_rel_stat(dirref_t dirref, char const *rel,
1045 struct stat *st)
1046 {
1047 #if QMAY_DIRREF_BY_FD
1048 147 ▶ if(likely(dirref_strategy == DIRREF_BY_FD)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(dirref_strategy == DIRREF_BY_FD)) { 147 0 147
1049 147 return fstatat(DIRREF_BYFD_FD(dirref), rel, st, 0);
1050 } else
1051 #endif /* QMAY_DIRREF_BY_FD */
1052 {
1053 0 struct dirref_by_name *byname =
1054 NUM2PTR(struct dirref_by_name *, dirref);
1055 char *path;
1056 int res, er;
1057 0 path = dirref_path_concat(byname->name, rel);
1058 0 ▶ if(!likely(path)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(path)) return -1; 0 0 0
1059 0 ▶ res = !likely(dirref_byname_ok(byname)) ? -1 : stat(path, st);
Branch: 1 branch
BranchTrueFalseTotal
res = !likely(dirref_byname_ok(byname)) ? -1 : stat(path, st); 0 0 0
1060 0 er = errno;
1061 0 free(path);
1062 0 errno = er;
1063 0 return res;
1064 }
1065 }
1066  
1067 84 PERL_STATIC_INLINE int dirref_rel_link(dirref_t dirref, char const *oldrel,
1068 char const *newrel)
1069 {
1070 #if QMAY_DIRREF_BY_FD
1071 84 ▶ if(likely(dirref_strategy == DIRREF_BY_FD)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(dirref_strategy == DIRREF_BY_FD)) { 84 0 84
1072 84 return linkat(DIRREF_BYFD_FD(dirref), oldrel,
1073 DIRREF_BYFD_FD(dirref), newrel, 0);
1074 } else
1075 #endif /* QMAY_DIRREF_BY_FD */
1076 {
1077 0 struct dirref_by_name *byname =
1078 NUM2PTR(struct dirref_by_name *, dirref);
1079 char *oldpath, *newpath;
1080 int res, er;
1081 0 oldpath = dirref_path_concat(byname->name, oldrel);
1082 0 ▶ if(!likely(oldpath)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(oldpath)) return -1; 0 0 0
1083 0 newpath = dirref_path_concat(byname->name, newrel);
1084 0 ▶ if(!likely(newpath)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(newpath)) { 0 0 0
1085 0 free(oldpath);
1086 0 errno = ENOMEM;
1087 0 return -1;
1088 }
1089 0 ▶ res = !likely(dirref_byname_ok(byname)) ? -1 :
Branch: 1 branch
BranchTrueFalseTotal
res = !likely(dirref_byname_ok(byname)) ? -1 : 0 0 0
1090 0 link(oldpath, newpath);
1091 0 er = errno;
1092 0 free(oldpath);
1093 0 free(newpath);
1094 0 errno = er;
1095 0 return res;
1096 }
1097 }
1098  
1099 188 static int dirref_rel_unlink(dirref_t dirref, char const *rel)
1100 {
1101 #if QMAY_DIRREF_BY_FD
1102 188 ▶ if(likely(dirref_strategy == DIRREF_BY_FD)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(dirref_strategy == DIRREF_BY_FD)) { 188 0 188
1103 188 return unlinkat(DIRREF_BYFD_FD(dirref), rel, 0);
1104 } else
1105 #endif /* QMAY_DIRREF_BY_FD */
1106 {
1107 0 struct dirref_by_name *byname =
1108 NUM2PTR(struct dirref_by_name *, dirref);
1109 char *path;
1110 int res, er;
1111 0 path = dirref_path_concat(byname->name, rel);
1112 0 ▶ if(!likely(path)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(path)) return -1; 0 0 0
1113 0 ▶ res = !likely(dirref_byname_ok(byname)) ? -1 : unlink(path);
Branch: 1 branch
BranchTrueFalseTotal
res = !likely(dirref_byname_ok(byname)) ? -1 : unlink(path); 0 0 0
1114 0 er = errno;
1115 0 free(path);
1116 0 errno = er;
1117 0 return res;
1118 }
1119 }
1120  
1121 /* fd closing on scope stack */
1122  
1123 460 static void THX_closefd_cleanup(pTHX_ void *fd_p_v)
1124 {
1125 460 int fd = *(int*)fd_p_v;
1126 PERL_UNUSED_THX();
1127 460 Safefree(fd_p_v);
1128 460 ▶ if(unlikely(fd != -1)) close(fd);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(fd != -1)) close(fd); 18 442 460
1129 460 }
1130  
1131 #define closefd_save(fd) THX_closefd_save(aTHX_ fd)
1132 460 static int *THX_closefd_save(pTHX_ int fd)
1133 {
1134 int *fd_p;
1135 460 Newx(fd_p, 1, int);
1136 460 *fd_p = fd;
1137 460 SAVEDESTRUCTOR_X(THX_closefd_cleanup, fd_p);
1138 460 return fd_p;
1139 }
1140  
1141 #define closefd_early(fdp) THX_closefd_early(aTHX_ fdp)
1142 442 static void THX_closefd_early(pTHX_ int *fd_p)
1143 {
1144 442 int fd = *fd_p;
1145 PERL_UNUSED_THX();
1146 442 ▶ if(likely(fd != -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(fd != -1)) { 442 0 442
1147 442 *fd_p = -1;
1148 442 (void) close(fd);
1149 }
1150 442 }
1151  
1152 typedef int *closefd_ref_t;
1153  
1154 /* directory stream closing on scope stack */
1155  
1156 1497 static void THX_closedirh_cleanup(pTHX_ void *dirh_p_v)
1157 {
1158 1497 DIR *dirh = *(DIR**)dirh_p_v;
1159 PERL_UNUSED_THX();
1160 1497 Safefree(dirh_p_v);
1161 1497 ▶ if(unlikely(dirh)) closedir(dirh);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(dirh)) closedir(dirh); 9 1488 1497
1162 1497 }
1163  
1164 #define closedirh_save(dirh) THX_closedirh_save(aTHX_ dirh)
1165 1497 PERL_STATIC_INLINE DIR **THX_closedirh_save(pTHX_ DIR *dirh)
1166 {
1167 DIR **dirh_p;
1168 1497 Newx(dirh_p, 1, DIR*);
1169 1497 *dirh_p = dirh;
1170 1497 SAVEDESTRUCTOR_X(THX_closedirh_cleanup, dirh_p);
1171 1497 return dirh_p;
1172 }
1173  
1174 #define closedirh_early(dirhp) THX_closedirh_early(aTHX_ dirhp)
1175 1488 PERL_STATIC_INLINE void THX_closedirh_early(pTHX_ DIR **dirh_p)
1176 {
1177 1488 DIR *dirh = *dirh_p;
1178 PERL_UNUSED_THX();
1179 1488 ▶ if(likely(dirh)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(dirh)) { 1488 0 1488
1180 1488 *dirh_p = NULL;
1181 1488 (void) closedir(dirh);
1182 }
1183 1488 }
1184  
1185 typedef DIR **closedirh_ref_t;
1186  
1187 /* file removal on scope stack */
1188  
1189 struct unlinkfile_cleanup_par {
1190 dirref_t dir;
1191 char filename[1]; /* struct hack */
1192 };
1193  
1194 231 static void THX_unlinkfile_cleanup(pTHX_ void *par_p_v)
1195 {
1196 231 struct unlinkfile_cleanup_par *par_p = par_p_v;
1197 231 dirref_t dir = par_p->dir;
1198 PERL_UNUSED_THX();
1199 231 ▶ if(!likely(dirref_is_null(dir)))
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(dirref_is_null(dir))) 0 231 231
1200 0 (void) dirref_rel_unlink(dir, par_p->filename);
1201 231 Safefree(par_p_v);
1202 231 }
1203  
1204 #define unlinkfile_save(dir, fn) THX_unlinkfile_save(aTHX_ dir, fn)
1205 231 static struct unlinkfile_cleanup_par *THX_unlinkfile_save(pTHX_ dirref_t dir,
1206 char const *filename)
1207 {
1208 struct unlinkfile_cleanup_par *par_p;
1209 char *par_p_c;
1210 231 size_t fnlen = strlen(filename) + 1;
1211 231 Newx(par_p_c, offsetof(struct unlinkfile_cleanup_par, filename) + fnlen,
1212 char);
1213 231 par_p = (struct unlinkfile_cleanup_par *)par_p_c;
1214 231 par_p->dir = dir;
1215 231 (void) memcpy(par_p->filename, filename, fnlen);
1216 231 SAVEDESTRUCTOR_X(THX_unlinkfile_cleanup, par_p);
1217 231 return par_p;
1218 }
1219  
1220 #define unlinkfile_cancel(par_p) THX_unlinkfile_cancel(aTHX_ par_p)
1221 114 PERL_STATIC_INLINE void THX_unlinkfile_cancel(pTHX_
1222 struct unlinkfile_cleanup_par *par_p)
1223 {
1224 PERL_UNUSED_THX();
1225 114 par_p->dir = dirref_null();
1226 114 }
1227  
1228 #define unlinkfile_early(par_p) THX_unlinkfile_early(aTHX_ par_p)
1229 117 PERL_STATIC_INLINE int THX_unlinkfile_early(pTHX_
1230 struct unlinkfile_cleanup_par *par_p)
1231 {
1232 117 dirref_t dir = par_p->dir;
1233 PERL_UNUSED_THX();
1234 117 ▶ if(unlikely(dirref_is_null(dir))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(dirref_is_null(dir))) return 0; 0 117 117
1235 117 par_p->dir = dirref_null();
1236 117 return dirref_rel_unlink(dir, par_p->filename);
1237 }
1238  
1239 typedef struct unlinkfile_cleanup_par *unlinkfile_ref_t;
1240  
1241 /*
1242 * string unwrapping
1243 *
1244 * A struct pvl encapsulates an octet string held as octets in memory.
1245 * The memory's allocation is independent of this structure; the memory
1246 * must have sufficient lifetime for the use to which the pvl will be put.
1247 * pvl.pv may therefore point into an SV's buffer, or into separate
1248 * mortally-allocated memory, or into a file mapping. The octet string
1249 * is not necessarily NUL-terminated; pvl.len must be used to determine
1250 * the length.
1251 *
1252 * A null value (representing the absence of a string) can be represented
1253 * as a pvl with pvl.pv null.
1254 *
1255 * pvl_from_arg() handles taking an octet string argument supplied
1256 * by a user of this module. It processes get magic exactly once.
1257 * The pvl that it returns points either into the argument's buffer or
1258 * to mortally-allocated memory.
1259 */
1260  
1261 struct pvl {
1262 char *pv;
1263 size_t len;
1264 };
1265  
1266 100440 PERL_STATIC_INLINE struct pvl pvl_null(void)
1267 {
1268 struct pvl pvl;
1269 100440 pvl.pv = NULL;
1270 100440 pvl.len = 0;
1271 100440 return pvl;
1272 }
1273  
1274 1029996 PERL_STATIC_INLINE bool pvl_is_null(struct pvl pvl)
1275 {
1276 1029996 return !pvl.pv;
1277 }
1278  
1279 #define pvl_from_arg(role, au, arg) THX_pvl_from_arg(aTHX_ role, au, arg)
1280 87662811 static struct pvl THX_pvl_from_arg(pTHX_ char const *role, bool allow_undef,
1281 SV *arg)
1282 {
1283 STRLEN len;
1284 size_t d;
1285 char *p, *q, *e;
1286 struct pvl pvl;
1287 87662811 SvGETMAGIC(arg);
1288 87662811 ▶ if(unlikely(sv_is_glob(arg) || sv_is_regexp(arg))) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(sv_is_glob(arg) || sv_is_regexp(arg))) goto invalid; 87662792 19 87662811
if(unlikely(sv_is_glob(arg) || sv_is_regexp(arg))) goto invalid; 19 87662773 87662792
1289 87662773 ▶ if(allow_undef && !SvOK(arg)) return pvl_null();
Branch: 2 branches
BranchTrueFalseTotal
if(allow_undef && !SvOK(arg)) return pvl_null(); 956959 86705814 87662773
if(allow_undef && !SvOK(arg)) return pvl_null(); 100440 856519 956959
1290 87562333 ▶ if(!likely(SvFLAGS(arg) &
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(SvFLAGS(arg) & 167 87562166 87562333
1291 (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK))) {
1292 167 invalid:
1293 251 ▶ croak("%s is %s", role,
Branch: 1 branch
BranchTrueFalseTotal
croak("%s is %s", role, 52 199 251
1294 allow_undef ? "neither an octet string nor undef" :
1295 "not an octet string");
1296 }
1297 87562166 pvl.pv = SvPV_nomg(arg, len);
1298 87562166 pvl.len = len;
1299 87562166 ▶ if(!unlikely(SvUTF8(arg))) return pvl;
Branch: 1 branch
BranchTrueFalseTotal
if(!unlikely(SvUTF8(arg))) return pvl; 87561854 312 87562166
1300 312 e = pvl.pv + pvl.len;
1301 1600 ▶ for(d = 0, p = pvl.pv; p != e; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(d = 0, p = pvl.pv; p != e; ) { 1022 266 1288
1302 1022 U8 c = (U8)*p++;
1303 1022 ▶ if(unlikely(c & 0x80)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(c & 0x80)) { 96 926 1022
1304 96 ▶ if(unlikely(c < 0xc2 || c > 0xc3 || p == e))
Branch: 4 branches
BranchTrueFalseTotal
if(unlikely(c < 0xc2 || c > 0xc3 || p == e)) 96 0 96
if(unlikely(c < 0xc2 || c > 0xc3 || p == e)) 46 50 96
if(unlikely(c < 0xc2 || c > 0xc3 || p == e)) 50 46 96
if(unlikely(c < 0xc2 || c > 0xc3 || p == e)) 0 50 50
1305 46 goto invalid;
1306 50 c = (U8)*p++;
1307 50 ▶ if(!likely(c >= 0x80 && c <= 0xbf)) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if(!likely(c >= 0x80 && c <= 0xbf)) goto invalid; 50 0 50
if(!likely(c >= 0x80 && c <= 0xbf)) goto invalid; 0 50 50
1308 50 d++;
1309 }
1310 }
1311 266 ▶ if(likely(d == 0)) return pvl;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(d == 0)) return pvl; 216 50 266
1312 50 p = pvl.pv;
1313 50 pvl.len -= d;
1314 50 Newx(pvl.pv, pvl.len, char);
1315 50 SAVEFREEPV(pvl.pv);
1316 366 ▶ for(q = pvl.pv; p != e; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for(q = pvl.pv; p != e; q++) { 316 50 366
1317 316 U8 c = (U8)*p++;
1318 316 ▶ if(unlikely(c & 0x80))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(c & 0x80)) 50 266 316
1319 50 c = ((c & 0x03) << 6) | (((U8)*p++) & 0x3f);
1320 316 *q = (char)c;
1321 }
1322 50 return pvl;
1323 }
1324  
1325 /*
1326 * event counter enumeration
1327 *
1328 * See the "event counters" section below. These need to be defined
1329 * early to feed into the MY_CXT definition below.
1330 */
1331  
1332 #if QWITH_TALLY
1333  
1334 enum {
1335 K_STRING_READ,
1336 K_STRING_WRITE,
1337 K_BNODE_READ,
1338 K_BNODE_WRITE,
1339 K_KEY_COMPARE,
1340 K_ROOT_CHANGE_ATTEMPT,
1341 K_ROOT_CHANGE_SUCCESS,
1342 K_FILE_CHANGE_ATTEMPT,
1343 K_FILE_CHANGE_SUCCESS,
1344 K_DATA_READ_OP,
1345 K_DATA_WRITE_OP,
1346 K_SZ
1347 };
1348  
1349 static char const * const tally_name_pv[K_SZ] = {
1350 "string_read",
1351 "string_write",
1352 "bnode_read",
1353 "bnode_write",
1354 "key_compare",
1355 "root_change_attempt",
1356 "root_change_success",
1357 "file_change_attempt",
1358 "file_change_success",
1359 "data_read_op",
1360 "data_write_op",
1361 };
1362  
1363 #endif /* QWITH_TALLY */
1364  
1365 /*
1366 * per-thread data
1367 */
1368  
1369 #define MY_CXT_KEY "Hash::SharedMem::_guts"XS_VERSION
1370 typedef struct {
1371 SV *safe_undef;
1372 HV *sizes_table;
1373 HV *shash_handle_stash;
1374 #if QWITH_TALLY
1375 SV *tally_name_sv[K_SZ];
1376 #endif /* QWITH_TALLY */
1377 } my_cxt_t;
1378 START_MY_CXT
1379  
1380 /*
1381 * fanout limit
1382 *
1383 * This parameter is currently fixed at compile time. The value 15 is the
1384 * result of an experiment with an amd64 system. (Perhaps it is a sweet
1385 * spot due to node buffers coming in just under a power of two size.)
1386 */
1387  
1388 #define MAXFANOUT 15
1389  
1390 /*
1391 * parameter word
1392 *
1393 * Variable aspects of the file format are encapsulated in a word quantity
1394 * that is included in file headers. Some of the parameters are currently
1395 * fixed at compile time, and others are runtime variable.
1396 */
1397  
1398 #if MAXFANOUT < 3 || MAXFANOUT >= BYTE_MAX || !(MAXFANOUT & 1)
1399 #error bad parameter: fanout limit unacceptable
1400 #endif /* MAXFANOUT < 3 || MAXFANOUT >= BYTE_MAX || !(MAXFANOUT & 1) */
1401  
1402 #define PARAMETER_WORD_FIXED_PART_VALUE (MAXFANOUT<<16)
1403  
1404 #define PARAMETER_WORD(lsl, psl) \
1405 (((word)(lsl)) | (((word)psl)<<8) | PARAMETER_WORD_FIXED_PART_VALUE)
1406  
1407 #define PARAMETER_WORD_LINE_SZ_LOG2(par) ((int)((par) & 0xff))
1408 #define PARAMETER_WORD_PAGE_SZ_LOG2(par) ((int)(((par) >> 8) & 0xff))
1409 #define PARAMETER_WORD_FIXED_PART(par) ((par) & ~(word)0xffff)
1410  
1411 420 PERL_STATIC_INLINE int llog2(long v)
1412 {
1413 int g;
1414 420 ▶ if(unlikely(v <= 0)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(v <= 0)) return -1; 84 336 420
1415 2856 ▶ for(g = 0; !(v & 1); g++) v >>= 1;
Branch: 1 branch
BranchTrueFalseTotal
for(g = 0; !(v & 1); g++) v >>= 1; 2520 336 2856
1416 336 ▶ return likely(v == 1) ? g : -1;
Branch: 1 branch
BranchTrueFalseTotal
return likely(v == 1) ? g : -1; 336 0 336
1417 }
1418  
1419 84 PERL_STATIC_INLINE int parameter_known_line_size_log2(void)
1420 {
1421 #if QHAVE_SYSCONF
1422 84 int h = -1, l;
1423 # ifdef _SC_LEVEL1_DCACHE_LINESIZE
1424 84 l = llog2(sysconf(_SC_LEVEL1_DCACHE_LINESIZE));
1425 84 ▶ if(likely(l > h)) h = l;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(l > h)) h = l; 84 0 84
1426 # endif /* _SC_LEVEL1_DCACHE_LINESIZE */
1427 # ifdef _SC_LEVEL2_CACHE_LINESIZE
1428 84 l = llog2(sysconf(_SC_LEVEL2_CACHE_LINESIZE));
1429 84 ▶ if(unlikely(l > h)) h = l;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(l > h)) h = l; 0 84 84
1430 # endif /* _SC_LEVEL2_DCACHE_LINESIZE */
1431 # ifdef _SC_LEVEL3_CACHE_LINESIZE
1432 84 l = llog2(sysconf(_SC_LEVEL3_CACHE_LINESIZE));
1433 84 ▶ if(unlikely(l > h)) h = l;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(l > h)) h = l; 0 84 84
1434 # endif /* _SC_LEVEL3_DCACHE_LINESIZE */
1435 # ifdef _SC_LEVEL4_CACHE_LINESIZE
1436 84 l = llog2(sysconf(_SC_LEVEL4_CACHE_LINESIZE));
1437 84 ▶ if(unlikely(l > h)) h = l;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(l > h)) h = l; 0 84 84
1438 # endif /* _SC_LEVEL4_DCACHE_LINESIZE */
1439 84 return h;
1440 #else /* !QHAVE_SYSCONF */
1441 return -1;
1442 #endif /* !QHAVE_SYSCONF */
1443 }
1444  
1445 84 PERL_STATIC_INLINE int parameter_known_page_size_log2(void)
1446 {
1447 int l;
1448 PERL_UNUSED_VAR(l);
1449 #if QHAVE_SYSCONF
1450 # ifdef _SC_PAGESIZE
1451 84 l = llog2(sysconf(_SC_PAGESIZE));
1452 84 ▶ if(likely(l != -1)) return l;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(l != -1)) return l; 84 0 84
1453 # endif /* _SC_PAGESIZE */
1454 # ifdef _SC_PAGE_SIZE
1455 # ifdef _SC_PAGESIZE
1456 if(_SC_PAGE_SIZE != _SC_PAGESIZE)
1457 # endif /* _SC_PAGESIZE */
1458 {
1459 l = llog2(sysconf(_SC_PAGE_SIZE));
1460 if(likely(l != -1)) return l;
1461 }
1462 # endif /* _SC_PAGE_SIZE */
1463 #endif /* QHAVE_SYSCONF */
1464 #if QHAVE_GETPAGESIZE
1465 0 l = llog2(getpagesize());
1466 0 ▶ if(likely(l != -1)) return l;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(l != -1)) return l; 0 0 0
1467 #endif /* QHAVE_GETPAGESIZE */
1468 0 return -1;
1469 }
1470  
1471 84 PERL_STATIC_INLINE word parameter_preferred(void)
1472 {
1473 84 int lsl = parameter_known_line_size_log2();
1474 84 int psl = parameter_known_page_size_log2();
1475 /*
1476 * Where line/page sizes are not definitively known, guess.
1477 * The standard guesses are line size 2^6 bytes and page size
1478 * 2^12 bytes, matching the ia32/amd64 processors that are common
1479 * in 2013. If one size is known and the other is not, the guess
1480 * for the unknown parameter will be modified if necessary such
1481 * that the guessed page size is no smaller than the guessed
1482 * line size. Known line and page sizes could nevertheless be
1483 * the other way round.
1484 */
1485 84 ▶ if(unlikely(psl == -1)) psl = unlikely(lsl > 12) ? lsl : 12;
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(psl == -1)) psl = unlikely(lsl > 12) ? lsl : 12; 0 84 84
if(unlikely(psl == -1)) psl = unlikely(lsl > 12) ? lsl : 12; 0 0 0
1486 84 ▶ if(unlikely(lsl == -1)) lsl = unlikely(psl < 6) ? psl : 6;
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(lsl == -1)) lsl = unlikely(psl < 6) ? psl : 6; 0 84 84
if(unlikely(lsl == -1)) lsl = unlikely(psl < 6) ? psl : 6; 0 0 0
1487 /*
1488 * Having determined (our best guess of) the system's actual
1489 * line and page size, these must now be modified to conform to
1490 * the requirements of the shash format. The shash line size
1491 * must be at least word size, and the shash page size must be
1492 * at least the line size. Sizes that are too big to deal with,
1493 * such that intra-page pointers wouldn't fit into a word, will
1494 * be reduced to a size that's still too bit to deal with but
1495 * at least is sure not to overflow the fields they have to fit.
1496 */
1497 84 ▶ if(unlikely(lsl < WORD_SZ_LOG2)) lsl = WORD_SZ_LOG2;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(lsl < WORD_SZ_LOG2)) lsl = WORD_SZ_LOG2; 0 84 84
1498 84 ▶ if(unlikely(psl < lsl)) psl = lsl;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(psl < lsl)) psl = lsl; 0 84 84
1499 84 ▶ if(unlikely(psl > WORD_NBIT)) psl = WORD_NBIT;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(psl > WORD_NBIT)) psl = WORD_NBIT; 0 84 84
1500 84 ▶ if(unlikely(lsl > WORD_NBIT)) lsl = WORD_NBIT;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(lsl > WORD_NBIT)) lsl = WORD_NBIT; 0 84 84
1501 84 return PARAMETER_WORD(lsl, psl);
1502 }
1503  
1504 /*
1505 * size parameters
1506 *
1507 * A notional line and page size must be chosen for each shash, and should
1508 * preferably (for performance) match the target machine architecture.
1509 * Variable aspects of file layout depend on the chosen line and page
1510 * size. File offsets are precomputed and stored in struct sizes.
1511 *
1512 * In the data file header, the zero padding after the initial immutable
1513 * words (filling the remainder of the first line, unless lines are
1514 * very small) is picked out as a feature of the header so that it can
1515 * be used to represent the empty btree node and the empty string.
1516 */
1517  
1518 #define DHD_MAGIC 0
1519 #define DHD_PARAM (DHD_MAGIC+WORD_SZ)
1520 #define DHD_LENGTH (DHD_PARAM+WORD_SZ)
1521 #define DHD_ZEROPAD (DHD_LENGTH+WORD_SZ)
1522  
1523 #define MFL_MAGIC 0
1524 #define MFL_PARAM (MFL_MAGIC+WORD_SZ)
1525  
1526 struct sizes {
1527 word line_align_bits, page_align_bits;
1528 word dhd_nextalloc_space, dhd_current_root, dhd_sz;
1529 word dhd_zeropad_sz;
1530 word mfl_lastalloc_datafileid, mfl_current_datafileid, mfl_sz;
1531 #if QWITH_DUP
1532 char margin; /* to be clobbered by SV duplication */
1533 #endif /* QWITH_DUP */
1534 };
1535  
1536 #define IS_LINE_ALIGNED(sizes, v) (!((v) & (sizes)->line_align_bits))
1537 #define LINE_ALIGN(sizes, v) ((((v)-1) | (sizes)->line_align_bits) + 1)
1538 #define IS_PAGE_ALIGNED(sizes, v) (!((v) & (sizes)->page_align_bits))
1539 #define PAGE_ALIGN(sizes, v) ((((v)-1) | (sizes)->page_align_bits) + 1)
1540  
1541 #define sizes_construct(lsl, psl) THX_sizes_construct(aTHX_ lsl, psl)
1542 27 PERL_STATIC_INLINE struct sizes const *THX_sizes_construct(pTHX_
1543 int line_sz_log2, int page_sz_log2)
1544 {
1545 struct sizes *sizes;
1546 PERL_UNUSED_THX();
1547 27 Newx(sizes, 1, struct sizes);
1548 27 ▶ if(unlikely(line_sz_log2 < WORD_SZ_LOG2 ||
Branch: 6 branches
BranchTrueFalseTotal
if(unlikely(line_sz_log2 < WORD_SZ_LOG2 || 27 0 27
if(unlikely(line_sz_log2 < WORD_SZ_LOG2 || 0 27 27
if(unlikely(line_sz_log2 < WORD_SZ_LOG2 || 27 0 27
if(unlikely(line_sz_log2 < WORD_SZ_LOG2 || 0 27 27
if(unlikely(line_sz_log2 < WORD_SZ_LOG2 || 0 27 27
if(unlikely(line_sz_log2 < WORD_SZ_LOG2 || 0 27 27
1549 page_sz_log2 < line_sz_log2 ||
1550 line_sz_log2 >= WORD_NBIT ||
1551 page_sz_log2 >= WORD_NBIT)) {
1552 0 bad_parameters:
1553 0 Safefree(sizes);
1554 0 return NULL;
1555 }
1556 27 sizes->line_align_bits = (((word)1) << line_sz_log2) - 1;
1557 27 sizes->page_align_bits = (((word)1) << page_sz_log2) - 1;
1558 27 sizes->dhd_nextalloc_space = LINE_ALIGN(sizes, DHD_ZEROPAD);
1559 27 ▶ if(!likely(sizes->dhd_nextalloc_space)) goto bad_parameters;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sizes->dhd_nextalloc_space)) goto bad_parameters; 0 27 27
1560 27 sizes->dhd_current_root =
1561 27 LINE_ALIGN(sizes, sizes->dhd_nextalloc_space + WORD_SZ);
1562 27 ▶ if(!likely(sizes->dhd_current_root)) goto bad_parameters;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sizes->dhd_current_root)) goto bad_parameters; 0 27 27
1563 27 sizes->dhd_sz = LINE_ALIGN(sizes, sizes->dhd_current_root + WORD_SZ);
1564 27 ▶ if(!likely(sizes->dhd_sz)) goto bad_parameters;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sizes->dhd_sz)) goto bad_parameters; 0 27 27
1565 27 sizes->dhd_zeropad_sz = sizes->dhd_nextalloc_space - DHD_ZEROPAD;
1566 27 sizes->mfl_lastalloc_datafileid =
1567 27 LINE_ALIGN(sizes, MFL_PARAM + WORD_SZ);
1568 27 ▶ if(!likely(sizes->mfl_lastalloc_datafileid)) goto bad_parameters;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sizes->mfl_lastalloc_datafileid)) goto bad_parameters; 0 27 27
1569 27 sizes->mfl_current_datafileid =
1570 27 LINE_ALIGN(sizes, sizes->mfl_lastalloc_datafileid + WORD_SZ);
1571 27 ▶ if(!likely(sizes->mfl_current_datafileid)) goto bad_parameters;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sizes->mfl_current_datafileid)) goto bad_parameters; 0 27 27
1572 27 sizes->mfl_sz =
1573 27 PAGE_ALIGN(sizes, sizes->mfl_current_datafileid + WORD_SZ);
1574 27 ▶ if(!likely(sizes->mfl_sz)) goto bad_parameters;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sizes->mfl_sz)) goto bad_parameters; 0 27 27
1575 27 return sizes;
1576 }
1577  
1578 #define sizes_lookup(par) THX_sizes_lookup(aTHX_ aMY_CXT_ par)
1579 193 static SV *THX_sizes_lookup(pTHX_ pMY_CXT_ word par)
1580 {
1581 193 int line_sz_log2 = PARAMETER_WORD_LINE_SZ_LOG2(par);
1582 193 int page_sz_log2 = PARAMETER_WORD_PAGE_SZ_LOG2(par);
1583 char key[2];
1584 SV **sizes_svp;
1585 193 key[0] = line_sz_log2;
1586 193 key[1] = page_sz_log2;
1587 193 sizes_svp = hv_fetch(MY_CXT.sizes_table, key, 2, 0);
1588 193 ▶ if(likely(sizes_svp)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(sizes_svp)) { 166 27 193
1589 166 return *sizes_svp;
1590 } else {
1591 struct sizes const *sizes =
1592 27 sizes_construct(line_sz_log2, page_sz_log2);
1593 SV *sizes_sv;
1594 27 ▶ if(!likely(sizes)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sizes)) return NULL; 0 27 27
1595 27 sizes_sv = newSV_type(SVt_PV);
1596 27 SvPV_set(sizes_sv, (char *)sizes);
1597 27 SvLEN_set(sizes_sv, sizeof(struct sizes));
1598 27 SvREADONLY_on(sizes_sv);
1599 27 (void) hv_store(MY_CXT.sizes_table, key, 2, sizes_sv, 0);
1600 27 return sizes_sv;
1601 }
1602 }
1603  
1604 /*
1605 * magic numbers
1606 */
1607  
1608 #define DATA_FILE_MAGIC WORD_C(0xc693dac5ed5e47c2)
1609 #define MASTER_FILE_MAGIC WORD_C(0xa58afd185cbf5af7)
1610  
1611 /*
1612 * reference-counted handling of mmaps
1613 *
1614 * We can have several objects referring to a single mapping, and those
1615 * objects' need for the mapping have largely unrelated lifetimes.
1616 * We want to keep the mapping as long as at least one object needs it.
1617 * We therefore reify mappings as SVs, so that ordinary Perl reference
1618 * counting is applied.
1619 *
1620 * However, in threaded builds we can end up needing to share a mapping
1621 * between threads. (The alternative is to duplicate the mapping, for
1622 * which there is no convenient technique.) SVs are not shared between
1623 * threads, and are duplicated across thread cloning, so each thread
1624 * has its own reference count. We therefore require a second layer of
1625 * reference counting, maintained using thread-safe atomic operators,
1626 * to manage the sharing between threads. (We could alternatively
1627 * have had objects that need the mapping directly own a thread-safe
1628 * counted reference, but presumably the intra-thread reference counting
1629 * is cheaper.)
1630 */
1631  
1632 554 static int THX_mmap_mg_free(pTHX_ SV *sv, MAGIC *mg)
1633 {
1634 void *addr;
1635 PERL_UNUSED_THX();
1636 #if QWITH_DUP
1637 if(unlikely(threadrc_dec((word*)mg->mg_ptr))) return 0;
1638 free(mg->mg_ptr);
1639 #endif /* QWITH_DUP */
1640 PERL_UNUSED_ARG(mg);
1641 554 addr = SvPVX(sv);
1642 554 ▶ if(likely(addr)) (void) munmap(addr, SvUVX(sv));
Branch: 1 branch
BranchTrueFalseTotal
if(likely(addr)) (void) munmap(addr, SvUVX(sv)); 554 0 554
1643 554 return 0;
1644 }
1645  
1646 #if QWITH_DUP
1647 static int THX_mmap_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
1648 {
1649 PERL_UNUSED_ARG(param);
1650 threadrc_inc((word*)mg->mg_ptr);
1651 return 0;
1652 }
1653 #endif /* QWITH_DUP */
1654  
1655 static MGVTBL const mmap_mgvtbl = {
1656 0, /* get */
1657 0, /* set */
1658 0, /* len */
1659 0, /* clear */
1660 THX_mmap_mg_free, /* free */
1661 #ifdef MGf_COPY
1662 0, /* copy */
1663 #endif /* MGf_COPY */
1664 #ifdef MGf_DUP
1665 # if QWITH_DUP
1666 THX_mmap_mg_dup, /* dup */
1667 # else /* !QWITH_DUP */
1668 0, /* dup */
1669 # endif /* !QWITH_DUP */
1670 #endif /* MGf_DUP */
1671 #ifdef MGf_LOCAL
1672 0, /* local */
1673 #endif /* MGf_LOCAL */
1674 };
1675  
1676 #define mmap_early_unmap(mapsv) THX_mmap_early_unmap(aTHX_ mapsv)
1677 109 PERL_STATIC_INLINE void THX_mmap_early_unmap(pTHX_ SV *mapsv)
1678 {
1679 109 (void) sv_unmagicext(mapsv, PERL_MAGIC_ext, (MGVTBL*)&mmap_mgvtbl);
1680 109 }
1681  
1682 #define mmap_as_sv(fd, len, wr) THX_mmap_as_sv(aTHX_ fd, len, wr)
1683 554 static SV *THX_mmap_as_sv(pTHX_ int fd, word len, bool writable)
1684 {
1685 SV *mapsv;
1686 void *addr;
1687 if(unlikely((word)(size_t)len != len || (word)(UV)len != len)) {
1688 errno = ENOMEM;
1689 return NULL;
1690 }
1691 554 mapsv = sv_2mortal(newSV_type(SVt_PVMG));
1692 #if QWITH_DUP
1693 {
1694 word *rcp;
1695 MAGIC *mg;
1696 /*
1697 * The last reference to this refcount word can be dropped
1698 * by any thread sharing the mapping, not necessarily this
1699 * one, so it must not be allocated through Perl's own
1700 * (per-interpreter-tracked) memory allocator: see dirref_t
1701 * for the same requirement on the same kind of object.
1702 */
1703 rcp = malloc(sizeof(word));
1704 if(unlikely(!rcp)) {
1705 errno = ENOMEM;
1706 return NULL;
1707 }
1708 *rcp = 0;
1709 mg = sv_magicext(mapsv, NULL, PERL_MAGIC_ext,
1710 (MGVTBL*)&mmap_mgvtbl, (char*)rcp, 0);
1711 mg->mg_flags |= MGf_DUP;
1712 }
1713 #else /* !QWITH_DUP */
1714 554 (void) sv_magicext(mapsv, NULL, PERL_MAGIC_ext, (MGVTBL*)&mmap_mgvtbl,
1715 NULL, 0);
1716 #endif /* !QWITH_DUP */
1717 554 addr = mmap(NULL, len,
1718 554 ▶ likely(writable) ? (PROT_READ|PROT_WRITE) : PROT_READ,
Branch: 1 branch
BranchTrueFalseTotal
likely(writable) ? (PROT_READ|PROT_WRITE) : PROT_READ, 351 203 554
1719 MAP_SHARED, fd, 0);
1720 554 ▶ if(unlikely(addr == MAP_FAILED)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(addr == MAP_FAILED)) return NULL; 0 554 554
1721 554 SvPV_set(mapsv, (char *)addr);
1722 554 SvUV_set(mapsv, len);
1723 554 return mapsv;
1724 }
1725  
1726 /*
1727 * event counters
1728 *
1729 * To make these cheap to use, each counter is just a word quantity.
1730 * It is possible for these to wrap.
1731 */
1732  
1733 #if QWITH_TALLY
1734  
1735 # define tally_boot() THX_tally_boot(aTHX_ aMY_CXT)
1736 31 PERL_STATIC_INLINE void THX_tally_boot(pTHX_ pMY_CXT)
1737 {
1738 int i;
1739 372 ▶ for(i = 0; i != K_SZ; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i != K_SZ; i++) 341 31 372
1740 341 MY_CXT.tally_name_sv[i] = newSVpv_share(tally_name_pv[i], 0);
1741 31 }
1742  
1743 struct tally { word k[K_SZ]; };
1744  
1745 # define tally_event(st, type) ((void) ((st)->k[type]++))
1746  
1747 # define tally_zero(st) THX_tally_zero(aTHX_ st)
1748 238 PERL_STATIC_INLINE void THX_tally_zero(pTHX_ struct tally *tally)
1749 {
1750 PERL_UNUSED_THX();
1751 238 Zero(tally, 1, struct tally);
1752 238 }
1753  
1754 147 PERL_STATIC_INLINE void tally_add(struct tally *a, struct tally const *b)
1755 {
1756 int i;
1757 1764 ▶ for(i = 0; i != K_SZ; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i != K_SZ; i++) 1617 147 1764
1758 1617 a->k[i] += b->k[i];
1759 147 }
1760  
1761 # define tally_newSVword(v) THX_tally_newSVword(aTHX_ v)
1762 1122 PERL_STATIC_INLINE SV *THX_tally_newSVword(pTHX_ word v)
1763 {
1764 if(likely((word)(UV)v == v)) {
1765 1122 return newSVuv((UV)v);
1766 } else {
1767 /*
1768 * UV isn't big enough. To represent the word value
1769 * exactly, generate a string in decimal. The exact
1770 * numerical value can be recovered from that if the user
1771 * tries hard, and if not then conversion to the default
1772 * numeric types will at least have familiar behaviour.
1773 *
1774 * There might be a printf format that can decimalise a
1775 * word, but there also might not be. As it won't make a
1776 * huge difference to performance, rather than have two
1777 * versions of the code, we just take the DIY approach.
1778 * We know unsigned int is at least 32 bits (because the
1779 * Perl core requires that) and we have a printf format
1780 * for it.
1781 */
1782 char buf[21], *p;
1783 (void) sprintf(buf, "%08u%06u%06u",
1784 (unsigned) (v / WORD_C(1000000000000)),
1785 (unsigned) ((v / WORD_C(1000000)) % WORD_C(1000000)),
1786 (unsigned) (v % WORD_C(1000000)));
1787 for(p = buf; p[0] == '0'; p++) ;
1788 return newSVpvn(p, buf+20 - p);
1789 }
1790 }
1791  
1792 # define tally_as_hvref(st) THX_tally_as_hvref(aTHX_ st)
1793 102 PERL_STATIC_INLINE SV *THX_tally_as_hvref(pTHX_ struct tally const *tally)
1794 {
1795 dMY_CXT;
1796 102 HV *hv = newHV();
1797 102 SV *hvref = newRV_ro_mortal_noinc((SV*)hv);
1798 int i;
1799 1224 ▶ for(i = 0; i != K_SZ; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i != K_SZ; i++) { 1122 102 1224
1800 1122 SV *v = tally_newSVword(tally->k[i]);
1801 1122 SvREADONLY_on(v);
1802 1122 (void) hv_store_ent(hv, MY_CXT.tally_name_sv[i], v,
1803 SvSHARED_HASH(MY_CXT.tally_name_sv[i]));
1804 }
1805 102 return hvref;
1806 }
1807  
1808 #else /* !QWITH_TALLY */
1809  
1810 # define tally_boot() ((void) 0)
1811 # define tally_event(st, type) ((void) 0)
1812 # define tally_zero(st) ((void) 0)
1813 # define tally_add(a, b) ((void) 0)
1814 # define tally_as_hvref(st) newRV_ro_mortal_noinc((SV*)newHV())
1815  
1816 #endif /* !QWITH_TALLY */
1817  
1818 /*
1819 * top-level shash representation
1820 *
1821 * The same structure is used both for live shash handles that can
1822 * update and for snapshot handles. Where a live shash has a memory
1823 * mapping of the master file, a snapshot has a frozen root pointer.
1824 * Both types of handle have a memory mapping of the data file. A live
1825 * shash also has a file descriptor pointing at the directory.
1826 *
1827 * The same mode flag set is used for opening modes and for handle modes,
1828 * because of the overlap.
1829 */
1830  
1831 #define STOREMODE_READ 0x01
1832 #define STOREMODE_WRITE 0x02
1833 #define STOREMODE_CREATE 0x04
1834 #define STOREMODE_EXCLUDE 0x08
1835 #define STOREMODE_SNAPSHOT 0x10
1836  
1837 struct shash {
1838 unsigned mode;
1839 word data_size;
1840 word parameter;
1841 #if QWITH_TALLY
1842 struct tally tally;
1843 #endif /* QWITH_TALLY */
1844 union {
1845 struct {
1846 word data_file_id;
1847 dirref_t dir;
1848 SV *master_mmap_sv;
1849 void *master_mmap;
1850 } live;
1851 struct {
1852 word root;
1853 } snapshot;
1854 } u;
1855 SV *top_pathname_sv;
1856 SV *data_mmap_sv;
1857 void *data_mmap;
1858 #if QWITH_DUP
1859 SV *sizes_sv;
1860 #endif /* QWITH_DUP */
1861 struct sizes const *sizes;
1862 /*
1863 * The last member of this structure must be one that can
1864 * be reconstructed from others, because the default scalar
1865 * duplication code doesn't quite copy the scalar's entire
1866 * allocated buffer. It expects a scalar's buffer to contain
1867 * a nul-terminated string, meaning that the last byte of the
1868 * buffer is either the terminating nul or junk past the end of
1869 * the string, so it doesn't actually copy that byte, but sets
1870 * it to nul. So when this structure is stored in a scalar's
1871 * buffer, with no nul terminator, the last member will be
1872 * clobbered in the process of duplication, before our custom
1873 * duplication code gets to run.
1874 */
1875 };
1876  
1877 852 static int THX_shash_mg_free(pTHX_ SV *sv, MAGIC *mg)
1878 {
1879 852 struct shash *sh = (struct shash *)SvPVX(sv);
1880 PERL_UNUSED_ARG(mg);
1881 852 ▶ if(!(sh->mode & STOREMODE_SNAPSHOT)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!(sh->mode & STOREMODE_SNAPSHOT)) { 825 27 852
1882 825 ▶ if(likely(sh->u.live.master_mmap_sv))
Branch: 1 branch
BranchTrueFalseTotal
if(likely(sh->u.live.master_mmap_sv)) 187 638 825
1883 187 SvREFCNT_dec_NN(sh->u.live.master_mmap_sv);
1884 825 ▶ if(likely(!dirref_is_null(sh->u.live.dir)))
Branch: 1 branch
BranchTrueFalseTotal
if(likely(!dirref_is_null(sh->u.live.dir))) 221 604 825
1885 221 dirref_close(sh->u.live.dir);
1886 }
1887 852 ▶ if(likely(sh->top_pathname_sv)) SvREFCNT_dec_NN(sh->top_pathname_sv);
Branch: 1 branch
BranchTrueFalseTotal
if(likely(sh->top_pathname_sv)) SvREFCNT_dec_NN(sh->top_pathname_sv); 822 30 852
1888 852 ▶ if(likely(sh->data_mmap_sv)) SvREFCNT_dec_NN(sh->data_mmap_sv);
Branch: 1 branch
BranchTrueFalseTotal
if(likely(sh->data_mmap_sv)) SvREFCNT_dec_NN(sh->data_mmap_sv); 155 697 852
1889 852 return 0;
1890 }
1891  
1892 #if QWITH_DUP
1893 static int THX_shash_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
1894 {
1895 struct shash *sh = (struct shash *)SvPVX(mg->mg_obj);
1896 if(!(sh->mode & STOREMODE_SNAPSHOT)) {
1897 if(likely(sh->u.live.master_mmap_sv)) {
1898 sh->u.live.master_mmap_sv =
1899 sv_dup_inc(sh->u.live.master_mmap_sv, param);
1900 sh->u.live.master_mmap =
1901 SvPVX(sh->u.live.master_mmap_sv);
1902 }
1903 sh->u.live.dir = dirref_dup(sh->u.live.dir);
1904 }
1905 sh->top_pathname_sv = sv_dup_inc(sh->top_pathname_sv, param);
1906 if(likely(sh->data_mmap_sv)) {
1907 sh->data_mmap_sv = sv_dup_inc(sh->data_mmap_sv, param);
1908 sh->data_mmap = SvPVX(sh->data_mmap_sv);
1909 }
1910 if(likely(sh->sizes_sv)) {
1911 sh->sizes_sv = sv_dup_inc(sh->sizes_sv, param);
1912 sh->sizes = (struct sizes const *)SvPVX(sh->sizes_sv);
1913 }
1914 return 0;
1915 }
1916 #endif /* QWITH_DUP */
1917  
1918 static MGVTBL const shash_mgvtbl = {
1919 0, /* get */
1920 0, /* set */
1921 0, /* len */
1922 0, /* clear */
1923 THX_shash_mg_free, /* free */
1924 #ifdef MGf_COPY
1925 0, /* copy */
1926 #endif /* MGf_COPY */
1927 #ifdef MGf_DUP
1928 # if QWITH_DUP
1929 THX_shash_mg_dup, /* dup */
1930 # else /* !QWITH_DUP */
1931 0, /* dup */
1932 # endif /* !QWITH_DUP */
1933 #endif /* MGf_DUP */
1934 #ifdef MGf_LOCAL
1935 0, /* local */
1936 #endif /* MGf_LOCAL */
1937 };
1938  
1939 #define shash_apply_magic(shsv) THX_shash_apply_magic(aTHX_ shsv)
1940 852 PERL_STATIC_INLINE void THX_shash_apply_magic(pTHX_ SV *shsv)
1941 {
1942 852 MAGIC *mg = sv_magicext(shsv, shsv, PERL_MAGIC_ext,
1943 (MGVTBL*)&shash_mgvtbl, NULL, 0);
1944 PERL_UNUSED_VAR(mg);
1945 #if QWITH_DUP
1946 mg->mg_flags |= MGf_DUP;
1947 #endif /* QWITH_DUP */
1948 852 }
1949  
1950 #define shash_or_null_from_svref(shsvref) \
1951 THX_shash_or_null_from_svref(aTHX_ shsvref)
1952 86720279 static struct shash *THX_shash_or_null_from_svref(pTHX_ SV *shsvref)
1953 {
1954 dMY_CXT;
1955 SV *shsv;
1956 86720279 SvGETMAGIC(shsvref);
1957 346879994 return likely(SvROK(shsvref) && (shsv = SvRV(shsvref)) &&
1958 SvOBJECT(shsv) &&
1959 SvSTASH(shsv) == MY_CXT.shash_handle_stash) ?
1960 86720279 ▶ (struct shash *)SvPVX(shsv) : NULL;
Branch: 6 branches
BranchTrueFalseTotal
(struct shash *)SvPVX(shsv) : NULL; 86719969 310 86720279
(struct shash *)SvPVX(shsv) : NULL; 86719969 0 86719969
(struct shash *)SvPVX(shsv) : NULL; 86719969 310 86720279
(struct shash *)SvPVX(shsv) : NULL; 86719777 192 86719969
(struct shash *)SvPVX(shsv) : NULL; 86719777 502 86720279
(struct shash *)SvPVX(shsv) : NULL; 86719715 62 86719777
1961 }
1962  
1963 #define arg_error_notshash() THX_arg_error_notshash(aTHX)
1964 PERL_STATIC_INLINE void THX_arg_error_notshash(pTHX) __attribute__noreturn__;
1965 528 PERL_STATIC_INLINE void THX_arg_error_notshash(pTHX)
1966 {
1967 PERL_UNUSED_THX();
1968 528 croak("handle is not a shared hash handle");
1969 }
1970  
1971 #define shash_from_svref(shsvref) THX_shash_from_svref(aTHX_ shsvref)
1972 86719936 static struct shash *THX_shash_from_svref(pTHX_ SV *shsvref)
1973 {
1974 86719936 struct shash *sh = shash_or_null_from_svref(shsvref);
1975 86719936 ▶ if(!likely(sh)) arg_error_notshash();
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sh)) arg_error_notshash(); 492 86719444 86719936
1976 86719444 return sh;
1977 }
1978  
1979 #define arg_is_shash(arg) THX_arg_is_shash(aTHX_ arg)
1980 343 PERL_STATIC_INLINE bool THX_arg_is_shash(pTHX_ SV *arg)
1981 {
1982 343 return cBOOL(shash_or_null_from_svref(arg));
1983 }
1984  
1985 #define arg_check_shash(arg) THX_arg_check_shash(aTHX_ arg)
1986 100 static void THX_arg_check_shash(pTHX_ SV *arg)
1987 {
1988 100 ▶ if(!likely(arg_is_shash(arg))) arg_error_notshash();
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(arg_is_shash(arg))) arg_error_notshash(); 36 64 100
1989 64 }
1990  
1991 #define shash_error(sh, act, msg) THX_shash_error(aTHX_ sh, act, msg)
1992 static void THX_shash_error(pTHX_ struct shash *sh, char const *action,
1993 char const *message) __attribute__noreturn__;
1994 669 static void THX_shash_error(pTHX_ struct shash *sh, char const *action,
1995 char const *message)
1996 {
1997 #if !PERL_VERSION_GE(5,8,1)
1998 SV *m = mess("can't %s shared hash %"SVf": %s", action,
1999 sh->top_pathname_sv, message);
2000 sv_setsv(ERRSV, m);
2001 croak(NULL);
2002 #else /* >=5.8.1 */
2003 # if !PERL_VERSION_GE(5,10,1)
2004 SvUTF8_off(ERRSV);
2005 # endif /* <5.10.1 */
2006 669 croak("can't %s shared hash %"SVf": %s", action,
2007 sh->top_pathname_sv, message);
2008 #endif /* >=5.8.1 */
2009 }
2010  
2011 #define shash_error_data(sh) THX_shash_error_data(aTHX_ sh)
2012 static void THX_shash_error_data(pTHX_ struct shash *sh)
2013 __attribute__noreturn__;
2014 0 static void THX_shash_error_data(pTHX_ struct shash *sh)
2015 {
2016 0 shash_error(sh, "use", "shared hash is corrupted");
2017 }
2018  
2019 #define shash_error_errnum(sh, act, en) \
2020 THX_shash_error_errnum(aTHX_ sh, act, en)
2021 static void THX_shash_error_errnum(pTHX_ struct shash *sh, char const *action,
2022 int errnum) __attribute__noreturn__;
2023 22 static void THX_shash_error_errnum(pTHX_ struct shash *sh, char const *action,
2024 int errnum)
2025 {
2026 22 shash_error(sh, action, my_strerror(errnum));
2027 }
2028  
2029 #define shash_unlinkfile_early(sh, act, par_p) \
2030 THX_shash_unlinkfile_early(aTHX_ sh, act, par_p)
2031 117 static void THX_shash_unlinkfile_early(pTHX_ struct shash *sh,
2032 char const *action, struct unlinkfile_cleanup_par *par_p)
2033 {
2034 int e;
2035 117 ▶ if(likely(unlinkfile_early(par_p) != -1)) return;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(unlinkfile_early(par_p) != -1)) return; 103 14 117
2036 14 e = errno;
2037 14 ▶ if(likely(e == ENOENT) || likely(e == EBUSY)) return;
Branch: 2 branches
BranchTrueFalseTotal
if(likely(e == ENOENT) || likely(e == EBUSY)) return; 0 14 14
if(likely(e == ENOENT) || likely(e == EBUSY)) return; 0 0 0
2038 0 shash_error_errnum(sh, action, e);
2039 }
2040  
2041 #define shash_error_errno(sh, act) THX_shash_error_errno(aTHX_ sh, act)
2042 static void THX_shash_error_errno(pTHX_ struct shash *sh, char const *action)
2043 __attribute__noreturn__;
2044 14 static void THX_shash_error_errno(pTHX_ struct shash *sh, char const *action)
2045 {
2046 14 shash_error_errnum(sh, action, errno);
2047 }
2048  
2049 #define shash_check_readable(sh, act) THX_shash_check_readable(aTHX_ sh, act)
2050 86269858 static void THX_shash_check_readable(pTHX_ struct shash *sh, char const *action)
2051 {
2052 86269858 ▶ if(!likely(sh->mode & STOREMODE_READ))
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sh->mode & STOREMODE_READ)) 491 86269367 86269858
2053 491 shash_error(sh, action,
2054 "shared hash was opened in unreadable mode");
2055 86269367 }
2056  
2057 #define shash_check_writable(sh, act) THX_shash_check_writable(aTHX_ sh, act)
2058 703515 static void THX_shash_check_writable(pTHX_ struct shash *sh, char const *action)
2059 {
2060 703515 ▶ if(unlikely(sh->mode & STOREMODE_SNAPSHOT))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(sh->mode & STOREMODE_SNAPSHOT)) 77 703438 703515
2061 77 shash_error(sh, action, "shared hash handle is a snapshot");
2062 703438 ▶ if(!likely(sh->mode & STOREMODE_WRITE))
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sh->mode & STOREMODE_WRITE)) 58 703380 703438
2063 58 shash_error(sh, action,
2064 "shared hash was opened in unwritable mode");
2065 703380 }
2066  
2067 /* shash file handling */
2068  
2069 #define FILENAME_PREFIX_LEN 10
2070 #define MASTER_FILENAME "iNmv0,m$%3"
2071 #define DATA_FILENAME_PREFIX "&\"JBLMEgGm"
2072 #define DATA_FILENAME_SUFFIX_LEN (WORD_SZ<<1)
2073 #define TEMP_FILENAME_PREFIX "DNaM6okQi;"
2074  
2075 #define DATA_FILENAME_BUFSIZE (FILENAME_PREFIX_LEN+DATA_FILENAME_SUFFIX_LEN+1)
2076  
2077 #define dir_make_data_filename(buf, fid) \
2078 THX_dir_make_data_filename(aTHX_ buf, fid)
2079 310 static void THX_dir_make_data_filename(pTHX_ char *buf, word fileid)
2080 {
2081 PERL_UNUSED_THX();
2082 310 (void) sprintf(buf, "%s%08x%08x",
2083 310 DATA_FILENAME_PREFIX, (unsigned)(fileid >> 32),
2084 (unsigned)(fileid & WORD_C(0xffffffff)));
2085 310 }
2086  
2087 #define TEMP_FILENAME_BUFSIZE (FILENAME_PREFIX_LEN+8+8+8+1)
2088  
2089 #define dir_make_temp_filename(buf) THX_dir_make_temp_filename(aTHX_ buf)
2090 84 PERL_STATIC_INLINE void THX_dir_make_temp_filename(pTHX_ char *buf)
2091 {
2092 unsigned s, ns;
2093 PERL_UNUSED_THX();
2094 #if QHAVE_CLOCK_GETTIME && defined(CLOCK_REALTIME)
2095 {
2096 struct timespec ts;
2097 84 ▶ if(likely(clock_gettime(CLOCK_REALTIME, &ts) == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(clock_gettime(CLOCK_REALTIME, &ts) == 0)) { 84 0 84
2098 84 s = ts.tv_sec;
2099 84 ns = ts.tv_nsec;
2100 84 goto got_time;
2101 }
2102 }
2103 #endif /* QHAVE_CLOCK_GETTIME && CLOCK_REALTIME */
2104 #if QHAVE_GETTIMEOFDAY
2105 {
2106 struct timeval tv;
2107 0 ▶ if(likely(gettimeofday(&tv, NULL) == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(gettimeofday(&tv, NULL) == 0)) { 0 0 0
2108 0 s = tv.tv_sec;
2109 0 ns = tv.tv_usec * 1000;
2110 0 goto got_time;
2111 }
2112 }
2113 #endif /* QHAVE_GETTIMEOFDAY */
2114 {
2115 0 s = time(NULL);
2116 0 ns = 0;
2117 0 goto got_time;
2118 }
2119 84 got_time:
2120 84 (void) sprintf(buf, "%s%08x%08x%08x", TEMP_FILENAME_PREFIX,
2121 s & 0xffffffffU, ns & 0xffffffffU,
2122 84 ((unsigned)getpid()) & 0xffffffffU);
2123 84 }
2124  
2125 enum {
2126 FILENAME_CLASS_BOGUS,
2127 FILENAME_CLASS_MASTER,
2128 FILENAME_CLASS_TEMP,
2129 FILENAME_CLASS_DATA
2130 };
2131  
2132 #define dir_filename_class(fn, id_p) THX_dir_filename_class(aTHX_ fn, id_p)
2133 5707 static int THX_dir_filename_class(pTHX_ char const *filename, word *id_p)
2134 {
2135 size_t fnlen;
2136 PERL_UNUSED_THX();
2137 5707 ▶ if(filename[0] == '.') return FILENAME_CLASS_MASTER;
Branch: 1 branch
BranchTrueFalseTotal
if(filename[0] == '.') return FILENAME_CLASS_MASTER; 2999 2708 5707
2138 2708 fnlen = strlen(filename);
2139 2708 ▶ if(fnlen == FILENAME_PREFIX_LEN &&
Branch: 1 branch
BranchTrueFalseTotal
if(fnlen == FILENAME_PREFIX_LEN && 1404 1304 2708
2140 1404 ▶ memcmp(filename, MASTER_FILENAME,
Branch: 1 branch
BranchTrueFalseTotal
memcmp(filename, MASTER_FILENAME, 1404 0 1404
2141 FILENAME_PREFIX_LEN) == 0)
2142 1404 return FILENAME_CLASS_MASTER;
2143 1304 ▶ if(fnlen >= FILENAME_PREFIX_LEN &&
Branch: 1 branch
BranchTrueFalseTotal
if(fnlen >= FILENAME_PREFIX_LEN && 1301 3 1304
2144 1301 ▶ memcmp(filename, TEMP_FILENAME_PREFIX,
Branch: 1 branch
BranchTrueFalseTotal
memcmp(filename, TEMP_FILENAME_PREFIX, 5 1296 1301
2145 FILENAME_PREFIX_LEN) == 0)
2146 5 return FILENAME_CLASS_TEMP;
2147 1299 ▶ if(likely(fnlen == FILENAME_PREFIX_LEN+DATA_FILENAME_SUFFIX_LEN &&
Branch: 2 branches
BranchTrueFalseTotal
if(likely(fnlen == FILENAME_PREFIX_LEN+DATA_FILENAME_SUFFIX_LEN && 1293 6 1299
if(likely(fnlen == FILENAME_PREFIX_LEN+DATA_FILENAME_SUFFIX_LEN && 1293 0 1293
2148 memcmp(filename, DATA_FILENAME_PREFIX,
2149 FILENAME_PREFIX_LEN) == 0)) {
2150 char const *p;
2151 1293 word id = 0;
2152 1293 for(p = filename+FILENAME_PREFIX_LEN; ; p++) {
2153 21981 char c = *p;
2154 word v;
2155 21981 ▶ if(!c) break;
Branch: 1 branch
BranchTrueFalseTotal
if(!c) break; 1293 20688 21981
2156 20688 ▶ if(likely(c >= '0' && c <= '9')) {
Branch: 2 branches
BranchTrueFalseTotal
if(likely(c >= '0' && c <= '9')) { 20688 0 20688
if(likely(c >= '0' && c <= '9')) { 20632 56 20688
2157 20632 v = c - '0';
2158 56 ▶ } else if(likely(c >= 'a' && c <= 'f')) {
Branch: 2 branches
BranchTrueFalseTotal
} else if(likely(c >= 'a' && c <= 'f')) { 56 0 56
} else if(likely(c >= 'a' && c <= 'f')) { 56 0 56
2159 56 v = c - 'a' + 10;
2160 } else {
2161 0 return FILENAME_CLASS_BOGUS;
2162 }
2163 20688 id = (id << 4) | v;
2164 }
2165 1293 ▶ if(likely(id != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(id != 0)) { 1290 3 1293
2166 1290 *id_p = id;
2167 1290 return FILENAME_CLASS_DATA;
2168 }
2169 }
2170 9 return FILENAME_CLASS_BOGUS;
2171 }
2172  
2173  
2174 typedef void (*iterate_fn_t)(pTHX_ struct shash *sh, char const *action,
2175 char const *fn, word arg);
2176  
2177 #define dir_iterate(sh, act, iter, arg) \
2178 THX_dir_iterate(aTHX_ sh, act, iter, arg)
2179 1497 static void THX_dir_iterate(pTHX_ struct shash *sh, char const *action,
2180 iterate_fn_t THX_iterate, word arg)
2181 {
2182 DIR *dirh;
2183 closedirh_ref_t dirhr;
2184 1497 int old_errno = errno;
2185 1497 dirh = dirref_dir_opendir(sh->u.live.dir);
2186 1497 ▶ if(!likely(dirh)) shash_error_errno(sh, action);
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(dirh)) shash_error_errno(sh, action); 0 1497 1497
2187 1497 dirhr = closedirh_save(dirh);
2188 5698 while(1) {
2189 struct dirent *de;
2190 7195 errno = 0;
2191 7195 de = readdir(dirh);
2192 7195 ▶ if(!likely(de)) break;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(de)) break; 1488 5707 7195
2193 5707 THX_iterate(aTHX_ sh, action, de->d_name, arg);
2194 }
2195 1488 ▶ if(unlikely(errno)) shash_error_errno(sh, action);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(errno)) shash_error_errno(sh, action); 0 1488 1488
2196 1488 errno = old_errno;
2197 1488 closedirh_early(dirhr);
2198 1488 }
2199  
2200 5048 static void THX_dir_clean_file(pTHX_ struct shash *sh, char const *action,
2201 char const *fn, word curfileid)
2202 {
2203 word fileid;
2204 5048 int cls = dir_filename_class(fn, &fileid);
2205 int e;
2206 5048 ▶ if(!unlikely(cls == FILENAME_CLASS_TEMP ||
Branch: 4 branches
BranchTrueFalseTotal
if(!unlikely(cls == FILENAME_CLASS_TEMP || 5046 2 5048
if(!unlikely(cls == FILENAME_CLASS_TEMP || 1210 3836 5046
if(!unlikely(cls == FILENAME_CLASS_TEMP || 14 1196 1210
if(!unlikely(cls == FILENAME_CLASS_TEMP || 5032 14 5046
2207 (cls == FILENAME_CLASS_DATA &&
2208 unlikely((curfileid - fileid - 1) <
2209 (((word)1) << 62)))))
2210 5032 return;
2211 16 ▶ if(!unlikely(dirref_rel_unlink(sh->u.live.dir, fn) == -1)) return;
Branch: 1 branch
BranchTrueFalseTotal
if(!unlikely(dirref_rel_unlink(sh->u.live.dir, fn) == -1)) return; 16 0 16
2212 0 e = errno;
2213 0 ▶ if(likely(e == ENOENT) || likely(e == EBUSY)) return;
Branch: 2 branches
BranchTrueFalseTotal
if(likely(e == ENOENT) || likely(e == EBUSY)) return; 0 0 0
if(likely(e == ENOENT) || likely(e == EBUSY)) return; 0 0 0
2214 0 shash_error_errnum(sh, action, e);
2215 }
2216  
2217 #define dir_clean(sh, act, curfileid) THX_dir_clean(aTHX_ sh, act, curfileid)
2218 1278 PERL_STATIC_INLINE void THX_dir_clean(pTHX_ struct shash *sh,
2219 char const *action, word curfileid)
2220 {
2221 1278 dir_iterate(sh, action, THX_dir_clean_file, curfileid);
2222 1278 }
2223  
2224 /*
2225 * shash operation fundamentals
2226 *
2227 * Allocation of space in the shash is slightly complexified in order to
2228 * use space efficiently, because at the low level allocation must be of
2229 * integral lines, but objects only need to be word-aligned. Allocation
2230 * of word-aligned space is performed by shash_alloc(). Where possible,
2231 * this allocates from a line already owned by this process. If it
2232 * needs to acquire a new line, and the new line happens to abut one
2233 * already owned (which happens if no other process allocated space in
2234 * the intervening time), it will take advantage of the contiguous region.
2235 *
2236 * Allocation is managed separately for each write operation. The state
2237 * of allocation is managed in a struct shash_alloc, which must be created
2238 * (on the stack) by the top-level mutation function. Principally this
2239 * structure records any partial line that is owned by this process
2240 * and available for allocation. When a write operation is complete,
2241 * the allocation state (and any unused partial line) is discarded.
2242 */
2243  
2244 #define NULL_PTR (~(word)0)
2245 #define ZEROPAD_PTR ((word)DHD_ZEROPAD)
2246 #define PTR_FLAG_ROLLOVER ((word)1)
2247  
2248 #define unchecked_pointer_loc(sh, ptr) (&WORD_AT(sh->data_mmap, ptr))
2249  
2250 #define pointer_loc(sh, ptr, sp) THX_pointer_loc(aTHX_ sh, ptr, sp)
2251 1823233526 static word *THX_pointer_loc(pTHX_ struct shash *sh, word ptr, word *spc_p)
2252 {
2253 1823233526 word ds = sh->data_size;
2254 1823233526 ▶ if(!likely(IS_WORD_ALIGNED(ptr))) shash_error_data(sh);
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(IS_WORD_ALIGNED(ptr))) shash_error_data(sh); 0 1823233526 1823233526
2255 1823233526 ▶ if(unlikely(ptr >= ds)) shash_error_data(sh);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(ptr >= ds)) shash_error_data(sh); 0 1823233526 1823233526
2256 1823233526 *spc_p = ds - ptr;
2257 1823233526 return unchecked_pointer_loc(sh, ptr);
2258 }
2259  
2260 #define shash_ensure_data_file(sh) THX_shash_ensure_data_file(aTHX_ sh)
2261 86806511 static void THX_shash_ensure_data_file(pTHX_ struct shash *sh)
2262 {
2263 word datafileid;
2264 char data_filename[DATA_FILENAME_BUFSIZE];
2265 int data_fd;
2266 closefd_ref_t fdr;
2267 struct stat statbuf;
2268 SV *mapsv;
2269 tmps_ix_t old_tmps_floor;
2270 86806511 datafileid = word_get(&WORD_AT(sh->u.live.master_mmap,
2271 sh->sizes->mfl_current_datafileid));
2272 86806511 ▶ if(likely(mapsv = sh->data_mmap_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(mapsv = sh->data_mmap_sv)) { 146 86806365 86806511
2273 86806445 ▶ if(likely(datafileid == sh->u.live.data_file_id)) return;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(datafileid == sh->u.live.data_file_id)) return; 86806323 42 86806365
2274 42 sh->data_mmap_sv = NULL;
2275 42 SvREFCNT_dec_NN(mapsv);
2276 }
2277 188 attempt_to_open_data:
2278 188 ▶ if(unlikely(datafileid == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(datafileid == 0)) { 80 108 188
2279 80 word dsz = PAGE_ALIGN(sh->sizes, sh->sizes->dhd_sz + WORD_SZ);
2280 char *map;
2281 80 ▶ if(unlikely(!dsz || (word)(size_t)dsz != dsz ||
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(!dsz || (word)(size_t)dsz != dsz || 0 80 80
2282 (word)(STRLEN)dsz != dsz))
2283 0 shash_error_errnum(sh, "use", ENOMEM);
2284 80 Newxz(map, dsz, char);
2285 80 WORD_AT(map, DHD_MAGIC) = DATA_FILE_MAGIC;
2286 80 WORD_AT(map, DHD_PARAM) = sh->parameter;
2287 80 WORD_AT(map, DHD_LENGTH) = dsz;
2288 80 WORD_AT(map, sh->sizes->dhd_nextalloc_space) = dsz;
2289 80 WORD_AT(map, sh->sizes->dhd_current_root) =
2290 80 sh->sizes->dhd_sz | PTR_FLAG_ROLLOVER;
2291 80 mapsv = newSV_type(SVt_PV);
2292 80 SvPV_set(mapsv, map);
2293 80 SvLEN_set(mapsv, dsz);
2294 80 sh->data_mmap = map;
2295 80 sh->data_mmap_sv = mapsv;
2296 80 sh->data_size = dsz;
2297 80 sh->u.live.data_file_id = 0;
2298 80 return;
2299 }
2300 108 dir_make_data_filename(data_filename, datafileid);
2301 108 data_fd = dirref_rel_open_cloexec(sh->u.live.dir, data_filename,
2302 108 ▶ likely(sh->mode & STOREMODE_WRITE) ? O_RDWR : O_RDONLY, 0);
Branch: 1 branch
BranchTrueFalseTotal
likely(sh->mode & STOREMODE_WRITE) ? O_RDWR : O_RDONLY, 0); 74 34 108
2303 108 ▶ if(unlikely(data_fd == -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(data_fd == -1)) { 0 108 108
2304 word newdatafileid;
2305 0 ▶ if(unlikely(errno != ENOENT)) shash_error_errno(sh, "use");
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(errno != ENOENT)) shash_error_errno(sh, "use"); 0 0 0
2306 0 newdatafileid = word_get(&WORD_AT(sh->u.live.master_mmap,
2307 sh->sizes->mfl_current_datafileid));
2308 0 ▶ if(likely(newdatafileid != datafileid)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(newdatafileid != datafileid)) { 0 0 0
2309 0 datafileid = newdatafileid;
2310 0 goto attempt_to_open_data;
2311 }
2312 0 shash_error_data(sh);
2313 }
2314 108 fdr = closefd_save(data_fd);
2315 108 ▶ if(unlikely(fstat(data_fd, &statbuf) == -1))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(fstat(data_fd, &statbuf) == -1)) 0 108 108
2316 0 shash_error_errno(sh, "use");
2317 108 ▶ if(!likely(S_ISREG(statbuf.st_mode) &&
Branch: 4 branches
BranchTrueFalseTotal
if(!likely(S_ISREG(statbuf.st_mode) && 108 0 108
if(!likely(S_ISREG(statbuf.st_mode) && 108 0 108
if(!likely(S_ISREG(statbuf.st_mode) && 108 0 108
if(!likely(S_ISREG(statbuf.st_mode) && 0 108 108
2318 (off_t)(word)statbuf.st_size == statbuf.st_size &&
2319 (word)statbuf.st_size >= sh->sizes->dhd_sz &&
2320 IS_PAGE_ALIGNED(sh->sizes, (word)statbuf.st_size)))
2321 0 shash_error_data(sh);
2322 108 sh->data_size = statbuf.st_size;
2323 108 old_tmps_floor = PL_tmps_floor;
2324 108 SAVETMPS;
2325 108 mapsv = mmap_as_sv(data_fd, sh->data_size,
2326 cBOOL(sh->mode & STOREMODE_WRITE));
2327 108 ▶ if(!likely(mapsv)) shash_error_errno(sh, "use");
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(mapsv)) shash_error_errno(sh, "use"); 0 108 108
2328 108 sh->u.live.data_file_id = datafileid;
2329 108 sh->data_mmap_sv = SvREFCNT_inc_simple_NN(mapsv);
2330 108 sh->data_mmap = SvPVX(mapsv);
2331 108 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 108 0 108
2332 108 PL_tmps_floor = old_tmps_floor;
2333 108 closefd_early(fdr);
2334 108 ▶ if(!likely(WORD_AT(sh->data_mmap, DHD_MAGIC) == DATA_FILE_MAGIC &&
Branch: 4 branches
BranchTrueFalseTotal
if(!likely(WORD_AT(sh->data_mmap, DHD_MAGIC) == DATA_FILE_MAGIC && 108 0 108
if(!likely(WORD_AT(sh->data_mmap, DHD_MAGIC) == DATA_FILE_MAGIC && 108 0 108
if(!likely(WORD_AT(sh->data_mmap, DHD_MAGIC) == DATA_FILE_MAGIC && 108 0 108
if(!likely(WORD_AT(sh->data_mmap, DHD_MAGIC) == DATA_FILE_MAGIC && 0 108 108
2335 WORD_AT(sh->data_mmap, DHD_PARAM) == sh->parameter &&
2336 WORD_AT(sh->data_mmap, DHD_LENGTH) == sh->data_size))
2337 0 shash_error_data(sh);
2338 }
2339  
2340 #define shash_error_toobig(sh, act) THX_shash_error_toobig(aTHX_ sh, act)
2341 static void THX_shash_error_toobig(pTHX_ struct shash *sh, char const *action)
2342 __attribute__noreturn__;
2343 0 static void THX_shash_error_toobig(pTHX_ struct shash *sh, char const *action)
2344 {
2345 0 shash_error(sh, action, "data too large for a shared hash");
2346 }
2347  
2348 struct shash_alloc {
2349 word prealloc_len;
2350 byte *prealloc_loc;
2351 char const *action;
2352 jmp_buf fulljb;
2353 };
2354  
2355 #define shash_alloc(sh, alloc, len, pp) \
2356 THX_shash_alloc(aTHX_ sh, alloc, len, pp)
2357 10978108 static word *THX_shash_alloc(pTHX_ struct shash *sh, struct shash_alloc *alloc,
2358 word len, word *ptr_p)
2359 {
2360 byte *prealloc_end, *loc;
2361 word *nextalloc_p, data_size, pos, epos;
2362 10978108 word wlen = WORD_ALIGN(len), llen;
2363 10978108 ▶ if(!likely(wlen) && unlikely(len))
Branch: 2 branches
BranchTrueFalseTotal
if(!likely(wlen) && unlikely(len)) 0 10978108 10978108
if(!likely(wlen) && unlikely(len)) 0 0 0
2364 0 shash_error_toobig(sh, alloc->action);
2365 10978108 ▶ if(unlikely(wlen <= alloc->prealloc_len)) goto got_prealloc;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(wlen <= alloc->prealloc_len)) goto got_prealloc; 5033858 5944250 10978108
2366 5944250 prealloc_end = alloc->prealloc_loc + alloc->prealloc_len;
2367 5944250 nextalloc_p = &WORD_AT(sh->data_mmap, sh->sizes->dhd_nextalloc_space);
2368 5944250 data_size = sh->data_size;
2369 5944250 pos = word_get(nextalloc_p);
2370 5944250 ▶ if(unlikely(!IS_LINE_ALIGNED(sh->sizes, pos) || pos > data_size))
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(!IS_LINE_ALIGNED(sh->sizes, pos) || pos > data_size)) 5944250 0 5944250
if(unlikely(!IS_LINE_ALIGNED(sh->sizes, pos) || pos > data_size)) 0 5944250 5944250
2371 0 shash_error_data(sh);
2372 5944250 ▶ if(likely(&BYTE_AT(sh->data_mmap, pos) == prealloc_end)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(&BYTE_AT(sh->data_mmap, pos) == prealloc_end)) { 5031991 912259 5944250
2373 5031991 llen = LINE_ALIGN(sh->sizes, wlen - alloc->prealloc_len);
2374 5031991 ▶ if(!likely(llen)) shash_error_toobig(sh, alloc->action);
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(llen)) shash_error_toobig(sh, alloc->action); 0 5031991 5031991
2375 5031991 epos = pos + llen;
2376 5031991 ▶ if(unlikely(epos < pos || epos > data_size))
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(epos < pos || epos > data_size)) 5031991 0 5031991
if(unlikely(epos < pos || epos > data_size)) 53 5031938 5031991
2377 53 longjmp(alloc->fulljb, 1);
2378 5031938 ▶ if(likely(word_cset(nextalloc_p, pos, epos))) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(word_cset(nextalloc_p, pos, epos))) { 5014426 17512 5031938
2379 5014426 alloc->prealloc_len += llen;
2380 5014426 goto got_prealloc;
2381 }
2382 }
2383 929771 llen = LINE_ALIGN(sh->sizes, wlen);
2384 929771 ▶ if(!likely(llen)) shash_error_toobig(sh, alloc->action);
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(llen)) shash_error_toobig(sh, alloc->action); 0 929771 929771
2385 while(1) {
2386 944806 pos = word_get(nextalloc_p);
2387 944806 ▶ if(unlikely(!IS_LINE_ALIGNED(sh->sizes, pos) ||
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(!IS_LINE_ALIGNED(sh->sizes, pos) || 944806 0 944806
if(unlikely(!IS_LINE_ALIGNED(sh->sizes, pos) || 0 944806 944806
2388 pos > data_size))
2389 0 shash_error_data(sh);
2390 944806 epos = pos + llen;
2391 944806 ▶ if(unlikely(epos < pos || epos > data_size))
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(epos < pos || epos > data_size)) 944806 0 944806
if(unlikely(epos < pos || epos > data_size)) 27 944779 944806
2392 27 longjmp(alloc->fulljb, 1);
2393 944779 ▶ if(likely(word_cset(nextalloc_p, pos, epos))) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(word_cset(nextalloc_p, pos, epos))) { 929744 15035 944779
2394 929744 byte *newalloc_loc = &BYTE_AT(sh->data_mmap, pos);
2395 929744 alloc->prealloc_loc = newalloc_loc;
2396 929744 alloc->prealloc_len = llen;
2397 929744 break;
2398 }
2399 }
2400 10978028 got_prealloc:
2401 10978028 loc = alloc->prealloc_loc;
2402 10978028 alloc->prealloc_loc += wlen;
2403 10978028 alloc->prealloc_len -= wlen;
2404 10978028 *ptr_p = loc - (byte*)sh->data_mmap;
2405 10978028 return (word*)loc;
2406 }
2407  
2408 /* strings in the shash */
2409  
2410 #define string_as_pvl(sh, ptr) THX_string_as_pvl(aTHX_ sh, ptr)
2411 1388151831 static struct pvl THX_string_as_pvl(pTHX_ struct shash *sh, word ptr)
2412 {
2413 word len, *loc, spc, alloclen;
2414 struct pvl pvl;
2415 1388151831 loc = pointer_loc(sh, ptr, &spc);
2416 1388151831 len = loc[0];
2417 1388151831 alloclen = len + WORD_SZ+1;
2418 1388151831 ▶ if(unlikely(alloclen < WORD_SZ+1 || alloclen > spc))
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(alloclen < WORD_SZ+1 || alloclen > spc)) 1388151831 0 1388151831
if(unlikely(alloclen < WORD_SZ+1 || alloclen > spc)) 0 1388151831 1388151831
2419 0 shash_error_data(sh);
2420 if(unlikely((word)(size_t)len != len))
2421 shash_error_errnum(sh, "use", ENOMEM);
2422 1388151831 pvl.pv = (char*)&loc[1];
2423 1388151831 pvl.len = len;
2424 1388151831 ▶ if(unlikely(pvl.pv[pvl.len])) shash_error_data(sh);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(pvl.pv[pvl.len])) shash_error_data(sh); 0 1388151831 1388151831
2425 1388151831 tally_event(&sh->tally, K_STRING_READ);
2426 1388151831 return pvl;
2427 }
2428  
2429 static MGVTBL const string_mmapref_mgvtbl;
2430  
2431 #define string_as_sv(sh, act, ptr) THX_string_as_sv(aTHX_ sh, act, ptr)
2432 67597734 static SV *THX_string_as_sv(pTHX_ struct shash *sh, char const *action,
2433 word ptr)
2434 {
2435 67597734 struct pvl pvl = string_as_pvl(sh, ptr);
2436 SV *sv;
2437 if(unlikely((size_t)(STRLEN)pvl.len != pvl.len))
2438 shash_error_errnum(sh, action, ENOMEM);
2439 67597734 TAINT;
2440 /*
2441 * There are two strategies available for returning the string
2442 * as an SV. We can copy into a plain string SV, or we can point
2443 * into the mmaped space. In the latter case the result SV needs
2444 * magic to keep a reference to the object representing the mmap,
2445 * to keep it mapped. In both time and memory, the overhead of
2446 * pointing into the mmap is pretty much fixed, but the overhead
2447 * of copying is roughly linear in the length of the string.
2448 * The base overhead for copying is much less than the fixed
2449 * overhead of mapping.
2450 *
2451 * We therefore want to copy short strings and map long strings.
2452 * Choosing the threshold at which to switch is a black art.
2453 *
2454 * Empirical result for perl 5.16 on amd64 with glibc 2.11
2455 * is that 119-octet strings are better copied and 120-octet
2456 * strings are better mapped, with a sharp step in the cost of
2457 * copying at that length. This is presumably due to the memory
2458 * allocator switching strategy when allocating 128 octets or more
2459 * (rounded up from 120+1).
2460 *
2461 * The memory allocations of interest are one XPV and the
2462 * buffer for copying, and one XPVMG and one MAGIC for mapping.
2463 * The ugly expression here tries to compare the two sets of
2464 * allocations. The XPVMG+MAGIC - XPV difference is compared
2465 * against the potential buffer size. It is presumed that the
2466 * buffer length will be rounded up to a word-aligned size.
2467 * The structure size difference is rounded up in an attempt to
2468 * find a threshold likely to be used by the memory allocator.
2469 * Ideally this would be rounded to the next power of 2, but we
2470 * can't implement that in a constant expression, so it's actually
2471 * rounded to the next multiple of the XPVMG size. The formula
2472 * is slightly contrived so as to achieve the exact 120-octet
2473 * threshold on the amd64 system used for speed trials (where
2474 * MAGIC is 40 octets, XPV is 32 octets, and XPVMG is 64 octets).
2475 */
2476 67597734 ▶ if(pvl.len < sizeof(XPVMG) *
Branch: 1 branch
BranchTrueFalseTotal
if(pvl.len < sizeof(XPVMG) * 67561740 35994 67597734
2477 ((sizeof(MAGIC)+sizeof(XPVMG)*2-1) / sizeof(XPVMG)) -
2478 sizeof(size_t)) {
2479 67561740 sv = newSVpvn_mortal(pvl.pv, pvl.len);
2480 } else {
2481 35994 sv = sv_2mortal(newSV_type(SVt_PVMG));
2482 35994 (void) sv_magicext(sv, sh->data_mmap_sv, PERL_MAGIC_ext,
2483 (MGVTBL*)&string_mmapref_mgvtbl, NULL, 0);
2484 35994 SvPV_set(sv, pvl.pv);
2485 35994 SvCUR_set(sv, pvl.len);
2486 35994 SvPOK_on(sv);
2487 35994 ▶ SvTAINTED_on(sv);
Branch: 1 branch
BranchTrueFalseTotal
SvTAINTED_on(sv); 27 35967 35994
2488 }
2489 67597734 SvREADONLY_on(sv);
2490 67597734 return sv;
2491 }
2492  
2493 #define string_cmp_pvl(sh, aptr, bpvl) THX_string_cmp_pvl(aTHX_ sh, aptr, bpvl)
2494 1295531066 static int THX_string_cmp_pvl(pTHX_ struct shash *sh, word aptr,
2495 struct pvl bpvl)
2496 {
2497 1295531066 struct pvl apvl = string_as_pvl(sh, aptr);
2498 int r;
2499 1295531066 tally_event(&sh->tally, K_KEY_COMPARE);
2500 1295531066 r = memcmp(apvl.pv, bpvl.pv, apvl.len < bpvl.len ? apvl.len : bpvl.len);
2501 1295531066 ▶ return r ? r : apvl.len == bpvl.len ? 0 : apvl.len < bpvl.len ? -1 : 1;
Branch: 3 branches
BranchTrueFalseTotal
return r ? r : apvl.len == bpvl.len ? 0 : apvl.len < bpvl.len ? -1 : 1; 83393947 1212137119 1295531066
return r ? r : apvl.len == bpvl.len ? 0 : apvl.len < bpvl.len ? -1 : 1; 45604737 37789210 83393947
return r ? r : apvl.len == bpvl.len ? 0 : apvl.len < bpvl.len ? -1 : 1; 37616573 7988164 45604737
2502 }
2503  
2504 #define string_eq_pvl(sh, aptr, bpvl) THX_string_eq_pvl(aTHX_ sh, aptr, bpvl)
2505 547396 PERL_STATIC_INLINE int THX_string_eq_pvl(pTHX_ struct shash *sh, word aptr,
2506 struct pvl bpvl)
2507 {
2508 547396 struct pvl apvl = string_as_pvl(sh, aptr);
2509 547396 ▶ return apvl.len == bpvl.len && memcmp(apvl.pv, bpvl.pv, apvl.len) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return apvl.len == bpvl.len && memcmp(apvl.pv, bpvl.pv, apvl.len) == 0; 547326 70 547396
return apvl.len == bpvl.len && memcmp(apvl.pv, bpvl.pv, apvl.len) == 0; 238614 308712 547326
2510 }
2511  
2512 #define string_write_from_pvl(sh, alloc, pvl) \
2513 THX_string_write_from_pvl(aTHX_ sh, alloc, pvl)
2514 7207348 static word THX_string_write_from_pvl(pTHX_ struct shash *sh,
2515 struct shash_alloc *alloc, struct pvl pvl)
2516 {
2517 word alloclen, ptr, *loc;
2518 if(unlikely((size_t)(word)pvl.len != pvl.len))
2519 shash_error_toobig(sh, alloc->action);
2520 7207348 ▶ if(unlikely(pvl.len == 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(pvl.len == 0) && 26 7207322 7207348
2521 26 ▶ likely(sh->sizes->dhd_zeropad_sz >= WORD_SZ+1))
Branch: 1 branch
BranchTrueFalseTotal
likely(sh->sizes->dhd_zeropad_sz >= WORD_SZ+1)) 26 0 26
2522 26 return ZEROPAD_PTR;
2523 7207322 alloclen = ((word)pvl.len) + WORD_SZ + 1;
2524 7207322 ▶ if(unlikely(alloclen < WORD_SZ+1))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(alloclen < WORD_SZ+1)) 0 7207322 7207322
2525 0 shash_error_toobig(sh, alloc->action);
2526 7207322 loc = shash_alloc(sh, alloc, alloclen, &ptr);
2527 7207302 loc[0] = pvl.len;
2528 7207302 (void) memcpy(&loc[1], pvl.pv, pvl.len);
2529 7207302 ((byte*)&loc[1])[pvl.len] = 0;
2530 7207302 tally_event(&sh->tally, K_STRING_WRITE);
2531 7207302 return ptr;
2532 }
2533  
2534 #define string_size(sh, ptr) THX_string_size(aTHX_ sh, ptr)
2535 6188574 PERL_STATIC_INLINE word THX_string_size(pTHX_ struct shash *sh, word ptr)
2536 {
2537 word spc;
2538 6188574 word len = pointer_loc(sh, ptr, &spc)[0];
2539 6188574 ▶ if(unlikely(len == 0) && likely(sh->sizes->dhd_zeropad_sz >= WORD_SZ+1))
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(len == 0) && likely(sh->sizes->dhd_zeropad_sz >= WORD_SZ+1)) 4 6188570 6188574
if(unlikely(len == 0) && likely(sh->sizes->dhd_zeropad_sz >= WORD_SZ+1)) 4 0 4
2540 4 return 0;
2541 6188570 return WORD_ALIGN(len + WORD_SZ+1);
2542 }
2543  
2544 #define string_migrate(shf, ptrf, sht, alloct) \
2545 THX_string_migrate(aTHX_ shf, ptrf, sht, alloct)
2546 6188274 PERL_STATIC_INLINE word THX_string_migrate(pTHX_ struct shash *shf, word ptrf,
2547 struct shash *sht, struct shash_alloc *alloct)
2548 {
2549 6188274 return string_write_from_pvl(sht, alloct, string_as_pvl(shf, ptrf));
2550 }
2551  
2552 /*
2553 * btrees in the shash
2554 *
2555 * Things are a bit asymmetric because we're only caching lower bound
2556 * keys in the btree nodes. For the most part, the first key in each
2557 * node is ignored, because it isn't needed in order to decide where
2558 * to descend. The other keys (numbering one less than the number of
2559 * subnodes) are treated as boundaries between the subnodes. Thus for
2560 * descent purposes the leftmost nodes are treated as if ther lower
2561 * bound key is the empty string (the leftmost possible key).
2562 *
2563 * The first key in a node is only interesting at the leaf layer, where
2564 * ordering relative to the target key needs to be fully resolved.
2565 * However, when descending to a non-leftmost node, the comparison
2566 * against the node's first key has already been made in the parent node.
2567 * So the node's first key only needs to be fully included in the search
2568 * process for the leftmost leaf node.
2569 *
2570 * When inserting into the btree, by using the same search mechanism we
2571 * always end up inserting subnodes after some existing subnode, except
2572 * in the necessary special case where the key of interest precedes the
2573 * first key in the shash.
2574 *
2575 * A cursor points to a location in a btree, keeping track of all of
2576 * its ancestor nodes and the relevant index in each node. It can be
2577 * used as if it points to an item in the btree, or as if it points to a
2578 * gap (either between items or to the left or right of all the items).
2579 * The cursor structure doesn't record which way it's being used; that's
2580 * a matter of interpretation. A cursor structure pointing at an item
2581 * is identical to the cursor structure pointing at the gap immediately
2582 * to the right of that item. In order to represent pointing at the gap
2583 * immediately to the left of the leftmost item, the cursor structure
2584 * may point to a notional item just to the left of the leftmost item,
2585 * in which case the index in each node is 0, except in the leaf layer,
2586 * where the index is -1.
2587 */
2588  
2589 #define MINFANOUT ((MAXFANOUT+1)>>1)
2590  
2591 #define LAYER_MAX 0x3f
2592 #define bnode_header_layer(h) ((h) & LAYER_MAX)
2593 #define bnode_header_fanout(h) (((h) >> 8) & BYTE_MAX)
2594 #define bnode_header_pad(h) ((h) & WORD_C(0xffffffffffff00c0))
2595 #define bnode_body_loc(loc) (&(loc)[1])
2596  
2597 #define bnode_check(sh, np, el, lp, fo) \
2598 THX_bnode_check(aTHX_ sh, np, el, lp, fo)
2599 428570645 static word const *THX_bnode_check(pTHX_ struct shash *sh, word ptr,
2600 int expect_layer, int *layer_p, int *fanout_p)
2601 {
2602 word header, spc;
2603 word const *loc;
2604 int layer, fanout;
2605 428570645 loc = pointer_loc(sh, ptr, &spc);
2606 428570645 header = loc[0];
2607 428570645 layer = bnode_header_layer(header);
2608 428570645 fanout = bnode_header_fanout(header);
2609 428570645 ▶ if(unlikely(bnode_header_pad(header) || fanout > MAXFANOUT ||
Branch: 4 branches
BranchTrueFalseTotal
if(unlikely(bnode_header_pad(header) || fanout > MAXFANOUT || 428570645 0 428570645
if(unlikely(bnode_header_pad(header) || fanout > MAXFANOUT || 0 428570645 428570645
if(unlikely(bnode_header_pad(header) || fanout > MAXFANOUT || 428570645 0 428570645
if(unlikely(bnode_header_pad(header) || fanout > MAXFANOUT || 0 428570645 428570645
2610 spc < WORD_SZ + (((size_t)fanout) << (WORD_SZ_LOG2+1))))
2611 0 shash_error_data(sh);
2612 428570645 ▶ if(unlikely(expect_layer == -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(expect_layer == -1)) { 86806579 341764066 428570645
2613 86806579 ▶ if(unlikely(fanout < 2 && layer != 0)) shash_error_data(sh);
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(fanout < 2 && layer != 0)) shash_error_data(sh); 200739 86605840 86806579
if(unlikely(fanout < 2 && layer != 0)) shash_error_data(sh); 0 200739 200739
2614 } else {
2615 341764066 ▶ if(unlikely(layer != expect_layer || fanout < MINFANOUT))
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(layer != expect_layer || fanout < MINFANOUT)) 341764066 0 341764066
if(unlikely(layer != expect_layer || fanout < MINFANOUT)) 0 341764066 341764066
2616 0 shash_error_data(sh);
2617 }
2618 428570645 *layer_p = layer;
2619 428570645 *fanout_p = fanout;
2620 428570645 tally_event(&sh->tally, K_BNODE_READ);
2621 428570645 return loc;
2622 }
2623  
2624 #define BNODE_SEARCH_EXACT INT_MIN
2625  
2626 #define bnode_search(sh, nl, fo, lm, kpvl) \
2627 THX_bnode_search(aTHX_ sh, nl, fo, lm, kpvl)
2628 418002497 static int THX_bnode_search(pTHX_ struct shash *sh, word const *loc,
2629 int fanout, bool leftmost, struct pvl keypvl)
2630 {
2631 int l, r;
2632 418002497 word const *nodebody = bnode_body_loc(loc);
2633 1675744353 ▶ for(l = unlikely(leftmost) ? -1 : 0, r = fanout - 1; l != r; ) {
Branch: 2 branches
BranchTrueFalseTotal
for(l = unlikely(leftmost) ? -1 : 0, r = fanout - 1; l != r; ) { 262232 417740265 418002497
for(l = unlikely(leftmost) ? -1 : 0, r = fanout - 1; l != r; ) { 1295531066 380213287 1675744353
2634 /* binary search invariant:
2635 * search key > lower bount of subnode [l]
2636 * search key < upper bound of subnode [r]
2637 */
2638 1295531066 int m = (l+r+1) >> 1;
2639 1295531066 int cmpm = string_cmp_pvl(sh, nodebody[m << 1], keypvl);
2640 1295531066 ▶ if(unlikely(cmpm == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(cmpm == 0)) { 37789210 1257741856 1295531066
2641 37789210 return BNODE_SEARCH_EXACT | (m + 1);
2642 1257741856 ▶ } else if(cmpm > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(cmpm > 0) { 573593326 684148530 1257741856
2643 573593326 r = m-1;
2644 } else {
2645 684148530 l = m;
2646 }
2647 }
2648 380213287 return l + 1;
2649 }
2650  
2651 #define bnode_write(sh, alloc, nh, ne, nb) \
2652 THX_bnode_write(aTHX_ sh, alloc, nh, ne, nb)
2653 3770850 static word THX_bnode_write(pTHX_ struct shash *sh, struct shash_alloc *alloc,
2654 int layer, int fanout, word const *nodebody)
2655 {
2656 word ptr, *loc;
2657 3770850 ▶ if(unlikely(fanout == 0) && likely(layer == 0) &&
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(fanout == 0) && likely(layer == 0) && 64 3770786 3770850
if(unlikely(fanout == 0) && likely(layer == 0) && 64 0 64
2658 64 ▶ likely(sh->sizes->dhd_zeropad_sz >= WORD_SZ))
Branch: 1 branch
BranchTrueFalseTotal
likely(sh->sizes->dhd_zeropad_sz >= WORD_SZ)) 64 0 64
2659 64 return ZEROPAD_PTR;
2660 3770786 loc = shash_alloc(sh, alloc, WORD_SZ + (fanout << (WORD_SZ_LOG2+1)),
2661 &ptr);
2662 3770726 loc[0] = layer | (fanout << 8);
2663 3770726 (void) memcpy(&loc[1], nodebody, fanout << (WORD_SZ_LOG2+1));
2664 3770726 tally_event(&sh->tally, K_BNODE_WRITE);
2665 3770726 return ptr;
2666 }
2667  
2668 struct cursor_entry {
2669 word nodeptr;
2670 short index;
2671 byte fanout;
2672 };
2673  
2674 struct cursor {
2675 byte root_layer;
2676 struct cursor_entry ent[LAYER_MAX+1];
2677 };
2678  
2679 #define btree_seek_key(sh, cs, rt, keypvl) \
2680 THX_btree_seek_key(aTHX_ sh, cs, rt, keypvl)
2681 86794163 static bool THX_btree_seek_key(pTHX_ struct shash *sh, struct cursor *cursor,
2682 word root, struct pvl keypvl)
2683 {
2684 86794163 bool leftmost = 1;
2685 int layer, pos, fanout;
2686 word ptr;
2687 word const *ndloc;
2688 86794163 ndloc = bnode_check(sh, root, -1, &layer, &fanout);
2689 86794163 cursor->root_layer = layer;
2690 86794163 cursor->ent[layer].nodeptr = root;
2691 while(1) {
2692 418002497 cursor->ent[layer].fanout = fanout;
2693 418002497 ▶ pos = bnode_search(sh, ndloc, fanout, leftmost && layer == 0,
Branch: 2 branches
BranchTrueFalseTotal
pos = bnode_search(sh, ndloc, fanout, leftmost && layer == 0, 112084158 305918339 418002497
pos = bnode_search(sh, ndloc, fanout, leftmost && layer == 0, 262232 111821926 112084158
2694 keypvl);
2695 418002497 cursor->ent[layer].index = (pos & ~BNODE_SEARCH_EXACT) - 1;
2696 418002497 ▶ if(unlikely(pos & BNODE_SEARCH_EXACT))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(pos & BNODE_SEARCH_EXACT)) 37789210 380213287 418002497
2697 37789210 goto exact_match;
2698 380213287 ▶ if(unlikely(layer == 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(layer == 0)) return 0; 49004953 331208334 380213287
2699 331208334 ▶ if(likely(pos != 1)) leftmost = 0;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(pos != 1)) leftmost = 0; 283804399 47403935 331208334
2700 331208334 ptr = bnode_body_loc(ndloc)[(pos<<1)-1];
2701 331208334 layer--;
2702 331208334 cursor->ent[layer].nodeptr = ptr;
2703 331208334 ndloc = bnode_check(sh, ptr, layer, &layer, &fanout);
2704 }
2705 37789210 exact_match:
2706 37789210 ▶ if(likely(layer == 0)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(layer == 0)) return 1; 34195191 3594019 37789210
2707 3594019 ptr = bnode_body_loc(ndloc)[((pos&~BNODE_SEARCH_EXACT)<<1)-1];
2708 while(1) {
2709 3973388 layer--;
2710 3973388 cursor->ent[layer].nodeptr = ptr;
2711 3973388 ndloc = bnode_check(sh, ptr, layer, &layer, &fanout);
2712 3973388 cursor->ent[layer].fanout = fanout;
2713 3973388 cursor->ent[layer].index = 0;
2714 3973388 ▶ if(likely(layer == 0)) break;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(layer == 0)) break; 3594019 379369 3973388
2715 379369 ptr = bnode_body_loc(ndloc)[1];
2716 }
2717 3594019 return 1;
2718 }
2719  
2720 #define btree_seek_min(sh, cs, rt) THX_btree_seek_min(aTHX_ sh, cs, rt)
2721 1415 static bool THX_btree_seek_min(pTHX_ struct shash *sh,
2722 struct cursor *cursor, word root)
2723 {
2724 int layer, fanout;
2725 word const *ndloc;
2726 1415 ndloc = bnode_check(sh, root, -1, &layer, &fanout);
2727 1415 cursor->root_layer = layer;
2728 1415 cursor->ent[layer].nodeptr = root;
2729 3759 while(1) {
2730 word ptr;
2731 5174 cursor->ent[layer].fanout = fanout;
2732 5174 cursor->ent[layer].index = 0;
2733 5174 ▶ if(unlikely(layer == 0)) return likely(fanout != 0);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(layer == 0)) return likely(fanout != 0); 1415 3759 5174
2734 3759 ptr = bnode_body_loc(ndloc)[1];
2735 3759 layer--;
2736 3759 cursor->ent[layer].nodeptr = ptr;
2737 3759 ndloc = bnode_check(sh, ptr, layer, &layer, &fanout);
2738 }
2739 }
2740  
2741 #define btree_seek_max(sh, cs, rt) THX_btree_seek_max(aTHX_ sh, cs, rt)
2742 5239 PERL_STATIC_INLINE bool THX_btree_seek_max(pTHX_ struct shash *sh,
2743 struct cursor *cursor, word root)
2744 {
2745 int layer, fanout;
2746 word const *ndloc;
2747 5239 ndloc = bnode_check(sh, root, -1, &layer, &fanout);
2748 5239 cursor->root_layer = layer;
2749 5239 cursor->ent[layer].nodeptr = root;
2750 20671 while(1) {
2751 word ptr;
2752 25910 cursor->ent[layer].fanout = fanout;
2753 25910 cursor->ent[layer].index = fanout - 1;
2754 25910 ▶ if(unlikely(layer == 0)) return likely(fanout != 0);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(layer == 0)) return likely(fanout != 0); 5239 20671 25910
2755 20671 ptr = bnode_body_loc(ndloc)[(fanout<<1)-1];
2756 20671 layer--;
2757 20671 cursor->ent[layer].nodeptr = ptr;
2758 20671 ndloc = bnode_check(sh, ptr, layer, &layer, &fanout);
2759 }
2760 }
2761  
2762 #define btree_seek_inc(sh, cs) THX_btree_seek_inc(aTHX_ sh, cs)
2763 38076625 static bool THX_btree_seek_inc(pTHX_ struct shash *sh, struct cursor *cursor)
2764 {
2765 38076625 int layer = 0, pos;
2766 while(1) {
2767 43648470 pos = cursor->ent[layer].index + 1;
2768 43648470 ▶ if(likely(pos != cursor->ent[layer].fanout)) break;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(pos != cursor->ent[layer].fanout)) break; 38075314 5573156 43648470
2769 5573156 ▶ if(unlikely(layer == cursor->root_layer)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(layer == cursor->root_layer)) return 0; 1311 5571845 5573156
2770 5571845 layer++;
2771 }
2772 38075314 cursor->ent[layer].index = pos;
2773 38075314 ▶ if(unlikely(layer != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(layer != 0)) { 5037057 33038257 38075314
2774 5037057 word ptr = bnode_body_loc(
2775 unchecked_pointer_loc(sh, cursor->ent[layer].nodeptr))
2776 5037057 [(pos<<1)+1];
2777 531032 while(1) {
2778 int fanout;
2779 word const *ndloc;
2780 5568089 layer--;
2781 5568089 cursor->ent[layer].nodeptr = ptr;
2782 5568089 ndloc = bnode_check(sh, ptr, layer, &layer, &fanout);
2783 5568089 cursor->ent[layer].fanout = fanout;
2784 5568089 cursor->ent[layer].index = 0;
2785 5568089 ▶ if(likely(layer == 0)) break;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(layer == 0)) break; 5037057 531032 5568089
2786 531032 ptr = bnode_body_loc(ndloc)[1];
2787 }
2788 }
2789 38075314 return 1;
2790 }
2791  
2792 #define btree_seek_dec(sh, cs) THX_btree_seek_dec(aTHX_ sh, cs)
2793 6092303 PERL_STATIC_INLINE bool THX_btree_seek_dec(pTHX_ struct shash *sh,
2794 struct cursor *cursor)
2795 {
2796 6092303 int layer = 0, pos;
2797 while(1) {
2798 6743200 pos = cursor->ent[layer].index - 1;
2799 6743200 ▶ if(likely(pos != -1)) break;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(pos != -1)) break; 6092115 651085 6743200
2800 651085 ▶ if(unlikely(layer == cursor->root_layer)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(layer == cursor->root_layer)) return 0; 188 650897 651085
2801 650897 layer++;
2802 }
2803 6092115 cursor->ent[layer].index = pos;
2804 6092115 ▶ if(unlikely(layer != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(layer != 0)) { 588113 5504002 6092115
2805 588113 word ptr = bnode_body_loc(
2806 unchecked_pointer_loc(sh, cursor->ent[layer].nodeptr))
2807 588113 [(pos<<1)+1];
2808 62101 while(1) {
2809 int fanout;
2810 word const *ndloc;
2811 650214 layer--;
2812 650214 cursor->ent[layer].nodeptr = ptr;
2813 650214 ndloc = bnode_check(sh, ptr, layer, &layer, &fanout);
2814 650214 cursor->ent[layer].fanout = fanout;
2815 650214 cursor->ent[layer].index = fanout - 1;
2816 650214 ▶ if(likely(layer == 0)) break;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(layer == 0)) break; 588113 62101 650214
2817 62101 ptr = bnode_body_loc(ndloc)[(fanout<<1)-1];
2818 }
2819 }
2820 6092115 return 1;
2821 }
2822  
2823 #define btree_cursor_key(sh, cs) THX_btree_cursor_key(aTHX_ sh, cs)
2824 67290218 PERL_STATIC_INLINE word THX_btree_cursor_key(pTHX_ struct shash *sh,
2825 struct cursor *cursor)
2826 {
2827 PERL_UNUSED_THX();
2828 67290218 return bnode_body_loc(unchecked_pointer_loc(sh, cursor->ent[0].nodeptr))
2829 67290218 [(cursor->ent[0].index << 1)];
2830 }
2831  
2832 #define btree_cursor_get(sh, cs) THX_btree_cursor_get(aTHX_ sh, cs)
2833 19391622 PERL_STATIC_INLINE word THX_btree_cursor_get(pTHX_ struct shash *sh,
2834 struct cursor *cursor)
2835 {
2836 PERL_UNUSED_THX();
2837 19391622 return bnode_body_loc(unchecked_pointer_loc(sh, cursor->ent[0].nodeptr))
2838 19391622 [(cursor->ent[0].index << 1) + 1];
2839 }
2840  
2841 #define btree_cursor_modify(sh, alloc, ocs, repl, ik, iv) \
2842 THX_btree_cursor_modify(aTHX_ sh, alloc, ocs, repl, ik, iv)
2843 700910 static word THX_btree_cursor_modify(pTHX_ struct shash *sh,
2844 struct shash_alloc *alloc, struct cursor *oldcursor,
2845 bool replace, word inskeyptr, word insvalptr)
2846 {
2847 700910 int ntorm, ntoin, layer = 0, posadj;
2848 700910 word inakey = NULL_PTR, inaval = NULL_PTR;
2849 700910 word inbkey = NULL_PTR, inbval = NULL_PTR;
2850 word nodebody[(MAXFANOUT+MINFANOUT-1)*2];
2851 700910 ntorm = replace ? 1 : 0;
2852 700910 posadj = replace ? 0 : 1;
2853 700910 ▶ if(inskeyptr == NULL_PTR) {
Branch: 1 branch
BranchTrueFalseTotal
if(inskeyptr == NULL_PTR) { 64063 636847 700910
2854 64063 ntoin = 0;
2855 } else {
2856 636847 ntoin = 1;
2857 636847 inakey = inskeyptr;
2858 636847 inaval = insvalptr;
2859 }
2860 do {
2861 3397265 int nfanout = oldcursor->ent[layer].fanout;
2862 3397265 int modpos = oldcursor->ent[layer].index + posadj;
2863 3397265 word *ndloc = unchecked_pointer_loc(sh,
2864 oldcursor->ent[layer].nodeptr);
2865 3397265 posadj = 0;
2866 3397265 (void) memcpy(nodebody, bnode_body_loc(ndloc),
2867 3397265 modpos << (WORD_SZ_LOG2+1));
2868 3397265 ▶ if(likely(ntoin)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(ntoin)) { 3333202 64063 3397265
2869 3333202 nodebody[modpos<<1] = inakey;
2870 3333202 nodebody[(modpos<<1)+1] = inaval;
2871 3333202 ▶ if(unlikely(ntoin > 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(ntoin > 1)) { 50910 3282292 3333202
2872 50910 nodebody[(modpos<<1)+2] = inbkey;
2873 50910 nodebody[(modpos<<1)+3] = inbval;
2874 }
2875 }
2876 3397265 (void) memcpy(nodebody + ((modpos+ntoin)<<1),
2877 3397265 bnode_body_loc(ndloc) + ((modpos+ntorm)<<1),
2878 3397265 (nfanout-(modpos+ntorm)) << (WORD_SZ_LOG2+1));
2879 3397265 nfanout = nfanout - ntorm + ntoin;
2880 3397265 ▶ if(likely(nfanout >= MINFANOUT)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(nfanout >= MINFANOUT)) { 3031113 366152 3397265
2881 3031113 ntorm = 1;
2882 } else {
2883 word const *upndloc;
2884 int uppos;
2885 366152 ▶ if(likely(layer == oldcursor->root_layer)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(layer == oldcursor->root_layer)) { 349017 17135 366152
2886 349017 ▶ if(unlikely(nfanout == 1) && likely(layer != 0))
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(nfanout == 1) && likely(layer != 0)) 65 348952 349017
if(unlikely(nfanout == 1) && likely(layer != 0)) 4 61 65
2887 4 return nodebody[1];
2888 349013 return bnode_write(sh, alloc, layer, nfanout,
2889 nodebody);
2890 }
2891 17135 ntorm = 2;
2892 17135 upndloc = unchecked_pointer_loc(sh,
2893 oldcursor->ent[layer+1].nodeptr);
2894 17135 uppos = oldcursor->ent[layer+1].index;
2895 17135 ▶ if(likely(uppos + 1 !=
Branch: 1 branch
BranchTrueFalseTotal
if(likely(uppos + 1 != 15746 1389 17135
2896 oldcursor->ent[layer+1].fanout)) {
2897 int adjnlayer, adjnfanout;
2898 15746 word adjndptr =
2899 15746 bnode_body_loc(upndloc)[(uppos<<1) + 3];
2900 word const *adjndloc =
2901 15746 bnode_check(sh, adjndptr, layer,
2902 &adjnlayer, &adjnfanout);
2903 15746 (void) memcpy(nodebody + (nfanout<<1),
2904 15746 bnode_body_loc(adjndloc),
2905 15746 adjnfanout << (WORD_SZ_LOG2+1));
2906 15746 nfanout += adjnfanout;
2907 } else {
2908 int adjnlayer, adjnfanout;
2909 word adjndptr;
2910 word const *adjndloc;
2911 1389 posadj = -1;
2912 1389 adjndptr =
2913 1389 bnode_body_loc(upndloc)[(uppos<<1) - 1];
2914 1389 adjndloc = bnode_check(sh, adjndptr, layer,
2915 &adjnlayer, &adjnfanout);
2916 1389 (void) memmove(nodebody + (adjnfanout<<1),
2917 1389 nodebody, nfanout << (WORD_SZ_LOG2+1));
2918 1389 (void) memcpy(nodebody,
2919 1389 bnode_body_loc(adjndloc),
2920 1389 adjnfanout << (WORD_SZ_LOG2+1));
2921 1389 nfanout += adjnfanout;
2922 }
2923 }
2924 3048248 ▶ if(unlikely(nfanout > MAXFANOUT)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(nfanout > MAXFANOUT)) { 50939 2997309 3048248
2925 50939 int splitpos = nfanout >> 1;
2926 50939 inakey = nodebody[0];
2927 50939 inaval = bnode_write(sh, alloc, layer, splitpos,
2928 nodebody);
2929 50939 inbkey = nodebody[splitpos << 1];
2930 50939 inbval = bnode_write(sh, alloc, layer, nfanout-splitpos,
2931 nodebody + (splitpos<<1));
2932 50937 ntoin = 2;
2933 } else {
2934 2997309 inakey = nodebody[0];
2935 2997309 inaval = bnode_write(sh, alloc, layer, nfanout,
2936 nodebody);
2937 2997259 ntoin = 1;
2938 }
2939 3048196 ▶ } while(layer++ != oldcursor->root_layer);
Branch: 1 branch
BranchTrueFalseTotal
} while(layer++ != oldcursor->root_layer); 2696355 351841 3048196
2940 351841 ▶ if(likely(ntoin == 1)) return inaval;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(ntoin == 1)) return inaval; 351814 27 351841
2941 27 ▶ if(unlikely(layer == LAYER_MAX+1))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(layer == LAYER_MAX+1)) 0 27 27
2942 0 shash_error_toobig(sh, alloc->action);
2943 27 nodebody[0] = inakey;
2944 27 nodebody[1] = inaval;
2945 27 nodebody[2] = inbkey;
2946 27 nodebody[3] = inbval;
2947 27 return bnode_write(sh, alloc, layer, 2, nodebody);
2948 }
2949  
2950 #define btree_cursor_set(sh, alloc, ocs, repl, keypvl, valpvl) \
2951 THX_btree_cursor_set(aTHX_ sh, alloc, ocs, repl, keypvl, valpvl)
2952 737057 static word THX_btree_cursor_set(pTHX_ struct shash *sh,
2953 struct shash_alloc *alloc, struct cursor *oldcursor,
2954 bool replace, struct pvl keypvl, struct pvl valpvl)
2955 {
2956 737057 ▶ if(pvl_is_null(valpvl)) {
Branch: 1 branch
BranchTrueFalseTotal
if(pvl_is_null(valpvl)) { 100169 636888 737057
2957 100169 ▶ if(!replace)
Branch: 1 branch
BranchTrueFalseTotal
if(!replace) 36106 64063 100169
2958 36106 return oldcursor->ent[oldcursor->root_layer].nodeptr;
2959 64063 return btree_cursor_modify(sh, alloc, oldcursor, 1,
2960 NULL_PTR, NULL_PTR);
2961 } else {
2962 word keyptr;
2963 636888 ▶ if(replace) {
Branch: 1 branch
BranchTrueFalseTotal
if(replace) { 254669 382219 636888
2964 254669 ▶ if(string_eq_pvl(sh, btree_cursor_get(sh, oldcursor),
Branch: 1 branch
BranchTrueFalseTotal
if(string_eq_pvl(sh, btree_cursor_get(sh, oldcursor), 21 254648 254669
2965 valpvl))
2966 21 return oldcursor->ent[oldcursor->root_layer]
2967 21 .nodeptr;
2968 254648 keyptr = btree_cursor_key(sh, oldcursor);
2969 } else {
2970 382219 keyptr = string_write_from_pvl(sh, alloc, keypvl);
2971 }
2972 636855 return btree_cursor_modify(sh, alloc, oldcursor, replace,
2973 keyptr, string_write_from_pvl(sh, alloc, valpvl));
2974 }
2975 }
2976  
2977 #define btree_count(sh, rt) THX_btree_count(aTHX_ sh, rt)
2978 512 static word THX_btree_count(pTHX_ struct shash *sh, word root)
2979 {
2980 struct cursor cur;
2981 512 word cnt = 0;
2982 512 ▶ if(!likely(btree_seek_min(sh, &cur, root))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(btree_seek_min(sh, &cur, root))) return 0; 50 462 512
2983 do {
2984 1214925 cnt += cur.ent[0].fanout;
2985 1214925 cur.ent[0].index = cur.ent[0].fanout - 1;
2986 1214925 ▶ } while(btree_seek_inc(sh, &cur));
Branch: 1 branch
BranchTrueFalseTotal
} while(btree_seek_inc(sh, &cur)); 1214463 462 1214925
2987 462 return cnt;
2988 }
2989  
2990 #define btree_size(sh, rt) THX_btree_size(aTHX_ sh, rt)
2991 188 static word THX_btree_size(pTHX_ struct shash *sh, word root)
2992 {
2993 struct cursor cur;
2994 word sz;
2995 188 ▶ if(!likely(btree_seek_min(sh, &cur, root))) {
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(btree_seek_min(sh, &cur, root))) { 82 106 188
2996 82 ▶ if(likely(sh->sizes->dhd_zeropad_sz >= WORD_SZ)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(likely(sh->sizes->dhd_zeropad_sz >= WORD_SZ)) return 0; 82 0 82
2997 0 sz = WORD_SZ;
2998 } else {
2999 106 sz = 0;
3000 do {
3001 int i;
3002 word asz;
3003 292233 word *loc = bnode_body_loc(
3004 unchecked_pointer_loc(sh, cur.ent[0].nodeptr));
3005 6480807 ▶ for(i = cur.ent[0].fanout << 1; i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = cur.ent[0].fanout << 1; i--; ) { 6188574 292233 6480807
3006 6188574 asz = string_size(sh, *loc++);
3007 6188574 sz += asz;
3008 6188574 ▶ if(unlikely(sz < asz)) return ~(word)0;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(sz < asz)) return ~(word)0; 0 6188574 6188574
3009 }
3010 /*
3011 * To account for all the space occupied by the
3012 * btree nodes, we allow a certain number of
3013 * bytes per entry, such that there is space for
3014 * an arbitrarily high btree of minimal fanout.
3015 * The objective is to allow enough space
3016 * per entry that for each minimally-filled
3017 * layer-0 node we allocate space for that node
3018 * and have one entry's allocation left over.
3019 * That one-entry-per-node excess then accounts
3020 * for the size of the layer-1 nodes with one
3021 * entry's allocation per layer-1 node left
3022 * over, and so on recursively. The space to
3023 * allow per entry is theoretically the size
3024 * of the minimally-filled node (WORD_SZ *
3025 * (1+2*MINFANOUT)) divided by MINFANOUT-1;
3026 * we round this up to an integral number of
3027 * bytes per entry.
3028 *
3029 * Nodes that are more than minimally filled lead
3030 * to this being an overestimate, because they
3031 * are more space-efficient both in themselves
3032 * and by using fewer higher-layer entries.
3033 * An underfilled root node can lead to needing
3034 * more bytes than this formula allows, but the
3035 * space allowed for the node will always be
3036 * strictly greater than the two words per entry
3037 * required by the node body. Because the size
3038 * is ultimately rounded up to word alignment
3039 * (actually to line alignment), it is rounded
3040 * up sufficiently to account for the single-word
3041 * header of the root node.
3042 */
3043 292233 asz = cur.ent[0].fanout *
3044 ((WORD_SZ*(1+2*MINFANOUT) + MINFANOUT-2) /
3045 (MINFANOUT-1));
3046 292233 sz += asz;
3047 292233 ▶ if(unlikely(sz < asz)) return ~(word)0;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(sz < asz)) return ~(word)0; 0 292233 292233
3048 292233 cur.ent[0].index = cur.ent[0].fanout - 1;
3049 292233 ▶ } while(btree_seek_inc(sh, &cur));
Branch: 1 branch
BranchTrueFalseTotal
} while(btree_seek_inc(sh, &cur)); 292127 106 292233
3050 }
3051 106 sz = LINE_ALIGN(sh->sizes, sz);
3052 106 ▶ return likely(sz) ? sz : ~(word)0;
Branch: 1 branch
BranchTrueFalseTotal
return likely(sz) ? sz : ~(word)0; 106 0 106
3053 }
3054  
3055 #define btree_migrate_at_layer(shf, ptrf, el, sht, alloct) \
3056 THX_btree_migrate_at_layer(aTHX_ shf, ptrf, el, sht, alloct)
3057 322623 static word THX_btree_migrate_at_layer(pTHX_ struct shash *shf, word ptrf,
3058 int expect_layer, struct shash *sht, struct shash_alloc *alloct)
3059 {
3060 int layer, fanout, i;
3061 word nodebody[MAXFANOUT*2];
3062 322623 word const *locf = bnode_body_loc(bnode_check(shf, ptrf, expect_layer,
3063 &layer, &fanout));
3064 322623 word *loct = nodebody;
3065 322623 ▶ if(likely(layer == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(layer == 0)) { 292270 30353 322623
3066 6480544 ▶ for(i = fanout << 1; i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = fanout << 1; i--; ) { 6188274 292270 6480544
3067 6188274 *loct++ = string_migrate(shf, *locf++, sht, alloct);
3068 }
3069 } else {
3070 352829 ▶ for(i = fanout; i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = fanout; i--; ) { 322476 30353 352829
3071 word spc;
3072 322476 word ptrt = btree_migrate_at_layer(shf, locf[1],
3073 layer-1, sht, alloct);
3074 322476 locf += 2;
3075 322476 *loct++ =
3076 322476 bnode_body_loc(pointer_loc(sht, ptrt, &spc))[0];
3077 322476 *loct++ = ptrt;
3078 }
3079 }
3080 322623 return bnode_write(sht, alloct, layer, fanout, nodebody);
3081 }
3082  
3083 #define btree_migrate(shf, ptrf, sht, act) \
3084 THX_btree_migrate(aTHX_ shf, ptrf, sht, act)
3085 147 static word THX_btree_migrate(pTHX_ struct shash *shf, word ptrf,
3086 struct shash *sht, char const *action)
3087 {
3088 struct shash_alloc new_alloc;
3089 147 ▶ if(unlikely(setjmp(new_alloc.fulljb)))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(setjmp(new_alloc.fulljb))) 0 147 147
3090 0 shash_error_errnum(sht, action, ENOSPC);
3091 147 new_alloc.action = action;
3092 147 new_alloc.prealloc_len = 0;
3093 147 return btree_migrate_at_layer(shf, ptrf, -1, sht, &new_alloc);
3094 }
3095  
3096 /* mechanism for reading from shash */
3097  
3098 #define shash_root_for_read(sh) THX_shash_root_for_read(aTHX_ sh)
3099 86014748 PERL_STATIC_INLINE word THX_shash_root_for_read(pTHX_ struct shash *sh)
3100 {
3101 86014748 ▶ if(sh->mode & STOREMODE_SNAPSHOT) {
Branch: 1 branch
BranchTrueFalseTotal
if(sh->mode & STOREMODE_SNAPSHOT) { 645 86014103 86014748
3102 645 return sh->u.snapshot.root;
3103 } else {
3104 86014103 shash_ensure_data_file(sh);
3105 86014103 return word_get(&WORD_AT(sh->data_mmap,
3106 86014103 sh->sizes->dhd_current_root)) &
3107 ~PTR_FLAG_ROLLOVER;
3108 }
3109 }
3110  
3111 /* mechanism for writing to shash */
3112  
3113 700907 PERL_STATIC_INLINE bool shash_change_root(struct shash *sh, word old, word new)
3114 {
3115 700907 tally_event(&sh->tally, K_ROOT_CHANGE_ATTEMPT);
3116 700907 ▶ if(likely(word_cset(
Branch: 1 branch
BranchTrueFalseTotal
if(likely(word_cset( 611992 88915 700907
3117 &WORD_AT(sh->data_mmap, sh->sizes->dhd_current_root),
3118 old, new))) {
3119 611992 tally_event(&sh->tally, K_ROOT_CHANGE_SUCCESS);
3120 611992 return 1;
3121 } else {
3122 88915 return 0;
3123 }
3124 }
3125  
3126 147 PERL_STATIC_INLINE bool shash_change_file(struct shash *sh, word old, word new)
3127 {
3128 147 tally_event(&sh->tally, K_FILE_CHANGE_ATTEMPT);
3129 147 ▶ if(likely(word_cset(&WORD_AT(sh->u.live.master_mmap,
Branch: 1 branch
BranchTrueFalseTotal
if(likely(word_cset(&WORD_AT(sh->u.live.master_mmap, 114 33 147
3130 sh->sizes->mfl_current_datafileid),
3131 old, new))) {
3132 114 tally_event(&sh->tally, K_FILE_CHANGE_SUCCESS);
3133 114 return 1;
3134 } else {
3135 33 return 0;
3136 }
3137 }
3138  
3139 80 PERL_STATIC_INLINE void shash_initiate_rollover(struct shash *sh)
3140 {
3141 80 word *root_p = &WORD_AT(sh->data_mmap, sh->sizes->dhd_current_root);
3142 0 while(1) {
3143 80 word root = word_get(root_p);
3144 80 ▶ if(unlikely(root & PTR_FLAG_ROLLOVER)) break;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(root & PTR_FLAG_ROLLOVER)) break; 24 56 80
3145 56 ▶ if(likely(shash_change_root(sh, root,
Branch: 1 branch
BranchTrueFalseTotal
if(likely(shash_change_root(sh, root, 56 0 56
3146 root | PTR_FLAG_ROLLOVER)))
3147 56 break;
3148 }
3149 80 }
3150  
3151 #define shash_try_rollover(sh, act, addsz) \
3152 THX_shash_try_rollover(aTHX_ sh, act, addsz)
3153 147 PERL_STATIC_INLINE word THX_shash_try_rollover(pTHX_ struct shash *sh,
3154 char const *action, word addsz)
3155 {
3156 char filename[DATA_FILENAME_BUFSIZE];
3157 word *allocfileid_p;
3158 word old_file_id, old_root_word, old_root;
3159 word new_file_id, new_root, new_sz;
3160 struct stat statbuf;
3161 int new_fd;
3162 unlinkfile_ref_t new_ulr;
3163 closefd_ref_t new_fdr;
3164 struct shash new_sh;
3165 SV *old_mmap_sv;
3166 tmps_ix_t old_tmps_floor;
3167 147 old_root_word = word_get(&WORD_AT(sh->data_mmap,
3168 sh->sizes->dhd_current_root));
3169 147 old_root = old_root_word & ~PTR_FLAG_ROLLOVER;
3170 147 new_sz = sh->sizes->dhd_sz + btree_size(sh, old_root);
3171 147 ▶ if(unlikely(new_sz < sh->sizes->dhd_sz || (new_sz & (((word)7) << 61))))
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely(new_sz < sh->sizes->dhd_sz || (new_sz & (((word)7) << 61)))) 147 0 147
if(unlikely(new_sz < sh->sizes->dhd_sz || (new_sz & (((word)7) << 61)))) 0 147 147
3172 0 shash_error_toobig(sh, action);
3173 147 new_sz <<= 3;
3174 147 new_sz += addsz;
3175 147 ▶ if(unlikely(new_sz < addsz)) shash_error_toobig(sh, action);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(new_sz < addsz)) shash_error_toobig(sh, action); 0 147 147
3176 147 new_sz = PAGE_ALIGN(sh->sizes, new_sz);
3177 147 ▶ if(unlikely(!new_sz)) shash_error_toobig(sh, action);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(!new_sz)) shash_error_toobig(sh, action); 0 147 147
3178 147 ▶ if(unlikely((off_t)new_sz < 0 || (word)(off_t)new_sz != new_sz))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely((off_t)new_sz < 0 || (word)(off_t)new_sz != new_sz)) 0 147 147
3179 0 shash_error_errnum(sh, action, EFBIG);
3180 147 tally_zero(&new_sh.tally);
3181 147 new_sh.sizes = sh->sizes;
3182 147 new_sh.parameter = sh->parameter;
3183 147 new_sh.top_pathname_sv = sh->top_pathname_sv;
3184 147 allocfileid_p = &WORD_AT(sh->u.live.master_mmap,
3185 sh->sizes->mfl_lastalloc_datafileid);
3186 do {
3187 149 old_file_id = word_get(allocfileid_p);
3188 149 new_file_id = old_file_id + 1;
3189 149 ▶ if(unlikely(new_file_id == 0)) new_file_id = 1;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(new_file_id == 0)) new_file_id = 1; 0 149 149
3190 149 ▶ } while(!likely(word_cset(allocfileid_p, old_file_id, new_file_id)));
Branch: 1 branch
BranchTrueFalseTotal
} while(!likely(word_cset(allocfileid_p, old_file_id, new_file_id))); 2 147 149
3191 147 ▶ if(unlikely(dirref_rel_stat(sh->u.live.dir, MASTER_FILENAME, &statbuf)
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(dirref_rel_stat(sh->u.live.dir, MASTER_FILENAME, &statbuf) 0 147 147
3192 == -1))
3193 0 shash_error_errno(sh, action);
3194 147 dir_make_data_filename(filename, new_file_id);
3195 147 new_fd = dirref_rel_open_cloexec(sh->u.live.dir, filename,
3196 O_RDWR|O_CREAT|O_EXCL, 0);
3197 147 ▶ if(unlikely(new_fd == -1)) shash_error_errno(sh, action);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(new_fd == -1)) shash_error_errno(sh, action); 0 147 147
3198 147 new_ulr = unlinkfile_save(sh->u.live.dir, filename);
3199 147 ▶ if(unlikely(fchown(new_fd, -1, statbuf.st_gid) == -1) &&
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(fchown(new_fd, -1, statbuf.st_gid) == -1) && 0 147 147
3200 0 ▶ unlikely(errno != EPERM))
Branch: 1 branch
BranchTrueFalseTotal
unlikely(errno != EPERM)) 0 0 0
3201 0 shash_error_errno(sh, action);
3202 147 ▶ if(unlikely(fchmod(new_fd, statbuf.st_mode &
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(fchmod(new_fd, statbuf.st_mode & 0 147 147
3203 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH))
3204 == -1))
3205 0 shash_error_errno(sh, action);
3206 147 ▶ if(unlikely(fchown(new_fd, statbuf.st_uid, -1) == -1) &&
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(fchown(new_fd, statbuf.st_uid, -1) == -1) && 0 147 147
3207 0 ▶ unlikely(errno != EPERM))
Branch: 1 branch
BranchTrueFalseTotal
unlikely(errno != EPERM)) 0 0 0
3208 0 shash_error_errno(sh, action);
3209 147 new_fdr = closefd_save(new_fd);
3210 147 ▶ if(unlikely(ftruncate(new_fd, new_sz) == -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(ftruncate(new_fd, new_sz) == -1)) { 0 147 147
3211 /*
3212 * A file-too-big error may be reported as either
3213 * EFBIG or EINVAL depending on OS. The former is more
3214 * enlightening to the user, so always report it that way.
3215 */
3216 0 int e = errno;
3217 0 ▶ shash_error_errnum(sh, action, e == EINVAL ? EFBIG : e);
Branch: 1 branch
BranchTrueFalseTotal
shash_error_errnum(sh, action, e == EINVAL ? EFBIG : e); 0 0 0
3218 }
3219 147 old_tmps_floor = PL_tmps_floor;
3220 147 SAVETMPS;
3221 147 new_sh.data_mmap_sv = mmap_as_sv(new_fd, new_sz, 1);
3222 147 ▶ if(!likely(new_sh.data_mmap_sv)) shash_error_errno(sh, action);
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(new_sh.data_mmap_sv)) shash_error_errno(sh, action); 0 147 147
3223 147 new_sh.data_mmap = SvPVX(new_sh.data_mmap_sv);
3224 147 new_sh.data_size = new_sz;
3225 147 closefd_early(new_fdr);
3226 147 WORD_AT(new_sh.data_mmap, DHD_MAGIC) = DATA_FILE_MAGIC;
3227 147 WORD_AT(new_sh.data_mmap, DHD_PARAM) = sh->parameter;
3228 147 WORD_AT(new_sh.data_mmap, DHD_LENGTH) = new_sz;
3229 147 WORD_AT(new_sh.data_mmap, sh->sizes->dhd_nextalloc_space) =
3230 147 sh->sizes->dhd_sz;
3231 147 WORD_AT(new_sh.data_mmap, sh->sizes->dhd_current_root) = new_root =
3232 147 btree_migrate(sh, old_root, &new_sh, action);
3233 147 tally_add(&sh->tally, &new_sh.tally);
3234 147 old_file_id = sh->u.live.data_file_id;
3235 147 ▶ if((!(old_root_word & PTR_FLAG_ROLLOVER) &&
Branch: 1 branch
BranchTrueFalseTotal
if((!(old_root_word & PTR_FLAG_ROLLOVER) && 1 146 147
3236 1 ▶ !likely(shash_change_root(sh, old_root_word,
Branch: 1 branch
BranchTrueFalseTotal
!likely(shash_change_root(sh, old_root_word, 1 0 1
3237 147 old_root_word | PTR_FLAG_ROLLOVER))) ||
3238 147 ▶ !likely(shash_change_file(sh,
Branch: 1 branch
BranchTrueFalseTotal
!likely(shash_change_file(sh, 33 114 147
3239 old_file_id, new_file_id))) {
3240 33 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 33 0 33
3241 33 PL_tmps_floor = old_tmps_floor;
3242 33 shash_unlinkfile_early(sh, action, new_ulr);
3243 33 return NULL_PTR;
3244 }
3245 114 unlinkfile_cancel(new_ulr);
3246 114 old_mmap_sv = sh->data_mmap_sv;
3247 114 sh->data_mmap_sv = NULL;
3248 114 SvREFCNT_dec_NN(old_mmap_sv);
3249 114 sh->data_mmap_sv = SvREFCNT_inc_simple_NN(new_sh.data_mmap_sv);
3250 114 sh->data_mmap = new_sh.data_mmap;
3251 114 sh->data_size = new_sh.data_size;
3252 114 sh->u.live.data_file_id = new_file_id;
3253 114 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 114 0 114
3254 114 PL_tmps_floor = old_tmps_floor;
3255 114 ▶ if(likely(old_file_id != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(old_file_id != 0)) { 55 59 114
3256 55 dir_make_data_filename(filename, old_file_id);
3257 55 ▶ if(unlikely(dirref_rel_unlink(sh->u.live.dir, filename)
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(dirref_rel_unlink(sh->u.live.dir, filename) 0 55 55
3258 == -1)) {
3259 0 int e = errno;
3260 0 ▶ if(!(likely(e == ENOENT) || likely(e == EBUSY)))
Branch: 2 branches
BranchTrueFalseTotal
if(!(likely(e == ENOENT) || likely(e == EBUSY))) 0 0 0
if(!(likely(e == ENOENT) || likely(e == EBUSY))) 0 0 0
3261 0 shash_error_errnum(sh, action, e);
3262 }
3263 }
3264 114 return new_root;
3265 }
3266  
3267 typedef word (*mutate_fn_t)(pTHX_ struct shash *sh, struct shash_alloc *alloc,
3268 word oldroot, void *mutate_arg);
3269  
3270 #define shash_mutate(sh, act, mut, marg) \
3271 THX_shash_mutate(aTHX_ sh, act, mut, marg)
3272 702345 static void THX_shash_mutate(pTHX_ struct shash *sh, char const *action,
3273 mutate_fn_t THX_mutate, void *mutate_arg)
3274 {
3275 struct shash_alloc alloc;
3276 702345 volatile word addsz = PAGE_ALIGN(sh->sizes, 1<<20);
3277 702345 volatile bool just_rolled_over = 0;
3278 702345 alloc.action = action;
3279 702345 ▶ if(unlikely(setjmp(alloc.fulljb))) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(setjmp(alloc.fulljb))) { 702345 80 702425
3280 80 ▶ if(unlikely(just_rolled_over)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(just_rolled_over)) { 4 76 80
3281 4 word newaddsz = addsz <<= 1;
3282 4 ▶ if(!likely(newaddsz)) shash_error_toobig(sh, action);
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(newaddsz)) shash_error_toobig(sh, action); 0 4 4
3283 }
3284 80 shash_initiate_rollover(sh);
3285 }
3286 88948 while(1) {
3287 word old_root, new_root;
3288 791373 just_rolled_over = 0;
3289 791373 shash_ensure_data_file(sh);
3290 791373 old_root = word_get(&WORD_AT(sh->data_mmap,
3291 sh->sizes->dhd_current_root));
3292 791373 ▶ if(unlikely(old_root & PTR_FLAG_ROLLOVER)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(old_root & PTR_FLAG_ROLLOVER)) { 146 791227 791373
3293 146 old_root = shash_try_rollover(sh, action, addsz);
3294 146 ▶ if(unlikely(old_root == NULL_PTR)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(old_root == NULL_PTR)) continue; 33 113 146
3295 113 dir_clean(sh, action, sh->u.live.data_file_id);
3296 113 just_rolled_over = 1;
3297 }
3298 791340 alloc.prealloc_len = 0;
3299 791340 new_root = THX_mutate(aTHX_ sh, &alloc, old_root, mutate_arg);
3300 791260 ▶ if(likely(new_root == old_root) ||
Branch: 1 branch
BranchTrueFalseTotal
if(likely(new_root == old_root) || 700850 90410 791260
3301 700850 ▶ likely(shash_change_root(sh,
Branch: 1 branch
BranchTrueFalseTotal
likely(shash_change_root(sh, 88915 611935 700850
3302 old_root, new_root)))
3303 break;
3304 }
3305 702345 tally_event(&sh->tally, K_DATA_WRITE_OP);
3306 702345 }
3307  
3308 /* shash opening and creation */
3309  
3310 #define mode_from_sv(sv) THX_mode_from_sv(aTHX_ sv)
3311 795 PERL_STATIC_INLINE unsigned THX_mode_from_sv(pTHX_ SV *modesv)
3312 {
3313 char const *modepv, *modeend, *p;
3314 STRLEN modelen;
3315 795 unsigned mode = 0;
3316 795 SvGETMAGIC(modesv);
3317 795 ▶ if(!likely(sv_is_string(modesv))) croak("mode is not a string");
Branch: 4 branches
BranchTrueFalseTotal
if(!likely(sv_is_string(modesv))) croak("mode is not a string"); 792 3 795
if(!likely(sv_is_string(modesv))) croak("mode is not a string"); 789 3 792
if(!likely(sv_is_string(modesv))) croak("mode is not a string"); 789 6 795
if(!likely(sv_is_string(modesv))) croak("mode is not a string"); 24 765 789
3318 765 modepv = SvPV_nomg(modesv, modelen);
3319 765 modeend = modepv + modelen;
3320 1289 ▶ for(p = modepv; p != modeend; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(p = modepv; p != modeend; p++) { 1041 248 1289
3321 1041 char c = *p;
3322 unsigned f;
3323 1041 switch(c) {
3324 191 case 'r': f = STOREMODE_READ; break;
3325 169 case 'w': f = STOREMODE_WRITE; break;
3326 134 case 'c': f = STOREMODE_CREATE; break;
3327 38 case 'e': f = STOREMODE_EXCLUDE; break;
3328 509 default: {
3329 509 f = 0;
3330 509 ▶ if(likely(c >= ' ' && c <= '~'))
Branch: 2 branches
BranchTrueFalseTotal
if(likely(c >= ' ' && c <= '~')) 92 417 509
if(likely(c >= ' ' && c <= '~')) 91 1 92
3331 91 croak("unknown open mode flag `%c'", c);
3332 else
3333 418 croak("unknown open mode flag");
3334 }
3335 }
3336 532 ▶ if(unlikely(mode & f))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(mode & f)) 8 524 532
3337 8 croak("duplicate open mode flag `%c'", c);
3338 524 mode |= f;
3339 }
3340 248 return mode;
3341 }
3342  
3343 #define mode_as_sv(m) THX_mode_as_sv(aTHX_ m)
3344 57 PERL_STATIC_INLINE SV *THX_mode_as_sv(pTHX_ unsigned mode)
3345 {
3346 char buf[4], *p;
3347 SV *modesv;
3348 57 p = buf;
3349 57 ▶ if(likely(mode & STOREMODE_READ)) *p++ = 'r';
Branch: 1 branch
BranchTrueFalseTotal
if(likely(mode & STOREMODE_READ)) *p++ = 'r'; 35 22 57
3350 57 ▶ if(likely(mode & STOREMODE_WRITE)) *p++ = 'w';
Branch: 1 branch
BranchTrueFalseTotal
if(likely(mode & STOREMODE_WRITE)) *p++ = 'w'; 19 38 57
3351 57 ▶ if(unlikely(mode & STOREMODE_CREATE)) *p++ = 'c';
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(mode & STOREMODE_CREATE)) *p++ = 'c'; 0 57 57
3352 57 ▶ if(unlikely(mode & STOREMODE_EXCLUDE)) *p++ = 'e';
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(mode & STOREMODE_EXCLUDE)) *p++ = 'e'; 0 57 57
3353 57 modesv = newSVpvn_mortal(buf, p - buf);
3354 57 SvREADONLY_on(modesv);
3355 57 return modesv;
3356 }
3357  
3358 #define shash_open_error_magic(sh) THX_shash_open_error_magic(aTHX_ sh)
3359 static void THX_shash_open_error_magic(pTHX_ struct shash *sh)
3360 __attribute__noreturn__;
3361 21 static void THX_shash_open_error_magic(pTHX_ struct shash *sh)
3362 {
3363 21 shash_error(sh, "open", "not a shared hash");
3364 }
3365  
3366 659 static void THX_shash_open_check_file(pTHX_ struct shash *sh,
3367 char const *action, char const *fn, word arg)
3368 {
3369 word id;
3370 PERL_UNUSED_ARG(action);
3371 PERL_UNUSED_ARG(arg);
3372 659 ▶ if(unlikely(dir_filename_class(fn, &id) == FILENAME_CLASS_BOGUS))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(dir_filename_class(fn, &id) == FILENAME_CLASS_BOGUS)) 9 650 659
3373 9 shash_open_error_magic(sh);
3374 650 }
3375  
3376 #define shash_open(psv, msv) THX_shash_open(aTHX_ psv, msv)
3377 825 static SV *THX_shash_open(pTHX_ SV *top_pathname_sv, SV *mode_sv)
3378 {
3379 dMY_CXT;
3380 char const *top_pathname_pv;
3381 unsigned mode;
3382 struct shash *sh;
3383 SV *shsv, *shsvref, *mapsv, *sizes_sv;
3384 dirref_t dir;
3385 int master_fd;
3386 struct stat statbuf;
3387 unlinkfile_ref_t ulr;
3388 char temp_filename[TEMP_FILENAME_BUFSIZE];
3389 closefd_ref_t fdr;
3390 void *map;
3391 825 shsv = newSV_type(SVt_PVMG);
3392 825 shsvref = newRV_ro_mortal_noinc(shsv);
3393 825 Newxz(sh, 1, struct shash);
3394 825 SvPV_set(shsv, (char *)sh);
3395 825 SvLEN_set(shsv, sizeof(struct shash));
3396 825 shash_apply_magic(shsv);
3397 825 (void) sv_bless(shsvref, MY_CXT.shash_handle_stash);
3398 825 SvGETMAGIC(top_pathname_sv);
3399 825 ▶ if(!likely(sv_is_string(top_pathname_sv)))
Branch: 4 branches
BranchTrueFalseTotal
if(!likely(sv_is_string(top_pathname_sv))) 822 3 825
if(!likely(sv_is_string(top_pathname_sv))) 819 3 822
if(!likely(sv_is_string(top_pathname_sv))) 819 6 825
if(!likely(sv_is_string(top_pathname_sv))) 24 795 819
3400 30 croak("filename is not a string");
3401 {
3402 STRLEN len;
3403 795 char *pv = SvPV_nomg(top_pathname_sv, len);
3404 795 sh->top_pathname_sv = newSVpvn(pv, len);
3405 795 ▶ if(unlikely(SvUTF8(top_pathname_sv)))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(SvUTF8(top_pathname_sv))) 9 786 795
3406 9 SvUTF8_on(sh->top_pathname_sv);
3407 }
3408 795 mode = mode_from_sv(mode_sv);
3409 248 ▶ if(likely(mode & (STOREMODE_WRITE|STOREMODE_CREATE)))
Branch: 1 branch
BranchTrueFalseTotal
if(likely(mode & (STOREMODE_WRITE|STOREMODE_CREATE))) 194 54 248
3410 194 ▶ TAINT_PROPER("shash_open");
Branch: 1 branch
BranchTrueFalseTotal
TAINT_PROPER("shash_open"); 27 167 194
3411 230 sh->mode = mode & (STOREMODE_READ|STOREMODE_WRITE);
3412 230 top_pathname_pv = SvPV_nolen(sh->top_pathname_sv);
3413 230 sh->u.live.dir = dir = dirref_open(top_pathname_pv, &statbuf);
3414 230 ▶ if(unlikely(dirref_is_null(dir))) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(dirref_is_null(dir))) { 89 141 230
3415 89 ▶ if(!likely(errno == ENOENT && (mode & STOREMODE_CREATE)))
Branch: 2 branches
BranchTrueFalseTotal
if(!likely(errno == ENOENT && (mode & STOREMODE_CREATE))) 89 0 89
if(!likely(errno == ENOENT && (mode & STOREMODE_CREATE))) 9 80 89
3416 9 shash_error_errno(sh, "open");
3417 80 ▶ if(unlikely(mkdir(top_pathname_pv, S_IRWXU|S_IRWXG|S_IRWXO)
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(mkdir(top_pathname_pv, S_IRWXU|S_IRWXG|S_IRWXO) 0 80 80
3418 0 == -1) &&
3419 0 ▶ errno != EEXIST)
Branch: 1 branch
BranchTrueFalseTotal
errno != EEXIST) 0 0 0
3420 0 shash_error_errno(sh, "open");
3421 80 sh->u.live.dir = dir = dirref_open(top_pathname_pv, &statbuf);
3422 80 ▶ if(unlikely(dirref_is_null(dir))) shash_error_errno(sh, "open");
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(dirref_is_null(dir))) shash_error_errno(sh, "open"); 0 80 80
3423 }
3424 221 ▶ if(!likely(S_ISDIR(statbuf.st_mode)))
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(S_ISDIR(statbuf.st_mode))) 2 219 221
3425 2 shash_open_error_magic(sh);
3426 219 dir_iterate(sh, "open", THX_shash_open_check_file, 0);
3427 210 master_fd = dirref_rel_open_cloexec(dir, MASTER_FILENAME,
3428 210 ▶ likely(mode & STOREMODE_WRITE) ? O_RDWR : O_RDONLY, 0);
Branch: 1 branch
BranchTrueFalseTotal
likely(mode & STOREMODE_WRITE) ? O_RDWR : O_RDONLY, 0); 137 73 210
3429 210 ▶ if(likely(master_fd != -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(master_fd != -1)) { 121 89 210
3430 121 opened_master:
3431 121 fdr = closefd_save(master_fd);
3432 121 ▶ if(unlikely(mode & STOREMODE_EXCLUDE))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(mode & STOREMODE_EXCLUDE)) 8 113 121
3433 8 shash_error_errnum(sh, "open", EEXIST);
3434 113 ▶ if(unlikely(fstat(master_fd, &statbuf) == -1))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(fstat(master_fd, &statbuf) == -1)) 0 113 113
3435 0 shash_error_errno(sh, "open");
3436 113 ▶ if(!likely(S_ISREG(statbuf.st_mode) &&
Branch: 2 branches
BranchTrueFalseTotal
if(!likely(S_ISREG(statbuf.st_mode) && 112 1 113
if(!likely(S_ISREG(statbuf.st_mode) && 0 112 112
3437 (off_t)(word)statbuf.st_size ==
3438 statbuf.st_size &&
3439 statbuf.st_size >= MFL_PARAM+WORD_SZ))
3440 1 shash_open_error_magic(sh);
3441 112 mapsv = mmap_as_sv(master_fd, MFL_PARAM+WORD_SZ, 0);
3442 112 ▶ if(!likely(mapsv)) shash_error_errno(sh, "open");
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(mapsv)) shash_error_errno(sh, "open"); 0 112 112
3443 112 map = SvPVX(mapsv);
3444 112 ▶ if(!likely(WORD_AT(map, MFL_MAGIC) == MASTER_FILE_MAGIC))
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(WORD_AT(map, MFL_MAGIC) == MASTER_FILE_MAGIC)) 3 109 112
3445 3 shash_open_error_magic(sh);
3446 109 sh->parameter = WORD_AT(map, MFL_PARAM);
3447 109 ▶ if(unlikely(PARAMETER_WORD_FIXED_PART(sh->parameter) !=
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(PARAMETER_WORD_FIXED_PART(sh->parameter) != 0 109 109
3448 PARAMETER_WORD_FIXED_PART_VALUE)) {
3449 0 bad_parameter:
3450 0 shash_error(sh, "open", "unsupported format");
3451 }
3452 109 sizes_sv = sizes_lookup(sh->parameter);
3453 109 ▶ if(!likely(sizes_sv)) goto bad_parameter;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sizes_sv)) goto bad_parameter; 0 109 109
3454 #if QWITH_DUP
3455 sh->sizes_sv = sizes_sv;
3456 #endif /* QWITH_DUP */
3457 109 sh->sizes = (struct sizes const *)SvPVX(sizes_sv);
3458 109 mmap_early_unmap(mapsv);
3459 109 ▶ if(!likely((word)statbuf.st_size == sh->sizes->mfl_sz))
Branch: 1 branch
BranchTrueFalseTotal
if(!likely((word)statbuf.st_size == sh->sizes->mfl_sz)) 6 103 109
3460 6 shash_open_error_magic(sh);
3461 103 mapsv = mmap_as_sv(master_fd, sh->sizes->mfl_sz,
3462 cBOOL(mode & STOREMODE_WRITE));
3463 103 ▶ if(!likely(mapsv)) shash_error_errno(sh, "open");
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(mapsv)) shash_error_errno(sh, "open"); 0 103 103
3464 103 sh->u.live.master_mmap_sv = SvREFCNT_inc_simple_NN(mapsv);
3465 103 sh->u.live.master_mmap = SvPVX(mapsv);
3466 103 closefd_early(fdr);
3467 103 ▶ if(likely(mode & STOREMODE_WRITE))
Branch: 1 branch
BranchTrueFalseTotal
if(likely(mode & STOREMODE_WRITE)) 46 57 103
3468 46 dir_clean(sh, "open",
3469 word_get(&WORD_AT(sh->u.live.master_mmap,
3470 sh->sizes->mfl_current_datafileid)));
3471 103 return shsvref;
3472 }
3473 89 ▶ if(!likely(errno == ENOENT && (mode & STOREMODE_CREATE)))
Branch: 2 branches
BranchTrueFalseTotal
if(!likely(errno == ENOENT && (mode & STOREMODE_CREATE))) 87 2 89
if(!likely(errno == ENOENT && (mode & STOREMODE_CREATE))) 3 84 87
3474 5 shash_error_errno(sh, "open");
3475 84 sh->parameter = parameter_preferred();
3476 84 sizes_sv = sizes_lookup(sh->parameter);
3477 84 ▶ if(!likely(sizes_sv)) shash_error_errnum(sh, "open", ENOMEM);
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sizes_sv)) shash_error_errnum(sh, "open", ENOMEM); 0 84 84
3478 #if QWITH_DUP
3479 sh->sizes_sv = sizes_sv;
3480 #endif /* QWITH_DUP */
3481 84 sh->sizes = (struct sizes const *)SvPVX(sizes_sv);
3482 84 ▶ if(unlikely((off_t)sh->sizes->mfl_sz < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely((off_t)sh->sizes->mfl_sz < 0 || 0 84 84
3483 (word)(off_t)sh->sizes->mfl_sz != sh->sizes->mfl_sz))
3484 0 shash_error_errnum(sh, "open", EFBIG);
3485 84 dir_make_temp_filename(temp_filename);
3486 84 master_fd = dirref_rel_open_cloexec(dir, temp_filename,
3487 O_RDWR|O_CREAT|O_EXCL,
3488 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
3489 84 ▶ if(unlikely(master_fd == -1)) shash_error_errno(sh, "open");
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(master_fd == -1)) shash_error_errno(sh, "open"); 0 84 84
3490 84 ulr = unlinkfile_save(dir, temp_filename);
3491 84 fdr = closefd_save(master_fd);
3492 84 ▶ if(unlikely(ftruncate(master_fd, sh->sizes->mfl_sz) == -1))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(ftruncate(master_fd, sh->sizes->mfl_sz) == -1)) 0 84 84
3493 0 shash_error_errno(sh, "open");
3494 84 mapsv = mmap_as_sv(master_fd, sh->sizes->mfl_sz, 1);
3495 84 ▶ if(!likely(mapsv)) shash_error_errno(sh, "open");
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(mapsv)) shash_error_errno(sh, "open"); 0 84 84
3496 84 sh->u.live.master_mmap_sv = SvREFCNT_inc_simple_NN(mapsv);
3497 84 sh->u.live.master_mmap = map = SvPVX(mapsv);
3498 84 closefd_early(fdr);
3499 84 WORD_AT(map, MFL_MAGIC) = MASTER_FILE_MAGIC;
3500 84 WORD_AT(map, MFL_PARAM) = sh->parameter;
3501 84 ▶ if(unlikely(dirref_rel_link(dir, temp_filename, MASTER_FILENAME)
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(dirref_rel_link(dir, temp_filename, MASTER_FILENAME) 0 84 84
3502 == -1)) {
3503 0 ▶ if(unlikely(errno != EEXIST))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(errno != EEXIST)) 0 0 0
3504 0 shash_error_errno(sh, "open");
3505 0 mmap_early_unmap(mapsv);
3506 0 sh->u.live.master_mmap_sv = NULL;
3507 0 SvREFCNT_dec_NN(mapsv);
3508 0 shash_unlinkfile_early(sh, "open", ulr);
3509 0 master_fd = dirref_rel_open_cloexec(dir, MASTER_FILENAME,
3510 0 ▶ likely(mode & STOREMODE_WRITE) ? O_RDWR : O_RDONLY, 0);
Branch: 1 branch
BranchTrueFalseTotal
likely(mode & STOREMODE_WRITE) ? O_RDWR : O_RDONLY, 0); 0 0 0
3511 0 ▶ if(unlikely(master_fd == -1)) shash_error_errno(sh, "open");
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(master_fd == -1)) shash_error_errno(sh, "open"); 0 0 0
3512 0 goto opened_master;
3513 }
3514 84 shash_unlinkfile_early(sh, "open", ulr);
3515 84 dir_clean(sh, "open", 0);
3516 84 return shsvref;
3517 }
3518  
3519 /*
3520 * API operations in base pp form
3521 *
3522 * These functions take a fixed number of arguments from the Perl stack,
3523 * and put their mortal result on the stack. At the C level they take no
3524 * arguments other than the Perl context and return no value. This is not
3525 * the format used for actual pp_ functions, which implement ops, as those
3526 * interact with PL_op. Nor is it the format used for XS function bodies,
3527 * which take a variable number of arguments delimited by a stack mark.
3528 * These pp1_ functions are the parts of the operations that are common
3529 * to ops and XS functions.
3530 */
3531  
3532 #define pp1_is_shash() THX_pp1_is_shash(aTHX)
3533 243 static void THX_pp1_is_shash(pTHX)
3534 {
3535 243 dSP;
3536 243 ▶ SETs(boolSV(arg_is_shash(TOPs)));
Branch: 1 branch
BranchTrueFalseTotal
SETs(boolSV(arg_is_shash(TOPs))); 207 36 243
3537 243 }
3538  
3539 #define pp1_check_shash() THX_pp1_check_shash(aTHX)
3540 59 static void THX_pp1_check_shash(pTHX)
3541 {
3542 59 dSP;
3543 59 arg_check_shash(POPs);
3544 25 ▶ if(unlikely(GIMME_V == G_SCALAR)) PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(GIMME_V == G_SCALAR)) PUSHs(&PL_sv_undef); 7 18 25
3545 25 PUTBACK;
3546 25 }
3547  
3548 #define pp1_shash_open() THX_pp1_shash_open(aTHX)
3549 754 static void THX_pp1_shash_open(pTHX)
3550 {
3551 SV *sh;
3552 754 dSP;
3553 754 SV *mode_sv = POPs;
3554 754 SV *top_pathname_sv = TOPs;
3555 754 PUTBACK;
3556 754 sh = shash_open(top_pathname_sv, mode_sv);
3557 156 SPAGAIN;
3558 156 SETs(sh);
3559 156 }
3560  
3561 #define pp1_shash_is_readable() THX_pp1_shash_is_readable(aTHX)
3562 77 static void THX_pp1_shash_is_readable(pTHX)
3563 {
3564 77 dSP;
3565 77 ▶ SETs(boolSV(likely(shash_from_svref(TOPs)->mode & STOREMODE_READ)));
Branch: 1 branch
BranchTrueFalseTotal
SETs(boolSV(likely(shash_from_svref(TOPs)->mode & STOREMODE_READ))); 38 22 60
3566 60 }
3567  
3568 #define pp1_shash_is_writable() THX_pp1_shash_is_writable(aTHX)
3569 77 static void THX_pp1_shash_is_writable(pTHX)
3570 {
3571 77 dSP;
3572 77 ▶ SETs(boolSV(likely(shash_from_svref(TOPs)->mode & STOREMODE_WRITE)));
Branch: 1 branch
BranchTrueFalseTotal
SETs(boolSV(likely(shash_from_svref(TOPs)->mode & STOREMODE_WRITE))); 16 44 60
3573 60 }
3574  
3575 #define pp1_shash_mode() THX_pp1_shash_mode(aTHX)
3576 74 static void THX_pp1_shash_mode(pTHX)
3577 {
3578 74 dSP;
3579 74 SETs(mode_as_sv(shash_from_svref(TOPs)->mode));
3580 57 }
3581  
3582 #define pp1_shash_exists() THX_pp1_shash_exists(aTHX)
3583 600422 static void THX_pp1_shash_exists(pTHX)
3584 {
3585 struct shash *sh;
3586 struct pvl keypvl;
3587 struct cursor cur;
3588 SV *resultsv;
3589 600422 dSP;
3590 600422 SV *keysv = POPs;
3591 600422 PUTBACK;
3592 600422 sh = shash_from_svref(TOPs);
3593 600389 keypvl = pvl_from_arg("key", 0, keysv);
3594 600352 shash_check_readable(sh, "read");
3595 600315 ▶ resultsv = boolSV(btree_seek_key(sh, &cur, shash_root_for_read(sh),
Branch: 1 branch
BranchTrueFalseTotal
resultsv = boolSV(btree_seek_key(sh, &cur, shash_root_for_read(sh), 300140 300175 600315
3596 keypvl));
3597 600315 tally_event(&sh->tally, K_DATA_READ_OP);
3598 600315 SPAGAIN;
3599 600315 SETs(resultsv);
3600 600315 }
3601  
3602 #define pp1_shash_length() THX_pp1_shash_length(aTHX)
3603 18001769 static void THX_pp1_shash_length(pTHX)
3604 {
3605 struct shash *sh;
3606 struct pvl keypvl;
3607 struct cursor cur;
3608 SV *resultsv;
3609 18001769 dSP;
3610 18001769 SV *keysv = POPs;
3611 18001769 PUTBACK;
3612 18001769 sh = shash_from_svref(TOPs);
3613 18001752 keypvl = pvl_from_arg("key", 0, keysv);
3614 18001738 shash_check_readable(sh, "read");
3615 18001719 ▶ if(likely(btree_seek_key(sh, &cur, shash_root_for_read(sh), keypvl))) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(btree_seek_key(sh, &cur, shash_root_for_read(sh), keypvl))) { 6092381 11909338 18001719
3616 struct pvl valpvl =
3617 6092381 string_as_pvl(sh, btree_cursor_get(sh, &cur));
3618 if(unlikely((size_t)(UV)valpvl.len != valpvl.len))
3619 shash_error_errnum(sh, "read", ENOMEM);
3620 6092381 TAINT;
3621 6092381 resultsv = sv_2mortal(newSVuv((UV)valpvl.len));
3622 6092381 SvREADONLY_on(resultsv);
3623 } else {
3624 11909338 resultsv = &PL_sv_undef;
3625 }
3626 18001719 tally_event(&sh->tally, K_DATA_READ_OP);
3627 18001719 SPAGAIN;
3628 18001719 SETs(resultsv);
3629 18001719 }
3630  
3631 #define pp1_shash_get() THX_pp1_shash_get(aTHX)
3632 18662113 static void THX_pp1_shash_get(pTHX)
3633 {
3634 struct shash *sh;
3635 struct pvl keypvl;
3636 struct cursor cur;
3637 SV *valsv;
3638 18662113 dSP;
3639 18662113 SV *keysv = POPs;
3640 18662113 PUTBACK;
3641 18662113 sh = shash_from_svref(TOPs);
3642 18662096 keypvl = pvl_from_arg("key", 0, keysv);
3643 18662070 shash_check_readable(sh, "read");
3644 18662015 valsv = btree_seek_key(sh, &cur, shash_root_for_read(sh), keypvl) ?
3645 18662015 ▶ string_as_sv(sh, "read", btree_cursor_get(sh, &cur)) :
Branch: 1 branch
BranchTrueFalseTotal
string_as_sv(sh, "read", btree_cursor_get(sh, &cur)) : 6654244 12007771 18662015
3646 &PL_sv_undef;
3647 18662015 tally_event(&sh->tally, K_DATA_READ_OP);
3648 18662015 SPAGAIN;
3649 18662015 SETs(valsv);
3650 18662015 }
3651  
3652 struct mutateargs_set {
3653 struct pvl keypvl;
3654 struct pvl newvalpvl;
3655 };
3656  
3657 498223 static word THX_mutate_set(pTHX_ struct shash *sh, struct shash_alloc *alloc,
3658 word oldroot, void *mutate_arg)
3659 {
3660 498223 struct mutateargs_set *args = (struct mutateargs_set *)mutate_arg;
3661 struct cursor oldcur;
3662 bool match;
3663 498223 match = btree_seek_key(sh, &oldcur, oldroot, args->keypvl);
3664 498223 return btree_cursor_set(sh, alloc, &oldcur, match,
3665 args->keypvl, args->newvalpvl);
3666 }
3667  
3668 #define pp1_shash_settish(au) THX_pp1_shash_settish(aTHX_ au)
3669 447873 static void THX_pp1_shash_settish(pTHX_ bool allow_undef)
3670 {
3671 SV *keysv, *newvalsv;
3672 struct mutateargs_set args;
3673 struct shash *sh;
3674 447873 dSP;
3675 447873 newvalsv = POPs;
3676 447873 keysv = POPs;
3677 447873 sh = shash_from_svref(POPs);
3678 447856 ▶ if(unlikely(GIMME_V == G_SCALAR)) PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(GIMME_V == G_SCALAR)) PUSHs(&PL_sv_undef); 110 447746 447856
3679 447856 PUTBACK;
3680 447856 args.keypvl = pvl_from_arg("key", 0, keysv);
3681 447831 args.newvalpvl = pvl_from_arg("new value", allow_undef, newvalsv);
3682 447808 shash_check_writable(sh, "write");
3683 447739 shash_mutate(sh, "write", THX_mutate_set, &args);
3684 447739 }
3685  
3686 #define pp1_shash_set() THX_pp1_shash_set(aTHX)
3687 447778 PERL_STATIC_INLINE void THX_pp1_shash_set(pTHX)
3688 {
3689 447778 pp1_shash_settish(1);
3690 447698 }
3691  
3692 #define pp1_shash_tied_store() THX_pp1_shash_tied_store(aTHX)
3693 95 PERL_STATIC_INLINE void THX_pp1_shash_tied_store(pTHX)
3694 {
3695 95 pp1_shash_settish(0);
3696 41 }
3697  
3698 struct mutateargs_gset {
3699 struct pvl keypvl;
3700 struct pvl newvalpvl;
3701 word oldvalptr;
3702 };
3703  
3704 178 static word THX_mutate_gset(pTHX_ struct shash *sh, struct shash_alloc *alloc,
3705 word oldroot, void *mutate_arg)
3706 {
3707 178 struct mutateargs_gset *args = (struct mutateargs_gset *)mutate_arg;
3708 struct cursor oldcur;
3709 bool match;
3710 178 match = btree_seek_key(sh, &oldcur, oldroot, args->keypvl);
3711 178 ▶ args->oldvalptr = match ? btree_cursor_get(sh, &oldcur) : NULL_PTR;
Branch: 1 branch
BranchTrueFalseTotal
args->oldvalptr = match ? btree_cursor_get(sh, &oldcur) : NULL_PTR; 103 75 178
3712 178 return btree_cursor_set(sh, alloc, &oldcur, match,
3713 args->keypvl, args->newvalpvl);
3714 }
3715  
3716 #define pp1_shash_gset() THX_pp1_shash_gset(aTHX)
3717 301 static void THX_pp1_shash_gset(pTHX)
3718 {
3719 SV *keysv, *newvalsv, *oldvalsv;
3720 struct mutateargs_gset args;
3721 struct shash *sh;
3722 301 dSP;
3723 301 newvalsv = POPs;
3724 301 keysv = POPs;
3725 301 PUTBACK;
3726 301 sh = shash_from_svref(TOPs);
3727 284 args.keypvl = pvl_from_arg("key", 0, keysv);
3728 258 args.newvalpvl = pvl_from_arg("new value", 1, newvalsv);
3729 245 shash_check_readable(sh, "update");
3730 206 shash_check_writable(sh, "update");
3731 178 shash_mutate(sh, "update", THX_mutate_gset, &args);
3732 178 ▶ oldvalsv = args.oldvalptr == NULL_PTR ? &PL_sv_undef :
Branch: 1 branch
BranchTrueFalseTotal
oldvalsv = args.oldvalptr == NULL_PTR ? &PL_sv_undef : 103 75 178
3733 103 string_as_sv(sh, "update", args.oldvalptr);
3734 178 SPAGAIN;
3735 178 SETs(oldvalsv);
3736 178 }
3737  
3738 #define pp1_shash_tied_delete() THX_pp1_shash_tied_delete(aTHX)
3739 76 PERL_STATIC_INLINE void THX_pp1_shash_tied_delete(pTHX)
3740 {
3741 76 dSP;
3742 76 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 76 76
3743 76 PUTBACK;
3744 76 pp1_shash_gset();
3745 28 }
3746  
3747 struct mutateargs_cset {
3748 struct pvl keypvl;
3749 struct pvl chkvalpvl;
3750 struct pvl newvalpvl;
3751 bool result;
3752 };
3753  
3754 292939 static word THX_mutate_cset(pTHX_ struct shash *sh, struct shash_alloc *alloc,
3755 word oldroot, void *mutate_arg)
3756 {
3757 292939 struct mutateargs_cset *args = (struct mutateargs_cset *)mutate_arg;
3758 struct cursor oldcur;
3759 bool match;
3760 292939 match = btree_seek_key(sh, &oldcur, oldroot, args->keypvl);
3761 292939 ▶ if(!likely(pvl_is_null(args->chkvalpvl) ? !match :
Branch: 4 branches
BranchTrueFalseTotal
if(!likely(pvl_is_null(args->chkvalpvl) ? !match : 140 292799 292939
if(!likely(pvl_is_null(args->chkvalpvl) ? !match : 292727 72 292799
if(!likely(pvl_is_null(args->chkvalpvl) ? !match : 238593 54134 292727
if(!likely(pvl_is_null(args->chkvalpvl) ? !match : 54283 238656 292939
3762 match && string_eq_pvl(sh,
3763 btree_cursor_get(sh, &oldcur),
3764 args->chkvalpvl))) {
3765 54283 args->result = 0;
3766 54283 return oldroot;
3767 }
3768 238656 args->result = 1;
3769 238656 return btree_cursor_set(sh, alloc, &oldcur, match,
3770 args->keypvl, args->newvalpvl);
3771 }
3772  
3773 #define pp1_shash_cset() THX_pp1_shash_cset(aTHX)
3774 254519 static void THX_pp1_shash_cset(pTHX)
3775 {
3776 SV *keysv, *chkvalsv, *newvalsv;
3777 struct mutateargs_cset args;
3778 struct shash *sh;
3779 254519 dSP;
3780 254519 newvalsv = POPs;
3781 254519 chkvalsv = POPs;
3782 254519 keysv = POPs;
3783 254519 PUTBACK;
3784 254519 sh = shash_from_svref(TOPs);
3785 254502 args.keypvl = pvl_from_arg("key", 0, keysv);
3786 254486 args.chkvalpvl = pvl_from_arg("check value", 1, chkvalsv);
3787 254475 args.newvalpvl = pvl_from_arg("new value", 1, newvalsv);
3788 254459 shash_check_readable(sh, "update");
3789 254440 shash_check_writable(sh, "update");
3790 254428 shash_mutate(sh, "update", THX_mutate_cset, &args);
3791 254428 SPAGAIN;
3792 254428 ▶ SETs(boolSV(likely(args.result)));
Branch: 1 branch
BranchTrueFalseTotal
SETs(boolSV(likely(args.result))); 200145 54283 254428
3793 254428 }
3794  
3795 #define pp1_shash_occupied() THX_pp1_shash_occupied(aTHX)
3796 268 static void THX_pp1_shash_occupied(pTHX)
3797 {
3798 int layer, fanout;
3799 268 dSP;
3800 268 struct shash *sh = shash_from_svref(TOPs);
3801 251 shash_check_readable(sh, "read");
3802 235 (void) bnode_check(sh, shash_root_for_read(sh), -1, &layer, &fanout);
3803 235 tally_event(&sh->tally, K_DATA_READ_OP);
3804 235 SPAGAIN;
3805 235 ▶ SETs(boolSV(fanout != 0));
Branch: 1 branch
BranchTrueFalseTotal
SETs(boolSV(fanout != 0)); 218 17 235
3806 235 }
3807  
3808 #define pp1_shash_count() THX_pp1_shash_count(aTHX)
3809 318 static void THX_pp1_shash_count(pTHX)
3810 {
3811 word count;
3812 SV *resultsv;
3813 318 dSP;
3814 318 struct shash *sh = shash_from_svref(TOPs);
3815 301 shash_check_readable(sh, "read");
3816 267 count = btree_count(sh, shash_root_for_read(sh));
3817 if(unlikely((word)(UV)count != count))
3818 shash_error_errnum(sh, "read", ENOMEM);
3819 267 TAINT;
3820 267 resultsv = sv_2mortal(newSVuv((UV)count));
3821 267 SvREADONLY_on(resultsv);
3822 267 tally_event(&sh->tally, K_DATA_READ_OP);
3823 267 SPAGAIN;
3824 267 SETs(resultsv);
3825 267 }
3826  
3827 #define pp1_shash_size() THX_pp1_shash_size(aTHX)
3828 74 static void THX_pp1_shash_size(pTHX)
3829 {
3830 word size;
3831 SV *resultsv;
3832 74 dSP;
3833 74 struct shash *sh = shash_from_svref(TOPs);
3834 57 shash_check_readable(sh, "read");
3835 41 size = btree_size(sh, shash_root_for_read(sh));
3836 if(unlikely((word)(UV)size != size))
3837 shash_error_errnum(sh, "read", ENOMEM);
3838 41 TAINT;
3839 41 resultsv = sv_2mortal(newSVuv((UV)size));
3840 41 SvREADONLY_on(resultsv);
3841 41 tally_event(&sh->tally, K_DATA_READ_OP);
3842 41 SPAGAIN;
3843 41 SETs(resultsv);
3844 41 }
3845  
3846 #define pp1_shash_key_min() THX_pp1_shash_key_min(aTHX)
3847 5525 static void THX_pp1_shash_key_min(pTHX)
3848 {
3849 int layer, fanout;
3850 word const *ndloc;
3851 SV *keysv;
3852 5525 dSP;
3853 5525 struct shash *sh = shash_from_svref(TOPs);
3854 5508 shash_check_readable(sh, "read");
3855 5380 ndloc = bnode_check(sh, shash_root_for_read(sh), -1, &layer, &fanout);
3856 10760 keysv = likely(fanout != 0) ?
3857 5380 ▶ string_as_sv(sh, "read", bnode_body_loc(ndloc)[0]) :
Branch: 1 branch
BranchTrueFalseTotal
string_as_sv(sh, "read", bnode_body_loc(ndloc)[0]) : 5299 81 5380
3858 &PL_sv_undef;
3859 5380 tally_event(&sh->tally, K_DATA_READ_OP);
3860 5380 SPAGAIN;
3861 5380 SETs(keysv);
3862 5380 }
3863  
3864 #define pp1_shash_key_max() THX_pp1_shash_key_max(aTHX)
3865 5272 static void THX_pp1_shash_key_max(pTHX)
3866 {
3867 struct cursor cur;
3868 SV *keysv;
3869 5272 dSP;
3870 5272 struct shash *sh = shash_from_svref(TOPs);
3871 5255 shash_check_readable(sh, "read");
3872 5239 keysv = likely(btree_seek_max(sh, &cur, shash_root_for_read(sh))) ?
3873 5239 ▶ string_as_sv(sh, "read", btree_cursor_key(sh, &cur)) :
Branch: 1 branch
BranchTrueFalseTotal
string_as_sv(sh, "read", btree_cursor_key(sh, &cur)) : 5222 17 5239
3874 &PL_sv_undef;
3875 5239 tally_event(&sh->tally, K_DATA_READ_OP);
3876 5239 SPAGAIN;
3877 5239 SETs(keysv);
3878 5239 }
3879  
3880 #define pp1_shash_key_ge() THX_pp1_shash_key_ge(aTHX)
3881 12184665 static void THX_pp1_shash_key_ge(pTHX)
3882 {
3883 struct shash *sh;
3884 struct pvl keypvl;
3885 struct cursor cur;
3886 SV *resultsv;
3887 12184665 dSP;
3888 12184665 SV *keysv = POPs;
3889 12184665 PUTBACK;
3890 12184665 sh = shash_from_svref(TOPs);
3891 12184648 keypvl = pvl_from_arg("key", 0, keysv);
3892 12184637 shash_check_readable(sh, "read");
3893 12184621 resultsv = btree_seek_key(sh, &cur, shash_root_for_read(sh), keypvl) ||
3894 6092318 ▶ likely(btree_seek_inc(sh, &cur)) ?
Branch: 1 branch
BranchTrueFalseTotal
likely(btree_seek_inc(sh, &cur)) ? 6092295 23 6092318
3895 18276939 ▶ string_as_sv(sh, "read", btree_cursor_key(sh, &cur)) :
Branch: 1 branch
BranchTrueFalseTotal
string_as_sv(sh, "read", btree_cursor_key(sh, &cur)) : 6092318 6092303 12184621
3896 &PL_sv_undef;
3897 12184621 tally_event(&sh->tally, K_DATA_READ_OP);
3898 12184621 SPAGAIN;
3899 12184621 SETs(resultsv);
3900 12184621 }
3901  
3902 #define pp1_shash_key_gt() THX_pp1_shash_key_gt(aTHX)
3903 12184955 static void THX_pp1_shash_key_gt(pTHX)
3904 {
3905 struct shash *sh;
3906 struct pvl keypvl;
3907 struct cursor cur;
3908 SV *resultsv;
3909 12184955 dSP;
3910 12184955 SV *keysv = POPs;
3911 12184955 PUTBACK;
3912 12184955 sh = shash_from_svref(TOPs);
3913 12184938 keypvl = pvl_from_arg("key", 0, keysv);
3914 12184927 shash_check_readable(sh, "read");
3915 12184911 (void) btree_seek_key(sh, &cur, shash_root_for_read(sh), keypvl);
3916 12184911 resultsv = likely(btree_seek_inc(sh, &cur)) ?
3917 12184911 ▶ string_as_sv(sh, "read", btree_cursor_key(sh, &cur)) :
Branch: 1 branch
BranchTrueFalseTotal
string_as_sv(sh, "read", btree_cursor_key(sh, &cur)) : 12184640 271 12184911
3918 &PL_sv_undef;
3919 12184911 tally_event(&sh->tally, K_DATA_READ_OP);
3920 12184911 SPAGAIN;
3921 12184911 SETs(resultsv);
3922 12184911 }
3923  
3924 #define pp1_shash_key_le() THX_pp1_shash_key_le(aTHX)
3925 12184665 static void THX_pp1_shash_key_le(pTHX)
3926 {
3927 struct shash *sh;
3928 struct pvl keypvl;
3929 struct cursor cur;
3930 SV *resultsv;
3931 12184665 dSP;
3932 12184665 SV *keysv = POPs;
3933 12184665 PUTBACK;
3934 12184665 sh = shash_from_svref(TOPs);
3935 12184648 keypvl = pvl_from_arg("key", 0, keysv);
3936 12184637 shash_check_readable(sh, "read");
3937 12184621 (void) btree_seek_key(sh, &cur, shash_root_for_read(sh), keypvl);
3938 24369242 resultsv = likely(cur.ent[0].index != -1) ?
3939 12184621 ▶ string_as_sv(sh, "read", btree_cursor_key(sh, &cur)) :
Branch: 1 branch
BranchTrueFalseTotal
string_as_sv(sh, "read", btree_cursor_key(sh, &cur)) : 12184418 203 12184621
3940 &PL_sv_undef;
3941 12184621 tally_event(&sh->tally, K_DATA_READ_OP);
3942 12184621 SPAGAIN;
3943 12184621 SETs(resultsv);
3944 12184621 }
3945  
3946 #define pp1_shash_key_lt() THX_pp1_shash_key_lt(aTHX)
3947 12184665 static void THX_pp1_shash_key_lt(pTHX)
3948 {
3949 struct shash *sh;
3950 struct pvl keypvl;
3951 struct cursor cur;
3952 SV *resultsv;
3953 12184665 dSP;
3954 12184665 SV *keysv = POPs;
3955 12184665 PUTBACK;
3956 12184665 sh = shash_from_svref(TOPs);
3957 12184648 keypvl = pvl_from_arg("key", 0, keysv);
3958 12184637 shash_check_readable(sh, "read");
3959 12184621 resultsv = (btree_seek_key(sh, &cur, shash_root_for_read(sh), keypvl) ?
3960 6092303 likely(btree_seek_dec(sh, &cur)) :
3961 18276924 ▶ likely(cur.ent[0].index != -1)) ?
Branch: 1 branch
BranchTrueFalseTotal
likely(cur.ent[0].index != -1)) ? 6092303 6092318 12184621
3962 12184621 ▶ string_as_sv(sh, "read", btree_cursor_key(sh, &cur)) :
Branch: 1 branch
BranchTrueFalseTotal
string_as_sv(sh, "read", btree_cursor_key(sh, &cur)) : 12184230 391 12184621
3963 &PL_sv_undef;
3964 12184621 tally_event(&sh->tally, K_DATA_READ_OP);
3965 12184621 SPAGAIN;
3966 12184621 SETs(resultsv);
3967 12184621 }
3968  
3969 #define pp1_shash_keys_array() THX_pp1_shash_keys_array(aTHX)
3970 278 static void THX_pp1_shash_keys_array(pTHX)
3971 {
3972 word root, count;
3973 AV *ar;
3974 SV *aref;
3975 278 dSP;
3976 278 struct shash *sh = shash_from_svref(TOPs);
3977 261 shash_check_readable(sh, "read");
3978 245 root = shash_root_for_read(sh);
3979 245 count = btree_count(sh, root);
3980 245 ▶ if(unlikely((array_ix_t)count < 0 || (word)(array_ix_t)count != count))
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely((array_ix_t)count < 0 || (word)(array_ix_t)count != count)) 0 245 245
3981 0 shash_error_errnum(sh, "read", ENOMEM);
3982 245 ar = newAV();
3983 245 aref = newRV_ro_mortal_noinc((SV*)ar);
3984 245 ▶ if(likely(count != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(count != 0)) { 224 21 245
3985 SV **abody;
3986 struct cursor cur;
3987 word i;
3988 224 av_fill(ar, count-1);
3989 224 abody = AvARRAY(ar);
3990 224 ▶ if(!likely(btree_seek_min(sh, &cur, root)))
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(btree_seek_min(sh, &cur, root))) 0 224 224
3991 0 shash_error_data(sh);
3992 224 for(i = 0; ; ) {
3993 6097482 abody[i] = SvREFCNT_inc_NN(string_as_sv(sh, "read",
3994 btree_cursor_key(sh, &cur)));
3995 6097482 i++;
3996 6097482 ▶ if(unlikely(i == count)) break;
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(i == count)) break; 224 6097258 6097482
3997 6097258 ▶ if(!likely(btree_seek_inc(sh, &cur)))
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(btree_seek_inc(sh, &cur))) 0 6097258 6097258
3998 0 shash_error_data(sh);
3999 }
4000 }
4001 245 SvREADONLY_on((SV*)ar);
4002 245 tally_event(&sh->tally, K_DATA_READ_OP);
4003 245 SPAGAIN;
4004 245 SETs(aref);
4005 245 }
4006  
4007 #define pp1_shash_keys_hash() THX_pp1_shash_keys_hash(aTHX)
4008 278 static void THX_pp1_shash_keys_hash(pTHX)
4009 {
4010 HV *h;
4011 SV *href;
4012 struct cursor cur;
4013 278 dSP;
4014 278 struct shash *sh = shash_from_svref(TOPs);
4015 261 shash_check_readable(sh, "read");
4016 245 h = newHV();
4017 245 href = newRV_ro_mortal_noinc((SV*)h);
4018 245 ▶ if(likely(btree_seek_min(sh, &cur, shash_root_for_read(sh)))) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(btree_seek_min(sh, &cur, shash_root_for_read(sh)))) { 224 21 245
4019 dMY_CXT;
4020 224 SV *safe_undef = MY_CXT.safe_undef;
4021 do {
4022 6097482 struct pvl pvl = string_as_pvl(sh,
4023 btree_cursor_key(sh, &cur));
4024 6097482 ▶ if(unlikely((I32)pvl.len < 0 ||
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely((I32)pvl.len < 0 || 6097482 0 6097482
if(unlikely((I32)pvl.len < 0 || 0 6097482 6097482
4025 (size_t)(I32)pvl.len != pvl.len))
4026 0 shash_error_errnum(sh, "read", ENOMEM);
4027 6097482 (void) hv_store(h, pvl.pv, (I32)pvl.len,
4028 SvREFCNT_inc_simple_NN(safe_undef), 0);
4029 6097482 ▶ } while(btree_seek_inc(sh, &cur));
Branch: 1 branch
BranchTrueFalseTotal
} while(btree_seek_inc(sh, &cur)); 6097258 224 6097482
4030 }
4031 245 tally_event(&sh->tally, K_DATA_READ_OP);
4032 245 SPAGAIN;
4033 245 SETs(href);
4034 245 }
4035  
4036 #define pp1_shash_group_get_hash() THX_pp1_shash_group_get_hash(aTHX)
4037 279 static void THX_pp1_shash_group_get_hash(pTHX)
4038 {
4039 HV *h;
4040 SV *href;
4041 struct cursor cur;
4042 279 dSP;
4043 279 struct shash *sh = shash_from_svref(TOPs);
4044 262 shash_check_readable(sh, "read");
4045 246 h = newHV();
4046 246 href = newRV_ro_mortal_noinc((SV*)h);
4047 246 ▶ if(likely(btree_seek_min(sh, &cur, shash_root_for_read(sh)))) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(btree_seek_min(sh, &cur, shash_root_for_read(sh)))) { 225 21 246
4048 do {
4049 6097498 struct pvl keypvl = string_as_pvl(sh,
4050 btree_cursor_key(sh, &cur));
4051 SV *valsv;
4052 6097498 ▶ if(unlikely((I32)keypvl.len < 0 ||
Branch: 2 branches
BranchTrueFalseTotal
if(unlikely((I32)keypvl.len < 0 || 6097498 0 6097498
if(unlikely((I32)keypvl.len < 0 || 0 6097498 6097498
4053 (size_t)(I32)keypvl.len != keypvl.len))
4054 0 shash_error_errnum(sh, "read", ENOMEM);
4055 6097498 valsv = string_as_sv(sh, "read",
4056 btree_cursor_get(sh, &cur));
4057 6097498 (void) hv_store(h, keypvl.pv, (I32)keypvl.len,
4058 SvREFCNT_inc_simple_NN(valsv), 0);
4059 6097498 ▶ } while(btree_seek_inc(sh, &cur));
Branch: 1 branch
BranchTrueFalseTotal
} while(btree_seek_inc(sh, &cur)); 6097273 225 6097498
4060 }
4061 246 tally_event(&sh->tally, K_DATA_READ_OP);
4062 246 SPAGAIN;
4063 246 SETs(href);
4064 246 }
4065  
4066 #define pp1_shash_snapshot() THX_pp1_shash_snapshot(aTHX)
4067 49 static void THX_pp1_shash_snapshot(pTHX)
4068 {
4069 SV *snapshsvref;
4070 49 dSP;
4071 49 SV *shsvref = TOPs;
4072 49 struct shash *sh = shash_from_svref(shsvref);
4073 32 ▶ if(unlikely(sh->mode & STOREMODE_SNAPSHOT)) {
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(sh->mode & STOREMODE_SNAPSHOT)) { 5 27 32
4074 5 snapshsvref = newRV_ro_mortal_inc(SvRV(shsvref));
4075 } else {
4076 dMY_CXT;
4077 27 word root = shash_root_for_read(sh);
4078 struct shash *snapsh;
4079 SV *snapshsv;
4080 27 snapshsv = newSV_type(SVt_PVMG);
4081 27 snapshsvref = newRV_ro_mortal_noinc(snapshsv);
4082 27 Newxz(snapsh, 1, struct shash);
4083 27 SvPV_set(snapshsv, (char *)snapsh);
4084 27 SvLEN_set(snapshsv, sizeof(struct shash));
4085 27 shash_apply_magic(snapshsv);
4086 27 (void) sv_bless(snapshsvref, MY_CXT.shash_handle_stash);
4087 #if QWITH_DUP
4088 snapsh->sizes_sv = sh->sizes_sv;
4089 #endif /* QWITH_DUP */
4090 27 snapsh->sizes = sh->sizes;
4091 27 snapsh->parameter = sh->parameter;
4092 27 snapsh->top_pathname_sv = SvREFCNT_inc_NN(sh->top_pathname_sv);
4093 27 snapsh->mode =
4094 27 (sh->mode & ~STOREMODE_WRITE) | STOREMODE_SNAPSHOT;
4095 27 snapsh->data_mmap_sv = SvREFCNT_inc_NN(sh->data_mmap_sv);
4096 27 snapsh->data_mmap = sh->data_mmap;
4097 27 snapsh->data_size = sh->data_size;
4098 27 snapsh->u.snapshot.root = root;
4099 }
4100 32 SETs(snapshsvref);
4101 32 }
4102  
4103 #define pp1_shash_is_snapshot() THX_pp1_shash_is_snapshot(aTHX)
4104 81 static void THX_pp1_shash_is_snapshot(pTHX)
4105 {
4106 81 dSP;
4107 81 ▶ SETs(boolSV(shash_from_svref(TOPs)->mode & STOREMODE_SNAPSHOT));
Branch: 1 branch
BranchTrueFalseTotal
SETs(boolSV(shash_from_svref(TOPs)->mode & STOREMODE_SNAPSHOT)); 31 33 64
4108 64 }
4109  
4110 #define pp1_shash_idle() THX_pp1_shash_idle(aTHX)
4111 63 static void THX_pp1_shash_idle(pTHX)
4112 {
4113 63 dSP;
4114 63 struct shash *sh = shash_from_svref(POPs);
4115 46 ▶ if(unlikely(GIMME_V == G_SCALAR)) PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(GIMME_V == G_SCALAR)) PUSHs(&PL_sv_undef); 4 42 46
4116 46 PUTBACK;
4117 46 ▶ if(!unlikely(sh->mode & STOREMODE_SNAPSHOT)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!unlikely(sh->mode & STOREMODE_SNAPSHOT)) { 29 17 46
4118 29 SV *mapsv = sh->data_mmap_sv;
4119 29 ▶ if(likely(mapsv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(likely(mapsv)) { 18 11 29
4120 18 sh->data_mmap_sv = NULL;
4121 18 SvREFCNT_dec_NN(mapsv);
4122 }
4123 }
4124 46 }
4125  
4126 #define pp1_shash_tidy() THX_pp1_shash_tidy(aTHX)
4127 1078 static void THX_pp1_shash_tidy(pTHX)
4128 {
4129 int tries;
4130 1078 dSP;
4131 1078 struct shash *sh = shash_from_svref(POPs);
4132 1061 ▶ if(unlikely(GIMME_V == G_SCALAR)) PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(GIMME_V == G_SCALAR)) PUSHs(&PL_sv_undef); 5 1056 1061
4133 1061 PUTBACK;
4134 1061 shash_check_writable(sh, "tidy");
4135 1035 ▶ for(tries = 3; tries--; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(tries = 3; tries--; ) { 1035 0 1035
4136 1035 shash_ensure_data_file(sh);
4137 1035 ▶ if(!likely(sh->u.live.data_file_id)) break;
Branch: 1 branch
BranchTrueFalseTotal
if(!likely(sh->u.live.data_file_id)) break; 1 1034 1035
4138 1034 ▶ if(likely(word_get(&WORD_AT(sh->data_mmap,
Branch: 1 branch
BranchTrueFalseTotal
if(likely(word_get(&WORD_AT(sh->data_mmap, 1033 1 1034
4139 sh->sizes->dhd_nextalloc_space)) <
4140 (sh->data_size >> 1)))
4141 1033 break;
4142 1 ▶ if(likely(shash_try_rollover(sh, "tidy", 1<<20) != NULL_PTR))
Branch: 1 branch
BranchTrueFalseTotal
if(likely(shash_try_rollover(sh, "tidy", 1<<20) != NULL_PTR)) 1 0 1
4143 1 break;
4144 }
4145 1035 dir_clean(sh, "tidy", sh->u.live.data_file_id);
4146 1035 }
4147  
4148 #define pp1_shash_tally_get() THX_pp1_shash_tally_get(aTHX)
4149 73 static void THX_pp1_shash_tally_get(pTHX)
4150 {
4151 73 dSP;
4152 73 struct shash *sh = shash_from_svref(TOPs);
4153 PERL_UNUSED_VAR(sh);
4154 56 SETs(tally_as_hvref(&sh->tally));
4155 56 }
4156  
4157 #define pp1_shash_tally_zero() THX_pp1_shash_tally_zero(aTHX)
4158 62 static void THX_pp1_shash_tally_zero(pTHX)
4159 {
4160 62 dSP;
4161 62 struct shash *sh = shash_from_svref(POPs);
4162 45 ▶ if(unlikely(GIMME_V == G_SCALAR)) PUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
if(unlikely(GIMME_V == G_SCALAR)) PUSHs(&PL_sv_undef); 37 8 45
4163 45 PUTBACK;
4164 PERL_UNUSED_VAR(sh);
4165 45 tally_zero(&sh->tally);
4166 45 }
4167  
4168 #define pp1_shash_tally_gzero() THX_pp1_shash_tally_gzero(aTHX)
4169 63 static void THX_pp1_shash_tally_gzero(pTHX)
4170 {
4171 63 dSP;
4172 63 struct shash *sh = shash_from_svref(TOPs);
4173 PERL_UNUSED_VAR(sh);
4174 46 SETs(tally_as_hvref(&sh->tally));
4175 46 tally_zero(&sh->tally);
4176 46 }
4177  
4178 /* API operations in pp form for ops */
4179  
4180 #ifdef cv_set_call_checker
4181  
4182 # define HSM_MAKE_PP(name) \
4183 static OP *THX_pp_##name(pTHX) \
4184 { \
4185 pp1_##name(); \
4186 return NORMAL; \
4187 }
4188  
4189 234 HSM_MAKE_PP(is_shash)
4190 49 HSM_MAKE_PP(check_shash)
4191 749 HSM_MAKE_PP(shash_open)
4192 65 HSM_MAKE_PP(shash_is_readable)
4193 65 HSM_MAKE_PP(shash_is_writable)
4194 60 HSM_MAKE_PP(shash_mode)
4195 600244 HSM_MAKE_PP(shash_exists)
4196 18001699 HSM_MAKE_PP(shash_length)
4197 18661686 HSM_MAKE_PP(shash_get)
4198 447764 HSM_MAKE_PP(shash_set)
4199 193 HSM_MAKE_PP(shash_gset)
4200 254439 HSM_MAKE_PP(shash_cset)
4201 250 HSM_MAKE_PP(shash_occupied)
4202 256 HSM_MAKE_PP(shash_count)
4203 62 HSM_MAKE_PP(shash_size)
4204 5259 HSM_MAKE_PP(shash_key_min)
4205 5254 HSM_MAKE_PP(shash_key_max)
4206 12184647 HSM_MAKE_PP(shash_key_ge)
4207 12184647 HSM_MAKE_PP(shash_key_gt)
4208 12184647 HSM_MAKE_PP(shash_key_le)
4209 12184647 HSM_MAKE_PP(shash_key_lt)
4210 256 HSM_MAKE_PP(shash_keys_array)
4211 256 HSM_MAKE_PP(shash_keys_hash)
4212 257 HSM_MAKE_PP(shash_group_get_hash)
4213 43 HSM_MAKE_PP(shash_snapshot)
4214 69 HSM_MAKE_PP(shash_is_snapshot)
4215 57 HSM_MAKE_PP(shash_idle)
4216 1072 HSM_MAKE_PP(shash_tidy)
4217 67 HSM_MAKE_PP(shash_tally_get)
4218 56 HSM_MAKE_PP(shash_tally_zero)
4219 57 HSM_MAKE_PP(shash_tally_gzero)
4220  
4221 #endif /* cv_set_call_checker */
4222  
4223 /* API operations as XS function bodies */
4224  
4225 #ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
4226 static void S_croak_xs_usage(const CV *, const char *);
4227 # define croak_xs_usage(cv, params) S_croak_xs_usage(cv, params)
4228 #endif /* !PERL_ARGS_ASSERT_CROAK_XS_USAGE */
4229  
4230 #define HSM_MAKE_XSFUNC(name, arity, argnames) \
4231 static void THX_xsfunc_##name(pTHX_ CV *cv) \
4232 { \
4233 dMARK; dSP; \
4234 if(unlikely(SP - MARK != arity)) croak_xs_usage(cv, argnames); \
4235 pp1_##name(); \
4236 }
4237  
4238 9 ▶HSM_MAKE_XSFUNC(is_shash, 1, "arg")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(is_shash, 1, "arg") 0 9 9
4239 10 ▶HSM_MAKE_XSFUNC(check_shash, 1, "arg")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(check_shash, 1, "arg") 0 10 10
4240 5 ▶HSM_MAKE_XSFUNC(shash_open, 2, "filename, mode")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_open, 2, "filename, mode") 0 5 5
4241 12 ▶HSM_MAKE_XSFUNC(shash_is_readable, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_is_readable, 1, "shash") 0 12 12
4242 12 ▶HSM_MAKE_XSFUNC(shash_is_writable, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_is_writable, 1, "shash") 0 12 12
4243 14 ▶HSM_MAKE_XSFUNC(shash_mode, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_mode, 1, "shash") 0 14 14
4244 178 ▶HSM_MAKE_XSFUNC(shash_exists, 2, "shash, key")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_exists, 2, "shash, key") 0 178 178
4245 70 ▶HSM_MAKE_XSFUNC(shash_length, 2, "shash, key")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_length, 2, "shash, key") 0 70 70
4246 427 ▶HSM_MAKE_XSFUNC(shash_get, 2, "shash, key")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_get, 2, "shash, key") 0 427 427
4247 14 ▶HSM_MAKE_XSFUNC(shash_set, 3, "shash, key, newvalue")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_set, 3, "shash, key, newvalue") 0 14 14
4248 95 ▶HSM_MAKE_XSFUNC(shash_tied_store, 3, "shash, key, newvalue")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_tied_store, 3, "shash, key, newvalue") 0 95 95
4249 32 ▶HSM_MAKE_XSFUNC(shash_gset, 3, "shash, key, newvalue")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_gset, 3, "shash, key, newvalue") 0 32 32
4250 76 ▶HSM_MAKE_XSFUNC(shash_tied_delete, 2, "shash, key")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_tied_delete, 2, "shash, key") 0 76 76
4251 80 ▶HSM_MAKE_XSFUNC(shash_cset, 4, "shash, key, chkvalue, newvalue")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_cset, 4, "shash, key, chkvalue, newvalue") 0 80 80
4252 18 ▶HSM_MAKE_XSFUNC(shash_occupied, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_occupied, 1, "shash") 0 18 18
4253 62 ▶HSM_MAKE_XSFUNC(shash_count, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_count, 1, "shash") 0 62 62
4254 12 ▶HSM_MAKE_XSFUNC(shash_size, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_size, 1, "shash") 0 12 12
4255 266 ▶HSM_MAKE_XSFUNC(shash_key_min, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_key_min, 1, "shash") 0 266 266
4256 18 ▶HSM_MAKE_XSFUNC(shash_key_max, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_key_max, 1, "shash") 0 18 18
4257 18 ▶HSM_MAKE_XSFUNC(shash_key_ge, 2, "shash, key")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_key_ge, 2, "shash, key") 0 18 18
4258 308 ▶HSM_MAKE_XSFUNC(shash_key_gt, 2, "shash, key")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_key_gt, 2, "shash, key") 0 308 308
4259 18 ▶HSM_MAKE_XSFUNC(shash_key_le, 2, "shash, key")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_key_le, 2, "shash, key") 0 18 18
4260 18 ▶HSM_MAKE_XSFUNC(shash_key_lt, 2, "shash, key")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_key_lt, 2, "shash, key") 0 18 18
4261 22 ▶HSM_MAKE_XSFUNC(shash_keys_array, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_keys_array, 1, "shash") 0 22 22
4262 22 ▶HSM_MAKE_XSFUNC(shash_keys_hash, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_keys_hash, 1, "shash") 0 22 22
4263 22 ▶HSM_MAKE_XSFUNC(shash_group_get_hash, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_group_get_hash, 1, "shash") 0 22 22
4264 6 ▶HSM_MAKE_XSFUNC(shash_snapshot, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_snapshot, 1, "shash") 0 6 6
4265 12 ▶HSM_MAKE_XSFUNC(shash_is_snapshot, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_is_snapshot, 1, "shash") 0 12 12
4266 6 ▶HSM_MAKE_XSFUNC(shash_idle, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_idle, 1, "shash") 0 6 6
4267 6 ▶HSM_MAKE_XSFUNC(shash_tidy, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_tidy, 1, "shash") 0 6 6
4268 6 ▶HSM_MAKE_XSFUNC(shash_tally_get, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_tally_get, 1, "shash") 0 6 6
4269 6 ▶HSM_MAKE_XSFUNC(shash_tally_zero, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_tally_zero, 1, "shash") 0 6 6
4270 6 ▶HSM_MAKE_XSFUNC(shash_tally_gzero, 1, "shash")
Branch: 1 branch
BranchTrueFalseTotal
HSM_MAKE_XSFUNC(shash_tally_gzero, 1, "shash") 0 6 6
4271  
4272 #ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
4273 # undef croak_xs_usage
4274 #endif /* !PERL_ARGS_ASSERT_CROAK_XS_USAGE */
4275  
4276 /* checker to turn function calls into custom ops */
4277  
4278 #ifdef cv_set_call_checker
4279 2464 static OP *THX_ck_entersub_args_hsm(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
4280 {
4281 2464 CV *cv = (CV*)ckobj;
4282 OP *pushop, *firstargop, *cvop, *lastargop, *argop, *newop;
4283 int nargs;
4284 2464 entersubop = ck_entersub_args_proto(entersubop, namegv, (SV*)cv);
4285 2464 pushop = cUNOPx(entersubop)->op_first;
4286 2464 ▶ if(!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if(!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 2464 0 2464
4287 2464 ▶ firstargop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
firstargop = OpSIBLING(pushop); 2464 0 2464
4288 7783 ▶ for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
Branch: 2 branches
BranchTrueFalseTotal
for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; 5319 0 5319
for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; 5319 2464 7783
4289 7783 ▶ for (nargs = 0, lastargop = pushop, argop = firstargop; argop != cvop;
Branch: 1 branch
BranchTrueFalseTotal
for (nargs = 0, lastargop = pushop, argop = firstargop; argop != cvop; 5319 2464 7783
4290 5319 lastargop = argop, argop = OpSIBLING(argop))
4291 5319 ▶ nargs++;
Branch: 1 branch
BranchTrueFalseTotal
nargs++; 5319 0 5319
4292 2464 ▶ if(unlikely(nargs != (int)CvPROTOLEN(cv))) return entersubop;
Branch: 4 branches
BranchTrueFalseTotal
if(unlikely(nargs != (int)CvPROTOLEN(cv))) return entersubop; 2464 0 2464
if(unlikely(nargs != (int)CvPROTOLEN(cv))) return entersubop; 2464 0 2464
if(unlikely(nargs != (int)CvPROTOLEN(cv))) return entersubop; 0 2464 2464
if(unlikely(nargs != (int)CvPROTOLEN(cv))) return entersubop; 0 2464 2464
4293 2464 OpMORESIB_set(pushop, cvop);
4294 2464 OpLASTSIB_set(lastargop, NULL);
4295 2464 op_free(entersubop);
4296 2464 newop = newUNOP(OP_NULL, 0, lastargop);
4297 2464 cUNOPx(newop)->op_first = firstargop;
4298 # ifdef XopENTRY_set
4299 2464 newop->op_type = OP_CUSTOM;
4300 # else /* !XopENTRY_set */
4301 newop->op_type = OP_DOFILE;
4302 # endif /* !XopENTRY_set */
4303 2464 newop->op_ppaddr = DPTR2FPTR(Perl_ppaddr_t, CvXSUBANY(cv).any_ptr);
4304 2464 return newop;
4305 }
4306 #endif /* cv_set_call_checker */
4307  
4308 MODULE = Hash::SharedMem PACKAGE = Hash::SharedMem
4309  
4310 PROTOTYPES: DISABLE
4311  
4312 BOOT:
4313 {
4314 31 dirref_ensure_strategy();
4315 31 ▶ (void) newCONSTSUB(NULL, "Hash::SharedMem::shash_referential_handle",
Branch: 1 branch
BranchTrueFalseTotal
(void) newCONSTSUB(NULL, "Hash::SharedMem::shash_referential_handle", 31 0 31
4316 boolSV(likely(dirref_referential())));
4317 }
4318  
4319 BOOT:
4320 {
4321 MY_CXT_INIT;
4322 31 MY_CXT.safe_undef = newSV(0);
4323 31 SvREADONLY_on(MY_CXT.safe_undef);
4324 31 MY_CXT.sizes_table = newHV();
4325 31 MY_CXT.shash_handle_stash = gv_stashpvs("Hash::SharedMem::Handle", 1);
4326 31 tally_boot();
4327 }
4328  
4329 #if QWITH_DUP
4330  
4331 void
4332 CLONE(...)
4333 CODE:
4334 PERL_UNUSED_VAR(items);
4335 {
4336 MY_CXT_CLONE;
4337 MY_CXT.safe_undef = newSV(0);
4338 SvREADONLY_on(MY_CXT.safe_undef);
4339 MY_CXT.sizes_table = newHV();
4340 MY_CXT.shash_handle_stash =
4341 gv_stashpvs("Hash::SharedMem::Handle", 1);
4342 tally_boot();
4343 }
4344  
4345 #endif /* QWITH_DUP */
4346  
4347 BOOT:
4348 {
4349 #ifdef cv_set_call_checker
4350 # define PPFUNC_(name) THX_pp_##name,
4351 #else /* !cv_set_call_checker */
4352 # define PPFUNC_(name) /**/
4353 #endif /* !cv_set_call_checker */
4354 #define HSM_FUNC_TO_INSTALL(name, arity) \
4355 { \
4356 "Hash::SharedMem::"#name, \
4357 PPFUNC_(name) \
4358 THX_xsfunc_##name, \
4359 (arity), \
4360 }
4361 struct {
4362 char const *fqsubname;
4363 #ifdef cv_set_call_checker
4364 Perl_ppaddr_t THX_pp;
4365 #endif /* cv_set_call_checker */
4366 XSUBADDR_t THX_xsfunc;
4367 int arity;
4368 31 } const funcs_to_install[] = {
4369 HSM_FUNC_TO_INSTALL(is_shash, 1),
4370 HSM_FUNC_TO_INSTALL(check_shash, 1),
4371 HSM_FUNC_TO_INSTALL(shash_open, 2),
4372 HSM_FUNC_TO_INSTALL(shash_is_readable, 1),
4373 HSM_FUNC_TO_INSTALL(shash_is_writable, 1),
4374 HSM_FUNC_TO_INSTALL(shash_mode, 1),
4375 HSM_FUNC_TO_INSTALL(shash_exists, 2),
4376 HSM_FUNC_TO_INSTALL(shash_length, 2),
4377 HSM_FUNC_TO_INSTALL(shash_get, 2),
4378 HSM_FUNC_TO_INSTALL(shash_set, 3),
4379 HSM_FUNC_TO_INSTALL(shash_gset, 3),
4380 HSM_FUNC_TO_INSTALL(shash_cset, 4),
4381 HSM_FUNC_TO_INSTALL(shash_occupied, 1),
4382 HSM_FUNC_TO_INSTALL(shash_count, 1),
4383 HSM_FUNC_TO_INSTALL(shash_size, 1),
4384 HSM_FUNC_TO_INSTALL(shash_key_min, 1),
4385 HSM_FUNC_TO_INSTALL(shash_key_max, 1),
4386 HSM_FUNC_TO_INSTALL(shash_key_ge, 2),
4387 HSM_FUNC_TO_INSTALL(shash_key_gt, 2),
4388 HSM_FUNC_TO_INSTALL(shash_key_le, 2),
4389 HSM_FUNC_TO_INSTALL(shash_key_lt, 2),
4390 HSM_FUNC_TO_INSTALL(shash_keys_array, 1),
4391 HSM_FUNC_TO_INSTALL(shash_keys_hash, 1),
4392 HSM_FUNC_TO_INSTALL(shash_group_get_hash, 1),
4393 HSM_FUNC_TO_INSTALL(shash_snapshot, 1),
4394 HSM_FUNC_TO_INSTALL(shash_is_snapshot, 1),
4395 HSM_FUNC_TO_INSTALL(shash_idle, 1),
4396 HSM_FUNC_TO_INSTALL(shash_tidy, 1),
4397 HSM_FUNC_TO_INSTALL(shash_tally_get, 1),
4398 HSM_FUNC_TO_INSTALL(shash_tally_zero, 1),
4399 HSM_FUNC_TO_INSTALL(shash_tally_gzero, 1),
4400 }, *fti;
4401 int i;
4402 992 ▶ for(i = C_ARRAY_LENGTH(funcs_to_install); i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = C_ARRAY_LENGTH(funcs_to_install); i--; ) { 961 31 992
4403 CV *fcv;
4404 #if defined(cv_set_call_checker) && defined(XopENTRY_set)
4405 XOP *xop;
4406 char const *shortname;
4407 #endif /* cv_set_call_checker && XopENTRY_set */
4408 961 fti = &funcs_to_install[i];
4409 961 fcv = newXSproto_portable((char*)fti->fqsubname,
4410 fti->THX_xsfunc, __FILE__, "$$$$"+4-fti->arity);
4411 #ifdef cv_set_call_checker
4412 # ifdef XopENTRY_set
4413 961 Newxz(xop, 1, XOP);
4414 961 shortname = fti->fqsubname + sizeof("Hash::SharedMem::")-1;
4415 961 XopENTRY_set(xop, xop_name, shortname);
4416 961 XopENTRY_set(xop, xop_desc, shortname);
4417 961 XopENTRY_set(xop, xop_class, OA_UNOP);
4418 961 Perl_custom_op_register(aTHX_ fti->THX_pp, xop);
4419 # endif /* XopENTRY_set */
4420 961 CvXSUBANY(fcv).any_ptr = FPTR2DPTR(void*, fti->THX_pp);
4421 961 cv_set_call_checker(fcv, THX_ck_entersub_args_hsm, (SV*)fcv);
4422 #else /* !cv_set_call_checker */
4423 PERL_UNUSED_VAR(fcv);
4424 #endif /* !cv_set_call_checker */
4425 }
4426 }
4427  
4428 BOOT:
4429 {
4430 31 HV *fstash = gv_stashpvs("Hash::SharedMem", 0);
4431 31 (void) hv_stores(fstash, "shash_getd",
4432 SvREFCNT_inc_NN(*hv_fetchs(fstash, "shash_exists", 0)));
4433 }
4434  
4435 MODULE = Hash::SharedMem PACKAGE = Hash::SharedMem::Handle
4436  
4437 PROTOTYPES: DISABLE
4438  
4439 SV *
4440 referential_handle(SV *classname)
4441 CODE:
4442 PERL_UNUSED_VAR(classname);
4443 2 ▶ RETVAL = boolSV(likely(dirref_referential()));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = boolSV(likely(dirref_referential())); 2 0 2
4444 OUTPUT:
4445 RETVAL
4446  
4447 SV *
4448 open(SV *classname, SV *filename, SV *mode)
4449 CODE:
4450 PERL_UNUSED_VAR(classname);
4451 36 PUTBACK;
4452 36 RETVAL = shash_open(filename, mode);
4453 16 SvREFCNT_inc_simple_void_NN(RETVAL);
4454 16 SPAGAIN;
4455 OUTPUT:
4456 RETVAL
4457  
4458 BOOT:
4459 {
4460 31 HV *fstash = gv_stashpvs("Hash::SharedMem", 0);
4461 31 HV *mstash = gv_stashpvs("Hash::SharedMem::Handle", 0);
4462 HE *he;
4463 1333 ▶ for(hv_iterinit(fstash); (he = hv_iternext(fstash)); ) {
Branch: 1 branch
BranchTrueFalseTotal
for(hv_iterinit(fstash); (he = hv_iternext(fstash)); ) { 1302 31 1333
4464 STRLEN klen;
4465 1302 ▶ char const *kpv = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
char const *kpv = HePV(he, klen); 0 1302 1302
4466 1302 ▶ if(klen > 6 && memcmp(kpv, "shash_", 6) == 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if(klen > 6 && memcmp(kpv, "shash_", 6) == 0 && 1240 62 1302
if(klen > 6 && memcmp(kpv, "shash_", 6) == 0 && 961 279 1240
4467 961 ▶ !(klen == 24 &&
Branch: 1 branch
BranchTrueFalseTotal
!(klen == 24 && 31 930 961
4468 31 ▶ memcmp(kpv+6, "referential_handle", 18)
Branch: 1 branch
BranchTrueFalseTotal
memcmp(kpv+6, "referential_handle", 18) 0 31 31
4469 930 == 0) &&
4470 930 ▶ !(klen == 10 && memcmp(kpv+6, "open", 4) == 0))
Branch: 2 branches
BranchTrueFalseTotal
!(klen == 10 && memcmp(kpv+6, "open", 4) == 0)) 248 682 930
!(klen == 10 && memcmp(kpv+6, "open", 4) == 0)) 217 31 248
4471 899 (void) hv_store(mstash, kpv+6, klen-6,
4472 SvREFCNT_inc_NN(HeVAL(he)), 0);
4473 }
4474 }
4475  
4476 SV *
4477 TIEHASH(SV *classname, SV *arg0, SV *arg1 = NULL)
4478 CODE:
4479 PERL_UNUSED_VAR(classname);
4480 74 ▶ if(!arg1) {
Branch: 1 branch
BranchTrueFalseTotal
if(!arg1) { 39 35 74
4481 39 arg_check_shash(arg0);
4482 37 RETVAL = newRV_ro_mortal_inc(SvRV(arg0));
4483 } else {
4484 35 PUTBACK;
4485 35 RETVAL = shash_open(arg0, arg1);
4486 15 SPAGAIN;
4487 }
4488 52 SvREFCNT_inc_simple_void_NN(RETVAL);
4489 OUTPUT:
4490 RETVAL
4491  
4492 void
4493 CLEAR(SV *shash)
4494 PPCODE:
4495 2 arg_check_shash(shash);
4496 2 croak("can't clear shared hash");
4497  
4498 BOOT:
4499 {
4500 31 HV *mstash = gv_stashpvs("Hash::SharedMem::Handle", 0);
4501 31 (void) hv_stores(mstash, "EXISTS",
4502 SvREFCNT_inc_NN(*hv_fetchs(mstash, "exists", 0)));
4503 31 (void) hv_stores(mstash, "FETCH",
4504 SvREFCNT_inc_NN(*hv_fetchs(mstash, "get", 0)));
4505 31 (void) newXSproto_portable("Hash::SharedMem::Handle::STORE",
4506 THX_xsfunc_shash_tied_store, __FILE__, "$$$");
4507 31 (void) newXSproto_portable("Hash::SharedMem::Handle::DELETE",
4508 THX_xsfunc_shash_tied_delete, __FILE__, "$$");
4509 #if PERL_VERSION_GE(5,25,3)
4510 31 (void) hv_stores(mstash, "SCALAR",
4511 SvREFCNT_inc_NN(*hv_fetchs(mstash, "count", 0)));
4512 #else /* <5.25.3 */
4513 (void) hv_stores(mstash, "SCALAR",
4514 SvREFCNT_inc_NN(*hv_fetchs(mstash, "occupied", 0)));
4515 #endif /* <5.25.3 */
4516 31 (void) hv_stores(mstash, "FIRSTKEY",
4517 SvREFCNT_inc_NN(*hv_fetchs(mstash, "key_min", 0)));
4518 31 (void) hv_stores(mstash, "NEXTKEY",
4519 SvREFCNT_inc_NN(*hv_fetchs(mstash, "key_gt", 0)));
4520 }
« blib/lib/Hash/SharedMem.pm ↑ Summary blib/lib/Hash/SharedMem/Handle.pm »