author | erikj |
Wed, 07 Mar 2018 13:26:15 -0800 | |
changeset 49357 | aaedb8343784 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
31673 | 1 |
/* |
36511 | 2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
31673 | 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 |
||
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
27 |
import java.lang.ref.WeakReference; |
31673 | 28 |
import java.nio.ByteBuffer; |
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
29 |
import java.util.Arrays; |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
30 |
import java.util.Comparator; |
31673 | 31 |
|
36511 | 32 |
/** |
33 |
* @implNote This class needs to maintain JDK 8 source compatibility. |
|
34 |
* |
|
35 |
* It is used internally in the JDK to implement jimage/jrtfs access, |
|
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
36 |
* but also compiled and delivered as part of the jrt-fs.jar to support access |
36511 | 37 |
* to the jimage file provided by the shipped JDK by tools running on JDK 8. |
38 |
*/ |
|
31673 | 39 |
class ImageBufferCache { |
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
40 |
private static final int MAX_CACHED_BUFFERS = 3; |
31673 | 41 |
private static final int LARGE_BUFFER = 0x10000; |
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
42 |
private static final ThreadLocal<BufferReference[]> CACHE = |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
43 |
new ThreadLocal<BufferReference[]>() { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
44 |
@Override |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
45 |
protected BufferReference[] initialValue() { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
46 |
// 1 extra slot to simplify logic of releaseBuffer() |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
47 |
return new BufferReference[MAX_CACHED_BUFFERS + 1]; |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
48 |
} |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
49 |
}; |
31673 | 50 |
|
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
51 |
private static ByteBuffer allocateBuffer(long size) { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
52 |
return ByteBuffer.allocateDirect((int)((size + 0xFFF) & ~0xFFF)); |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
53 |
} |
31673 | 54 |
|
55 |
static ByteBuffer getBuffer(long size) { |
|
36511 | 56 |
if (size < 0 || Integer.MAX_VALUE < size) { |
57 |
throw new IndexOutOfBoundsException("size"); |
|
58 |
} |
|
59 |
||
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
60 |
ByteBuffer result = null; |
31673 | 61 |
|
62 |
if (size > LARGE_BUFFER) { |
|
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
63 |
result = allocateBuffer(size); |
31673 | 64 |
} else { |
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
65 |
BufferReference[] cache = CACHE.get(); |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
66 |
|
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
67 |
// buffers are ordered by decreasing capacity |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
68 |
// cache[MAX_CACHED_BUFFERS] is always null |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
69 |
for (int i = MAX_CACHED_BUFFERS - 1; i >= 0; i--) { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
70 |
BufferReference reference = cache[i]; |
31673 | 71 |
|
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
72 |
if (reference != null) { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
73 |
ByteBuffer buffer = reference.get(); |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
74 |
|
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
75 |
if (buffer != null && size <= buffer.capacity()) { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
76 |
cache[i] = null; |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
77 |
result = buffer; |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
78 |
result.rewind(); |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
79 |
break; |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
80 |
} |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
81 |
} |
31673 | 82 |
} |
83 |
||
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
84 |
if (result == null) { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
85 |
result = allocateBuffer(size); |
31673 | 86 |
} |
87 |
} |
|
88 |
||
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
89 |
result.limit((int)size); |
31673 | 90 |
|
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
91 |
return result; |
31673 | 92 |
} |
93 |
||
94 |
static void releaseBuffer(ByteBuffer buffer) { |
|
95 |
if (buffer.capacity() > LARGE_BUFFER) { |
|
96 |
return; |
|
97 |
} |
|
98 |
||
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
99 |
BufferReference[] cache = CACHE.get(); |
31673 | 100 |
|
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
101 |
// expunge cleared BufferRef(s) |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
102 |
for (int i = 0; i < MAX_CACHED_BUFFERS; i++) { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
103 |
BufferReference reference = cache[i]; |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
104 |
if (reference != null && reference.get() == null) { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
105 |
cache[i] = null; |
31673 | 106 |
} |
107 |
} |
|
108 |
||
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
109 |
// insert buffer back with new BufferRef wrapping it |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
110 |
cache[MAX_CACHED_BUFFERS] = new BufferReference(buffer); |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
111 |
Arrays.sort(cache, DECREASING_CAPACITY_NULLS_LAST); |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
112 |
// squeeze the smallest one out |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
113 |
cache[MAX_CACHED_BUFFERS] = null; |
31673 | 114 |
} |
115 |
||
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
116 |
private static Comparator<BufferReference> DECREASING_CAPACITY_NULLS_LAST = |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
117 |
new Comparator<BufferReference>() { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
118 |
@Override |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
119 |
public int compare(BufferReference br1, BufferReference br2) { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
120 |
return Integer.compare(br2 == null ? 0 : br2.capacity, |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
121 |
br1 == null ? 0 : br1.capacity); |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
122 |
} |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
123 |
}; |
31673 | 124 |
|
37342
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
125 |
private static class BufferReference extends WeakReference<ByteBuffer> { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
126 |
// saved capacity so that DECREASING_CAPACITY_NULLS_LAST comparator |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
127 |
// is stable in the presence of GC clearing the WeakReference concurrently |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
128 |
final int capacity; |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
129 |
|
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
130 |
BufferReference(ByteBuffer buffer) { |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
131 |
super(buffer); |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
132 |
capacity = buffer.capacity(); |
3f54fbfc2706
8151807: ImageBufferCache should release buffers when all classes are loaded
plevart
parents:
36511
diff
changeset
|
133 |
} |
31673 | 134 |
} |
135 |
} |