1 typedef struct ptable_ent {
2 struct ptable_ent *next;
7 typedef struct ptable {
13 #ifndef PTABLE_VAL_FREE
14 # define PTABLE_VAL_FREE(V)
17 STATIC ptable *ptable_new(void) {
18 ptable *t = PerlMemShared_malloc(sizeof *t);
21 t->ary = PerlMemShared_calloc(t->max + 1, sizeof *t->ary);
25 #define PTABLE_HASH(ptr) \
26 ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
28 STATIC ptable_ent *ptable_find(const ptable * const t, const void * const key) {
30 const UV hash = PTABLE_HASH(key);
32 ent = t->ary[hash & t->max];
33 for (; ent; ent = ent->next) {
41 STATIC void *ptable_fetch(const ptable * const t, const void * const key) {
42 const ptable_ent *const ent = ptable_find(t, key);
44 return ent ? ent->val : NULL;
47 STATIC void ptable_split(ptable * const t) {
48 ptable_ent **ary = t->ary;
49 const UV oldsize = t->max + 1;
50 UV newsize = oldsize * 2;
53 ary = PerlMemShared_realloc(ary, newsize * sizeof(*ary));
54 Zero(&ary[oldsize], newsize - oldsize, sizeof(*ary));
58 for (i = 0; i < oldsize; i++, ary++) {
59 ptable_ent **curentp, **entp, *ent;
62 curentp = ary + oldsize;
63 for (entp = ary, ent = *ary; ent; ent = *entp) {
64 if ((newsize & PTABLE_HASH(ent->key)) != i) {
75 STATIC void ptable_store(ptable * const t, const void * const key, void * const val) {
76 ptable_ent *ent = ptable_find(t, key);
79 void *oldval = ent->val;
80 PTABLE_VAL_FREE(oldval);
83 const UV i = PTABLE_HASH(key) & t->max;
84 ent = PerlMemShared_malloc(sizeof *ent);
87 ent->next = t->ary[i];
90 if (ent->next && t->items > t->max)
97 STATIC void ptable_clear(ptable * const t) {
99 register ptable_ent ** const array = t->ary;
103 ptable_ent *entry = array[i];
105 ptable_ent * const oentry = entry;
106 void *val = oentry->val;
108 PTABLE_VAL_FREE(val);
109 PerlMemShared_free(entry);
118 STATIC void ptable_free(ptable * const t) {
122 PerlMemShared_free(t->ary);
123 PerlMemShared_free(t);