1 /*
  2  *  Copyright (c) 2020, 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 
 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     }
 86 
 87     @Override
 88     Object base() {
 89         return null;
 90     }
 91 
 92     // factories
 93 
 94     public static MemorySegment makeNativeSegment(long bytesSize, long alignmentBytes, ResourceScopeImpl scope) {
 95         scope.checkValidStateSlow();
 96         if (VM.isDirectMemoryPageAligned()) {
 97             alignmentBytes = Math.max(alignmentBytes, nioAccess.pageSize());
 98         }
 99         long alignedSize = alignmentBytes > MAX_MALLOC_ALIGN ?
100                 bytesSize + (alignmentBytes - 1) :
101                 bytesSize;
102 
103         nioAccess.reserveMemory(alignedSize, bytesSize);
104 
105         long buf = unsafe.allocateMemory(alignedSize);
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 }