Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/share/classes/java/io/ClassCache.java
67794 views
1
/*
2
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.io;
27
28
import java.lang.ref.Reference;
29
import java.lang.ref.ReferenceQueue;
30
import java.lang.ref.SoftReference;
31
import java.util.Objects;
32
33
// Maps Class instances to values of type T. Under memory pressure, the
34
// mapping is released (under soft references GC policy) and would be
35
// recomputed the next time it is queried. The mapping is bound to the
36
// lifetime of the class: when the class is unloaded, the mapping is
37
// removed too.
38
abstract class ClassCache<T> {
39
40
private static class CacheRef<T> extends SoftReference<T> {
41
private final Class<?> type;
42
private T strongReferent;
43
44
CacheRef(T referent, ReferenceQueue<T> queue, Class<?> type) {
45
super(referent, queue);
46
this.type = type;
47
this.strongReferent = referent;
48
}
49
50
Class<?> getType() {
51
return type;
52
}
53
54
T getStrong() {
55
return strongReferent;
56
}
57
58
void clearStrong() {
59
strongReferent = null;
60
}
61
}
62
63
private final ReferenceQueue<T> queue;
64
private final ClassValue<CacheRef<T>> map;
65
66
protected abstract T computeValue(Class<?> cl);
67
68
protected ClassCache() {
69
queue = new ReferenceQueue<>();
70
map = new ClassValue<>() {
71
@Override
72
protected CacheRef<T> computeValue(Class<?> type) {
73
T v = ClassCache.this.computeValue(type);
74
Objects.requireNonNull(v);
75
return new CacheRef<>(v, queue, type);
76
}
77
};
78
}
79
80
T get(Class<?> cl) {
81
while (true) {
82
processQueue();
83
84
CacheRef<T> ref = map.get(cl);
85
86
// Case 1: A recently created CacheRef.
87
// We might still have strong referent, and can return it.
88
// This guarantees progress for at least one thread on every CacheRef.
89
// Clear the strong referent before returning to make the cache soft.
90
T strongVal = ref.getStrong();
91
if (strongVal != null) {
92
ref.clearStrong();
93
return strongVal;
94
}
95
96
// Case 2: Older or recently cleared CacheRef.
97
// Check if its soft referent is still available, and return it.
98
T val = ref.get();
99
if (val != null) {
100
return val;
101
}
102
103
// Case 3: The reference was cleared.
104
// Clear the mapping and retry.
105
map.remove(cl);
106
}
107
}
108
109
private void processQueue() {
110
Reference<? extends T> ref;
111
while((ref = queue.poll()) != null) {
112
CacheRef<? extends T> cacheRef = (CacheRef<? extends T>)ref;
113
map.remove(cacheRef.getType());
114
}
115
}
116
}
117
118