Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/jdk/internal/jimage/ImageBufferCache.java
41159 views
1
/*
2
* Copyright (c) 2014, 2016, 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
package jdk.internal.jimage;
26
27
import java.lang.ref.WeakReference;
28
import java.nio.ByteBuffer;
29
import java.util.AbstractMap;
30
import java.util.Arrays;
31
import java.util.Comparator;
32
import java.util.Map;
33
34
/**
35
* @implNote This class needs to maintain JDK 8 source compatibility.
36
*
37
* It is used internally in the JDK to implement jimage/jrtfs access,
38
* but also compiled and delivered as part of the jrt-fs.jar to support access
39
* to the jimage file provided by the shipped JDK by tools running on JDK 8.
40
*/
41
class ImageBufferCache {
42
private static final int MAX_CACHED_BUFFERS = 3;
43
private static final int LARGE_BUFFER = 0x10000;
44
45
/*
46
* We used to have a class BufferReference extending from WeakReference<ByteBuffer>.
47
* BufferReference class had an instance field called "capacity". This field was
48
* used to make DECREASING_CAPACITY_NULLS_LAST comparator stable in the presence
49
* of GC clearing the WeakReference concurrently.
50
*
51
* But this scheme results in metaspace leak. The thread local is alive till the
52
* the thread is alive. And so ImageBufferCache$BufferReference class was kept alive.
53
* Because this class and ImageBufferCache$BufferReference are all loaded by a URL
54
* class loader from jrt-fs.jar, the class loader and so all the classes loaded by it
55
* were alive!
56
*
57
* Solution is to avoid using a URL loader loaded class type with thread local. All we
58
* need is a pair of WeakReference<ByteBuffer>, Integer (saved capacity for stability
59
* of comparator). We use Map.Entry as pair implementation. With this, all types used
60
* with thread local are bootstrap types and so no metaspace leak.
61
*/
62
@SuppressWarnings("unchecked")
63
private static final ThreadLocal<Map.Entry<WeakReference<ByteBuffer>, Integer>[]> CACHE =
64
new ThreadLocal<Map.Entry<WeakReference<ByteBuffer>, Integer>[]>() {
65
@Override
66
protected Map.Entry<WeakReference<ByteBuffer>, Integer>[] initialValue() {
67
// 1 extra slot to simplify logic of releaseBuffer()
68
return (Map.Entry<WeakReference<ByteBuffer>, Integer>[])new Map.Entry<?,?>[MAX_CACHED_BUFFERS + 1];
69
}
70
};
71
72
private static ByteBuffer allocateBuffer(long size) {
73
return ByteBuffer.allocateDirect((int)((size + 0xFFF) & ~0xFFF));
74
}
75
76
static ByteBuffer getBuffer(long size) {
77
if (size < 0 || Integer.MAX_VALUE < size) {
78
throw new IndexOutOfBoundsException("size");
79
}
80
81
ByteBuffer result = null;
82
83
if (size > LARGE_BUFFER) {
84
result = allocateBuffer(size);
85
} else {
86
Map.Entry<WeakReference<ByteBuffer>, Integer>[] cache = CACHE.get();
87
88
// buffers are ordered by decreasing capacity
89
// cache[MAX_CACHED_BUFFERS] is always null
90
for (int i = MAX_CACHED_BUFFERS - 1; i >= 0; i--) {
91
Map.Entry<WeakReference<ByteBuffer>, Integer> reference = cache[i];
92
93
if (reference != null) {
94
ByteBuffer buffer = getByteBuffer(reference);
95
96
if (buffer != null && size <= buffer.capacity()) {
97
cache[i] = null;
98
result = buffer;
99
result.rewind();
100
break;
101
}
102
}
103
}
104
105
if (result == null) {
106
result = allocateBuffer(size);
107
}
108
}
109
110
result.limit((int)size);
111
112
return result;
113
}
114
115
static void releaseBuffer(ByteBuffer buffer) {
116
if (buffer.capacity() > LARGE_BUFFER) {
117
return;
118
}
119
120
Map.Entry<WeakReference<ByteBuffer>, Integer>[] cache = CACHE.get();
121
122
// expunge cleared BufferRef(s)
123
for (int i = 0; i < MAX_CACHED_BUFFERS; i++) {
124
Map.Entry<WeakReference<ByteBuffer>, Integer> reference = cache[i];
125
if (reference != null && getByteBuffer(reference) == null) {
126
cache[i] = null;
127
}
128
}
129
130
// insert buffer back with new BufferRef wrapping it
131
cache[MAX_CACHED_BUFFERS] = newCacheEntry(buffer);
132
Arrays.sort(cache, DECREASING_CAPACITY_NULLS_LAST);
133
// squeeze the smallest one out
134
cache[MAX_CACHED_BUFFERS] = null;
135
}
136
137
private static Map.Entry<WeakReference<ByteBuffer>, Integer> newCacheEntry(ByteBuffer bb) {
138
return new AbstractMap.SimpleEntry<WeakReference<ByteBuffer>, Integer>(
139
new WeakReference<ByteBuffer>(bb), bb.capacity());
140
}
141
142
private static int getCapacity(Map.Entry<WeakReference<ByteBuffer>, Integer> e) {
143
return e == null? 0 : e.getValue();
144
}
145
146
private static ByteBuffer getByteBuffer(Map.Entry<WeakReference<ByteBuffer>, Integer> e) {
147
return e == null? null : e.getKey().get();
148
}
149
150
private static Comparator<Map.Entry<WeakReference<ByteBuffer>, Integer>> DECREASING_CAPACITY_NULLS_LAST =
151
new Comparator<Map.Entry<WeakReference<ByteBuffer>, Integer>>() {
152
@Override
153
public int compare(Map.Entry<WeakReference<ByteBuffer>, Integer> br1,
154
Map.Entry<WeakReference<ByteBuffer>, Integer> br2) {
155
return Integer.compare(getCapacity(br1), getCapacity(br2));
156
}
157
};
158
}
159
160