< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/AbstractMemorySegmentImpl.java

Print this page

 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 jdk.internal.foreign;
 27 
 28 import jdk.incubator.foreign.MemoryAddress;
 29 import jdk.incubator.foreign.MemorySegment;
 30 import jdk.incubator.foreign.SequenceLayout;
 31 import jdk.internal.access.JavaNioAccess;
 32 import jdk.internal.access.SharedSecrets;
 33 import jdk.internal.access.foreign.MemorySegmentProxy;
 34 import jdk.internal.access.foreign.UnmapperProxy;

 35 import jdk.internal.vm.annotation.ForceInline;
 36 import sun.security.action.GetPropertyAction;
 37 
 38 import java.lang.invoke.VarHandle;
 39 import java.nio.ByteBuffer;
 40 import java.util.ArrayList;
 41 import java.util.List;
 42 import java.util.Objects;
 43 import java.util.Random;
 44 import java.util.Spliterator;
 45 import java.util.function.Consumer;
 46 
 47 /**
 48  * This abstract class provides an immutable implementation for the {@code MemorySegment} interface. This class contains information
 49  * about the segment's spatial and temporal bounds; each memory segment implementation is associated with an owner thread which is set at creation time.
 50  * Access to certain sensitive operations on the memory segment will fail with {@code IllegalStateException} if the
 51  * segment is either in an invalid state (e.g. it has already been closed) or if access occurs from a thread other
 52  * than the owner thread. See {@link MemoryScope} for more details on management of temporal bounds. Subclasses
 53  * are defined for each memory segment kind, see {@link NativeMemorySegmentImpl}, {@link HeapMemorySegmentImpl} and
 54  * {@link MappedMemorySegmentImpl}.
 55  */
 56 public abstract class AbstractMemorySegmentImpl implements MemorySegment, MemorySegmentProxy {
 57 


 58     private static final boolean enableSmallSegments =
 59             Boolean.parseBoolean(GetPropertyAction.privilegedGetProperty("jdk.incubator.foreign.SmallSegments", "true"));
 60 
 61     final static int ACCESS_MASK = READ | WRITE | CLOSE | ACQUIRE | HANDOFF;
 62     final static int FIRST_RESERVED_FLAG = 1 << 16; // upper 16 bits are reserved
 63     final static int SMALL = FIRST_RESERVED_FLAG;
 64     final static long NONCE = new Random().nextLong();
 65     final static int DEFAULT_MASK = READ | WRITE | CLOSE | ACQUIRE | HANDOFF;
 66 
 67     final static JavaNioAccess nioAccess = SharedSecrets.getJavaNioAccess();
 68 
 69     final long length;
 70     final int mask;
 71     final Thread owner;
 72     final MemoryScope scope;
 73 
 74     @ForceInline
 75     AbstractMemorySegmentImpl(long length, int mask, Thread owner, MemoryScope scope) {
 76         this.length = length;
 77         this.mask = mask;

 96     @Override
 97     public AbstractMemorySegmentImpl asSlice(long offset, long newSize) {
 98         checkBounds(offset, newSize);
 99         return asSliceNoCheck(offset, newSize);
100     }
101 
102     private AbstractMemorySegmentImpl asSliceNoCheck(long offset, long newSize) {
103         return dup(offset, newSize, mask, owner, scope);
104     }
105 
106     @SuppressWarnings("unchecked")
107     public static <S extends MemorySegment> Spliterator<S> spliterator(S segment, SequenceLayout sequenceLayout) {
108         ((AbstractMemorySegmentImpl)segment).checkValidState();
109         if (sequenceLayout.byteSize() != segment.byteSize()) {
110             throw new IllegalArgumentException();
111         }
112         return (Spliterator<S>)new SegmentSplitter(sequenceLayout.elementLayout().byteSize(), sequenceLayout.elementCount().getAsLong(),
113                 (AbstractMemorySegmentImpl)segment.withAccessModes(segment.accessModes() & ~CLOSE));
114     }
115 









116     @Override
117     @ForceInline
118     public final MemoryAddress baseAddress() {
119         return new MemoryAddressImpl(this, 0);
120     }
121 
122     @Override
123     public final ByteBuffer asByteBuffer() {
124         if (!isSet(READ)) {
125             throw unsupportedAccessMode(READ);
126         }
127         checkIntSize("ByteBuffer");
128         ByteBuffer _bb = makeByteBuffer();
129         if (!isSet(WRITE)) {
130             //scope is IMMUTABLE - obtain a RO byte buffer
131             _bb = _bb.asReadOnlyBuffer();
132         }
133         return _bb;
134     }
135 

 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 jdk.internal.foreign;
 27 
 28 import jdk.incubator.foreign.MemoryAddress;
 29 import jdk.incubator.foreign.MemorySegment;
 30 import jdk.incubator.foreign.SequenceLayout;
 31 import jdk.internal.access.JavaNioAccess;
 32 import jdk.internal.access.SharedSecrets;
 33 import jdk.internal.access.foreign.MemorySegmentProxy;
 34 import jdk.internal.access.foreign.UnmapperProxy;
 35 import jdk.internal.misc.Unsafe;
 36 import jdk.internal.vm.annotation.ForceInline;
 37 import sun.security.action.GetPropertyAction;
 38 
 39 import java.lang.invoke.VarHandle;
 40 import java.nio.ByteBuffer;
 41 import java.util.ArrayList;
 42 import java.util.List;
 43 import java.util.Objects;
 44 import java.util.Random;
 45 import java.util.Spliterator;
 46 import java.util.function.Consumer;
 47 
 48 /**
 49  * This abstract class provides an immutable implementation for the {@code MemorySegment} interface. This class contains information
 50  * about the segment's spatial and temporal bounds; each memory segment implementation is associated with an owner thread which is set at creation time.
 51  * Access to certain sensitive operations on the memory segment will fail with {@code IllegalStateException} if the
 52  * segment is either in an invalid state (e.g. it has already been closed) or if access occurs from a thread other
 53  * than the owner thread. See {@link MemoryScope} for more details on management of temporal bounds. Subclasses
 54  * are defined for each memory segment kind, see {@link NativeMemorySegmentImpl}, {@link HeapMemorySegmentImpl} and
 55  * {@link MappedMemorySegmentImpl}.
 56  */
 57 public abstract class AbstractMemorySegmentImpl implements MemorySegment, MemorySegmentProxy {
 58 
 59     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
 60 
 61     private static final boolean enableSmallSegments =
 62             Boolean.parseBoolean(GetPropertyAction.privilegedGetProperty("jdk.incubator.foreign.SmallSegments", "true"));
 63 
 64     final static int ACCESS_MASK = READ | WRITE | CLOSE | ACQUIRE | HANDOFF;
 65     final static int FIRST_RESERVED_FLAG = 1 << 16; // upper 16 bits are reserved
 66     final static int SMALL = FIRST_RESERVED_FLAG;
 67     final static long NONCE = new Random().nextLong();
 68     final static int DEFAULT_MASK = READ | WRITE | CLOSE | ACQUIRE | HANDOFF;
 69 
 70     final static JavaNioAccess nioAccess = SharedSecrets.getJavaNioAccess();
 71 
 72     final long length;
 73     final int mask;
 74     final Thread owner;
 75     final MemoryScope scope;
 76 
 77     @ForceInline
 78     AbstractMemorySegmentImpl(long length, int mask, Thread owner, MemoryScope scope) {
 79         this.length = length;
 80         this.mask = mask;

 99     @Override
100     public AbstractMemorySegmentImpl asSlice(long offset, long newSize) {
101         checkBounds(offset, newSize);
102         return asSliceNoCheck(offset, newSize);
103     }
104 
105     private AbstractMemorySegmentImpl asSliceNoCheck(long offset, long newSize) {
106         return dup(offset, newSize, mask, owner, scope);
107     }
108 
109     @SuppressWarnings("unchecked")
110     public static <S extends MemorySegment> Spliterator<S> spliterator(S segment, SequenceLayout sequenceLayout) {
111         ((AbstractMemorySegmentImpl)segment).checkValidState();
112         if (sequenceLayout.byteSize() != segment.byteSize()) {
113             throw new IllegalArgumentException();
114         }
115         return (Spliterator<S>)new SegmentSplitter(sequenceLayout.elementLayout().byteSize(), sequenceLayout.elementCount().getAsLong(),
116                 (AbstractMemorySegmentImpl)segment.withAccessModes(segment.accessModes() & ~CLOSE));
117     }
118 
119     public static void fill(MemorySegment segment, byte value) {
120         AbstractMemorySegmentImpl segmentImpl = (AbstractMemorySegmentImpl) segment;
121         segmentImpl.checkValidState();
122         if (!segmentImpl.isSet(WRITE)) {
123             throw segmentImpl.unsupportedAccessMode(WRITE);
124         }
125         UNSAFE.setMemory(segmentImpl.base(), segmentImpl.min(), segmentImpl.length, value);
126     }
127 
128     @Override
129     @ForceInline
130     public final MemoryAddress baseAddress() {
131         return new MemoryAddressImpl(this, 0);
132     }
133 
134     @Override
135     public final ByteBuffer asByteBuffer() {
136         if (!isSet(READ)) {
137             throw unsupportedAccessMode(READ);
138         }
139         checkIntSize("ByteBuffer");
140         ByteBuffer _bb = makeByteBuffer();
141         if (!isSet(WRITE)) {
142             //scope is IMMUTABLE - obtain a RO byte buffer
143             _bb = _bb.asReadOnlyBuffer();
144         }
145         return _bb;
146     }
147 
< prev index next >