SafeKeys.xs

filter: Statementstmt 83.6% 51 / 61 Branchbran 78.5% 22 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.0% 73 / 89 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Hash/SafeKeys.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 /* #include "INLINE.h" */
5  
6 #define STATES_INITIAL_SIZE 10
7  
8 struct _iterator_state {
9 I32 riter;
10 HE* eiter;
11 };
12 typedef struct _iterator_state iterator_state;
13  
14 static int module_initialized = 0;
15 iterator_state **STATES;
16 int STATES_size;
17  
18 1635 void _initialize()
19 {
20 int i;
21 1635 ▶ if (module_initialized) return;
Branch: 1 branch
BranchTrueFalseTotal
if (module_initialized) return; 1633 2 1635
22 2 STATES = malloc(STATES_INITIAL_SIZE*sizeof(iterator_state *));
23 2 STATES_size = STATES_INITIAL_SIZE;
24 22 ▶ for (i=0; i<STATES_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<STATES_size; i++) { 20 2 22
25 20 STATES[i] = (iterator_state*) 0;
26 }
27 2 module_initialized = 1;
28 }
29  
30 4 void _resize_STATES()
31 {
32 int i;
33 4 int new_size = STATES_size * 2;
34 4 iterator_state **new_STATES = malloc(new_size*sizeof(iterator_state*));
35 154 ▶ for (i=0; i<STATES_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<STATES_size; i++) { 150 4 154
36 150 new_STATES[i] = STATES[i];
37 }
38 154 ▶ for (; i<new_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i<new_size; i++) { 150 4 154
39 150 new_STATES[i] = (iterator_state*) 0;
40 }
41 4 free(STATES);
42 4 STATES = new_STATES;
43 4 STATES_size = new_size;
44 4 }
45  
46 817 int save_iterator_state(SV* hvref)
47 {
48 int i;
49 817 ▶ if (hvref == (SV*) 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hvref == (SV*) 0) { 0 817 817
50 0 warn("Hash::SafeKeys::save_iterator_state: null input!");
51 0 return -1;
52 }
53 817 HV* hv = (HV*) SvRV(hvref);
54 817 ▶ if (hv == (HV*) 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv == (HV*) 0) { 0 817 817
55 0 warn("Hash::SafeKeys::save_iterator_state: null input!");
56 0 return -1;
57 }
58 817 iterator_state *state = malloc(sizeof(iterator_state));
59 817 _initialize();
60  
61 5867 ▶ for (i=0; i<STATES_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<STATES_size; i++) { 5863 4 5867
62 5863 ▶ if (STATES[i] == (iterator_state*) 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (STATES[i] == (iterator_state*) 0) { 813 5050 5863
63 813 break;
64 }
65 }
66 817 ▶ if (i >= STATES_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= STATES_size) { 4 813 817
67 4 i = STATES_size;
68 4 _resize_STATES();
69 }
70  
71 817 state->riter = HvRITER(hv);
72 817 state->eiter = HvEITER(hv);
73 817 STATES[i] = state;
74 817 hv_iterinit(hv);
75 817 return i;
76 }
77  
78 818 int restore_iterator_state(SV* hvref, int i)
79 {
80 818 ▶ if (hvref == (SV*) 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hvref == (SV*) 0) { 0 818 818
81 0 warn("Hash::SafeKeys::restore_iterator_state: null input");
82 0 return 0;
83 }
84 818 HV* hv = (HV*) SvRV(hvref);
85 818 ▶ if (hv == (HV*) 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv == (HV*) 0) { 0 818 818
86 0 warn("Hash::SafeKeys::restore_iterator_state: null input");
87 0 return 0;
88 }
89 818 _initialize();
90 818 ▶ if (i < 0 || i >= STATES_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (i < 0 || i >= STATES_size) { 818 0 818
if (i < 0 || i >= STATES_size) { 0 818 818
91 0 warn("Hash::SafeKeys::restore_iterator_state: "
92 "invalid restore key %d", i);
93 0 return 0;
94 }
95 818 iterator_state *state = STATES[i];
96 818 ▶ if (state != (iterator_state*) 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (state != (iterator_state*) 0) { 817 1 818
97 817 HvRITER(hv) = state->riter;
98 817 HvEITER(hv) = state->eiter;
99 817 free(state);
100 817 STATES[i] = (iterator_state*) 0;
101 817 return 1;
102 }
103 1 warn("Hash::SafeKeys::restore_iterator_state: "
104 "operation failed for key %d", i);
105 1 STATES[i] = (iterator_state*) 0;
106 1 return 0;
107 }
108  
109  
110 MODULE = Hash::SafeKeys PACKAGE = Hash::SafeKeys
111  
112 PROTOTYPES: DISABLE
113  
114 int
115 save_iterator_state (hvref)
116 SV * hvref
117  
118 int
119 restore_iterator_state (hvref, i)
120 SV * hvref
121 int i
122  
« blib/lib/Hash/SafeKeys.pm ↑ Summary