1 /* This file is part of the re::engine::Plugin Perl module.
2 * See http://search.cpan.org/dist/re-engine-Plugin/ */
4 /* This is a pointer table implementation essentially copied from the ptr_table
5 * implementation in perl's sv.c, except that it has been modified to use memory
6 * shared across threads.
7 * Copyright goes to the original authors, bug reports to me. */
9 /* This header is designed to be included several times with different
10 * definitions for PTABLE_NAME and PTABLE_VAL_FREE(). */
14 # define VOID2(T, P) static_cast<T>(P)
16 # define VOID2(T, P) (P)
24 /* Context for PerlMemShared_* functions */
26 #ifdef PERL_IMPLICIT_SYS
28 # define pPTBLMS_ pTHX_
30 # define aPTBLMS_ aTHX_
39 # define pPTBL pPTBLMS
42 # define pPTBL_ pPTBLMS_
45 # define aPTBL aPTBLMS
48 # define aPTBL_ aPTBLMS_
52 # define PTABLE_NAME ptable
55 #ifndef PTABLE_VAL_FREE
56 # define PTABLE_VAL_FREE(V)
60 # define PTABLE_PASTE(A, B) A ## B
61 # define PTABLE_JOIN(A, B) PTABLE_PASTE(A, B)
65 # define PTABLE_PREFIX(X) PTABLE_JOIN(PTABLE_NAME, X)
69 typedef struct ptable_ent {
70 struct ptable_ent *next;
74 #define ptable_ent ptable_ent
75 #endif /* !ptable_ent */
78 typedef struct ptable {
87 STATIC ptable *ptable_new(pPTBLMS) {
88 #define ptable_new() ptable_new(aPTBLMS)
89 ptable *t = VOID2(ptable *, PerlMemShared_malloc(sizeof *t));
92 t->ary = VOID2(ptable_ent **,
93 PerlMemShared_calloc(t->max + 1, sizeof *t->ary));
96 #endif /* !ptable_new */
99 # define PTABLE_HASH(ptr) \
100 ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
104 STATIC ptable_ent *ptable_find(const ptable * const t, const void * const key) {
105 #define ptable_find ptable_find
107 const UV hash = PTABLE_HASH(key);
109 ent = t->ary[hash & t->max];
110 for (; ent; ent = ent->next) {
117 #endif /* !ptable_find */
120 STATIC void *ptable_fetch(const ptable * const t, const void * const key) {
121 #define ptable_fetch ptable_fetch
122 const ptable_ent *const ent = ptable_find(t, key);
124 return ent ? ent->val : NULL;
126 #endif /* !ptable_fetch */
129 STATIC void ptable_split(pPTBLMS_ ptable * const t) {
130 #define ptable_split(T) ptable_split(aPTBLMS_ (T))
131 ptable_ent **ary = t->ary;
132 const size_t oldsize = t->max + 1;
133 size_t newsize = oldsize * 2;
136 ary = VOID2(ptable_ent **, PerlMemShared_realloc(ary, newsize * sizeof(*ary)));
137 Zero(&ary[oldsize], newsize - oldsize, sizeof(*ary));
141 for (i = 0; i < oldsize; i++, ary++) {
142 ptable_ent **curentp, **entp, *ent;
145 curentp = ary + oldsize;
146 for (entp = ary, ent = *ary; ent; ent = *entp) {
147 if ((newsize & PTABLE_HASH(ent->key)) != i) {
149 ent->next = *curentp;
157 #endif /* !ptable_split */
159 STATIC void PTABLE_PREFIX(_store)(pPTBL_ ptable * const t, const void * const key, void * const val) {
160 ptable_ent *ent = ptable_find(t, key);
163 void *oldval = ent->val;
164 PTABLE_VAL_FREE(oldval);
167 const size_t i = PTABLE_HASH(key) & t->max;
168 ent = VOID2(ptable_ent *, PerlMemShared_malloc(sizeof *ent));
171 ent->next = t->ary[i];
174 if (ent->next && t->items > t->max)
179 STATIC void PTABLE_PREFIX(_delete)(pPTBL_ ptable * const t, const void * const key) {
180 ptable_ent *prev, *ent;
181 const size_t i = PTABLE_HASH(key) & t->max;
185 for (; ent; prev = ent, ent = ent->next) {
192 prev->next = ent->next;
194 t->ary[i] = ent->next;
195 PTABLE_VAL_FREE(ent->val);
196 PerlMemShared_free(ent);
201 STATIC void ptable_walk(pTHX_ ptable * const t, void (*cb)(pTHX_ ptable_ent *ent, void *userdata), void *userdata) {
202 #define ptable_walk(T, CB, UD) ptable_walk(aTHX_ (T), (CB), (UD))
204 register ptable_ent ** const array = t->ary;
208 for (entry = array[i]; entry; entry = entry->next)
210 cb(aTHX_ entry, userdata);
214 #endif /* !ptable_walk */
216 STATIC void PTABLE_PREFIX(_clear)(pPTBL_ ptable * const t) {
218 register ptable_ent ** const array = t->ary;
222 ptable_ent *entry = array[i];
224 ptable_ent * const oentry = entry;
225 void *val = oentry->val;
227 PTABLE_VAL_FREE(val);
228 PerlMemShared_free(oentry);
237 STATIC void PTABLE_PREFIX(_free)(pPTBL_ ptable * const t) {
240 PTABLE_PREFIX(_clear)(aPTBL_ t);
241 PerlMemShared_free(t->ary);
242 PerlMemShared_free(t);
251 #undef PTABLE_VAL_FREE