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
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) {
|
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
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) {
|