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 
  27 package jdk.internal.foreign;
  28 
  29 import jdk.incubator.foreign.MemorySegment;
  30 import jdk.internal.access.JavaNioAccess;
  31 import jdk.internal.access.SharedSecrets;
  32 import jdk.internal.access.foreign.UnmapperProxy;
  33 import jdk.internal.misc.Unsafe;
  34 import sun.nio.ch.FileChannelImpl;
  35 import sun.security.action.GetBooleanAction;
  36 
  37 import java.io.IOException;
  38 import java.nio.ByteBuffer;
  39 import java.nio.channels.FileChannel;
  40 import java.nio.file.OpenOption;
  41 import java.nio.file.Path;
  42 import java.nio.file.StandardOpenOption;
  43 import java.util.function.Supplier;
  44 
  45 /**
  46  * This class contains misc helper functions to support creation of memory segments.
  47  */
  48 public final class Utils {
  49 
  50     private static Unsafe unsafe = Unsafe.getUnsafe();
  51 
  52     // The maximum alignment supported by malloc - typically 16 on 64-bit platforms.
  53     private final static long MAX_ALIGN = 16;
  54 
  55     private static final JavaNioAccess javaNioAccess = SharedSecrets.getJavaNioAccess();
  56 
  57     private static final boolean skipZeroMemory = GetBooleanAction.privilegedGetProperty("jdk.internal.foreign.skipZeroMemory");
  58 
  59     public static long alignUp(long n, long alignment) {
  60         return (n + alignment - 1) & -alignment;
  61     }
  62 
  63     public static long bitsToBytesOrThrow(long bits, Supplier<RuntimeException> exFactory) {
  64         if (bits % 8 == 0) {
  65             return bits / 8;
  66         } else {
  67             throw exFactory.get();
  68         }
  69     }
  70 
  71     // segment factories
  72 
  73     public static MemorySegment makeNativeSegment(long bytesSize, long alignmentBytes) {
  74         long alignedSize = bytesSize;
  75 
  76         if (alignmentBytes > MAX_ALIGN) {
  77             alignedSize = bytesSize + (alignmentBytes - 1);
  78         }
  79 
  80         long buf = unsafe.allocateMemory(alignedSize);
  81         if (!skipZeroMemory) {
  82             unsafe.setMemory(buf, alignedSize, (byte)0);
  83         }
  84         long alignedBuf = Utils.alignUp(buf, alignmentBytes);
  85         MemoryScope scope = new MemoryScope(null, () -> unsafe.freeMemory(buf));
  86         MemorySegment segment = new MemorySegmentImpl(buf, null, alignedSize, 0, Thread.currentThread(), scope);
  87         if (alignedBuf != buf) {
  88             long delta = alignedBuf - buf;
  89             segment = segment.asSlice(delta, bytesSize);
  90         }
  91         return segment;
  92     }
  93 
  94     public static MemorySegment makeArraySegment(byte[] arr) {
  95         return makeArraySegment(arr, arr.length, Unsafe.ARRAY_BYTE_BASE_OFFSET, Unsafe.ARRAY_BYTE_INDEX_SCALE);
  96     }
  97 
  98     public static MemorySegment makeArraySegment(char[] arr) {
  99         return makeArraySegment(arr, arr.length, Unsafe.ARRAY_CHAR_BASE_OFFSET, Unsafe.ARRAY_CHAR_INDEX_SCALE);
 100     }
 101 
 102     public static MemorySegment makeArraySegment(short[] arr) {
 103         return makeArraySegment(arr, arr.length, Unsafe.ARRAY_SHORT_BASE_OFFSET, Unsafe.ARRAY_SHORT_INDEX_SCALE);
 104     }
 105 
 106     public static MemorySegment makeArraySegment(int[] arr) {
 107         return makeArraySegment(arr, arr.length, Unsafe.ARRAY_INT_BASE_OFFSET, Unsafe.ARRAY_INT_INDEX_SCALE);
 108     }
 109 
 110     public static MemorySegment makeArraySegment(float[] arr) {
 111         return makeArraySegment(arr, arr.length, Unsafe.ARRAY_FLOAT_BASE_OFFSET, Unsafe.ARRAY_FLOAT_INDEX_SCALE);
 112     }
 113 
 114     public static MemorySegment makeArraySegment(long[] arr) {
 115         return makeArraySegment(arr, arr.length, Unsafe.ARRAY_LONG_BASE_OFFSET, Unsafe.ARRAY_LONG_INDEX_SCALE);
 116     }
 117 
 118     public static MemorySegment makeArraySegment(double[] arr) {
 119         return makeArraySegment(arr, arr.length, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, Unsafe.ARRAY_DOUBLE_INDEX_SCALE);
 120     }
 121 
 122     private static MemorySegment makeArraySegment(Object arr, int size, int base, int scale) {
 123         MemoryScope scope = new MemoryScope(null, null);
 124         return new MemorySegmentImpl(base, arr, size * scale, 0, Thread.currentThread(), scope);
 125     }
 126 
 127     public static MemorySegment makeBufferSegment(ByteBuffer bb) {
 128         long bbAddress = javaNioAccess.getBufferAddress(bb);
 129         Object base = javaNioAccess.getBufferBase(bb);
 130 
 131         int pos = bb.position();
 132         int limit = bb.limit();
 133 
 134         MemoryScope bufferScope = new MemoryScope(bb, null);
 135         return new MemorySegmentImpl(bbAddress + pos, base, limit - pos, 0, Thread.currentThread(), bufferScope);
 136     }
 137 
 138     // create and map a file into a fresh segment
 139     public static MemorySegment makeMappedSegment(Path path, long bytesSize, FileChannel.MapMode mapMode) throws IOException {
 140         if (bytesSize <= 0) throw new IllegalArgumentException("Requested bytes size must be > 0.");
 141         try (FileChannelImpl channelImpl = (FileChannelImpl)FileChannel.open(path, openOptions(mapMode))) {
 142             UnmapperProxy unmapperProxy = channelImpl.mapInternal(mapMode, 0L, bytesSize);
 143             MemoryScope scope = new MemoryScope(null, unmapperProxy::unmap);
 144             return new MemorySegmentImpl(unmapperProxy.address(), null, bytesSize, 0, Thread.currentThread(), scope);
 145         }
 146     }
 147 
 148     private static OpenOption[] openOptions(FileChannel.MapMode mapMode) {
 149         if (mapMode == FileChannel.MapMode.READ_ONLY) {
 150             return new OpenOption[] { StandardOpenOption.READ };
 151         } else if (mapMode == FileChannel.MapMode.READ_WRITE || mapMode == FileChannel.MapMode.PRIVATE) {
 152             return new OpenOption[] { StandardOpenOption.READ, StandardOpenOption.WRITE };
 153         } else {
 154             throw new UnsupportedOperationException("Unsupported map mode: " + mapMode);
 155         }
 156     }
 157 }