include/hamt/hamt_sv.h

filter: Statementstmt 100% 18 / 18 Branchbran 84.3% 27 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.0% 45 / 50 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_mut.h ↑ Summary include/hamt/hamt_xs.h »
1 #ifndef HAMT_SV_H
2 #define HAMT_SV_H
3  
4 /* The core's three value macros over an SV, and the seed. This is the one
5 * header between perl and the core: it comes after perl.h and before every
6 * other hamt/ header.
7 *
8 * `cx` is the interpreter on a perl that has one to pass and NULL on a perl
9 * that does not, so a release reaches the right interpreter without a dTHX
10 * lookup for every value. */
11  
12 #ifdef PERL_IMPLICIT_CONTEXT
13 # define HAMT_CX ((void *)aTHX)
14 #else
15 # define HAMT_CX NULL
16 #endif
17  
18 /* Not during global destruction. By then perl is freeing every scalar in an
19 * order of its own, and one this library still holds may already be gone;
20 * every object has had its DESTROY, so nothing is lost but memory the process
21 * is about to give back anyway. */
22 5805463 static void hamt_sv_release_cx(void *cx, SV *sv) {
23 dTHXa(cx);
24 PERL_UNUSED_ARG(cx);
25 5805463 ▶ if (PL_dirty) return;
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) return; 3 5805460 5805463
26 5805460 SvREFCNT_dec(sv);
27 }
28  
29 /* Equal as `equals` means it by default: the same scalar; or two references
30 * to the same thing; or two non-references that are the same string. Two
31 * undefs are equal and an undef equals nothing else. */
32 201088 static int hamt_sv_eq_cx(void *cx, SV *a, SV *b) {
33 dTHXa(cx);
34 PERL_UNUSED_ARG(cx);
35 201088 ▶ if (a == b) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 1; 14 201074 201088
36 201074 ▶ if (SvROK(a) || SvROK(b))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(a) || SvROK(b)) 200297 777 201074
if (SvROK(a) || SvROK(b)) 1 200296 200297
37 778 ▶ return SvROK(a) && SvROK(b) && SvRV(a) == SvRV(b);
Branch: 3 branches
BranchTrueFalseTotal
return SvROK(a) && SvROK(b) && SvRV(a) == SvRV(b); 777 1 778
return SvROK(a) && SvROK(b) && SvRV(a) == SvRV(b); 774 3 777
return SvROK(a) && SvROK(b) && SvRV(a) == SvRV(b); 105 669 774
38 200296 ▶ if (!SvOK(a) || !SvOK(b))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(a) || !SvOK(b)) 200269 27 200296
if (!SvOK(a) || !SvOK(b)) 4 200265 200269
39 31 ▶ return !SvOK(a) && !SvOK(b);
Branch: 2 branches
BranchTrueFalseTotal
return !SvOK(a) && !SvOK(b); 27 4 31
return !SvOK(a) && !SvOK(b); 20 7 27
40 200265 return sv_eq(a, b) ? 1 : 0;
41 }
42  
43 #define HAMT_VAL_RETAIN(cx, v) SvREFCNT_inc_simple_void_NN((SV *)(v))
44 #define HAMT_VAL_RELEASE(cx, v) hamt_sv_release_cx((cx), (SV *)(v))
45 #define HAMT_VAL_EQ(cx, a, b) hamt_sv_eq_cx((cx), (SV *)(a), (SV *)(b))
46 #define HAMT_FATAL(msg) Perl_croak_nocontext("HAMT: %s", (msg))
47  
48 /* Every block the core holds, counted. A set has no values whose
49 * destruction could show that a trie was freed, so this is what lets a test
50 * say "nothing was leaked" about one; it costs an increment. It is one
51 * number for the process and nothing locks it, so it means something only
52 * while one thread is using the library, which is when the tests read it.
53 *
54 * malloc and free here are the C library's: hamt_compat.h, which follows,
55 * takes back the names XSUB.h redefines. */
56 static long hamt_live_blocks = 0;
57  
58 /* One pool, used by the interpreter that loaded the library and by no
59 * other: a pool is one thread's, and a second interpreter is a second
60 * thread. Any other interpreter allocates and frees directly. Both make
61 * blocks of the same sizes, so it does not matter which of them frees a
62 * block the other allocated.
63 *
64 * The pool is not perl's memory, so nothing perl frees on its way out can
65 * leave it dangling: it is closed when the interpreter exits and whatever is
66 * freed after that goes straight back to the C library.
67 *
68 * HAMT_NO_POOL at compile time takes it out, for a build that a memory
69 * checker is going to read. */
70 #include "hamt/hamt_compat.h"
71 #include "hamt/hamt_pool.h"
72  
73 #ifndef HAMT_NO_POOL
74 static hamt_pool hamt_pool_one;
75 static void *hamt_pool_owner = NULL;
76 # define HAMT_POOL_FOR(cx) \
77 ((hamt_pool_one.open && (cx) == hamt_pool_owner) ? &hamt_pool_one : NULL)
78 #else
79 # define HAMT_POOL_FOR(cx) ((hamt_pool *)NULL)
80 #endif
81  
82 3191957 static void *hamt_xs_alloc(void *cx, size_t bytes) {
83 3191957 ▶ void *p = hamt_pool_alloc(HAMT_POOL_FOR(cx), bytes);
Branch: 2 branches
BranchTrueFalseTotal
void *p = hamt_pool_alloc(HAMT_POOL_FOR(cx), bytes); 3191957 0 3191957
void *p = hamt_pool_alloc(HAMT_POOL_FOR(cx), bytes); 3191957 0 3191957
84 PERL_UNUSED_ARG(cx);
85 3191957 ▶ if (p) hamt_live_blocks++;
Branch: 1 branch
BranchTrueFalseTotal
if (p) hamt_live_blocks++; 3191957 0 3191957
86 3191957 return p;
87 }
88 3191957 static void hamt_xs_free(void *cx, void *p, size_t bytes) {
89 PERL_UNUSED_ARG(cx);
90 3191957 hamt_live_blocks--;
91 3191957 ▶ hamt_pool_free(HAMT_POOL_FOR(cx), p, bytes);
Branch: 2 branches
BranchTrueFalseTotal
hamt_pool_free(HAMT_POOL_FOR(cx), p, bytes); 3191957 0 3191957
hamt_pool_free(HAMT_POOL_FOR(cx), p, bytes); 3191957 0 3191957
92 3191957 }
93  
94 #define HAMT_ALLOC(cx, bytes) hamt_xs_alloc((cx), (bytes))
95 #define HAMT_FREE(cx, p, bytes) hamt_xs_free((cx), (p), (bytes))
96  
97 #endif
« include/hamt/hamt_mut.h ↑ Summary include/hamt/hamt_xs.h »