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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2000-2009. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_fun.h"
#include "hash.h"
static Hash erts_fun_table;
#include "erl_smp.h"
static erts_smp_rwmtx_t erts_fun_table_lock;
#define erts_fun_read_lock() erts_smp_rwmtx_rlock(&erts_fun_table_lock)
#define erts_fun_read_unlock() erts_smp_rwmtx_runlock(&erts_fun_table_lock)
#define erts_fun_write_lock() erts_smp_rwmtx_rwlock(&erts_fun_table_lock)
#define erts_fun_write_unlock() erts_smp_rwmtx_rwunlock(&erts_fun_table_lock)
#define erts_fun_init_lock() erts_smp_rwmtx_init(&erts_fun_table_lock, \
"fun_tab")
static HashValue fun_hash(ErlFunEntry* obj);
static int fun_cmp(ErlFunEntry* obj1, ErlFunEntry* obj2);
static ErlFunEntry* fun_alloc(ErlFunEntry* template);
static void fun_free(ErlFunEntry* obj);
/*
* The address field of every fun that has no loaded code will point
* to unloaded_fun[]. The -1 in unloaded_fun[0] will be interpreted
* as an illegal arity when attempting to call a fun.
*/
static BeamInstr unloaded_fun_code[3] = {NIL, -1, 0};
static BeamInstr* unloaded_fun = unloaded_fun_code + 2;
void
erts_init_fun_table(void)
{
HashFunctions f;
erts_fun_init_lock();
f.hash = (H_FUN) fun_hash;
f.cmp = (HCMP_FUN) fun_cmp;
f.alloc = (HALLOC_FUN) fun_alloc;
f.free = (HFREE_FUN) fun_free;
hash_init(ERTS_ALC_T_FUN_TABLE, &erts_fun_table, "fun_table", 16, f);
}
void
erts_fun_info(int to, void *to_arg)
{
int lock = !ERTS_IS_CRASH_DUMPING;
if (lock)
erts_fun_read_lock();
hash_info(to, to_arg, &erts_fun_table);
if (lock)
erts_fun_read_unlock();
}
int erts_fun_table_sz(void)
{
int sz;
int lock = !ERTS_IS_CRASH_DUMPING;
if (lock)
erts_fun_read_lock();
sz = hash_table_sz(&erts_fun_table);
if (lock)
erts_fun_read_unlock();
return sz;
}
ErlFunEntry*
erts_put_fun_entry(Eterm mod, int uniq, int index)
{
ErlFunEntry template;
ErlFunEntry* fe;
long refc;
ASSERT(is_atom(mod));
template.old_uniq = uniq;
template.old_index = index;
template.module = mod;
erts_fun_write_lock();
fe = (ErlFunEntry *) hash_put(&erts_fun_table, (void*) &template);
sys_memset(fe->uniq, 0, sizeof(fe->uniq));
fe->index = 0;
refc = erts_refc_inctest(&fe->refc, 0);
if (refc < 2) /* New or pending delete */
erts_refc_inc(&fe->refc, 1);
erts_fun_write_unlock();
return fe;
}
ErlFunEntry*
erts_put_fun_entry2(Eterm mod, int old_uniq, int old_index,
byte* uniq, int index, int arity)
{
ErlFunEntry template;
ErlFunEntry* fe;
long refc;
ASSERT(is_atom(mod));
template.old_uniq = old_uniq;
template.old_index = old_index;
template.module = mod;
erts_fun_write_lock();
fe = (ErlFunEntry *) hash_put(&erts_fun_table, (void*) &template);
sys_memcpy(fe->uniq, uniq, sizeof(fe->uniq));
fe->index = index;
fe->arity = arity;
refc = erts_refc_inctest(&fe->refc, 0);
if (refc < 2) /* New or pending delete */
erts_refc_inc(&fe->refc, 1);
erts_fun_write_unlock();
return fe;
}
struct my_key {
Eterm mod;
byte* uniq;
int index;
ErlFunEntry* fe;
};
ErlFunEntry*
erts_get_fun_entry(Eterm mod, int uniq, int index)
{
ErlFunEntry template;
ErlFunEntry *ret;
ASSERT(is_atom(mod));
template.old_uniq = uniq;
template.old_index = index;
template.module = mod;
erts_fun_read_lock();
ret = (ErlFunEntry *) hash_get(&erts_fun_table, (void*) &template);
if (ret) {
long refc = erts_refc_inctest(&ret->refc, 1);
if (refc < 2) /* Pending delete */
erts_refc_inc(&ret->refc, 1);
}
erts_fun_read_unlock();
return ret;
}
static void
erts_erase_fun_entry_unlocked(ErlFunEntry* fe)
{
hash_erase(&erts_fun_table, (void *) fe);
}
void
erts_erase_fun_entry(ErlFunEntry* fe)
{
erts_fun_write_lock();
#ifdef ERTS_SMP
/*
* We have to check refc again since someone might have looked up
* the fun entry and incremented refc after last check.
*/
if (erts_refc_dectest(&fe->refc, -1) <= 0)
#endif
{
if (fe->address != unloaded_fun)
erl_exit(1,
"Internal error: "
"Invalid reference count found on #Fun<%T.%d.%d>: "
" About to erase fun still referred by code.\n",
fe->module, fe->old_index, fe->old_uniq);
erts_erase_fun_entry_unlocked(fe);
}
erts_fun_write_unlock();
}
#ifndef HYBRID /* FIND ME! */
void
erts_cleanup_funs(ErlFunThing* funp)
{
while (funp) {
ErlFunEntry* fe = funp->fe;
if (erts_refc_dectest(&fe->refc, 0) == 0) {
erts_erase_fun_entry(fe);
}
funp = funp->next;
}
}
#endif
void
erts_cleanup_funs_on_purge(BeamInstr* start, BeamInstr* end)
{
int limit;
HashBucket** bucket;
ErlFunEntry* to_delete = NULL;
int i;
erts_fun_write_lock();
limit = erts_fun_table.size;
bucket = erts_fun_table.bucket;
for (i = 0; i < limit; i++) {
HashBucket* b = bucket[i];
while (b) {
ErlFunEntry* fe = (ErlFunEntry *) b;
BeamInstr* addr = fe->address;
if (start <= addr && addr < end) {
fe->address = unloaded_fun;
if (erts_refc_dectest(&fe->refc, 0) == 0) {
fe->address = (void *) to_delete;
to_delete = fe;
}
}
b = b->next;
}
}
while (to_delete != NULL) {
ErlFunEntry* next = (ErlFunEntry *) to_delete->address;
erts_erase_fun_entry_unlocked(to_delete);
to_delete = next;
}
erts_fun_write_unlock();
}
void
erts_dump_fun_entries(int to, void *to_arg)
{
int limit;
HashBucket** bucket;
int i;
int lock = !ERTS_IS_CRASH_DUMPING;
if (lock)
erts_fun_read_lock();
limit = erts_fun_table.size;
bucket = erts_fun_table.bucket;
for (i = 0; i < limit; i++) {
HashBucket* b = bucket[i];
while (b) {
ErlFunEntry* fe = (ErlFunEntry *) b;
erts_print(to, to_arg, "=fun\n");
erts_print(to, to_arg, "Module: %T\n", fe->module);
erts_print(to, to_arg, "Uniq: %d\n", fe->old_uniq);
erts_print(to, to_arg, "Index: %d\n",fe->old_index);
erts_print(to, to_arg, "Address: %p\n", fe->address);
#ifdef HIPE
erts_print(to, to_arg, "Native_address: %p\n", fe->native_address);
#endif
erts_print(to, to_arg, "Refc: %d\n", erts_refc_read(&fe->refc, 1));
b = b->next;
}
}
if (lock)
erts_fun_read_unlock();
}
static HashValue
fun_hash(ErlFunEntry* obj)
{
return (HashValue) (obj->old_uniq ^ obj->old_index ^ atom_val(obj->module));
}
static int
fun_cmp(ErlFunEntry* obj1, ErlFunEntry* obj2)
{
return !(obj1->module == obj2->module &&
obj1->old_uniq == obj2->old_uniq &&
obj1->old_index == obj2->old_index);
}
static ErlFunEntry*
fun_alloc(ErlFunEntry* template)
{
ErlFunEntry* obj = (ErlFunEntry *) erts_alloc(ERTS_ALC_T_FUN_ENTRY,
sizeof(ErlFunEntry));
obj->old_uniq = template->old_uniq;
obj->old_index = template->old_index;
obj->module = template->module;
erts_refc_init(&obj->refc, -1);
obj->address = unloaded_fun;
#ifdef HIPE
obj->native_address = NULL;
#endif
return obj;
}
static void
fun_free(ErlFunEntry* obj)
{
erts_free(ERTS_ALC_T_FUN_ENTRY, (void *) obj);
}
|