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.  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 java.foreign.memory;
  27 
  28 import jdk.internal.foreign.MemoryScopeImpl;
  29 
  30 import java.foreign.layout.Layout;
  31 import java.util.Optional;
  32 
  33 /**
  34  * A scope models a unit of resource lifecycle management. It provides primitives for memory allocation, as well
  35  * as a basic ownership model for allocating resources (e.g. pointers). Each scope has a parent scope (except the
  36  * global scope, which acts as root of the ownership model).
  37  * <p>
  38  *  A scope supports two terminal operation: first, a scope
  39  * can be closed (see {@link MemoryScope#close()}), which implies that all resources associated with that scope can be reclaimed; secondly,
  40  * a scope can be merged into the parent scope (see {@link MemoryScope#merge()}). After a terminal operation, a scope will no longer be available
  41  * for allocation.
  42  * <p>
  43  * Scope supports the {@link AutoCloseable} interface which enables thread-confided scopes to be used in conjunction
  44  * with the try-with-resources construct.
  45  */
  46 public interface MemoryScope extends AutoCloseable {
  47 
  48     /**
  49      * Allocate region of memory with given {@code LayoutType}.
  50      * @param layout the memory layout to be allocated.
  51      * @return an address pointing to the newly allocated memory region.
  52      */
  53     MemoryAddress allocate(Layout layout);
  54 
  55     /**
  56      * The parent of this scope.
  57      * @return the parent of this scope.
  58      */
  59     MemoryScope parent();
  60 
  61     /**
  62      * Returns the confinement thread (if any).
  63      * @return the confinement thread (if any).
  64      */
  65     Optional<Thread> confinementThread();
  66 
  67     /**
  68      * Closes this scope. All associated resources will be freed as a result of this operation.
  69      * Any existing resources (e.g. pointers) associated with this scope will no longer be accessible.
  70      *  As this is a terminal operation, this scope will no longer be available for further allocation.
  71      */
  72     @Override
  73     void close();
  74 
  75     /**
  76      * Copies all resources of this scope to the parent scope. As this is a terminal operation, this scope will no
  77      * longer be available for further allocation.
  78      */
  79     void merge();
  80 
  81     /**
  82      * Create a scope whose parent is the current scope.
  83      * @return the new scope.
  84      */
  85     MemoryScope fork();
  86 
  87     /**
  88      * Create a scope whose parent is the current scope, with given charateristics.
  89      * @param charateristics bitmask of the scope properties.
  90      * @return the new scope.
  91      */
  92     MemoryScope fork(long charateristics);
  93 
  94     /**
  95      * Retrieves the global scope associated with this VM.
  96      * @return the global scope.
  97      */
  98     static MemoryScope globalScope() {
  99         SecurityManager security = System.getSecurityManager();
 100         if (security != null) {
 101             security.checkPermission(new RuntimePermission("java.foreign.memory.MemoryScope", "globalScope"));
 102         }
 103         return MemoryScopeImpl.GLOBAL; //FIXME
 104     }
 105 
 106     /**
 107      * Returns the set of characteristics associated with this scope. Values can be {@link MemoryScope#ADDRESS_SERIALIZABLE},
 108      * {@link MemoryScope#EXECUTABLE}, {@link MemoryScope#UNALIGNED_ACCESS} and {@link MemoryScope#IMMUTABLE}. In the general case,
 109      * charateristics can only be made stricter when going from a parent scope to a children scope (see {@link MemoryScope#fork(long)}.
 110      * A notable exception to this is rule are {@link MemoryScope#IMMUTABLE} - which can be both enforced and relaxed by children to support
 111      * various immutability life-cycles, and {@link MemoryScope#PINNED} which can only ever be set in global scopes.
 112      * @return a rapresentation of the characteristics.
 113      */
 114     long characteristics();
 115 
 116     /**
 117      * Whether addresses generated by this scope can be serialized into memory. If unset, cannot be set during fork
 118      * (see {@link MemoryScope#fork()}.
 119      */
 120     long ADDRESS_SERIALIZABLE = 1;
 121 
 122     /**
 123      * Whether addresses generated by this scope can point to executable code. If unset, cannot be set during fork
 124      * (see {@link MemoryScope#fork()}.
 125      */
 126     long EXECUTABLE = ADDRESS_SERIALIZABLE << 1;
 127 
 128     /**
 129      * Whether accesses at addresses generated by this scope can be unaligned. If unset, cannot be set during fork
 130      * (see {@link MemoryScope#fork()}.
 131      */
 132     long UNALIGNED_ACCESS = EXECUTABLE << 1;
 133 
 134     /**
 135      * Whether addresses generated by this scope are read-only. Can be set or unset during fork.
 136      * This allows to create a temporary writeable scope which can then be merged (see {@link MemoryScope#merge()} into
 137      * an immutable parent scope; conversely, it allows to create immutable children scopes of mutable parent scopes. Under
 138      * this scenario, an immutable children of a mutable parent cannot be merged into it (doing so will effectively make
 139      * all pointers of the immutable scope writeable again) - the only terminal operation supported in such a scenario
 140      * is {@link MemoryScope#close}.
 141      */
 142     long IMMUTABLE = UNALIGNED_ACCESS << 1;
 143 
 144     /**
 145      * Whether this scope rejects terminal operations (see {@link MemoryScope#close()}, {@link MemoryScope#merge()}.
 146      */
 147     long PINNED = IMMUTABLE << 1;
 148 
 149     /**
 150      * Whether this scope ignores liveness checks. Can be useful in performance-sensitive context, where a user
 151      * might want to trade off safety for maximum performances.
 152      */
 153     long UNCHECKED = PINNED << 1;
 154 
 155     /**
 156      * Whether this scope allows access confined to given thread. Can be useful to restrict access to resources
 157      * managed by this scope to a single thread, to prevent memory races.
 158      */
 159     long CONFINED = UNCHECKED << 1;
 160 }