-
Notifications
You must be signed in to change notification settings - Fork 3
/
ptr_table.h
185 lines (152 loc) · 4.69 KB
/
ptr_table.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "ppport.h"
#if 0
#define ptr_table_new() (Perl_ptr_table(aTHX))
#define ptr_table_find(tbl, sv) (ptr_table_find(tbl, sv))
#define ptr_table_fetch(tbl, sv) (ptr_table_fetch(tbl, sv))
#define ptr_table_store(tbl, oldsv, newsv) (ptr_table_store(tbl, oldsv, newsv))
#define ptr_table_split(tbl) (ptr_table_split(tbl))
#define ptr_table_free(tbl) (ptr_table_free(tbl))
#else
#define ptr_table_new() (my_ptr_table_new(aTHX))
#define ptr_table_find(tbl, sv) (my_ptr_table_find(aTHX_ tbl, sv))
#define ptr_table_fetch(tbl, sv) (my_ptr_table_fetch(aTHX_ tbl, sv))
#define ptr_table_store(tbl, oldsv, newsv) (my_ptr_table_store(aTHX_ tbl, oldsv, newsv))
#define ptr_table_split(tbl) (my_ptr_table_split(aTHX_ tbl))
#define ptr_table_free(tbl) (my_ptr_table_free(aTHX_ tbl))
typedef struct my_ptr_tbl_ent MY_PTR_TBL_ENT_t;
typedef struct my_ptr_tbl MY_PTR_TBL_t;
struct my_ptr_tbl_ent {
struct my_ptr_tbl_ent* next;
const void* oldval;
void* newval;
};
struct my_ptr_tbl {
struct my_ptr_tbl_ent** tbl_ary;
UV tbl_max;
UV tbl_items;
struct my_ptr_tbl_arena *tbl_arena;
struct my_ptr_tbl_ent *tbl_arena_next;
struct my_ptr_tbl_ent *tbl_arena_end;
};
struct my_ptr_tbl_arena {
struct my_ptr_tbl_arena *next;
struct my_ptr_tbl_ent array[1023/3]; /* as ptr_tbl_ent has 3 pointers. */
};
/* create a new pointer-mapping table */
static MY_PTR_TBL_t *
my_ptr_table_new(pTHX)
{
MY_PTR_TBL_t *tbl;
PERL_UNUSED_CONTEXT;
Newx(tbl, 1, MY_PTR_TBL_t);
tbl->tbl_max = 511;
tbl->tbl_items = 0;
tbl->tbl_arena = NULL;
tbl->tbl_arena_next = NULL;
tbl->tbl_arena_end = NULL;
Newxz(tbl->tbl_ary, tbl->tbl_max + 1, MY_PTR_TBL_ENT_t*);
return tbl;
}
#define MY_PTR_TABLE_HASH(ptr) \
((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
/* map an existing pointer using a table */
static MY_PTR_TBL_ENT_t *
my_ptr_table_find(MY_PTR_TBL_t *const tbl, const void *const sv)
{
MY_PTR_TBL_ENT_t *tblent;
const UV hash = MY_PTR_TABLE_HASH(sv);
tblent = tbl->tbl_ary[hash & tbl->tbl_max];
for (; tblent; tblent = tblent->next) {
if (tblent->oldval == sv)
return tblent;
}
return NULL;
}
static void *
my_ptr_table_fetch(pTHX_ MY_PTR_TBL_t *const tbl, const void *const sv)
{
MY_PTR_TBL_ENT_t const *const tblent = my_ptr_table_find(tbl, sv);
PERL_UNUSED_CONTEXT;
return tblent ? tblent->newval : NULL;
}
/* double the hash bucket size of an existing ptr table */
static void
my_ptr_table_split(pTHX_ MY_PTR_TBL_t *const tbl)
{
MY_PTR_TBL_ENT_t **ary = tbl->tbl_ary;
const UV oldsize = tbl->tbl_max + 1;
UV newsize = oldsize * 2;
UV i;
PERL_UNUSED_CONTEXT;
Renew(ary, newsize, MY_PTR_TBL_ENT_t*);
Zero(&ary[oldsize], newsize-oldsize, MY_PTR_TBL_ENT_t*);
tbl->tbl_max = --newsize;
tbl->tbl_ary = ary;
for (i=0; i < oldsize; i++, ary++) {
MY_PTR_TBL_ENT_t **entp = ary;
MY_PTR_TBL_ENT_t *ent = *ary;
MY_PTR_TBL_ENT_t **curentp;
if (!ent)
continue;
curentp = ary + oldsize;
do {
if ((newsize & MY_PTR_TABLE_HASH(ent->oldval)) != i) {
*entp = ent->next;
ent->next = *curentp;
*curentp = ent;
}
else
entp = &ent->next;
ent = *entp;
} while (ent);
}
}
/* add a new entry to a pointer-mapping table */
static void
my_ptr_table_store(pTHX_ MY_PTR_TBL_t *const tbl, const void *const oldsv, void *const newsv)
{
MY_PTR_TBL_ENT_t *tblent = my_ptr_table_find(tbl, oldsv);
PERL_UNUSED_CONTEXT;
if (tblent) {
tblent->newval = newsv;
} else {
const UV entry = MY_PTR_TABLE_HASH(oldsv) & tbl->tbl_max;
if (tbl->tbl_arena_next == tbl->tbl_arena_end) {
struct my_ptr_tbl_arena *new_arena;
Newx(new_arena, 1, struct my_ptr_tbl_arena);
new_arena->next = tbl->tbl_arena;
tbl->tbl_arena = new_arena;
tbl->tbl_arena_next = new_arena->array;
tbl->tbl_arena_end = new_arena->array
+ sizeof(new_arena->array) / sizeof(new_arena->array[0]);
}
tblent = tbl->tbl_arena_next++;
tblent->oldval = oldsv;
tblent->newval = newsv;
tblent->next = tbl->tbl_ary[entry];
tbl->tbl_ary[entry] = tblent;
tbl->tbl_items++;
if (tblent->next && tbl->tbl_items > tbl->tbl_max)
my_ptr_table_split(tbl);
}
}
/* clear and free a ptr table */
static void
my_ptr_table_free(pTHX_ MY_PTR_TBL_t *const tbl)
{
struct my_ptr_tbl_arena *arena;
if (!tbl) {
return;
}
arena = tbl->tbl_arena;
while (arena) {
struct my_ptr_tbl_arena *next = arena->next;
Safefree(arena);
arena = next;
}
Safefree(tbl->tbl_ary);
Safefree(tbl);
}
#define PTR_TBL_ENT_t MY_PTR_TBL_ENT_t
#define PTR_TBL_t MY_PTR_TBL_t
#endif