|
1 /* |
|
2 * Copyright (c) 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 |
|
24 |
|
25 package org.graalvm.compiler.hotspot; |
|
26 |
|
27 import org.graalvm.compiler.debug.GraalError; |
|
28 |
|
29 import jdk.vm.ci.meta.JavaConstant; |
|
30 |
|
31 /** |
|
32 * A context for scoping the lifetime of foreign objects. |
|
33 * |
|
34 * The need for this mechanism is best explained with an example. When folding a GETFIELD bytecode |
|
35 * denoting a {@code final static} non-primitive field, libgraal can create a {@link JavaConstant} |
|
36 * wrapping a handle to the field's value in the HotSpot heap. This handle must be released before |
|
37 * HotSpot can reclaim the object it references. Performing a compilation in the scope of a |
|
38 * {@linkplain HotSpotGraalServices#openLocalCompilationContext local} context ensures the handle is |
|
39 * released once the compilation completes, allowing the HotSpot GC to subsequently reclaim the |
|
40 * HotSpot object. When libgraal creates data structures that outlive a single compilation and may |
|
41 * contain foreign object references (e.g. snippet graphs), it must enter the |
|
42 * {@linkplain HotSpotGraalServices#enterGlobalCompilationContext global} context. Foreign object |
|
43 * handles created in the global context are only released once their {@link JavaConstant} wrappers |
|
44 * are reclaimed by the libgraal GC. |
|
45 * |
|
46 * {@link CompilationContext}s have no impact on {@link JavaConstant}s that do not encapsulate a |
|
47 * foreign object reference. |
|
48 * |
|
49 * The object returned by {@link HotSpotGraalServices#enterGlobalCompilationContext} or |
|
50 * {@link HotSpotGraalServices#openLocalCompilationContext} should be used in a try-with-resources |
|
51 * statement. Failure to close a context will almost certainly result in foreign objects being |
|
52 * leaked. |
|
53 */ |
|
54 public class CompilationContext implements AutoCloseable { |
|
55 private final AutoCloseable impl; |
|
56 |
|
57 CompilationContext(AutoCloseable impl) { |
|
58 this.impl = impl; |
|
59 } |
|
60 |
|
61 @Override |
|
62 public void close() { |
|
63 try { |
|
64 impl.close(); |
|
65 } catch (Exception e) { |
|
66 GraalError.shouldNotReachHere(e); |
|
67 } |
|
68 } |
|
69 } |