< prev index next >

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

Print this page

 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 package jdk.internal.foreign;
 28 
 29 import jdk.incubator.foreign.MemoryAddress;
 30 import jdk.incubator.foreign.MemorySegment;
 31 import jdk.incubator.foreign.ResourceScope;
 32 import jdk.incubator.foreign.SegmentAllocator;
 33 import jdk.internal.misc.Unsafe;
 34 import jdk.internal.misc.VM;
 35 import jdk.internal.vm.annotation.ForceInline;
 36 import sun.security.action.GetBooleanAction;
 37 
 38 import java.nio.ByteBuffer;
 39 
 40 /**
 41  * Implementation for native memory segments. A native memory segment is essentially a wrapper around
 42  * a native long address.
 43  */
 44 public class NativeMemorySegmentImpl extends AbstractMemorySegmentImpl {
 45 
 46     public static final MemorySegment EVERYTHING = makeNativeSegmentUnchecked(MemoryAddress.NULL, Long.MAX_VALUE, null, ResourceScopeImpl.GLOBAL);
 47 
 48     private static final Unsafe unsafe = Unsafe.getUnsafe();
 49 
 50     public static final SegmentAllocator IMPLICIT_ALLOCATOR = (size, align) -> MemorySegment.allocateNative(size, align, ResourceScope.newImplicitScope());
 51 
 52     // The maximum alignment supported by malloc - typically 16 on
 53     // 64-bit platforms and 8 on 32-bit platforms.
 54     private static final long MAX_MALLOC_ALIGN = Unsafe.ADDRESS_SIZE == 4 ? 8 : 16;
 55 
 56     private static final boolean skipZeroMemory = GetBooleanAction.privilegedGetProperty("jdk.internal.foreign.skipZeroMemory");
 57 
 58     final long min;
 59 
 60     @ForceInline
 61     NativeMemorySegmentImpl(long min, long length, int mask, ResourceScopeImpl scope) {
 62         super(length, mask, scope);
 63         this.min = min;
 64     }
 65 







 66     @Override
 67     NativeMemorySegmentImpl dup(long offset, long size, int mask, ResourceScopeImpl scope) {
 68         return new NativeMemorySegmentImpl(min + offset, size, mask, scope);
 69     }
 70 
 71     @Override
 72     ByteBuffer makeByteBuffer() {
 73         return nioAccess.newDirectByteBuffer(min(), (int) this.length, null,
 74                 scope == ResourceScopeImpl.GLOBAL ? null : this);
 75     }
 76 
 77     @Override
 78     public boolean isNative() {
 79         return true;
 80     }
 81 
 82     @Override
 83     long min() {
 84         return min;
 85     }

106         if (!skipZeroMemory) {
107             unsafe.setMemory(buf, alignedSize, (byte)0);
108         }
109         long alignedBuf = Utils.alignUp(buf, alignmentBytes);
110         AbstractMemorySegmentImpl segment = new NativeMemorySegmentImpl(buf, alignedSize,
111                 defaultAccessModes(alignedSize), scope);
112         scope.addOrCleanupIfFail(new ResourceScopeImpl.ResourceList.ResourceCleanup() {
113             @Override
114             public void cleanup() {
115                 unsafe.freeMemory(buf);
116                 nioAccess.unreserveMemory(alignedSize, bytesSize);
117             }
118         });
119         if (alignedSize != bytesSize) {
120             long delta = alignedBuf - buf;
121             segment = segment.asSlice(delta, bytesSize);
122         }
123         return segment;
124     }
125 
126     public static MemorySegment makeNativeSegmentUnchecked(MemoryAddress min, long bytesSize, Runnable cleanupAction, ResourceScopeImpl scope) {
127         scope.checkValidStateSlow();
128         AbstractMemorySegmentImpl segment = new NativeMemorySegmentImpl(min.toRawLongValue(), bytesSize, defaultAccessModes(bytesSize), scope);
129         if (cleanupAction != null) {
130             scope.addCloseAction(cleanupAction);
131         }
132         return segment;
133     }
134 }

 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 package jdk.internal.foreign;
 28 
 29 import jdk.incubator.foreign.MemoryAddress;
 30 import jdk.incubator.foreign.MemorySegment;


 31 import jdk.internal.misc.Unsafe;
 32 import jdk.internal.misc.VM;
 33 import jdk.internal.vm.annotation.ForceInline;
 34 import sun.security.action.GetBooleanAction;
 35 
 36 import java.nio.ByteBuffer;
 37 
 38 /**
 39  * Implementation for native memory segments. A native memory segment is essentially a wrapper around
 40  * a native long address.
 41  */
 42 public class NativeMemorySegmentImpl extends AbstractMemorySegmentImpl {
 43 
 44     public static final MemorySegment EVERYTHING = makeNativeSegmentUnchecked(MemoryAddress.NULL, Long.MAX_VALUE, ResourceScopeImpl.GLOBAL);
 45 
 46     private static final Unsafe unsafe = Unsafe.getUnsafe();
 47 


 48     // The maximum alignment supported by malloc - typically 16 on
 49     // 64-bit platforms and 8 on 32-bit platforms.
 50     private static final long MAX_MALLOC_ALIGN = Unsafe.ADDRESS_SIZE == 4 ? 8 : 16;
 51 
 52     private static final boolean skipZeroMemory = GetBooleanAction.privilegedGetProperty("jdk.internal.foreign.skipZeroMemory");
 53 
 54     final long min;
 55 
 56     @ForceInline
 57     NativeMemorySegmentImpl(long min, long length, int mask, ResourceScopeImpl scope) {
 58         super(length, mask, scope);
 59         this.min = min;
 60     }
 61 
 62     @ForceInline
 63     @Override
 64     public MemoryAddress address() {
 65         checkValidState();
 66         return MemoryAddress.ofLong(unsafeGetOffset());
 67     }
 68 
 69     @Override
 70     NativeMemorySegmentImpl dup(long offset, long size, int mask, ResourceScopeImpl scope) {
 71         return new NativeMemorySegmentImpl(min + offset, size, mask, scope);
 72     }
 73 
 74     @Override
 75     ByteBuffer makeByteBuffer() {
 76         return nioAccess.newDirectByteBuffer(min(), (int) this.length, null,
 77                 scope == ResourceScopeImpl.GLOBAL ? null : this);
 78     }
 79 
 80     @Override
 81     public boolean isNative() {
 82         return true;
 83     }
 84 
 85     @Override
 86     long min() {
 87         return min;
 88     }

109         if (!skipZeroMemory) {
110             unsafe.setMemory(buf, alignedSize, (byte)0);
111         }
112         long alignedBuf = Utils.alignUp(buf, alignmentBytes);
113         AbstractMemorySegmentImpl segment = new NativeMemorySegmentImpl(buf, alignedSize,
114                 defaultAccessModes(alignedSize), scope);
115         scope.addOrCleanupIfFail(new ResourceScopeImpl.ResourceList.ResourceCleanup() {
116             @Override
117             public void cleanup() {
118                 unsafe.freeMemory(buf);
119                 nioAccess.unreserveMemory(alignedSize, bytesSize);
120             }
121         });
122         if (alignedSize != bytesSize) {
123             long delta = alignedBuf - buf;
124             segment = segment.asSlice(delta, bytesSize);
125         }
126         return segment;
127     }
128 
129     public static MemorySegment makeNativeSegmentUnchecked(MemoryAddress min, long bytesSize, ResourceScopeImpl scope) {
130         scope.checkValidStateSlow();
131         AbstractMemorySegmentImpl segment = new NativeMemorySegmentImpl(min.toRawLongValue(), bytesSize, defaultAccessModes(bytesSize), scope);



132         return segment;
133     }
134 }
< prev index next >