/
global_cache.cpp
213 lines (175 loc) · 6.29 KB
/
global_cache.cpp
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
#include "vm.hpp"
#include "global_cache.hpp"
#include "object_position.hpp"
#include "objectmemory.hpp"
#include "lookup_data.hpp"
#include "dispatch.hpp"
#include "builtin/class.hpp"
#include "builtin/module.hpp"
#include "builtin/object.hpp"
#include "builtin/methodtable.hpp"
#include "builtin/alias.hpp"
#include "builtin/cache.hpp"
namespace rubinius {
static bool hierarchy_resolve(STATE, Symbol* name, Dispatch& msg, LookupData& lookup,
Symbol** visibility)
{
Module* module = lookup.from;
MethodTableBucket* entry;
bool skip_vis_check = false;
do {
entry = module->method_table()->find_entry(state, name);
/* Nothing, there? Ok, keep looking. */
if(!entry) goto keep_looking;
/* A 'false' method means to terminate method lookup.
* (eg. undef_method) */
if(entry->undef_p(state)) return false;
/* If this was a private send, then we can handle use
* any method seen. */
if(lookup.min_visibility == G(sym_private) || skip_vis_check) {
/* nil means that the actual method object is 'up' from here */
if(entry->method()->nil_p()) goto keep_looking;
*visibility = entry->visibility();
if(Alias* alias = try_as<Alias>(entry->method())) {
msg.method = alias->original_exec();
msg.module = alias->original_module();
} else {
msg.method = entry->method();
msg.module = module;
}
break;
} else {
/* The method is private, but this wasn't a private send. */
if(entry->private_p(state)) {
return false;
} else if(entry->protected_p(state)) {
/* The method is protected, but it's not being called from
* the same module, or we only want public methods. */
if(lookup.min_visibility == G(sym_public) || !lookup.recv->kind_of_p(state, module)) {
return false;
}
}
/* The method was callable, but we need to keep looking
* for the implementation, so make the invocation bypass all further
* visibility checks */
if(entry->method()->nil_p()) {
skip_vis_check = true;
goto keep_looking;
}
if(Alias* alias = try_as<Alias>(entry->method())) {
msg.method = alias->original_exec();
msg.module = alias->original_module();
} else {
msg.method = entry->method();
msg.module = module;
}
*visibility = entry->visibility();
break;
}
keep_looking:
module = module->superclass();
/* No more places to look, we couldn't find it. */
if(module->nil_p()) return false;
} while(1);
return true;
}
MethodCacheEntry* GlobalCache::lookup_public(STATE, Module* mod, Class* cls, Symbol* name) {
utilities::thread::SpinLock::LockGuard guard(lock_);
Symbol* entry_name = entry_names[CPU_CACHE_HASH(mod, name)];
CacheEntry* entry = entries + CPU_CACHE_HASH(mod, name);
if(entry_name == name &&
entry->klass == mod &&
entry->visibility != G(sym_private) &&
!entry->method_missing) {
return MethodCacheEntry::create(state, cls, entry->module,
entry->method, eNone, false);
}
return NULL;
}
MethodCacheEntry* GlobalCache::lookup_private(STATE, Module* mod, Class* cls, Symbol* name) {
utilities::thread::SpinLock::LockGuard guard(lock_);
Symbol* entry_name = entry_names[CPU_CACHE_HASH(mod, name)];
CacheEntry* entry = entries + CPU_CACHE_HASH(mod, name);
if(entry_name == name &&
entry->klass == mod &&
!entry->method_missing) {
return MethodCacheEntry::create(state, cls, entry->module,
entry->method, eNone, false);
}
return NULL;
}
bool GlobalCache::resolve(STATE, Symbol* name, Dispatch& msg, LookupData& lookup) {
return state->vm()->global_cache()->resolve_i(state, name, msg, lookup);
}
bool GlobalCache::resolve_i(STATE, Symbol* name, Dispatch& msg, LookupData& lookup) {
utilities::thread::SpinLock::LockGuard guard(lock_);
Module* klass = lookup.from;
CacheEntry* entry = this->lookup(state, klass, name);
if(entry) {
if(lookup.min_visibility == G(sym_private) || entry->visibility == G(sym_public) || lookup.min_visibility == entry->visibility) {
msg.method = entry->method;
msg.module = entry->module;
msg.method_missing = entry->method_missing;
return true;
}
}
Symbol* visibility = G(sym_protected);
if(hierarchy_resolve(state, name, msg, lookup, &visibility)) {
retain_i(state, lookup.from, name,
msg.module, msg.method, msg.method_missing, visibility);
return true;
}
return false;
}
void GlobalCache::prune_young() {
CacheEntry* entry;
for(size_t i = 0; i < CPU_CACHE_SIZE; i++) {
entry = &entries[i];
bool clear = false;
Object* klass = reinterpret_cast<Object*>(entry->klass);
if(!klass) continue;
if(klass->young_object_p()) {
if(klass->forwarded_p()) {
Module* fwd = (Module*)klass->forward();
entry->klass = fwd;
} else {
clear = true;
}
}
Object* mod = reinterpret_cast<Object*>(entry->module);
if(mod->young_object_p()) {
if(mod->forwarded_p()) {
entry->module = (Module*)mod->forward();
} else {
clear = true;
}
}
Object* exec = reinterpret_cast<Object*>(entry->method);
if(exec->young_object_p()) {
if(exec->forwarded_p()) {
entry->method = (Executable*)exec->forward();
} else {
clear = true;
}
}
if(clear) {
entry_names[i] = NULL;
entry->clear();
}
}
}
void GlobalCache::prune_unmarked(int mark) {
CacheEntry* entry;
for(size_t i = 0; i < CPU_CACHE_SIZE; i++) {
entry = &entries[i];
Object* klass = reinterpret_cast<Object*>(entry->klass);
if(!klass) continue;
Object* mod = reinterpret_cast<Object*>(entry->module);
Object* exec = reinterpret_cast<Object*>(entry->method);
if(!klass->marked_p(mark) || !mod->marked_p(mark) || !exec->marked_p(mark)) {
entry_names[i] = NULL;
entry->clear();
}
}
}
}