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