1 /* This is a pointer table implementation essentially copied from the ptr_table
2 * implementation in perl's sv.c, except that it has been modified to use memory
3 * shared across threads. */
5 /* This header is designed to be included several times with different
6 * definitions for PTABLE_NAME and PTABLE_VAL_FREE(). */
13 /* Context for PerlMemShared_* functions */
15 #ifdef PERL_IMPLICIT_SYS
17 # define pPTBLMS_ pTHX_
19 # define aPTBLMS_ aTHX_
28 # define pPTBL pPTBLMS
31 # define pPTBL_ pPTBLMS_
34 # define aPTBL aPTBLMS
37 # define aPTBL_ aPTBLMS_
41 # define PTABLE_NAME ptable
44 #ifndef PTABLE_VAL_FREE
45 # define PTABLE_VAL_FREE(V)
49 # define PTABLE_PASTE(A, B) A ## B
50 # define PTABLE_JOIN(A, B) PTABLE_PASTE(A, B)
54 # define PTABLE_PREFIX(X) PTABLE_JOIN(PTABLE_NAME, X)
58 typedef struct ptable_ent {
59 struct ptable_ent *next;
63 #define ptable_ent ptable_ent
64 #endif /* !ptable_ent */
67 typedef struct ptable {
76 STATIC ptable *ptable_new(pPTBLMS) {
77 #define ptable_new() ptable_new(aPTBLMS)
78 ptable *t = (ptable *)PerlMemShared_malloc(sizeof *t);
81 t->ary = (ptable_ent **)PerlMemShared_calloc(t->max + 1, sizeof *t->ary);
84 #endif /* !ptable_new */
87 # define PTABLE_HASH(ptr) \
88 ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
92 STATIC ptable_ent *ptable_find(const ptable * const t, const void * const key) {
93 #define ptable_find ptable_find
95 const UV hash = PTABLE_HASH(key);
97 ent = t->ary[hash & t->max];
98 for (; ent; ent = ent->next) {
105 #endif /* !ptable_find */
108 STATIC void *ptable_fetch(const ptable * const t, const void * const key) {
109 #define ptable_fetch ptable_fetch
110 const ptable_ent *const ent = ptable_find(t, key);
112 return ent ? ent->val : NULL;
114 #endif /* !ptable_fetch */
117 STATIC void ptable_split(pPTBLMS_ ptable * const t) {
118 #define ptable_split(T) ptable_split(aPTBLMS_ (T))
119 ptable_ent **ary = t->ary;
120 const UV oldsize = t->max + 1;
121 UV newsize = oldsize * 2;
124 ary = (ptable_ent **)PerlMemShared_realloc(ary, newsize * sizeof(*ary));
125 Zero(&ary[oldsize], newsize - oldsize, sizeof(*ary));
129 for (i = 0; i < oldsize; i++, ary++) {
130 ptable_ent **curentp, **entp, *ent;
133 curentp = ary + oldsize;
134 for (entp = ary, ent = *ary; ent; ent = *entp) {
135 if ((newsize & PTABLE_HASH(ent->key)) != i) {
137 ent->next = *curentp;
145 #endif /* !ptable_split */
147 STATIC void PTABLE_PREFIX(_store)(pPTBL_ ptable * const t, const void * const key, void * const val) {
148 ptable_ent *ent = ptable_find(t, key);
151 void *oldval = ent->val;
152 PTABLE_VAL_FREE(oldval);
155 const UV i = PTABLE_HASH(key) & t->max;
156 ent = (ptable_ent *)PerlMemShared_malloc(sizeof *ent);
159 ent->next = t->ary[i];
162 if (ent->next && t->items > t->max)
168 STATIC void ptable_walk(pTHX_ ptable * const t, void (*cb)(pTHX_ ptable_ent *ent, void *userdata), void *userdata) {
169 #define ptable_walk(T, CB, UD) ptable_walk(aTHX_ (T), (CB), (UD))
171 register ptable_ent ** const array = t->ary;
175 for (entry = array[i]; entry; entry = entry->next)
176 cb(aTHX_ entry, userdata);
180 #endif /* !ptable_walk */
182 STATIC void PTABLE_PREFIX(_clear)(pPTBL_ ptable * const t) {
184 register ptable_ent ** const array = t->ary;
188 ptable_ent *entry = array[i];
190 ptable_ent * const oentry = entry;
191 void *val = oentry->val;
193 PTABLE_VAL_FREE(val);
194 PerlMemShared_free(oentry);
203 STATIC void PTABLE_PREFIX(_free)(pPTBL_ ptable * const t) {
206 PTABLE_PREFIX(_clear)(aPTBL_ t);
207 PerlMemShared_free(t->ary);
208 PerlMemShared_free(t);
217 #undef PTABLE_VAL_FREE