fb64 commented on code in PR #1334: URL: https://github.com/apache/arrow-java/pull/1334#discussion_r4208963735
########## memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/NativeMemory.java: ########## @@ -0,0 +1,83 @@ +/* + * 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.FunctionDescriptor; +import java.lang.foreign.Linker; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.ValueLayout; +import java.lang.invoke.MethodHandle; + +/** + * Native memory from the C library's {@code malloc} and {@code free}, called through the foreign + * {@link Linker}. + * + * <p>An {@link java.lang.foreign.Arena} per buffer is avoided on purpose: closing a shared arena + * handshakes with every JVM thread, which makes freeing orders of magnitude slower than {@code + * sun.misc.Unsafe}, and its allocations are always zeroed. + */ +final class NativeMemory { + + private static final Linker LINKER = Linker.nativeLinker(); + private static final MethodHandle MALLOC = + LINKER.downcallHandle( + find("malloc"), FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG)); + private static final MethodHandle FREE = + LINKER.downcallHandle(find("free"), FunctionDescriptor.ofVoid(ValueLayout.ADDRESS)); + + private NativeMemory() {} + + private static MemorySegment find(String name) { + return LINKER + .defaultLookup() + .find(name) + .orElseThrow(() -> new IllegalStateException("C library function not found: " + name)); + } + + /** + * Returns the address of {@code bytes} of uninitialized native memory. + * + * @throws OutOfMemoryError if {@code malloc} cannot allocate them, like {@code + * sun.misc.Unsafe#allocateMemory} + */ + static long allocate(long bytes) { Review Comment: Good catch, done: `NativeMemory.allocate` now throws `IllegalArgumentException` for a negative size, like `Unsafe.allocateMemory`, with a test. Before, the `OutOfMemoryError` was even treated as fatal by JUnit and killed the test JVM 😅 -- 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]
