Encoder.xs

filter: Statementstmt 48.2% 84 / 174 Branchbran 24.0% 25 / 104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 39.2% 109 / 278 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« srl_common.h ↑ Summary srl_buffer.h »
1 /* Must be defined before including Perl header files or we slow down by 2x! */
2 #define PERL_NO_GET_CONTEXT
3  
4 #include "EXTERN.h"
5 #include "perl.h"
6 #include "XSUB.h"
7  
8 #define NEED_newSV_type_GLOBAL
9 #include "ppport.h"
10  
11 #include "srl_encoder.h"
12 #include "srl_buffer.h"
13 #include "srl_common.h"
14 #include "srl_protocol.h"
15 #include "srl_siphash.h"
16  
17 /* new() marks its handle; a blessed integer from elsewhere is not one. */
18 static MGVTBL srl_handle_vtbl;
19 static int
20 14 srl_is_handle(SV *sv)
21 {
22 MAGIC *mg;
23 14 ▶ if (SvTYPE(sv) < SVt_PVMG)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(sv) < SVt_PVMG) 0 14 14
24 0 return 0;
25 14 ▶ for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) 14 0 14
26 14 ▶ if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &srl_handle_vtbl)
Branch: 2 branches
BranchTrueFalseTotal
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &srl_handle_vtbl) 14 0 14
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &srl_handle_vtbl) 14 0 14
27 14 return 1;
28 0 return 0;
29 }
30 #define SRL_IS_HANDLE(sv) srl_is_handle(sv)
31  
32 /* Generated code for exposing C constants to Perl */
33 #include "ptable.h"
34  
35 #ifndef GvCV_set
36 # define GvCV_set(gv, cv) (GvCV(gv) = (cv))
37 #endif
38  
39 #if defined(cv_set_call_checker) && defined(XopENTRY_set)
40 # define USE_CUSTOM_OPS 1
41 #else
42 # define USE_CUSTOM_OPS 0
43 #endif
44  
45 #define pp1_sereal_encode_with_object(has_hdr) THX_pp1_sereal_encode_with_object(aTHX_ has_hdr)
46 static void
47 7 THX_pp1_sereal_encode_with_object(pTHX_ U8 has_hdr)
48 {
49 SV *encoder_ref_sv, *encoder_sv, *body_sv, *header_sv;
50 srl_encoder_t *enc;
51 char *stash_name;
52 SV *ret_sv;
53 7 dSP;
54  
55 7 ▶ header_sv = has_hdr ? POPs : NULL;
Branch: 1 branch
BranchTrueFalseTotal
header_sv = has_hdr ? POPs : NULL; 0 7 7
56 7 body_sv = POPs;
57 7 PUTBACK;
58  
59 7 encoder_ref_sv = TOPs;
60  
61 7 ▶ if (!expect_true(
Branch: 14 branches
BranchTrueFalseTotal
if (!expect_true( 7 0 7
if (!expect_true( 7 0 7
if (!expect_true( 7 0 7
if (!expect_true( 7 0 7
if (!expect_true( 7 0 7
if (!expect_true( 7 0 7
if (!expect_true( 0 7 7
if (!expect_true( 0 0 0
if (!expect_true( 7 0 7
if (!expect_true( 0 7 7
if (!expect_true( 7 0 7
if (!expect_true( 7 0 7
if (!expect_true( 7 0 7
if (!expect_true( 0 7 7
62 encoder_ref_sv &&
63 SvROK(encoder_ref_sv) &&
64 (encoder_sv = SvRV(encoder_ref_sv)) &&
65 SvOBJECT(encoder_sv) &&
66 (stash_name= HvNAME(SvSTASH(encoder_sv))) &&
67 !strcmp(stash_name, "Sereal::Encoder") &&
68 SRL_IS_HANDLE(encoder_sv)
69 ))
70 {
71 0 croak("handle is not a Sereal::Encoder handle");
72 }
73 /* we should never have an IV smaller than a PTR */
74 7 enc= INT2PTR(srl_encoder_t *,SvIV(encoder_sv));
75 /* User code may clear the variable containing the encoder while it runs.
76 * Keep the owner alive until the encode cleanup hook has finished. */
77 7 SAVEFREESV(SvREFCNT_inc(encoder_sv));
78  
79 7 ▶ if (header_sv && !SvOK(header_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (header_sv && !SvOK(header_sv)) 0 7 7
if (header_sv && !SvOK(header_sv)) 0 0 0
80 0 header_sv = NULL;
81  
82 /* We always copy the string since we might reuse the string buffer. That
83 * means we already have to do a malloc and we might as well use the
84 * opportunity to allocate only as much memory as we really need to hold
85 * the output. */
86 7 ret_sv= srl_dump_data_structure_mortal_sv(aTHX_ enc, body_sv, header_sv, SRL_ENC_SV_COPY_ALWAYS);
87 7 SPAGAIN;
88 7 TOPs = ret_sv;
89 7 }
90  
91 #if USE_CUSTOM_OPS
92  
93 #ifdef PERL_RC_STACK
94 /* Custom ops bypass the XSUB wrapper, so balance their stack references. */
95 XSPP_wrapped(THX_pp_sereal_encode_with_object, 2 + !!PL_op->op_private, 0)
96 #else
97 static OP *
98 0 THX_pp_sereal_encode_with_object(pTHX)
99 #endif
100 {
101 0 pp1_sereal_encode_with_object(PL_op->op_private);
102 0 return NORMAL;
103 }
104  
105 static OP *
106 0 THX_ck_entersub_args_sereal_encode_with_object(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
107 {
108 OP *pushop, *firstargop, *cvop, *lastargop, *argop, *newop;
109 int arity;
110  
111 /* Walk the OP structure under the "entersub" to validate that we
112 * can use the custom OP implementation. */
113  
114 0 entersubop = ck_entersub_args_proto(entersubop, namegv, ckobj);
115 0 pushop = cUNOPx(entersubop)->op_first;
116 0 ▶ if (!OpHAS_SIBLING(pushop))
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) 0 0 0
117 0 pushop = cUNOPx(pushop)->op_first;
118 0 ▶ firstargop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
firstargop = OpSIBLING(pushop); 0 0 0
119  
120 0 ▶ for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
Branch: 2 branches
BranchTrueFalseTotal
for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; 0 0 0
for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; 0 0 0
121  
122 0 ▶ for (arity = 0, lastargop = pushop, argop = firstargop; argop != cvop;
Branch: 1 branch
BranchTrueFalseTotal
for (arity = 0, lastargop = pushop, argop = firstargop; argop != cvop; 0 0 0
123 0 lastargop = argop, argop = OpSIBLING(argop))
124 {
125 0 ▶ arity++;
Branch: 1 branch
BranchTrueFalseTotal
arity++; 0 0 0
126 }
127  
128 0 ▶ if (expect_false(arity < 2 || arity > 3))
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(arity < 2 || arity > 3)) 0 0 0
if (expect_false(arity < 2 || arity > 3)) 0 0 0
if (expect_false(arity < 2 || arity > 3)) 0 0 0
129 0 return entersubop;
130  
131 /* If we get here, we can replace the entersub with a suitable
132 * sereal_encode_with_object custom OP. */
133  
134 #ifdef op_sibling_splice
135 /* op_sibling_splice is new in 5.31 and we have to do things differenly */
136  
137 /* cut out all ops between the pushmark and the RV2CV */
138 0 op_sibling_splice(NULL, pushop, arity, NULL);
139 /* then throw everything else out */
140 0 op_free(entersubop);
141 0 newop = newUNOP(OP_NULL, 0, NULL);
142  
143 #else
144  
145 OpMORESIB_set(pushop, cvop);
146 OpLASTSIB_set(lastargop, op_parent(lastargop));
147 op_free(entersubop);
148 newop = newUNOP(OP_NULL, 0, firstargop);
149  
150 #endif
151  
152 0 newop->op_type = OP_CUSTOM;
153 0 newop->op_private = arity == 3;
154 0 newop->op_ppaddr = THX_pp_sereal_encode_with_object;
155  
156 #ifdef op_sibling_splice
157  
158 /* attach the spliced-out args as children of the custom op, while
159 * deleting the stub op created by newUNOP() */
160 0 op_free(op_sibling_splice(newop, NULL, 1, firstargop));
161  
162 #endif
163  
164 0 return newop;
165 }
166  
167 #endif /* USE_CUSTOM_OPS */
168  
169 static void
170 7 THX_xsfunc_sereal_encode_with_object(pTHX_ CV *cv)
171 {
172 7 dMARK;
173 7 dSP;
174 7 SSize_t arity = SP - MARK;
175 PERL_UNUSED_ARG(cv);
176 7 ▶ if (arity < 2 || arity > 3)
Branch: 2 branches
BranchTrueFalseTotal
if (arity < 2 || arity > 3) 7 0 7
if (arity < 2 || arity > 3) 0 7 7
177 0 croak("bad Sereal encoder usage");
178 7 pp1_sereal_encode_with_object(arity == 3);
179 7 }
180  
181 #define MY_CXT_KEY "Sereal::Encoder::_stash" XS_VERSION
182  
183 typedef struct {
184 sv_with_hash options[SRL_ENC_OPT_COUNT];
185 U8 known_option_heads[SRL_OPTION_INDEX_BUCKETS];
186 U8 known_option_next[SRL_ENC_OPT_COUNT];
187 } my_cxt_t;
188  
189 START_MY_CXT
190  
191 static void
192 30 srl_init_my_cxt(pTHX)
193 {
194 dMY_CXT;
195 30 Zero(MY_CXT.known_option_heads, SRL_OPTION_INDEX_BUCKETS, U8);
196 30 Zero(MY_CXT.known_option_next, SRL_ENC_OPT_COUNT, U8);
197 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_ALIASED_DEDUPE_STRINGS, SRL_ENC_OPT_STR_ALIASED_DEDUPE_STRINGS );
198 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CANONICAL, SRL_ENC_OPT_STR_CANONICAL );
199 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CANONICAL_REFS, SRL_ENC_OPT_STR_CANONICAL_REFS );
200 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_COMPRESS, SRL_ENC_OPT_STR_COMPRESS );
201 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_COMPRESS_LEVEL, SRL_ENC_OPT_STR_COMPRESS_LEVEL );
202 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_COMPRESS_THRESHOLD, SRL_ENC_OPT_STR_COMPRESS_THRESHOLD );
203 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CROAK_ON_BLESS, SRL_ENC_OPT_STR_CROAK_ON_BLESS );
204 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_DEDUPE_STRINGS, SRL_ENC_OPT_STR_DEDUPE_STRINGS );
205 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_FREEZE_CALLBACKS, SRL_ENC_OPT_STR_FREEZE_CALLBACKS );
206 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_MAX_RECURSION_DEPTH, SRL_ENC_OPT_STR_MAX_RECURSION_DEPTH );
207 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_NO_BLESS_OBJECTS, SRL_ENC_OPT_STR_NO_BLESS_OBJECTS );
208 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_NO_SHARED_HASHKEYS, SRL_ENC_OPT_STR_NO_SHARED_HASHKEYS );
209 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_PROTOCOL_VERSION, SRL_ENC_OPT_STR_PROTOCOL_VERSION );
210 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_SNAPPY, SRL_ENC_OPT_STR_SNAPPY );
211 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_SNAPPY_INCR, SRL_ENC_OPT_STR_SNAPPY_INCR );
212 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_SNAPPY_THRESHOLD, SRL_ENC_OPT_STR_SNAPPY_THRESHOLD );
213 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_SORT_KEYS, SRL_ENC_OPT_STR_SORT_KEYS );
214 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_STRINGIFY_UNKNOWN, SRL_ENC_OPT_STR_STRINGIFY_UNKNOWN );
215 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_UNDEF_UNKNOWN, SRL_ENC_OPT_STR_UNDEF_UNKNOWN );
216 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_USE_PROTOCOL_V1, SRL_ENC_OPT_STR_USE_PROTOCOL_V1 );
217 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_WARN_UNKNOWN, SRL_ENC_OPT_STR_WARN_UNKNOWN );
218 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_USE_STANDARD_DOUBLE, SRL_ENC_OPT_STR_USE_STANDARD_DOUBLE );
219 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_FREEZE_ALLOW_CLASSES, SRL_ENC_OPT_STR_FREEZE_ALLOW_CLASSES );
220 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_FREEZE_DENY_ACTION, SRL_ENC_OPT_STR_FREEZE_DENY_ACTION );
221 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_KEY_V1, SRL_ENC_OPT_STR_CHECKSUM_KEY_V1 );
222 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_KEY_V2, SRL_ENC_OPT_STR_CHECKSUM_KEY_V2 );
223 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_KEY_V3, SRL_ENC_OPT_STR_CHECKSUM_KEY_V3 );
224 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_POLICY, SRL_ENC_OPT_STR_CHECKSUM_POLICY );
225 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_UNKNOWN_OPTION_POLICY, SRL_ENC_OPT_STR_UNKNOWN_OPTION_POLICY );
226 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_KEY, SRL_ENC_OPT_STR_CHECKSUM_KEY );
227 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_MODE, SRL_ENC_OPT_STR_CHECKSUM_MODE );
228 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_FLOAT_TYPE, SRL_ENC_OPT_STR_FLOAT_TYPE );
229 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_ALLOW_CLASSES, SRL_ENC_OPT_STR_ALLOW_CLASSES );
230 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_TRACK_CLASSES, SRL_ENC_OPT_STR_TRACK_CLASSES );
231 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_REFUSE_OBJECTS, SRL_ENC_OPT_STR_REFUSE_OBJECTS );
232 30 SRL_INIT_OPTION( SRL_ENC_OPT_IDX_PARANOID, SRL_ENC_OPT_STR_PARANOID );
233 30 }
234  
235 MODULE = Sereal::Encoder PACKAGE = Sereal::Encoder
236 PROTOTYPES: DISABLE
237  
238 SV *
239 prepare_checksum_key(key = NULL, opt_key = NULL)
240 SV *key;
241 SV *opt_key;
242 PREINIT:
243 6 STRLEN key_len = 0;
244 6 STRLEN opt_key_len = 0;
245 6 const U8 *key_bytes = (const U8 *)"";
246 6 ▶ const U8 *opt_key_bytes = (const U8 *)"";
Branch: 1 branch
BranchTrueFalseTotal
const U8 *opt_key_bytes = (const U8 *)""; 0 6 6
247 U8 digest[16];
248 CODE:
249 6 ▶ if (key && SvOK(key))
Branch: 2 branches
BranchTrueFalseTotal
if (key && SvOK(key)) 6 0 6
if (key && SvOK(key)) 6 0 6
250 6 key_bytes = (const U8 *)SvPVbyte(key, key_len);
251 6 ▶ if (opt_key && SvOK(opt_key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opt_key && SvOK(opt_key)) { 0 6 6
if (opt_key && SvOK(opt_key)) { 0 0 0
252 0 opt_key_bytes = (const U8 *)SvPVbyte(opt_key, opt_key_len);
253 }
254 6 srl_siphash_prepare_key(key_bytes, (size_t)key_len,
255 opt_key_bytes, (size_t)opt_key_len, digest);
256 6 RETVAL = newSVpvn((const char *)digest, sizeof(digest));
257 OUTPUT: RETVAL
258  
259 SV *
260 siphash24(data, key = NULL)
261 SV *data;
262 SV *key;
263 PREINIT:
264 STRLEN data_len;
265 STRLEN key_len;
266 const U8 *data_bytes;
267 const U8 *key_bytes;
268 srl_siphash_ctx sip;
269 srl_u64 digest;
270 U8 digest_bytes[8];
271 unsigned int i;
272 static const U8 zero_key[16] = { 0 };
273 CODE:
274 0 data_bytes = (const U8 *)SvPVbyte(data, data_len);
275 0 ▶ if (key == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (key == NULL) { 0 0 0
276 0 key_bytes = zero_key;
277 }
278 else {
279 0 key_bytes = (const U8 *)SvPVbyte(key, key_len);
280 0 ▶ if (key_len != 16)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len != 16) 0 0 0
281 0 croak("siphash24 key must be exactly 16 bytes");
282 }
283 0 srl_siphash_init_rounds(&sip, key_bytes, 2, 4, 0);
284 0 srl_siphash_update(&sip, data_bytes, (size_t)data_len);
285 0 digest = srl_siphash_final64(&sip);
286 0 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 0 0 0
287 0 digest_bytes[i] = (U8)(digest >> (8 * i));
288 0 RETVAL = newSVpvn((const char *)digest_bytes, sizeof(digest_bytes));
289 OUTPUT: RETVAL
290  
291 SV *
292 siphash24_128(data, key = NULL)
293 SV *data;
294 SV *key;
295 PREINIT:
296 STRLEN data_len;
297 STRLEN key_len;
298 const U8 *data_bytes;
299 const U8 *key_bytes;
300 srl_siphash_ctx sip;
301 U8 digest[16];
302 static const U8 zero_key[16] = { 0 };
303 CODE:
304 0 data_bytes = (const U8 *)SvPVbyte(data, data_len);
305 0 ▶ if (key == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (key == NULL) { 0 0 0
306 0 key_bytes = zero_key;
307 }
308 else {
309 0 key_bytes = (const U8 *)SvPVbyte(key, key_len);
310 0 ▶ if (key_len != 16)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len != 16) 0 0 0
311 0 croak("siphash24_128 key must be exactly 16 bytes");
312 }
313 0 srl_siphash_init_rounds(&sip, key_bytes, 2, 4, 1);
314 0 srl_siphash_update(&sip, data_bytes, (size_t)data_len);
315 0 srl_siphash_final128(&sip, digest);
316 0 RETVAL = newSVpvn((const char *)digest, sizeof(digest));
317 OUTPUT: RETVAL
318  
319 SV *
320 siphash36_128(data, key = NULL)
321 SV *data;
322 SV *key;
323 PREINIT:
324 STRLEN data_len;
325 STRLEN key_len;
326 const U8 *data_bytes;
327 const U8 *key_bytes;
328 srl_siphash_ctx sip;
329 U8 digest[16];
330 static const U8 zero_key[16] = { 0 };
331 CODE:
332 0 data_bytes = (const U8 *)SvPVbyte(data, data_len);
333 0 ▶ if (key == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (key == NULL) { 0 0 0
334 0 key_bytes = zero_key;
335 }
336 else {
337 0 key_bytes = (const U8 *)SvPVbyte(key, key_len);
338 0 ▶ if (key_len != 16)
Branch: 1 branch
BranchTrueFalseTotal
if (key_len != 16) 0 0 0
339 0 croak("siphash36_128 key must be exactly 16 bytes");
340 }
341 0 srl_siphash_init_rounds(&sip, key_bytes, 3, 6, 1);
342 0 srl_siphash_update(&sip, data_bytes, (size_t)data_len);
343 0 srl_siphash_final128(&sip, digest);
344 0 RETVAL = newSVpvn((const char *)digest, sizeof(digest));
345 OUTPUT: RETVAL
346  
347 BOOT:
348 {
349 {
350 MY_CXT_INIT;
351 30 srl_init_my_cxt(aTHX);
352 }
353 #if USE_CUSTOM_OPS
354 {
355 /* Perl borrows this descriptor; it must outlive every interpreter. */
356 static const XOP xop = {
357 XOPf_xop_name | XOPf_xop_desc | XOPf_xop_class,
358 "sereal_encode_with_object", "sereal_encode_with_object", OA_UNOP
359 };
360 30 Perl_custom_op_register(aTHX_ THX_pp_sereal_encode_with_object, &xop);
361 }
362 #endif /* USE_CUSTOM_OPS */
363 {
364 GV *gv;
365 30 CV *cv = newXSproto_portable("Sereal::Encoder::sereal_encode_with_object",
366 THX_xsfunc_sereal_encode_with_object, __FILE__, "$$;$");
367 #if USE_CUSTOM_OPS
368 30 cv_set_call_checker(cv, THX_ck_entersub_args_sereal_encode_with_object, (SV*)cv);
369 #endif /* USE_CUSTOM_OPS */
370 30 gv = gv_fetchpv("Sereal::Encoder::encode", GV_ADDMULTI, SVt_PVCV);
371 30 GvCV_set(gv, cv);
372 }
373 }
374  
375 srl_encoder_t *
376 new(CLASS, opt = NULL)
377 char *CLASS;
378 HV *opt;
379 PREINIT:
380 dMY_CXT;
381 CODE:
382 7 RETVAL = srl_build_encoder_struct(aTHX_ opt, MY_CXT.options,
383 MY_CXT.known_option_heads, MY_CXT.known_option_next);
384 7 RETVAL->flags |= SRL_F_REUSE_ENCODER;
385 OUTPUT: RETVAL
386  
387 void
388 DESTROY(enc)
389 srl_encoder_t *enc;
390 CODE:
391 7 srl_destroy_encoder(aTHX_ enc);
392  
393 U32
394 flags(enc)
395 srl_encoder_t *enc;
396 CODE:
397 0 ▶ RETVAL = enc->flags;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = enc->flags; 0 0 0
398 OUTPUT: RETVAL
399  
400 void
401 encode_sereal(src, opt = NULL)
402 SV *src;
403 HV *opt;
404 PREINIT:
405 srl_encoder_t *enc;
406 dMY_CXT;
407 PPCODE:
408 0 enc = srl_build_encoder_struct(aTHX_ opt, MY_CXT.options,
409 MY_CXT.known_option_heads, MY_CXT.known_option_next);
410 assert(enc != NULL);
411 /* Avoid copy by stealing string buffer if it is not too large.
412 * This makes sense in the functional interface since the string
413 * buffer isn't ever going to be reused. */
414 0 ST(0) = srl_dump_data_structure_mortal_sv(aTHX_ enc, src, NULL, SRL_ENC_SV_REUSE_MAYBE);
415 0 XSRETURN(1);
416  
417 void
418 encode_sereal_with_header_data(src, hdr_user_data_src, opt = NULL)
419 SV *src;
420 SV *hdr_user_data_src;
421 HV *opt;
422 PREINIT:
423 srl_encoder_t *enc;
424 dMY_CXT;
425 PPCODE:
426 0 ▶ if (!SvOK(hdr_user_data_src))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(hdr_user_data_src)) 0 0 0
427 0 hdr_user_data_src = NULL;
428 0 enc = srl_build_encoder_struct(aTHX_ opt, MY_CXT.options,
429 MY_CXT.known_option_heads, MY_CXT.known_option_next);
430 assert(enc != NULL);
431 /* Avoid copy by stealing string buffer if it is not too large.
432 * This makes sense in the functional interface since the string
433 * buffer isn't ever going to be reused. */
434 0 ST(0) = srl_dump_data_structure_mortal_sv(aTHX_ enc, src, hdr_user_data_src, SRL_ENC_SV_REUSE_MAYBE);
435 0 XSRETURN(1);
436  
437 #if defined(USE_ITHREADS) && defined(MY_CXT_KEY)
438 void
439 CLONE(...)
440 CODE:
441 MY_CXT_CLONE;
442 srl_init_my_cxt(aTHX);
443  
444 #endif
445  
446 MODULE = Sereal::Encoder PACKAGE = Sereal::Encoder::_ptabletest
447  
448 void
449 test()
450 PREINIT:
451 PTABLE_t *tbl;
452 PTABLE_ITER_t *iter;
453 PTABLE_ENTRY_t *ent;
454 0 UV i, n = 20;
455 char *check[20];
456 0 char fail[5] = "not ";
457 0 char noop[1] = "";
458 CODE:
459 0 tbl = PTABLE_new_size(10);
460 0 ▶ for (i = 0; i < (UV)n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (UV)n; ++i) { 0 0 0
461 0 PTABLE_store(tbl, INT2PTR(void *,(1000+i)), INT2PTR(void *, (1000+i)));
462 0 check[i] = fail;
463 }
464 0 ▶ for (i = 0; i < (UV)n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (UV)n; ++i) { 0 0 0
465 0 const UV res = PTR2UV(PTABLE_fetch(tbl, INT2PTR(void *, (1000+i))));
466 0 ▶ printf("%sok %u - fetch %u\n", (res == (UV)(1000+i)) ? noop : fail, (unsigned int)(1+i), (unsigned int)(i+1));
Branch: 1 branch
BranchTrueFalseTotal
printf("%sok %u - fetch %u\n", (res == (UV)(1000+i)) ? noop : fail, (unsigned int)(1+i), (unsigned int)(i+1)); 0 0 0
467 }
468 0 iter = PTABLE_iter_new(tbl);
469 0 ▶ while ( NULL != (ent = PTABLE_iter_next(iter)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( NULL != (ent = PTABLE_iter_next(iter)) ) { 0 0 0
470 0 const UV res = (PTR2UV(ent->value)) - 1000;
471 0 ▶ if (res < 20)
Branch: 1 branch
BranchTrueFalseTotal
if (res < 20) 0 0 0
472 0 check[res] = noop;
473 else
474 0 abort();
475 }
476 0 ▶ for (i = 0; i < (UV)n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (UV)n; ++i) { 0 0 0
477 0 printf("%sok %u - iter %u\n", check[i], (unsigned int)(21+i), (unsigned int)(i+1));
478 }
479 0 PTABLE_iter_free(iter);
480 0 PTABLE_free(tbl);
« srl_common.h ↑ Summary srl_buffer.h »