ptable.h

filter: Statementstmt 20.2% 14 / 69 Branchbran 11.1% 4 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 17.1% 18 / 105 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« PPAddr.xs ↑ Summary blib/lib/B/Hooks/OP/PPAddr.pm »
1 /*
2 * This is copied more or less verbatim from the pointer table implementation in sv.c
3 */
4  
5 #include "ppport.h"
6  
7 #define PTABLE_HASH(ptr) ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
8  
9 struct PTABLE_entry {
10 struct PTABLE_entry *next;
11 void *key;
12 void *value;
13 };
14  
15 struct PTABLE {
16 struct PTABLE_entry **tbl_ary;
17 UV tbl_max;
18 UV tbl_items;
19 };
20  
21 typedef struct PTABLE_entry PTABLE_ENTRY_t;
22 typedef struct PTABLE PTABLE_t;
23  
24 static PTABLE_t * PTABLE_new(void);
25 static PTABLE_ENTRY_t * PTABLE_find(PTABLE_t *tbl, const void *key);
26 static void * PTABLE_fetch(PTABLE_t *tbl, const void *key);
27 static void PTABLE_store(PTABLE_t *tbl, void *key, void *value);
28 static void PTABLE_grow(PTABLE_t *tbl);
29 static void PTABLE_clear(PTABLE_t *tbl);
30 static void PTABLE_free(PTABLE_t *tbl);
31  
32 /* create a new pointer => pointer table */
33  
34 static PTABLE_t *
35 1 PTABLE_new(void)
36 {
37 PTABLE_t *tbl;
38 1 Newxz(tbl, 1, PTABLE_t);
39 1 tbl->tbl_max = 511;
40 1 tbl->tbl_items = 0;
41 1 ▶ Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTABLE_ENTRY_t*);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTABLE_ENTRY_t*); 0 1 1
42 1 return tbl;
43 }
44  
45 /* map an existing pointer using a table */
46  
47 static PTABLE_ENTRY_t *
48 0 PTABLE_find(PTABLE_t *tbl, const void *key) {
49 PTABLE_ENTRY_t *tblent;
50 0 const UV hash = PTABLE_HASH(key);
51 0 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
52 0 ▶ for (; tblent; tblent = tblent->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (; tblent; tblent = tblent->next) { 0 0 0
53 0 ▶ if (tblent->key == key)
Branch: 1 branch
BranchTrueFalseTotal
if (tblent->key == key) 0 0 0
54 0 return tblent;
55 }
56 0 return NULL;
57 }
58  
59 static void *
60 0 PTABLE_fetch(PTABLE_t *tbl, const void *key)
61 {
62 0 PTABLE_ENTRY_t const *const tblent = PTABLE_find(tbl, key);
63 0 ▶ return tblent ? tblent->value : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return tblent ? tblent->value : NULL; 0 0 0
64 }
65  
66 /* add a new entry to a pointer => pointer table */
67  
68 static void
69 0 PTABLE_store(PTABLE_t *tbl, void *key, void *value)
70 {
71 0 PTABLE_ENTRY_t *tblent = PTABLE_find(tbl, key);
72  
73 0 ▶ if (tblent) {
Branch: 1 branch
BranchTrueFalseTotal
if (tblent) { 0 0 0
74 0 tblent->value = value;
75 } else {
76 0 const UV entry = PTABLE_HASH(key) & tbl->tbl_max;
77 0 Newx(tblent, 1, PTABLE_ENTRY_t);
78  
79 0 tblent->key = key;
80 0 tblent->value = value;
81 0 tblent->next = tbl->tbl_ary[entry];
82 0 tbl->tbl_ary[entry] = tblent;
83 0 tbl->tbl_items++;
84 0 ▶ if (tblent->next && (tbl->tbl_items > tbl->tbl_max))
Branch: 2 branches
BranchTrueFalseTotal
if (tblent->next && (tbl->tbl_items > tbl->tbl_max)) 0 0 0
if (tblent->next && (tbl->tbl_items > tbl->tbl_max)) 0 0 0
85 0 PTABLE_grow(tbl);
86 }
87 0 }
88  
89 /* double the hash bucket size of an existing ptr table */
90  
91 static void
92 0 PTABLE_grow(PTABLE_t *tbl)
93 {
94 0 PTABLE_ENTRY_t **ary = tbl->tbl_ary;
95 0 const UV oldsize = tbl->tbl_max + 1;
96 0 UV newsize = oldsize * 2;
97 UV i;
98  
99 0 ▶ Renew(ary, newsize, PTABLE_ENTRY_t*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(ary, newsize, PTABLE_ENTRY_t*); 0 0 0
100 0 ▶ Zero(&ary[oldsize], newsize - oldsize, PTABLE_ENTRY_t*);
Branch: 1 branch
BranchTrueFalseTotal
Zero(&ary[oldsize], newsize - oldsize, PTABLE_ENTRY_t*); 0 0 0
101 0 tbl->tbl_max = --newsize;
102 0 tbl->tbl_ary = ary;
103  
104 0 ▶ for (i = 0; i < oldsize; i++, ary++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < oldsize; i++, ary++) { 0 0 0
105 PTABLE_ENTRY_t **curentp, **entp, *ent;
106 0 ▶ if (!*ary)
Branch: 1 branch
BranchTrueFalseTotal
if (!*ary) 0 0 0
107 0 continue;
108 0 curentp = ary + oldsize;
109 0 ▶ for (entp = ary, ent = *ary; ent; ent = *entp) {
Branch: 1 branch
BranchTrueFalseTotal
for (entp = ary, ent = *ary; ent; ent = *entp) { 0 0 0
110 0 ▶ if ((newsize & PTABLE_HASH(ent->key)) != i) {
Branch: 1 branch
BranchTrueFalseTotal
if ((newsize & PTABLE_HASH(ent->key)) != i) { 0 0 0
111 0 *entp = ent->next;
112 0 ent->next = *curentp;
113 0 *curentp = ent;
114 0 continue;
115 } else {
116 0 entp = &ent->next;
117 }
118 }
119 }
120 0 }
121  
122 /* remove all the entries from a ptr table */
123  
124 static void
125 1 PTABLE_clear(PTABLE_t *tbl)
126 {
127 1 ▶ if (tbl && tbl->tbl_items) {
Branch: 2 branches
BranchTrueFalseTotal
if (tbl && tbl->tbl_items) { 1 0 1
if (tbl && tbl->tbl_items) { 0 1 1
128 0 register PTABLE_ENTRY_t * * const array = tbl->tbl_ary;
129 0 UV riter = tbl->tbl_max;
130  
131 do {
132 0 PTABLE_ENTRY_t *entry = array[riter];
133  
134 0 ▶ while (entry) {
Branch: 1 branch
BranchTrueFalseTotal
while (entry) { 0 0 0
135 0 PTABLE_ENTRY_t * const oentry = entry;
136 0 entry = entry->next;
137 0 Safefree(oentry);
138 }
139  
140 /* chocolateboy 2008-01-08
141 *
142 * make sure we clear the array entry, so that subsequent probes fail
143 */
144  
145 0 array[riter] = NULL;
146 0 ▶ } while (riter--);
Branch: 1 branch
BranchTrueFalseTotal
} while (riter--); 0 0 0
147  
148 0 tbl->tbl_items = 0;
149 }
150 1 }
151  
152 /* clear and free a ptr table */
153  
154 static void
155 1 PTABLE_free(PTABLE_t *tbl)
156 {
157 1 ▶ if (!tbl) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tbl) { 0 1 1
158 0 return;
159 }
160 1 PTABLE_clear(tbl);
161 1 Safefree(tbl->tbl_ary);
162 1 Safefree(tbl);
163 }
« PPAddr.xs ↑ Summary blib/lib/B/Hooks/OP/PPAddr.pm »