fb64 commented on code in PR #1334: URL: https://github.com/apache/arrow-java/pull/1334#discussion_r4250295934
########## memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/FfmMemoryAccessor.java: ########## @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.arrow.memory.ffm; + +import java.lang.foreign.MemorySegment; +import java.lang.foreign.ValueLayout; +import java.nio.ByteBuffer; +import org.apache.arrow.memory.util.MemoryUtilAccessor; + +/** + * {@link MemoryUtilAccessor} backed by {@code java.lang.foreign} ({@link MemorySegment} and the + * foreign {@link java.lang.foreign.Linker}). Does not use {@code sun.misc.Unsafe} or reflection + * into {@code java.nio} internals, so it requires neither {@code --add-opens} nor {@code + * sun.misc.Unsafe} availability. + * + * <p><b>Required JVM flag.</b> This accessor calls the restricted methods {@link + * MemorySegment#reinterpret(long)} and {@link java.lang.foreign.Linker#downcallHandle}, the latter + * for {@code malloc} and {@code free}. That is a real, current requirement, not a caveat that this + * module already handles for you: classpath (unnamed-module) consumers must pass {@code + * --enable-native-access=ALL-UNNAMED} and module-path consumers must pass {@code + * --enable-native-access=org.apache.arrow.memory.ffm} on the JVM command line. Without it the JVM + * prints a warning on every restricted call today, and a future JDK that enables restricted-method + * enforcement by default will turn that into a hard {@link IllegalCallerException}. The {@code + * Enable-Native-Access: ALL-UNNAMED} entry in this module's jar manifest does <em>not</em> cover + * this: the JVM only honours that attribute in the manifest of the jar it was launched with via + * {@code java -jar}, never for a jar that is merely a classpath dependency. + * + * <p>{@link #allocateMemory}/{@link #freeMemory} call {@code malloc}/{@code free} directly, like + * the {@code sun.misc.Unsafe}-backed accessor, so {@link #freeMemory} accepts any address returned + * by {@code malloc}. + */ +public final class FfmMemoryAccessor implements MemoryUtilAccessor { + + public static final MemoryUtilAccessor INSTANCE = new FfmMemoryAccessor(); + + private FfmMemoryAccessor() {} + + private static MemorySegment segment(long address, long byteSize) { Review Comment: I pushed the two fixes (described previously) that avoid creating a MemorySegment and duplicating the buffer. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
