include/sa/sa_serialise.h

filter: Statementstmt 100% 9 / 9 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.3% 14 / 15 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/Shared/Arena/Install/Files.pm ↑ Summary blib/lib/Shared/Arena.pm »
1 #ifndef SA_SERIALISE_H
2 #define SA_SERIALISE_H
3  
4 /* sa_serialise.h - a map or cache value as a Perl structure. NOT perl-free:
5 * this is the one header under include/sa/ besides sa_xop.h that needs an
6 * interpreter, because a codec has nothing to do without one.
7 *
8 * A map or cache made with `serialise => 1` stores a Perl structure and gives
9 * THE SAME structure back: types, the UTF-8 flag, a blessing, shared and
10 * cyclic references. Struct::Codec is the encoding and the decoder, reached
11 * through its ABI table so a set or a get costs no Perl frame; the table is
12 * resolved once at BOOT, in xs/cache.xs, on the Frozen model in Arena.xs.
13 *
14 * The flag itself lives in the SHARED tenant header (sa_cache.h, sa_hash.h),
15 * because a process that treats a cache as bytes and another that treats it
16 * as encoded values are two caches that disagree about every value. A bind
17 * that asks for the other setting is refused with SA_E_SHAPE.
18 *
19 * The C ABI in sa_abi.h stays bytes-level: a serialised tenant's values are
20 * Struct::Codec's encoding, and a C consumer writing one encodes through
21 * sc_abi.h or not at all.
22 */
23  
24 #include "sc_abi.h"
25  
26 static const sc_abi *SA_SC = NULL;
27  
28 /* A value encodes onto the caller's stack when the room left in the slot is at
29 * most this. That is where the small-value win is: no allocation on a set. */
30 #define SA_SER_STACK 4096
31  
32 /* Encode `value` for a slot whose key already takes `klen` of `pair_max`.
33 *
34 * Up to SA_SER_STACK bytes of room it is `encode_to` straight into `stack`,
35 * which allocates nothing on the too-small path either. Past that it is
36 * `encode`, which allocates what the value needs rather than what the slot
37 * could hold, into a mortal.
38 *
39 * Returns the length and sets *out, or 0 when the value will not fit: the
40 * same refusal an oversized byte value gets, so a caller sees one behaviour
41 * for "too big" whatever the value was. A value that cannot be encoded at all
42 * croaks through the codec, before anything is stored.
43 *
44 * Both entries can run Perl (an anonymous sub is deparsed), which may
45 * reallocate the value stack. A door that captured `sp` or `mark` before
46 * calling this must SPAGAIN and re-derive `mark` from TOPMARK afterwards. */
47 32 static STRLEN sa_ser_encode(pTHX_ SV *value, uint64_t pair_max, STRLEN klen,
48 char *stack, const char **out)
49 {
50 32 ▶ STRLEN cap = pair_max > klen ? (STRLEN)(pair_max - klen) : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN cap = pair_max > klen ? (STRLEN)(pair_max - klen) : 0; 32 0 32
51 32 ▶ if (cap <= SA_SER_STACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (cap <= SA_SER_STACK) { 28 4 32
52 28 STRLEN n = (SA_SC->encode_to)(aTHX_ value, stack, cap, NULL);
53 24 *out = stack;
54 24 return n;
55 }
56 {
57 4 SV *enc = sv_2mortal((SA_SC->encode)(aTHX_ value));
58 4 *out = SvPVX(enc);
59 4 ▶ return SvCUR(enc) <= cap ? SvCUR(enc) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return SvCUR(enc) <= cap ? SvCUR(enc) : 0; 2 2 4
60 }
61 }
62  
63 /* The structure back from a slot's bytes, as a mortal. Corrupt bytes croak
64 * with the codec's reason; nothing here ever hands bytes back as a value. The
65 * same warning about the value stack applies: decoding an anonymous sub runs
66 * Perl. */
67 #define SA_SER_DECODE(p, len) \
68 sv_2mortal((SA_SC->decode)(aTHX_ (p), (STRLEN)(len)))
69  
70 /* One message from the XSUB and both incr doors. */
71 #define SA_SER_INCR_MSG "Shared::Arena::Map: incr on a serialised map"
72  
73 #endif /* SA_SERIALISE_H */
« blib/arch/Shared/Arena/Install/Files.pm ↑ Summary blib/lib/Shared/Arena.pm »