Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/support/ChainedCallSite.java
41161 views
1
/*
2
* Copyright (c) 2010, 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
26
/*
27
* This file is available under and governed by the GNU General Public
28
* License version 2 only, as published by the Free Software Foundation.
29
* However, the following notice accompanied the original version of this
30
* file, and Oracle licenses the original version of this file under the BSD
31
* license:
32
*/
33
/*
34
Copyright 2009-2013 Attila Szegedi
35
36
Redistribution and use in source and binary forms, with or without
37
modification, are permitted provided that the following conditions are
38
met:
39
* Redistributions of source code must retain the above copyright
40
notice, this list of conditions and the following disclaimer.
41
* Redistributions in binary form must reproduce the above copyright
42
notice, this list of conditions and the following disclaimer in the
43
documentation and/or other materials provided with the distribution.
44
* Neither the name of the copyright holder nor the names of
45
contributors may be used to endorse or promote products derived from
46
this software without specific prior written permission.
47
48
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
49
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
51
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
52
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
55
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
58
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59
*/
60
61
package jdk.dynalink.support;
62
63
import java.lang.invoke.MethodHandle;
64
import java.lang.invoke.MethodHandles;
65
import java.util.Arrays;
66
import java.util.LinkedList;
67
import jdk.dynalink.CallSiteDescriptor;
68
import jdk.dynalink.linker.GuardedInvocation;
69
import jdk.dynalink.linker.support.Lookup;
70
71
/**
72
* A relinkable call site that implements a polymorphic inline caching strategy.
73
* It remembers up to 8 {@link GuardedInvocation}s it was linked with, and on
74
* each relink request builds a cascading chain of method handles of one
75
* invocation falling back to the next one. The number of remembered invocations
76
* can be customized by overriding {@link #getMaxChainLength()} in a subclass.
77
* When this call site is relinked with a new invocation and the length of the
78
* chain is already at the maximum, it will throw away the oldest invocation.
79
* Invocations with invalidated switch points and ones for which their
80
* invalidating exception triggered are removed eagerly from the chain. The
81
* invocations are never reordered; the most recently linked method handle is
82
* always at the start of the chain and the least recently linked at its end.
83
* The call site can be safely relinked on more than one thread concurrently.
84
* Race conditions in linking are resolved by throwing away the
85
* {@link GuardedInvocation} produced on the losing thread without incorporating
86
* it into the chain, so it can lead to repeated linking for the same arguments.
87
*/
88
public class ChainedCallSite extends AbstractRelinkableCallSite {
89
private static final MethodHandle PRUNE_CATCHES;
90
private static final MethodHandle PRUNE_SWITCHPOINTS;
91
static {
92
final MethodHandle PRUNE = Lookup.findOwnSpecial(MethodHandles.lookup(), "prune", MethodHandle.class,
93
MethodHandle.class, boolean.class);
94
PRUNE_CATCHES = MethodHandles.insertArguments(PRUNE, 2, true);
95
PRUNE_SWITCHPOINTS = MethodHandles.insertArguments(PRUNE, 2, false);
96
}
97
98
/**
99
* Contains the invocations currently linked into this call site's target. They are used when we are
100
* relinking to rebuild the guardWithTest chain. Valid values for this field are: {@code null} if there's
101
* no linked invocations, or an instance of {@link GuardedInvocation} if there is exactly one previous
102
* invocation, or an instance of {@code GuardedInvocation[]} if there is more than one previous
103
* invocation.
104
*/
105
private Object invocations;
106
107
/**
108
* Creates a new chained call site.
109
* @param descriptor the descriptor for the call site.
110
*/
111
public ChainedCallSite(final CallSiteDescriptor descriptor) {
112
super(descriptor);
113
}
114
115
/**
116
* The maximum number of method handles in the chain. Defaults to 8. You can
117
* override it in a subclass if you need to change the value.
118
* @return the maximum number of method handles in the chain. The return
119
* value is checked, and if your override returns a value less than 1, a
120
* {@link RuntimeException} will be thrown.
121
*/
122
protected int getMaxChainLength() {
123
return 8;
124
}
125
126
@Override
127
public void relink(final GuardedInvocation guardedInvocation, final MethodHandle relinkAndInvoke) {
128
relinkInternal(guardedInvocation, relinkAndInvoke, false, false);
129
}
130
131
@Override
132
public void resetAndRelink(final GuardedInvocation guardedInvocation, final MethodHandle relinkAndInvoke) {
133
relinkInternal(guardedInvocation, relinkAndInvoke, true, false);
134
}
135
136
private MethodHandle relinkInternal(final GuardedInvocation invocation, final MethodHandle relink, final boolean reset, final boolean removeCatches) {
137
final Object currentInvocations = invocations;
138
final LinkedList<GuardedInvocation> newInvocations;
139
if (currentInvocations == null || reset) {
140
newInvocations = new LinkedList<>();
141
} else if (currentInvocations instanceof GuardedInvocation) {
142
newInvocations = new LinkedList<>();
143
newInvocations.add((GuardedInvocation)currentInvocations);
144
} else if (currentInvocations instanceof GuardedInvocation[]) {
145
newInvocations = new LinkedList<>(Arrays.asList(((GuardedInvocation[])currentInvocations)));
146
} else {
147
throw new AssertionError();
148
}
149
150
// First, prune the chain of invalidated switchpoints, we always do this
151
// We also remove any catches if the remove catches flag is set
152
newInvocations.removeIf(inv ->
153
inv.hasBeenInvalidated() || (removeCatches && inv.getException() != null)
154
);
155
156
// prune() is allowed to invoke this method with invocation == null meaning we're just pruning the chain and not
157
// adding any new invocations to it.
158
if(invocation != null) {
159
// Remove oldest entry if we're at max length
160
if(newInvocations.size() == checkMaxChainLength(getMaxChainLength())) {
161
newInvocations.removeFirst();
162
}
163
newInvocations.addLast(invocation);
164
}
165
166
// prune-and-invoke is used as the fallback for invalidated switchpoints. If a switchpoint gets invalidated, we
167
// rebuild the chain and get rid of all invalidated switchpoints instead of letting them linger.
168
final MethodHandle pruneAndInvokeSwitchPoints = makePruneAndInvokeMethod(relink, PRUNE_SWITCHPOINTS);
169
final MethodHandle pruneAndInvokeCatches = makePruneAndInvokeMethod(relink, PRUNE_CATCHES);
170
171
// Fold the new chain
172
MethodHandle target = relink;
173
for(final GuardedInvocation inv: newInvocations) {
174
target = inv.compose(target, pruneAndInvokeSwitchPoints, pruneAndInvokeCatches);
175
}
176
177
switch (newInvocations.size()) {
178
case 0:
179
invocations = null;
180
break;
181
case 1:
182
invocations = newInvocations.getFirst();
183
break;
184
default:
185
invocations = newInvocations.toArray(new GuardedInvocation[0]);
186
}
187
setTarget(target);
188
return target;
189
}
190
191
private static int checkMaxChainLength(final int maxChainLength) {
192
if (maxChainLength > 0) {
193
return maxChainLength;
194
}
195
throw new RuntimeException("getMaxChainLength() returned a non-positive value");
196
197
}
198
/**
199
* Creates a method that rebuilds our call chain, pruning it of any invalidated switchpoints, and then invokes that
200
* chain.
201
* @param relinkAndInvoke the ultimate fallback for the chain passed from the dynamic linker.
202
* @return a method handle for prune-and-invoke
203
*/
204
private MethodHandle makePruneAndInvokeMethod(final MethodHandle relinkAndInvoke, final MethodHandle prune) {
205
// Bind prune to (this, relink)
206
final MethodHandle boundPrune = MethodHandles.insertArguments(prune, 0, this, relinkAndInvoke);
207
// Make it ignore all incoming arguments
208
final MethodHandle ignoreArgsPrune = MethodHandles.dropArguments(boundPrune, 0, type().parameterList());
209
// Invoke prune, then invoke the call site target with original arguments
210
return MethodHandles.foldArguments(MethodHandles.exactInvoker(type()), ignoreArgsPrune);
211
}
212
213
@SuppressWarnings("unused")
214
private MethodHandle prune(final MethodHandle relink, final boolean catches) {
215
return relinkInternal(null, relink, false, catches);
216
}
217
}
218
219