1 /*
2 * Copyright (c) 2018, 2019, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package jdk.vm.ci.hotspot;
24
25 import static jdk.vm.ci.hotspot.UnsafeAccess.UNSAFE;
26
27 /**
28 * This class manages a set of {@code jobject} and {@code jmetadata} handles whose lifetimes are
29 * dependent on associated {@link IndirectHotSpotObjectConstantImpl} and
30 * {@link MetaspaceHandleObject} wrapper objects respectively.
31 *
32 * The general theory of operation is that all wrappers are created by calling into the VM which
33 * calls back out to actually create the wrapper instance. During the call the VM keeps the object
34 * or metadata reference alive through the use of handles. Once the call completes the wrapper
35 * object is registered here and will be scanned during metadata scanning. The weakness of the
36 * reference to the wrapper object allows the handles to be reclaimed when they are no longer used.
37 */
38 final class HandleCleaner extends Cleaner {
39
40 /**
41 * A {@code jmetadata} or {@code jobject} handle.
42 */
43 private final long handle;
44
45 /**
46 * Specifies if {@link #handle} is a {@code jobject} or {@code jmetadata}.
47 */
48 private final boolean isJObject;
49
50 private HandleCleaner(Object wrapper, long handle, boolean isJObject) {
51 super(wrapper);
52 this.handle = handle;
53 this.isJObject = isJObject;
54 }
55
56 /**
57 * Releases the resource associated with {@code this.handle}.
58 */
59 @Override
60 void doCleanup() {
61 if (isJObject) {
62 // The sentinel value used to denote a free handle is
63 // an object on the HotSpot heap so we call into the
64 // VM to set the target of an object handle to this value.
65 CompilerToVM.compilerToVM().deleteGlobalHandle(handle);
66 } else {
67 // Setting the target of a jmetadata handle to 0 enables
68 // the handle to be reused. See MetadataHandleBlock in
69 // jvmciRuntime.cpp for more info.
70 long value = UNSAFE.getLong(null, handle);
71 UNSAFE.compareAndSetLong(null, handle, value, 0);
72 }
73 }
74
75 /**
76 * Registers a cleaner for {@code handle}. The cleaner will release the handle some time after
77 * {@code wrapper} is detected as unreachable by the garbage collector.
78 */
79 @SuppressWarnings("unused")
80 static void create(Object wrapper, long handle) {
81 assert wrapper instanceof IndirectHotSpotObjectConstantImpl || wrapper instanceof MetaspaceHandleObject;
82 new HandleCleaner(wrapper, handle, wrapper instanceof IndirectHotSpotObjectConstantImpl);
83 }
84 }