Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/marlin/OffHeapArray.java
38918 views
1
/*
2
* Copyright (c) 2007, 2015, 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 sun.java2d.marlin;
27
28
import java.lang.ref.PhantomReference;
29
import java.lang.ref.ReferenceQueue;
30
import java.security.AccessController;
31
import java.security.PrivilegedAction;
32
import java.util.Vector;
33
import static sun.java2d.marlin.MarlinConst.logUnsafeMalloc;
34
import sun.misc.ThreadGroupUtils;
35
import sun.misc.Unsafe;
36
37
/**
38
*
39
* @author bourgesl
40
*/
41
final class OffHeapArray {
42
43
// unsafe reference
44
static final Unsafe unsafe;
45
// size of int / float
46
static final int SIZE_INT;
47
48
// RendererContext reference queue
49
private static final ReferenceQueue<Object> rdrQueue
50
= new ReferenceQueue<Object>();
51
// reference list
52
private static final Vector<OffHeapReference> refList
53
= new Vector<OffHeapReference>(32);
54
55
static {
56
unsafe = Unsafe.getUnsafe();
57
SIZE_INT = Unsafe.ARRAY_INT_INDEX_SCALE;
58
59
// Mimics Java2D Disposer:
60
AccessController.doPrivileged(
61
(PrivilegedAction<Void>) () -> {
62
/*
63
* The thread must be a member of a thread group
64
* which will not get GCed before VM exit.
65
* Make its parent the top-level thread group.
66
*/
67
final ThreadGroup rootTG
68
= ThreadGroupUtils.getRootThreadGroup();
69
final Thread t = new Thread(rootTG, new OffHeapDisposer(),
70
"MarlinRenderer Disposer");
71
t.setContextClassLoader(null);
72
t.setDaemon(true);
73
t.setPriority(Thread.MAX_PRIORITY);
74
t.start();
75
return null;
76
}
77
);
78
}
79
80
/* members */
81
long address;
82
long length;
83
int used;
84
85
OffHeapArray(final Object parent, final long len) {
86
// note: may throw OOME:
87
this.address = unsafe.allocateMemory(len);
88
this.length = len;
89
this.used = 0;
90
if (logUnsafeMalloc) {
91
MarlinUtils.logInfo(System.currentTimeMillis()
92
+ ": OffHeapArray.allocateMemory = "
93
+ len + " to addr = " + this.address);
94
}
95
96
// Create the phantom reference to ensure freeing off-heap memory:
97
refList.add(new OffHeapReference(parent, this));
98
}
99
100
/*
101
* As realloc may change the address, updating address is MANDATORY
102
* @param len new array length
103
* @throws OutOfMemoryError if the allocation is refused by the system
104
*/
105
void resize(final long len) {
106
// note: may throw OOME:
107
this.address = unsafe.reallocateMemory(address, len);
108
this.length = len;
109
if (logUnsafeMalloc) {
110
MarlinUtils.logInfo(System.currentTimeMillis()
111
+ ": OffHeapArray.reallocateMemory = "
112
+ len + " to addr = " + this.address);
113
}
114
}
115
116
void free() {
117
unsafe.freeMemory(this.address);
118
if (logUnsafeMalloc) {
119
MarlinUtils.logInfo(System.currentTimeMillis()
120
+ ": OffHeapEdgeArray.free = "
121
+ this.length
122
+ " at addr = " + this.address);
123
}
124
}
125
126
void fill(final byte val) {
127
unsafe.setMemory(this.address, this.length, val);
128
}
129
130
static final class OffHeapReference extends PhantomReference<Object> {
131
132
private final OffHeapArray array;
133
134
OffHeapReference(final Object parent, final OffHeapArray edges) {
135
super(parent, rdrQueue);
136
this.array = edges;
137
}
138
139
void dispose() {
140
// free off-heap blocks
141
this.array.free();
142
}
143
}
144
145
static final class OffHeapDisposer implements Runnable {
146
@Override
147
public void run() {
148
final Thread currentThread = Thread.currentThread();
149
OffHeapReference ref;
150
151
// check interrupted:
152
for (; !currentThread.isInterrupted();) {
153
try {
154
ref = (OffHeapReference)rdrQueue.remove();
155
ref.dispose();
156
157
refList.remove(ref);
158
159
} catch (InterruptedException ie) {
160
MarlinUtils.logException("OffHeapDisposer interrupted:",
161
ie);
162
}
163
}
164
}
165
}
166
}
167
168