include/hamt/hamt_sv.h

filter: Statementstmt 100% 19 / 19 Branchbran 88.8% 31 / 36 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.7% 50 / 55 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_pool.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 7236397 static void hamt_sv_release_cx(void *cx, SV *sv) {
23 dTHXa(cx);
24 PERL_UNUSED_ARG(cx);
25 7236397 ▶ if (PL_dirty) return;
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) return; 3 7236394 7236397
26 7236394 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 227097 static int hamt_sv_eq_cx(void *cx, SV *a, SV *b) {
33 dTHXa(cx);
34 PERL_UNUSED_ARG(cx);
35 227097 ▶ if (a == b) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 1; 3270 223827 227097
36 223827 ▶ if (SvROK(a) || SvROK(b))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(a) || SvROK(b)) 223050 777 223827
if (SvROK(a) || SvROK(b)) 1 223049 223050
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 223049 ▶ if (!SvOK(a) || !SvOK(b))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(a) || !SvOK(b)) 221942 1107 223049
if (!SvOK(a) || !SvOK(b)) 141 221801 221942
39 1248 ▶ return !SvOK(a) && !SvOK(b);
Branch: 2 branches
BranchTrueFalseTotal
return !SvOK(a) && !SvOK(b); 1107 141 1248
return !SvOK(a) && !SvOK(b); 1099 8 1107
40 221801 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 /* For the tests: when above zero, counts down with each allocation and
83 * the one that takes it to zero fails, as the C library would with no
84 * memory. Nothing else reads it. */
85 static IV hamt_alloc_fail_in = 0;
86  
87 4117705 static void *hamt_xs_alloc(void *cx, size_t bytes) {
88 void *p;
89 PERL_UNUSED_ARG(cx);
90 4117705 ▶ if (hamt_alloc_fail_in > 0 && --hamt_alloc_fail_in == 0) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (hamt_alloc_fail_in > 0 && --hamt_alloc_fail_in == 0) return NULL; 918490 3199215 4117705
if (hamt_alloc_fail_in > 0 && --hamt_alloc_fail_in == 0) return NULL; 18420 900070 918490
91 4099285 ▶ p = hamt_pool_alloc(HAMT_POOL_FOR(cx), bytes);
Branch: 2 branches
BranchTrueFalseTotal
p = hamt_pool_alloc(HAMT_POOL_FOR(cx), bytes); 4099285 0 4099285
p = hamt_pool_alloc(HAMT_POOL_FOR(cx), bytes); 4099285 0 4099285
92 /*
93 # uncoverable branch false
94 # uncoverable condition right count:1 */
95 4099285 ▶ if (p) hamt_live_blocks++;
Branch: 1 branch
BranchTrueFalseTotal
if (p) hamt_live_blocks++; 4099285 0 4099285
96 4099285 return p;
97 }
98 4099285 static void hamt_xs_free(void *cx, void *p, size_t bytes) {
99 PERL_UNUSED_ARG(cx);
100 4099285 hamt_live_blocks--;
101 4099285 ▶ hamt_pool_free(HAMT_POOL_FOR(cx), p, bytes);
Branch: 2 branches
BranchTrueFalseTotal
hamt_pool_free(HAMT_POOL_FOR(cx), p, bytes); 4099285 0 4099285
hamt_pool_free(HAMT_POOL_FOR(cx), p, bytes); 4099285 0 4099285
102 4099285 }
103  
104 #define HAMT_ALLOC(cx, bytes) hamt_xs_alloc((cx), (bytes))
105 #define HAMT_FREE(cx, p, bytes) hamt_xs_free((cx), (p), (bytes))
106  
107 #endif
« include/hamt/hamt_pool.h ↑ Summary include/hamt/hamt_xs.h »