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