lib/Evo/Class/Attrs/eca.c

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 73 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/Evo/Class/Attrs/XS.xs ↑ Summary lib/Evo/Class/Attrs/helpers.c »
1 static int eca_mg_free(pTHX_ SV *sv, MAGIC *mg);
2 static int eca_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param);
3  
4 static ECAslot *eca_dup(const ECAslot *src);
5 static void eca_destroy(ECAslot *slot);
6  
7 static MGVTBL ECA_TBL = {0, 0, 0, 0, eca_mg_free, 0, eca_mg_dup, 0};
8  
9 /*
10 * ECAslot is a C structure
11 * eca_new_sv creates an SV with a pointer to that structure. Magic need to
12 * duplicate a structure(threads) and clear it. Don't forget:
13 * mg->mg_flags |= MGf_DUP;
14 *
15 * slot->key contains a "shared" string.
16 *
17 * value, check, inject could be NULLs
18 *
19 * Don't try to move magic for CV, because one slot can be bound to both (new
20 * and accessor)
21 */
22  
23 0 static int eca_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
24 0 SV *sv = mg->mg_obj;
25 0 ECAslot *dup = eca_dup(sv2slot(sv));
26 0 sv_setuv(sv, PTR2UV(dup));
27 PERL_UNUSED_VAR(param);
28 0 return 0;
29 }
30  
31 0 static int eca_mg_free(pTHX_ SV *sv, MAGIC *mg) {
32 0 ECAslot *slot = sv2slot(mg->mg_obj);
33 PERL_UNUSED_VAR(sv);
34 0 eca_destroy(slot);
35 0 return 0;
36 }
37  
38 0 static ECAslot *eca_init(char *name, ECAtype type, SV *value, SV *check,
39 bool is_ro, SV *inject, bool is_method) {
40 dTHX;
41  
42 0 ECAslot *slot = calloc(1, sizeof(ECAslot));
43 0 ▶ if (!slot) croak("Can't allocate memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!slot) croak("Can't allocate memory"); 0 0 0
44  
45 0 switch (type) {
46  
47 0 case ECA_OPTIONAL:
48 0 break;
49 0 case ECA_LAZY: // no need to store undef
50 0 ▶ if (SvTRUE(value)) slot->value = newSVsv(value);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(value)) slot->value = newSVsv(value); 0 0 0
51 0 break;
52 0 case ECA_DEFAULT:
53 case ECA_DEFAULT_CODE:
54 case ECA_REQUIRED:
55 0 slot->value = newSVsv(value);
56 0 break;
57 0 default:
58 0 croak("Bad type: %d", type);
59 break;
60 }
61  
62 0 ▶ if (SvTRUE(check)) slot->check = newSVsv(check);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(check)) slot->check = newSVsv(check); 0 0 0
63 0 ▶ if (SvTRUE(inject)) slot->inject = newSVsv(inject);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(inject)) slot->inject = newSVsv(inject); 0 0 0
64 0 slot->is_ro = is_ro;
65 0 slot->is_method = is_method;
66 0 slot->type = type;
67 0 slot->key = newSVpv_share(name, 0);
68 0 return slot;
69 }
70  
71 0 static SV *eca_new_sv(char *name, ECAtype type, SV *value, SV *check,
72 bool is_ro, SV *inject, bool is_method) {
73  
74 dTHX;
75 0 ECAslot *slot = eca_init(name, type, value, check, is_ro, inject, is_method);
76 0 SV *result_sv = newSVuv(PTR2UV(slot));
77 MAGIC *mg =
78 0 sv_magicext(result_sv, result_sv, PERL_MAGIC_ext, &ECA_TBL, NULL, 0);
79 0 mg->mg_flags |= MGf_DUP; // to invoke attrs_dup
80 0 return result_sv;
81 }
82  
83 0 static void eca_destroy(ECAslot *slot) {
84 dTHX;
85 0 ▶ if (slot->value) SvREFCNT_dec(slot->value);
Branch: 1 branch
BranchTrueFalseTotal
if (slot->value) SvREFCNT_dec(slot->value); 0 0 0
86 0 ▶ if (slot->check) SvREFCNT_dec(slot->check);
Branch: 1 branch
BranchTrueFalseTotal
if (slot->check) SvREFCNT_dec(slot->check); 0 0 0
87 0 ▶ if (slot->inject) SvREFCNT_dec(slot->inject);
Branch: 1 branch
BranchTrueFalseTotal
if (slot->inject) SvREFCNT_dec(slot->inject); 0 0 0
88 0 SvREFCNT_dec(slot->key);
89  
90 0 free(slot);
91 0 }
92  
93 0 static ECAslot *eca_dup(const ECAslot *src) {
94 dTHX;
95 0 ECAslot *dup = calloc(1, sizeof(*src));
96 0 ▶ if (!dup) croak("can't locate memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!dup) croak("can't locate memory"); 0 0 0
97  
98 0 dup->key = newSVpv_share(SvPV_nolen(src->key), 0);
99 0 ▶ if (src->value) dup->value = newSVsv(src->value);
Branch: 1 branch
BranchTrueFalseTotal
if (src->value) dup->value = newSVsv(src->value); 0 0 0
100 0 ▶ if (src->check) dup->check = newSVsv(src->check);
Branch: 1 branch
BranchTrueFalseTotal
if (src->check) dup->check = newSVsv(src->check); 0 0 0
101 0 ▶ if (src->inject) dup->inject = newSVsv(src->inject);
Branch: 1 branch
BranchTrueFalseTotal
if (src->inject) dup->inject = newSVsv(src->inject); 0 0 0
102 0 return dup;
103 }
« lib/Evo/Class/Attrs/XS.xs ↑ Summary lib/Evo/Class/Attrs/helpers.c »