Repository navigation
Expand file tree
/
Copy pathObjectTable.java
More file actions
390 lines (350 loc) · 12.4 KB
/
Copy pathObjectTable.java
File metadata and controls
390 lines (350 loc) · 12.4 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package com.tns;
import java.lang.ref.WeakReference;
/**
* A runtime's registry of the Java objects its JS wrappers refer to, by object id. An entry
* holds its object strongly while the JS wrapper is reachable and through a WeakReference
* after the wrapper was collected (until the object is collected too).
*
* Any thread may read and findOrRegister: every access synchronizes on the table. Neither
* table allocates per entry.
*
* A monitor rather than java.util.concurrent locks: those live in the boot classpath but
* outside the boot image, so on device they run interpreted, and an app's profile can't
* compile them. Uncontended monitor enter/exit is compiled into every caller.
*/
final class ObjectTable {
static final int NO_ID = -1;
// Set in findOrRegister's result when that call registered the object.
static final long FRESH = 1L << 32;
interface IdSource {
int newId();
}
private static Object deref(Object value) {
return value instanceof WeakRef ? ((WeakRef) value).get() : value;
}
/*
* id -> entry, as a radix tree indexed by id: chunks of leaves of slots. Ids are unsigned
* 32-bit values carried in an int (NO_ID is the one excluded value); they come from a
* monotonic counter and are never reused, so a leaf or chunk that empties is dropped
* unless it is the newest; an id registered after its leaf was dropped re-creates it.
* Leaves and chunks are small, so a long-lived object keeps at most one of each alive.
*/
private static final int LEAF_BITS = 6;
private static final int LEAF_SIZE = 1 << LEAF_BITS;
private static final int LEAF_MASK = LEAF_SIZE - 1;
private static final int CHUNK_BITS = 6;
private static final int CHUNK_MASK = (1 << CHUNK_BITS) - 1;
private static final int CHUNK_SHIFT = LEAF_BITS + CHUNK_BITS;
private static final class Leaf {
// The object, or a WeakRef to it; null for no entry.
final Object[] values = new Object[LEAF_SIZE];
// The object's identity hash, to find its identity index entry on removal.
final int[] hashes = new int[LEAF_SIZE];
int live;
}
private static final class Chunk {
final Leaf[] leaves = new Leaf[1 << CHUNK_BITS];
int liveLeaves;
}
private Chunk[] chunks = new Chunk[16];
// id >>> LEAF_BITS of the highest id registered.
private int newestLeaf = 0;
private int idCount = 0;
// identity hash -> id + 1, open addressing; the id's entry decides which object it holds.
// identityIndexHashes[i] is the hash of identityIndex[i]'s object, so a probe only looks
// the id up when the hashes match.
private int[] identityIndex = new int[1024];
private int[] identityIndexHashes = new int[1024];
private int identityCount = 0;
/** Registers obj under id, held strongly, replacing whatever id held before. */
void putStrong(int id, Object obj) {
synchronized (this) {
putStrongLocked(id, obj);
}
}
private void putStrongLocked(int id, Object obj) {
putStrongLocked(id, obj, System.identityHashCode(obj));
}
private void putStrongLocked(int id, Object obj, int hash) {
Leaf existingLeaf = leafFor(id);
if (existingLeaf != null) {
Object value = existingLeaf.values[id & LEAF_MASK];
if (value != null) {
if (deref(value) == obj) {
existingLeaf.values[id & LEAF_MASK] = obj;
return;
}
removeLocked(id);
}
}
insertEntry(id, obj, hash);
idCount++;
if ((identityCount + 1) * 2 > identityIndex.length) {
growIdentity();
}
insertIdentity(hash, id);
identityCount++;
}
/**
* The id obj is registered under, registering it strongly under a new id from ids when
* it has none; FRESH is set in the result when this call registered it. The lookup and
* the insert happen under one lock, so concurrent calls agree on one id.
*/
long findOrRegister(Object obj, IdSource ids) {
synchronized (this) {
int hash = System.identityHashCode(obj);
int id = findIdLocked(obj, hash);
if (id != NO_ID) {
return id & 0xFFFFFFFFL;
}
id = ids.newId();
putStrongLocked(id, obj, hash);
return (id & 0xFFFFFFFFL) | FRESH;
}
}
/**
* The id obj is registered under, or NO_ID. From a thread without the isolate lock, the
* object may have been released since, which the caller must check under the lock.
*/
int findId(Object obj) {
synchronized (this) {
return findIdLocked(obj);
}
}
private int findIdLocked(Object obj) {
return findIdLocked(obj, System.identityHashCode(obj));
}
private int findIdLocked(Object obj, int hash) {
int mask = identityIndex.length - 1;
int i = mix(hash) & mask;
while (true) {
int entry = identityIndex[i];
if (entry == 0) {
return NO_ID;
}
int id = entry - 1;
if (identityIndexHashes[i] == hash && getLocked(id) == obj) {
return id;
}
i = (i + 1) & mask;
}
}
/** The object registered under id; null if there is none or it was collected. */
Object get(int id) {
synchronized (this) {
return getLocked(id);
}
}
private Object getLocked(int id) {
Object value = rawLocked(id);
return value == null ? null : deref(value);
}
boolean contains(int id) {
synchronized (this) {
return rawLocked(id) != null;
}
}
/**
* The JS wrapper for id was collected: hold the object weakly from now on. Returns whether
* the object is still alive; an entry whose object was already collected is removed.
*/
boolean weakenAndCheckAlive(int id) {
synchronized (this) {
return weakenAndCheckAliveLocked(id);
}
}
/**
* weakenAndCheckAlive for ids[0..count), under one lock; overwrites the id of every
* object no longer alive with NO_ID.
*/
void weakenAndCheckAlive(int[] ids, int count) {
if (count == 0) {
return;
}
synchronized (this) {
weakenAndCheckAliveAllLocked(ids, count);
}
}
private void weakenAndCheckAliveAllLocked(int[] ids, int count) {
for (int i = 0; i < count; i++) {
if (!weakenAndCheckAliveLocked(ids[i])) {
ids[i] = NO_ID;
}
}
}
private boolean weakenAndCheckAliveLocked(int id) {
Leaf leaf = leafFor(id);
Object value = leaf == null ? null : leaf.values[id & LEAF_MASK];
if (value == null) {
return false;
}
if (value instanceof WeakRef) {
if (((WeakRef) value).get() != null) {
return true;
}
removeLocked(id);
return false;
}
leaf.values[id & LEAF_MASK] = new WeakRef(value);
return true;
}
void remove(int id) {
synchronized (this) {
removeLocked(id);
}
}
private void removeLocked(int id) {
Leaf leaf = leafFor(id);
int i = id & LEAF_MASK;
if (leaf == null || leaf.values[i] == null) {
return;
}
int hash = leaf.hashes[i];
leaf.values[i] = null;
leaf.hashes[i] = 0;
leaf.live--;
int leafNumber = id >>> LEAF_BITS;
if (leafNumber != newestLeaf) {
releaseIfEmpty(leafNumber);
}
idCount--;
removeIdentity(hash, id);
identityCount--;
}
int size() {
return idCount;
}
// Distinguishes the table's own weak entries from a registered object that happens to be
// a WeakReference.
private static final class WeakRef extends WeakReference<Object> {
WeakRef(Object referent) {
super(referent);
}
}
private Leaf leafFor(int id) {
if (id == NO_ID) {
return null;
}
int c = id >>> CHUNK_SHIFT;
Chunk chunk = c < chunks.length ? chunks[c] : null;
return chunk == null ? null : chunk.leaves[(id >>> LEAF_BITS) & CHUNK_MASK];
}
// The entry for id as stored (the object or a WeakRef to it), or null.
private Object rawLocked(int id) {
Leaf leaf = leafFor(id);
return leaf == null ? null : leaf.values[id & LEAF_MASK];
}
// Adds an entry for an id that has none.
private void insertEntry(int id, Object value, int hash) {
int c = id >>> CHUNK_SHIFT;
if (c >= chunks.length) {
int length = chunks.length;
while (length <= c) {
length *= 2;
}
Chunk[] grown = new Chunk[length];
System.arraycopy(chunks, 0, grown, 0, chunks.length);
chunks = grown;
}
Chunk chunk = chunks[c];
if (chunk == null) {
chunk = new Chunk();
chunks[c] = chunk;
}
int l = (id >>> LEAF_BITS) & CHUNK_MASK;
Leaf leaf = chunk.leaves[l];
if (leaf == null) {
leaf = new Leaf();
chunk.leaves[l] = leaf;
chunk.liveLeaves++;
}
int i = id & LEAF_MASK;
leaf.values[i] = value;
leaf.hashes[i] = hash;
leaf.live++;
int leafNumber = id >>> LEAF_BITS;
if (leafNumber > newestLeaf) {
int previous = newestLeaf;
newestLeaf = leafNumber;
// Kept while it was the newest; nothing else drops it once it is empty.
releaseIfEmpty(previous);
}
}
// Drops leaf leafNumber if it is empty, then its chunk if that empties and is not the
// newest.
private void releaseIfEmpty(int leafNumber) {
int c = leafNumber >>> CHUNK_BITS;
Chunk chunk = c < chunks.length ? chunks[c] : null;
if (chunk == null) {
return;
}
int l = leafNumber & CHUNK_MASK;
Leaf leaf = chunk.leaves[l];
if (leaf == null || leaf.live != 0) {
return;
}
chunk.leaves[l] = null;
if (--chunk.liveLeaves == 0 && c != (newestLeaf >>> CHUNK_BITS)) {
chunks[c] = null;
}
}
private void insertIdentity(int hash, int id) {
insertIdentity(identityIndex, identityIndexHashes, hash, id);
}
private static void insertIdentity(int[] index, int[] hashes, int hash, int id) {
int mask = index.length - 1;
int i = mix(hash) & mask;
while (index[i] != 0) {
i = (i + 1) & mask;
}
hashes[i] = hash;
index[i] = id + 1;
}
private void removeIdentity(int hash, int id) {
int mask = identityIndex.length - 1;
int i = mix(hash) & mask;
while (identityIndex[i] != id + 1) {
i = (i + 1) & mask;
}
int hole = i;
int j = i;
while (true) {
j = (j + 1) & mask;
int entry = identityIndex[j];
if (entry == 0) {
break;
}
int home = mix(identityIndexHashes[j]) & mask;
if (!isBetween(hole, home, j)) {
identityIndex[hole] = entry;
identityIndexHashes[hole] = identityIndexHashes[j];
hole = j;
}
}
identityIndex[hole] = 0;
identityIndexHashes[hole] = 0;
}
// Whether home lies cyclically in (hole, j], i.e. the entry at j must stay put.
private static boolean isBetween(int hole, int home, int j) {
return hole <= j ? (hole < home && home <= j) : (hole < home || home <= j);
}
private void growIdentity() {
int[] oldIndex = identityIndex;
int[] oldHashes = identityIndexHashes;
int[] index = new int[oldIndex.length * 2];
int[] hashes = new int[index.length];
for (int i = 0; i < oldIndex.length; i++) {
if (oldIndex[i] != 0) {
insertIdentity(index, hashes, oldHashes[i], oldIndex[i] - 1);
}
}
identityIndexHashes = hashes;
identityIndex = index;
}
private static int mix(int h) {
h ^= (h >>> 16);
h *= 0x85ebca6b;
h ^= (h >>> 13);
return h;
}
}