(long, int) not available");
- }
-
- @SuppressWarnings(
- "nullness:argument") // to handle null assignment on third party dependency: Unsafe
- private static void copyMemory(
- @Nullable Object srcBase,
- long srcOffset,
- @Nullable Object destBase,
- long destOffset,
- long bytes) {
- UNSAFE.copyMemory(srcBase, srcOffset, destBase, destOffset, bytes);
+ return ACCESSOR.directBuffer(address, capacity);
}
public static void copyMemory(long srcAddress, long destAddress, long bytes) {
- UNSAFE.copyMemory(srcAddress, destAddress, bytes);
+ ACCESSOR.copyMemory(srcAddress, destAddress, bytes);
}
public static void copyToMemory(byte[] src, long srcIndex, long destAddress, long bytes) {
- copyMemory(src, BYTE_ARRAY_BASE_OFFSET + srcIndex, null, destAddress, bytes);
+ ACCESSOR.copyToMemory(src, srcIndex, destAddress, bytes);
}
public static void copyFromMemory(long srcAddress, byte[] dest, long destIndex, long bytes) {
- copyMemory(null, srcAddress, dest, BYTE_ARRAY_BASE_OFFSET + destIndex, bytes);
+ ACCESSOR.copyFromMemory(srcAddress, dest, destIndex, bytes);
}
public static byte getByte(long address) {
- return UNSAFE.getByte(address);
+ return ACCESSOR.getByte(address);
}
public static void putByte(long address, byte value) {
- UNSAFE.putByte(address, value);
+ ACCESSOR.putByte(address, value);
}
public static short getShort(long address) {
- return UNSAFE.getShort(address);
+ return ACCESSOR.getShort(address);
}
public static void putShort(long address, short value) {
- UNSAFE.putShort(address, value);
+ ACCESSOR.putShort(address, value);
}
public static int getInt(long address) {
- return UNSAFE.getInt(address);
+ return ACCESSOR.getInt(address);
}
public static void putInt(long address, int value) {
- UNSAFE.putInt(address, value);
+ ACCESSOR.putInt(address, value);
}
public static long getLong(long address) {
- return UNSAFE.getLong(address);
+ return ACCESSOR.getLong(address);
}
public static void putLong(long address, long value) {
- UNSAFE.putLong(address, value);
+ ACCESSOR.putLong(address, value);
}
public static void setMemory(long address, long bytes, byte value) {
- UNSAFE.setMemory(address, bytes, value);
+ ACCESSOR.setMemory(address, bytes, value);
}
public static int getInt(byte[] bytes, int index) {
- return UNSAFE.getInt(bytes, BYTE_ARRAY_BASE_OFFSET + index);
+ return ACCESSOR.getInt(bytes, index);
}
public static long getLong(byte[] bytes, int index) {
- return UNSAFE.getLong(bytes, BYTE_ARRAY_BASE_OFFSET + index);
+ return ACCESSOR.getLong(bytes, index);
}
public static long allocateMemory(long bytes) {
- return UNSAFE.allocateMemory(bytes);
+ return ACCESSOR.allocateMemory(bytes);
}
+ /**
+ * Frees native memory at the given address.
+ *
+ * Behavior depends on the accessor selected by {@value #MEMORY_ACCESSOR_TYPE_PROPERTY_NAME}.
+ * The default Unsafe accessor frees any valid native address, whatever allocated it. The FFM
+ * accessor ({@code org.apache.arrow.memory.ffm.FfmMemoryAccessor}) only frees addresses that came
+ * from its own {@link #allocateMemory}, because it releases the owning {@code Arena} rather than
+ * the address; an address from any other source (JNI, a foreign {@code malloc}) is a silent no-op
+ * there.
+ */
public static void freeMemory(long address) {
- UNSAFE.freeMemory(address);
+ ACCESSOR.freeMemory(address);
}
}
diff --git a/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtilAccessor.java b/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtilAccessor.java
new file mode 100644
index 0000000000..10acddbc52
--- /dev/null
+++ b/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtilAccessor.java
@@ -0,0 +1,62 @@
+/*
+ * 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.util;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Pluggable backend for {@link MemoryUtil}'s low-level memory operations. The default
+ * implementation ({@link UnsafeMemoryAccessor}) is backed by {@code sun.misc.Unsafe}; the {@code
+ * arrow-memory-ffm} module supplies an alternative backed by {@code java.lang.foreign}.
+ */
+public interface MemoryUtilAccessor {
+ long allocateMemory(long bytes);
+
+ void freeMemory(long address);
+
+ byte getByte(long address);
+
+ void putByte(long address, byte value);
+
+ short getShort(long address);
+
+ void putShort(long address, short value);
+
+ int getInt(long address);
+
+ void putInt(long address, int value);
+
+ long getLong(long address);
+
+ void putLong(long address, long value);
+
+ void setMemory(long address, long bytes, byte value);
+
+ void copyMemory(long srcAddress, long destAddress, long bytes);
+
+ void copyToMemory(byte[] src, long srcIndex, long destAddress, long bytes);
+
+ void copyFromMemory(long srcAddress, byte[] dest, long destIndex, long bytes);
+
+ int getInt(byte[] bytes, int index);
+
+ long getLong(byte[] bytes, int index);
+
+ long getByteBufferAddress(ByteBuffer buf);
+
+ ByteBuffer directBuffer(long address, int capacity);
+}
diff --git a/memory/memory-core/src/main/java/org/apache/arrow/memory/util/UnsafeMemoryAccessor.java b/memory/memory-core/src/main/java/org/apache/arrow/memory/util/UnsafeMemoryAccessor.java
new file mode 100644
index 0000000000..08474f2f46
--- /dev/null
+++ b/memory/memory-core/src/main/java/org/apache/arrow/memory/util/UnsafeMemoryAccessor.java
@@ -0,0 +1,265 @@
+/*
+ * 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.util;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InaccessibleObjectException;
+import java.lang.reflect.InvocationTargetException;
+import java.nio.ByteBuffer;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import sun.misc.Unsafe;
+
+/** {@link MemoryUtilAccessor} backed by {@code sun.misc.Unsafe}. The default accessor. */
+final class UnsafeMemoryAccessor implements MemoryUtilAccessor {
+ private static final org.slf4j.Logger logger =
+ org.slf4j.LoggerFactory.getLogger(UnsafeMemoryAccessor.class);
+
+ static final MemoryUtilAccessor INSTANCE = new UnsafeMemoryAccessor();
+
+ private static final @Nullable Constructor> DIRECT_BUFFER_CONSTRUCTOR;
+ private static final Unsafe UNSAFE;
+ private static final long BYTE_ARRAY_BASE_OFFSET;
+ private static final long BYTE_BUFFER_ADDRESS_OFFSET;
+
+ // Java 1.8, 9, 11, 17, 21 becomes 1, 9, 11, 17, and 21.
+ @SuppressWarnings("StringSplitter")
+ private static final int majorVersion =
+ Integer.parseInt(System.getProperty("java.specification.version").split("\\D+")[0]);
+
+ static {
+ try {
+ // try to get the unsafe object
+ final Object maybeUnsafe =
+ AccessController.doPrivileged(
+ new PrivilegedAction() {
+ @Override
+ @SuppressWarnings({"nullness:argument", "nullness:return"})
+ // incompatible argument for parameter obj of Field.get
+ // incompatible types in return
+ public Object run() {
+ try {
+ final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
+ unsafeField.setAccessible(true);
+ return unsafeField.get(null);
+ } catch (Throwable e) {
+ return e;
+ }
+ }
+ });
+
+ if (maybeUnsafe instanceof Throwable) {
+ throw (Throwable) maybeUnsafe;
+ }
+
+ UNSAFE = (Unsafe) maybeUnsafe;
+
+ // get the offset of the data inside a byte array object
+ BYTE_ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
+
+ // get the offset of the address field in a java.nio.Buffer object
+ long maybeOffset;
+ Field addressField = java.nio.Buffer.class.getDeclaredField("address");
+ try {
+ addressField.setAccessible(true);
+ maybeOffset = UNSAFE.objectFieldOffset(addressField);
+ } catch (InaccessibleObjectException e) {
+ maybeOffset = -1;
+ logger.debug(
+ "Cannot access the address field of java.nio.Buffer. DirectBuffer operations wont be available",
+ e);
+ }
+ BYTE_BUFFER_ADDRESS_OFFSET = maybeOffset;
+
+ Constructor> directBufferConstructor;
+ long address = -1;
+ final ByteBuffer direct = ByteBuffer.allocateDirect(1);
+ try {
+
+ final Object maybeDirectBufferConstructor =
+ AccessController.doPrivileged(
+ new PrivilegedAction() {
+ @Override
+ public Object run() {
+ try {
+ final Constructor> constructor =
+ (majorVersion >= 21)
+ ? direct.getClass().getDeclaredConstructor(long.class, long.class)
+ : direct.getClass().getDeclaredConstructor(long.class, int.class);
+ constructor.setAccessible(true);
+ logger.debug("Constructor for direct buffer found and made accessible");
+ return constructor;
+ } catch (NoSuchMethodException e) {
+ logger.debug("Cannot get constructor for direct buffer allocation", e);
+ return e;
+ } catch (SecurityException e) {
+ logger.debug("Cannot get constructor for direct buffer allocation", e);
+ return e;
+ } catch (InaccessibleObjectException e) {
+ logger.debug("Cannot get constructor for direct buffer allocation", e);
+ return e;
+ }
+ }
+ });
+
+ if (maybeDirectBufferConstructor instanceof Constructor>) {
+ address = UNSAFE.allocateMemory(1);
+ // try to use the constructor now
+ try {
+ ((Constructor>) maybeDirectBufferConstructor).newInstance(address, 1);
+ directBufferConstructor = (Constructor>) maybeDirectBufferConstructor;
+ logger.debug("direct buffer constructor: available");
+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
+ logger.warn("unable to instantiate a direct buffer via constructor", e);
+ directBufferConstructor = null;
+ }
+ } else {
+ logger.debug(
+ "direct buffer constructor: unavailable", (Throwable) maybeDirectBufferConstructor);
+ directBufferConstructor = null;
+ }
+ } finally {
+ if (address != -1) {
+ UNSAFE.freeMemory(address);
+ }
+ }
+ DIRECT_BUFFER_CONSTRUCTOR = directBufferConstructor;
+ } catch (Throwable e) {
+ // This exception will get swallowed, but it's necessary for the static analysis that ensures
+ // the static fields above get initialized
+ final RuntimeException failure =
+ new RuntimeException(
+ "Failed to initialize MemoryUtil. You must start Java with "
+ + "`--add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED` "
+ + "(See https://arrow.apache.org/docs/java/install.html)",
+ e);
+ failure.printStackTrace();
+ throw failure;
+ }
+ }
+
+ private UnsafeMemoryAccessor() {}
+
+ @Override
+ public long getByteBufferAddress(ByteBuffer buf) {
+ if (BYTE_BUFFER_ADDRESS_OFFSET != -1) {
+ return UNSAFE.getLong(buf, BYTE_BUFFER_ADDRESS_OFFSET);
+ }
+ throw new UnsupportedOperationException(
+ "Byte buffer address cannot be obtained because sun.misc.Unsafe or java.nio.DirectByteBuffer.(long, int) is not available");
+ }
+
+ @Override
+ public ByteBuffer directBuffer(long address, int capacity) {
+ if (DIRECT_BUFFER_CONSTRUCTOR != null) {
+ if (capacity < 0) {
+ throw new IllegalArgumentException("Capacity is negative, has to be positive or 0");
+ }
+ try {
+ return (ByteBuffer) DIRECT_BUFFER_CONSTRUCTOR.newInstance(address, capacity);
+ } catch (Throwable cause) {
+ throw new Error(cause);
+ }
+ }
+ throw new UnsupportedOperationException(
+ "sun.misc.Unsafe or java.nio.DirectByteBuffer.(long, int) not available");
+ }
+
+ @Override
+ public void copyMemory(long srcAddress, long destAddress, long bytes) {
+ UNSAFE.copyMemory(srcAddress, destAddress, bytes);
+ }
+
+ @Override
+ @SuppressWarnings("nullness:argument")
+ public void copyToMemory(byte[] src, long srcIndex, long destAddress, long bytes) {
+ UNSAFE.copyMemory(src, BYTE_ARRAY_BASE_OFFSET + srcIndex, null, destAddress, bytes);
+ }
+
+ @Override
+ @SuppressWarnings("nullness:argument")
+ public void copyFromMemory(long srcAddress, byte[] dest, long destIndex, long bytes) {
+ UNSAFE.copyMemory(null, srcAddress, dest, BYTE_ARRAY_BASE_OFFSET + destIndex, bytes);
+ }
+
+ @Override
+ public byte getByte(long address) {
+ return UNSAFE.getByte(address);
+ }
+
+ @Override
+ public void putByte(long address, byte value) {
+ UNSAFE.putByte(address, value);
+ }
+
+ @Override
+ public short getShort(long address) {
+ return UNSAFE.getShort(address);
+ }
+
+ @Override
+ public void putShort(long address, short value) {
+ UNSAFE.putShort(address, value);
+ }
+
+ @Override
+ public int getInt(long address) {
+ return UNSAFE.getInt(address);
+ }
+
+ @Override
+ public void putInt(long address, int value) {
+ UNSAFE.putInt(address, value);
+ }
+
+ @Override
+ public long getLong(long address) {
+ return UNSAFE.getLong(address);
+ }
+
+ @Override
+ public void putLong(long address, long value) {
+ UNSAFE.putLong(address, value);
+ }
+
+ @Override
+ public void setMemory(long address, long bytes, byte value) {
+ UNSAFE.setMemory(address, bytes, value);
+ }
+
+ @Override
+ public int getInt(byte[] bytes, int index) {
+ return UNSAFE.getInt(bytes, BYTE_ARRAY_BASE_OFFSET + index);
+ }
+
+ @Override
+ public long getLong(byte[] bytes, int index) {
+ return UNSAFE.getLong(bytes, BYTE_ARRAY_BASE_OFFSET + index);
+ }
+
+ @Override
+ public long allocateMemory(long bytes) {
+ return UNSAFE.allocateMemory(bytes);
+ }
+
+ @Override
+ public void freeMemory(long address) {
+ UNSAFE.freeMemory(address);
+ }
+}
diff --git a/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestMemoryUtilAccessorResolution.java b/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestMemoryUtilAccessorResolution.java
new file mode 100644
index 0000000000..ed299eceeb
--- /dev/null
+++ b/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestMemoryUtilAccessorResolution.java
@@ -0,0 +1,41 @@
+/*
+ * 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.util;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+public class TestMemoryUtilAccessorResolution {
+
+ @Test
+ public void unknownFfmAccessorTypeFailsWithActionableMessage() {
+ // This test runs in its own JVM (see memory-core/pom.xml Surefire execution added below)
+ // with -Darrow.memory.accessor.type=FFM and arrow-memory-ffm NOT on the classpath.
+ //
+ // MemoryUtil.getByte(0) triggers MemoryUtil class initialization for the first time in this
+ // JVM, which resolves the accessor eagerly via a static final field. Since the resolution
+ // failure happens inside , the JVM wraps the RuntimeException thrown by
+ // resolveAccessor()/loadFfmAccessor() in an ExceptionInInitializerError (JLS 12.4.2); the
+ // original RuntimeException with the actionable message is available as its cause.
+ Throwable thrown = assertThrows(Throwable.class, () -> MemoryUtil.getByte(0));
+ Throwable cause = thrown instanceof ExceptionInInitializerError ? thrown.getCause() : thrown;
+ assertTrue(cause instanceof RuntimeException);
+ assertTrue(cause.getMessage() != null && cause.getMessage().contains("arrow-memory-ffm"));
+ }
+}
diff --git a/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestMemoryUtilAllocationManagerFallback.java b/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestMemoryUtilAllocationManagerFallback.java
new file mode 100644
index 0000000000..c956b3f3bb
--- /dev/null
+++ b/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestMemoryUtilAllocationManagerFallback.java
@@ -0,0 +1,38 @@
+/*
+ * 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.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class TestMemoryUtilAllocationManagerFallback {
+
+ @Test
+ public void allocationManagerTypeFfmFallsBackToUnsafeWithoutFfmOnClasspath() {
+ // This test runs in its own JVM (see memory-core/pom.xml Surefire execution added below)
+ // with -Darrow.allocation.manager.type=FFM, arrow.memory.accessor.type left unset, and
+ // arrow-memory-ffm NOT on the classpath.
+ //
+ // Unlike an explicit arrow.memory.accessor.type=FFM request, this preference is only inferred
+ // from a different property. MemoryUtil must fall back to Unsafe instead of failing inside
+ // , which would otherwise permanently poison the class for the rest of the JVM's life
+ // (JLS 12.4.2) over a signal that may not even be load-bearing.
+ assertEquals(
+ "org.apache.arrow.memory.util.UnsafeMemoryAccessor", MemoryUtil.getAccessorClassName());
+ }
+}
diff --git a/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestMemoryUtilInferredFfmAccessorFallback.java b/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestMemoryUtilInferredFfmAccessorFallback.java
new file mode 100644
index 0000000000..09c693fb95
--- /dev/null
+++ b/memory/memory-core/src/test/java/org/apache/arrow/memory/util/TestMemoryUtilInferredFfmAccessorFallback.java
@@ -0,0 +1,59 @@
+/*
+ * 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.util;
+
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import org.apache.arrow.memory.DefaultAllocationManagerOption.AllocationManagerType;
+import org.junit.jupiter.api.Test;
+
+/**
+ * When FFM is only inferred from arrow.allocation.manager.type=FFM, a failure to load the FFM
+ * accessor must fall back to Unsafe, including the {@link LinkageError}s a JVM throws for it.
+ */
+public class TestMemoryUtilInferredFfmAccessorFallback {
+
+ @Test
+ public void fallsBackToUnsafeWhenFfmAccessorTargetsANewerJdk() {
+ // What JDK 21 and earlier throw when loading arrow-memory-ffm, which is compiled for release 22
+ MemoryUtilAccessor accessor =
+ MemoryUtil.resolveAccessor(
+ "",
+ AllocationManagerType.FFM,
+ () -> {
+ throw new UnsupportedClassVersionError(
+ "org/apache/arrow/memory/ffm/FfmMemoryAccessor has been compiled by a more"
+ + " recent version of the Java Runtime (class file version 66.0)");
+ });
+
+ assertSame(UnsafeMemoryAccessor.INSTANCE, accessor);
+ }
+
+ @Test
+ public void fallsBackToUnsafeWhenFfmAccessorInitializationFails() {
+ MemoryUtilAccessor accessor =
+ MemoryUtil.resolveAccessor(
+ "",
+ AllocationManagerType.FFM,
+ () -> {
+ throw new ExceptionInInitializerError(
+ new IllegalCallerException("Illegal native access"));
+ });
+
+ assertSame(UnsafeMemoryAccessor.INSTANCE, accessor);
+ }
+}
diff --git a/memory/memory-ffm/pom.xml b/memory/memory-ffm/pom.xml
new file mode 100644
index 0000000000..b4b821add5
--- /dev/null
+++ b/memory/memory-ffm/pom.xml
@@ -0,0 +1,155 @@
+
+
+
+ 4.0.0
+
+ org.apache.arrow
+ arrow-memory
+ 20.0.0-SNAPSHOT
+
+
+ arrow-memory-ffm
+ Arrow Memory - FFM
+ Allocator and utils for allocating memory in Arrow based on the Java Foreign Function and Memory API
+
+
+
+ 22
+
+
+
+
+ org.apache.arrow
+ arrow-memory-core
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+
+
+ ALL-UNNAMED
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+
+
+
+ **/TestMemoryUtilFfmWiring.java
+ **/TestFfmArrowBufByteBuffer.java
+
+ **/TestAllocationManagerTypeImpliesAccessor.java
+
+ **/TestFfmMemoryAccessorWithoutAddOpens.java
+
+ **/TestFfmAllocationManagerFactoryFirstAccess.java
+
+
+
+
+
+ ffm-accessor-wiring-test
+
+ test
+
+ test
+
+
+
+ **/TestMemoryUtilFfmWiring.java
+ **/TestFfmArrowBufByteBuffer.java
+
+
+ FFM
+
+
+
+
+
+ allocation-manager-type-implies-accessor-test
+
+ test
+
+ test
+
+
+
+ **/TestAllocationManagerTypeImpliesAccessor.java
+
+
+ FFM
+
+
+
+
+
+ no-add-opens-tests
+
+ test
+
+ test
+
+
+
+
+ **/TestFfmMemoryAccessorWithoutAddOpens.java
+
+
+
+
+
+ factory-first-access-test
+
+ test
+
+ test
+
+
+
+ **/TestFfmAllocationManagerFactoryFirstAccess.java
+
+
+
+
+
+
+
+
diff --git a/memory/memory-ffm/src/main/java/module-info.java b/memory/memory-ffm/src/main/java/module-info.java
new file mode 100644
index 0000000000..7fa26e4ce3
--- /dev/null
+++ b/memory/memory-ffm/src/main/java/module-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+module org.apache.arrow.memory.ffm {
+ exports org.apache.arrow.memory.ffm to
+ org.apache.arrow.memory.core;
+
+ requires org.apache.arrow.memory.core;
+}
diff --git a/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/DefaultAllocationManagerFactory.java b/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/DefaultAllocationManagerFactory.java
new file mode 100644
index 0000000000..41ff8cdc47
--- /dev/null
+++ b/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/DefaultAllocationManagerFactory.java
@@ -0,0 +1,37 @@
+/*
+ * 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 org.apache.arrow.memory.AllocationManager;
+import org.apache.arrow.memory.ArrowBuf;
+import org.apache.arrow.memory.BufferAllocator;
+
+/** The default Allocation Manager Factory for this module. */
+public class DefaultAllocationManagerFactory implements AllocationManager.Factory {
+
+ public static final AllocationManager.Factory FACTORY = FfmAllocationManager.FACTORY;
+
+ @Override
+ public AllocationManager create(BufferAllocator accountingAllocator, long size) {
+ return FACTORY.create(accountingAllocator, size);
+ }
+
+ @Override
+ public ArrowBuf empty() {
+ return FfmAllocationManager.FACTORY.empty();
+ }
+}
diff --git a/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/FfmAllocationManager.java b/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/FfmAllocationManager.java
new file mode 100644
index 0000000000..20f44d3590
--- /dev/null
+++ b/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/FfmAllocationManager.java
@@ -0,0 +1,71 @@
+/*
+ * 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 org.apache.arrow.memory.AllocationManager;
+import org.apache.arrow.memory.ArrowBuf;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.ReferenceManager;
+
+/**
+ * Allocation manager based on the Foreign Function & Memory API: buffers are allocated with the
+ * C library's {@code malloc} and freed with {@code free}, both called through the foreign linker.
+ */
+public final class FfmAllocationManager extends AllocationManager {
+
+ // Must be initialized before EMPTY: creating an ArrowBuf may initialize BaseAllocator, whose
+ // default config can read FACTORY back while this class is still initializing.
+ public static final AllocationManager.Factory FACTORY =
+ new Factory() {
+ @Override
+ public AllocationManager create(BufferAllocator accountingAllocator, long size) {
+ return new FfmAllocationManager(accountingAllocator, size);
+ }
+
+ @Override
+ public ArrowBuf empty() {
+ return EMPTY;
+ }
+ };
+
+ private static final ArrowBuf EMPTY =
+ new ArrowBuf(ReferenceManager.NO_OP, null, 0, NativeMemory.allocate(0));
+
+ private final long allocatedSize;
+ private final long allocatedAddress;
+
+ FfmAllocationManager(BufferAllocator accountingAllocator, long requestedSize) {
+ super(accountingAllocator);
+ this.allocatedAddress = NativeMemory.allocate(requestedSize);
+ this.allocatedSize = requestedSize;
+ }
+
+ @Override
+ public long getSize() {
+ return allocatedSize;
+ }
+
+ @Override
+ protected long memoryAddress() {
+ return allocatedAddress;
+ }
+
+ @Override
+ protected void release0() {
+ NativeMemory.free(allocatedAddress);
+ }
+}
diff --git a/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/FfmMemoryAccessor.java b/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/FfmMemoryAccessor.java
new file mode 100644
index 0000000000..879f99030e
--- /dev/null
+++ b/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/FfmMemoryAccessor.java
@@ -0,0 +1,172 @@
+/*
+ * 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.
+ *
+ * Required JVM flag. This accessor calls the restricted methods {@link
+ * MemorySegment#reinterpret(long)}, once when the class is initialized, and {@link
+ * java.lang.foreign.Linker#downcallHandle}, 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 not 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.
+ *
+ *
{@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 {
+
+ // Spans the whole address space, so an address is used directly as an offset and no segment is
+ // created per access, whether or not escape analysis removes it. It owns no memory and is never
+ // closed: buffers are still allocated and freed with malloc/free.
+ private static final MemorySegment ALL = MemorySegment.NULL.reinterpret(Long.MAX_VALUE);
+
+ public static final MemoryUtilAccessor INSTANCE = new FfmMemoryAccessor();
+
+ private FfmMemoryAccessor() {}
+
+ // byte[] indexes and lengths are ints in MemorySegment.copy, so a valid call always fits.
+ private static int checkedInt(long value) {
+ if (value < 0 || value > Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("value out of int range: " + value);
+ }
+ return (int) value;
+ }
+
+ /** Allocates {@code bytes} of uninitialized native memory with the C library's {@code malloc}. */
+ @Override
+ public long allocateMemory(long bytes) {
+ return NativeMemory.allocate(bytes);
+ }
+
+ /** Frees native memory with the C library's {@code free}. */
+ @Override
+ public void freeMemory(long address) {
+ NativeMemory.free(address);
+ }
+
+ @Override
+ public byte getByte(long address) {
+ return ALL.get(ValueLayout.JAVA_BYTE, address);
+ }
+
+ @Override
+ public void putByte(long address, byte value) {
+ ALL.set(ValueLayout.JAVA_BYTE, address, value);
+ }
+
+ @Override
+ public short getShort(long address) {
+ return ALL.get(ValueLayout.JAVA_SHORT_UNALIGNED, address);
+ }
+
+ @Override
+ public void putShort(long address, short value) {
+ ALL.set(ValueLayout.JAVA_SHORT_UNALIGNED, address, value);
+ }
+
+ @Override
+ public int getInt(long address) {
+ return ALL.get(ValueLayout.JAVA_INT_UNALIGNED, address);
+ }
+
+ @Override
+ public void putInt(long address, int value) {
+ ALL.set(ValueLayout.JAVA_INT_UNALIGNED, address, value);
+ }
+
+ @Override
+ public long getLong(long address) {
+ return ALL.get(ValueLayout.JAVA_LONG_UNALIGNED, address);
+ }
+
+ @Override
+ public void putLong(long address, long value) {
+ ALL.set(ValueLayout.JAVA_LONG_UNALIGNED, address, value);
+ }
+
+ @Override
+ public void setMemory(long address, long bytes, byte value) {
+ ALL.asSlice(address, bytes).fill(value);
+ }
+
+ @Override
+ public void copyMemory(long srcAddress, long destAddress, long bytes) {
+ MemorySegment.copy(ALL, srcAddress, ALL, destAddress, bytes);
+ }
+
+ @Override
+ public void copyToMemory(byte[] src, long srcIndex, long destAddress, long bytes) {
+ MemorySegment.copy(
+ src, checkedInt(srcIndex), ALL, ValueLayout.JAVA_BYTE, destAddress, checkedInt(bytes));
+ }
+
+ @Override
+ public void copyFromMemory(long srcAddress, byte[] dest, long destIndex, long bytes) {
+ MemorySegment.copy(
+ ALL, ValueLayout.JAVA_BYTE, srcAddress, dest, checkedInt(destIndex), checkedInt(bytes));
+ }
+
+ @Override
+ public int getInt(byte[] bytes, int index) {
+ return MemorySegment.ofArray(bytes).get(ValueLayout.JAVA_INT_UNALIGNED, index);
+ }
+
+ @Override
+ public long getLong(byte[] bytes, int index) {
+ return MemorySegment.ofArray(bytes).get(ValueLayout.JAVA_LONG_UNALIGNED, index);
+ }
+
+ /**
+ * Returns the address of byte 0 of {@code buf}'s backing memory, independent of {@code buf}'s
+ * current position.
+ *
+ * @implNote {@code MemorySegment.ofBuffer(buf)} starts at the buffer's position, so subtracting
+ * the position gives byte 0 without changing {@code buf}. Callers (notably {@link
+ * org.apache.arrow.memory.ArrowBuf}) add {@code position()} or an explicit index on top of
+ * the returned address themselves, matching the {@code sun.misc.Unsafe}-backed accessor,
+ * which reads the raw {@code java.nio.Buffer.address} field.
+ */
+ @Override
+ public long getByteBufferAddress(ByteBuffer buf) {
+ return MemorySegment.ofBuffer(buf).address() - buf.position();
+ }
+
+ @Override
+ public ByteBuffer directBuffer(long address, int capacity) {
+ if (capacity < 0) {
+ throw new IllegalArgumentException("Capacity is negative, has to be positive or 0");
+ }
+ return ALL.asSlice(address, capacity).asByteBuffer();
+ }
+}
diff --git a/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/NativeMemory.java b/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/NativeMemory.java
new file mode 100644
index 0000000000..6e4563e49c
--- /dev/null
+++ b/memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/NativeMemory.java
@@ -0,0 +1,88 @@
+/*
+ * 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}.
+ *
+ *
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 IllegalArgumentException if {@code bytes} is negative, like {@code
+ * sun.misc.Unsafe#allocateMemory}
+ * @throws OutOfMemoryError if {@code malloc} cannot allocate them, like {@code
+ * sun.misc.Unsafe#allocateMemory}
+ */
+ static long allocate(long bytes) {
+ if (bytes < 0) {
+ throw new IllegalArgumentException("Negative allocation size: " + bytes);
+ }
+ long address;
+ try {
+ address = ((MemorySegment) MALLOC.invokeExact(bytes)).address();
+ } catch (RuntimeException | Error e) {
+ throw e;
+ } catch (Throwable t) {
+ throw new IllegalStateException(t);
+ }
+ // malloc(0) may legitimately return NULL
+ if (address == 0 && bytes != 0) {
+ throw new OutOfMemoryError("Unable to allocate " + bytes + " bytes of native memory");
+ }
+ return address;
+ }
+
+ /** Frees memory returned by {@link #allocate}, or by any other call to {@code malloc}. */
+ static void free(long address) {
+ try {
+ FREE.invokeExact(MemorySegment.ofAddress(address));
+ } catch (RuntimeException | Error e) {
+ throw e;
+ } catch (Throwable t) {
+ throw new IllegalStateException(t);
+ }
+ }
+}
diff --git a/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestAllocationManagerFfm.java b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestAllocationManagerFfm.java
new file mode 100644
index 0000000000..f70bca8bf8
--- /dev/null
+++ b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestAllocationManagerFfm.java
@@ -0,0 +1,43 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.arrow.memory.AllocationManager;
+import org.apache.arrow.memory.DefaultAllocationManagerOption;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+/** Test cases for {@link AllocationManager} type resolution. */
+public class TestAllocationManagerFfm {
+
+ @AfterEach
+ public void after() {
+ // Avoid leaking this JVM-wide property into other tests sharing this Surefire fork.
+ System.clearProperty(DefaultAllocationManagerOption.ALLOCATION_MANAGER_TYPE_PROPERTY_NAME);
+ }
+
+ @Test
+ public void testAllocationManagerType() {
+ System.setProperty(DefaultAllocationManagerOption.ALLOCATION_MANAGER_TYPE_PROPERTY_NAME, "FFM");
+ DefaultAllocationManagerOption.AllocationManagerType mgrType =
+ DefaultAllocationManagerOption.getDefaultAllocationManagerType();
+
+ assertEquals(DefaultAllocationManagerOption.AllocationManagerType.FFM, mgrType);
+ }
+}
diff --git a/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestAllocationManagerTypeImpliesAccessor.java b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestAllocationManagerTypeImpliesAccessor.java
new file mode 100644
index 0000000000..9f03502e48
--- /dev/null
+++ b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestAllocationManagerTypeImpliesAccessor.java
@@ -0,0 +1,47 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.arrow.memory.util.MemoryUtil;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Verifies that setting only {@code arrow.allocation.manager.type=FFM} (with {@code
+ * arrow.memory.accessor.type} left unset) is enough for {@link MemoryUtil} to resolve to {@code
+ * FfmMemoryAccessor} on its own, so choosing the FFM allocation manager avoids {@code
+ * sun.misc.Unsafe} entirely without a second property. Runs in a dedicated Surefire execution (see
+ * memory-ffm/pom.xml) so the property is set before MemoryUtil's static init, and doesn't leak into
+ * this module's other tests.
+ */
+public class TestAllocationManagerTypeImpliesAccessor {
+
+ @Test
+ public void allocationManagerTypeAloneSelectsFfmAccessor() {
+ assertEquals(
+ "org.apache.arrow.memory.ffm.FfmMemoryAccessor", MemoryUtil.getAccessorClassName());
+
+ long address = MemoryUtil.allocateMemory(8);
+ try {
+ MemoryUtil.putLong(address, 123456789L);
+ assertEquals(123456789L, MemoryUtil.getLong(address));
+ } finally {
+ MemoryUtil.freeMemory(address);
+ }
+ }
+}
diff --git a/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmAllocationManager.java b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmAllocationManager.java
new file mode 100644
index 0000000000..59c7b53072
--- /dev/null
+++ b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmAllocationManager.java
@@ -0,0 +1,85 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.arrow.memory.AllocationManager;
+import org.apache.arrow.memory.ArrowBuf;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.BufferLedger;
+import org.apache.arrow.memory.RootAllocator;
+import org.junit.jupiter.api.Test;
+
+/** Test cases for {@link FfmAllocationManager}. */
+public class TestFfmAllocationManager {
+
+ private BufferAllocator createFfmAllocator() {
+ return new RootAllocator(
+ RootAllocator.configBuilder()
+ .allocationManagerFactory(FfmAllocationManager.FACTORY)
+ .build());
+ }
+
+ private void readWriteArrowBuf(ArrowBuf buffer) {
+ for (long i = 0; i < buffer.capacity() / 8; i++) {
+ buffer.setLong(i * 8, i);
+ }
+ for (long i = 0; i < buffer.capacity() / 8; i++) {
+ assertEquals(i, buffer.getLong(i * 8));
+ }
+ }
+
+ @Test
+ public void testBufferAllocation() {
+ final long bufSize = 4096L;
+ try (BufferAllocator allocator = createFfmAllocator();
+ ArrowBuf buffer = allocator.buffer(bufSize)) {
+ assertInstanceOf(BufferLedger.class, buffer.getReferenceManager());
+ BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager();
+
+ AllocationManager allocMgr = bufferLedger.getAllocationManager();
+ assertInstanceOf(FfmAllocationManager.class, allocMgr);
+ FfmAllocationManager ffmMgr = (FfmAllocationManager) allocMgr;
+
+ assertEquals(bufSize, ffmMgr.getSize());
+ readWriteArrowBuf(buffer);
+ }
+ }
+
+ @Test
+ public void testBufferIsFreedOnClose() {
+ try (BufferAllocator allocator = createFfmAllocator()) {
+ assertEquals(0, allocator.getAllocatedMemory());
+ try (ArrowBuf buffer = allocator.buffer(1024)) {
+ assertEquals(1024, allocator.getAllocatedMemory());
+ }
+ assertEquals(0, allocator.getAllocatedMemory());
+ }
+ }
+
+ @Test
+ public void failedNativeAllocationThrowsOutOfMemoryError() {
+ // 4 EiB is beyond any platform's address space, so the native allocation must fail
+ try (BufferAllocator allocator = createFfmAllocator()) {
+ assertThrows(
+ OutOfMemoryError.class, () -> FfmAllocationManager.FACTORY.create(allocator, 1L << 62));
+ }
+ }
+}
diff --git a/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmAllocationManagerFactoryFirstAccess.java b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmAllocationManagerFactoryFirstAccess.java
new file mode 100644
index 0000000000..709e7e9d5c
--- /dev/null
+++ b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmAllocationManagerFactoryFirstAccess.java
@@ -0,0 +1,44 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.arrow.memory.AllocationManager;
+import org.apache.arrow.memory.ArrowBuf;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.junit.jupiter.api.Test;
+
+public class TestFfmAllocationManagerFactoryFirstAccess {
+
+ @Test
+ public void factoryIsUsableWhenAccessedBeforeAnyAllocator() {
+ // This test runs in its own JVM (see memory-ffm/pom.xml Surefire execution) so that
+ // FfmAllocationManager is the first Arrow memory class initialized. Its static init creates an
+ // ArrowBuf, which initializes BaseAllocator, whose default config resolves the default factory:
+ // with only arrow-memory-ffm on the classpath, that is FfmAllocationManager.FACTORY itself.
+ AllocationManager.Factory factory = FfmAllocationManager.FACTORY;
+
+ try (BufferAllocator allocator =
+ new RootAllocator(
+ RootAllocator.configBuilder().allocationManagerFactory(factory).build());
+ ArrowBuf buf = allocator.buffer(64)) {
+ assertEquals(64, buf.capacity());
+ }
+ }
+}
diff --git a/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmArrowBufByteBuffer.java b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmArrowBufByteBuffer.java
new file mode 100644
index 0000000000..0584355d51
--- /dev/null
+++ b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmArrowBufByteBuffer.java
@@ -0,0 +1,141 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.nio.ByteBuffer;
+import org.apache.arrow.memory.ArrowBuf;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.memory.util.MemoryUtil;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Regression tests for {@link ArrowBuf}'s direct-{@link ByteBuffer} paths under the FFM accessor.
+ *
+ *
{@link ArrowBuf#getBytes(long, ByteBuffer)} and {@link ArrowBuf#setBytes(long, ByteBuffer)}
+ * add {@code position()} (or an explicit index) on top of what {@link
+ * MemoryUtil#getByteBufferAddress} returns, so that address must be position-independent. An
+ * earlier version of {@link FfmMemoryAccessor#getByteBufferAddress} returned {@code
+ * MemorySegment.ofBuffer(buf).address()}, which spans {@code [position, limit)} and therefore
+ * double-applied the offset: reads and writes silently landed at the wrong native address for any
+ * buffer with a non-zero position. Every buffer here is direct and positioned past 0 on purpose.
+ *
+ *
Runs in a Surefire execution with {@code arrow.memory.accessor.type=FFM} (see
+ * memory-ffm/pom.xml), since {@link ArrowBuf} reaches the accessor through {@link MemoryUtil},
+ * whose choice of accessor is fixed at class-initialization time.
+ */
+public class TestFfmArrowBufByteBuffer {
+
+ private static BufferAllocator createFfmAllocator() {
+ return new RootAllocator(
+ RootAllocator.configBuilder()
+ .allocationManagerFactory(FfmAllocationManager.FACTORY)
+ .build());
+ }
+
+ private static ByteBuffer directBufferOf(byte... values) {
+ ByteBuffer buf = ByteBuffer.allocateDirect(values.length);
+ buf.put(values);
+ buf.clear();
+ return buf;
+ }
+
+ @Test
+ public void accessorIsFfm() {
+ // Guards against this test silently exercising the Unsafe accessor instead.
+ assertEquals(
+ "FFM", System.getProperty(MemoryUtil.MEMORY_ACCESSOR_TYPE_PROPERTY_NAME, "Unsafe"));
+ }
+
+ @Test
+ public void setBytesFromPositionedDirectBuffer() {
+ ByteBuffer src = directBufferOf((byte) 10, (byte) 11, (byte) 12, (byte) 13, (byte) 14);
+ src.position(2);
+
+ try (BufferAllocator allocator = createFfmAllocator();
+ ArrowBuf buffer = allocator.buffer(8)) {
+ buffer.setZero(0, 8);
+ buffer.setBytes(0, src);
+
+ assertEquals((byte) 12, buffer.getByte(0));
+ assertEquals((byte) 13, buffer.getByte(1));
+ assertEquals((byte) 14, buffer.getByte(2));
+ assertEquals((byte) 0, buffer.getByte(3));
+ assertEquals(5, src.position());
+ }
+ }
+
+ @Test
+ public void setBytesFromPositionedDirectBufferWithSrcIndex() {
+ ByteBuffer src = directBufferOf((byte) 20, (byte) 21, (byte) 22, (byte) 23, (byte) 24);
+ src.position(4);
+
+ try (BufferAllocator allocator = createFfmAllocator();
+ ArrowBuf buffer = allocator.buffer(8)) {
+ buffer.setZero(0, 8);
+ // srcIndex is absolute, so position() must not shift the source address.
+ buffer.setBytes(0, src, 1, 2);
+
+ assertEquals((byte) 21, buffer.getByte(0));
+ assertEquals((byte) 22, buffer.getByte(1));
+ assertEquals((byte) 0, buffer.getByte(2));
+ }
+ }
+
+ @Test
+ public void getBytesIntoPositionedDirectBuffer() {
+ ByteBuffer dst = ByteBuffer.allocateDirect(8);
+ dst.position(5);
+
+ try (BufferAllocator allocator = createFfmAllocator();
+ ArrowBuf buffer = allocator.buffer(8)) {
+ for (int i = 0; i < 8; i++) {
+ buffer.setByte(i, (byte) (30 + i));
+ }
+ buffer.getBytes(0, dst);
+
+ assertEquals(8, dst.position());
+ assertEquals((byte) 30, dst.get(5));
+ assertEquals((byte) 31, dst.get(6));
+ assertEquals((byte) 32, dst.get(7));
+ // Bytes before the destination position must be untouched.
+ for (int i = 0; i < 5; i++) {
+ assertEquals((byte) 0, dst.get(i));
+ }
+ }
+ }
+
+ @Test
+ public void roundTripThroughPositionedDirectBuffers() {
+ ByteBuffer src = directBufferOf((byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6);
+ src.position(3);
+ ByteBuffer dst = ByteBuffer.allocateDirect(6);
+ dst.position(3);
+
+ try (BufferAllocator allocator = createFfmAllocator();
+ ArrowBuf buffer = allocator.buffer(8)) {
+ buffer.setBytes(0, src);
+ buffer.getBytes(0, dst);
+
+ assertEquals((byte) 4, dst.get(3));
+ assertEquals((byte) 5, dst.get(4));
+ assertEquals((byte) 6, dst.get(5));
+ }
+ }
+}
diff --git a/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmMemoryAccessor.java b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmMemoryAccessor.java
new file mode 100644
index 0000000000..183fd75870
--- /dev/null
+++ b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmMemoryAccessor.java
@@ -0,0 +1,147 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+public class TestFfmMemoryAccessor {
+
+ private long address = 0;
+
+ @AfterEach
+ public void after() {
+ if (address != 0) {
+ FfmMemoryAccessor.INSTANCE.freeMemory(address);
+ address = 0;
+ }
+ }
+
+ @Test
+ public void putAndGetPrimitives() {
+ address = FfmMemoryAccessor.INSTANCE.allocateMemory(16);
+
+ FfmMemoryAccessor.INSTANCE.putByte(address, (byte) 42);
+ assertEquals((byte) 42, FfmMemoryAccessor.INSTANCE.getByte(address));
+
+ FfmMemoryAccessor.INSTANCE.putShort(address + 2, (short) 1234);
+ assertEquals((short) 1234, FfmMemoryAccessor.INSTANCE.getShort(address + 2));
+
+ FfmMemoryAccessor.INSTANCE.putInt(address + 4, 123456789);
+ assertEquals(123456789, FfmMemoryAccessor.INSTANCE.getInt(address + 4));
+
+ FfmMemoryAccessor.INSTANCE.putLong(address + 8, 9876543210123L);
+ assertEquals(9876543210123L, FfmMemoryAccessor.INSTANCE.getLong(address + 8));
+ }
+
+ @Test
+ public void copyToAndFromMemoryRoundTrips() {
+ address = FfmMemoryAccessor.INSTANCE.allocateMemory(8);
+ byte[] src = {1, 2, 3, 4, 5, 6, 7, 8};
+ FfmMemoryAccessor.INSTANCE.copyToMemory(src, 0, address, 8);
+
+ byte[] dest = new byte[8];
+ FfmMemoryAccessor.INSTANCE.copyFromMemory(address, dest, 0, 8);
+ assertArrayEquals(src, dest);
+ }
+
+ @Test
+ public void copyMemoryBetweenAddresses() {
+ long src = FfmMemoryAccessor.INSTANCE.allocateMemory(8);
+ long dst = FfmMemoryAccessor.INSTANCE.allocateMemory(8);
+ try {
+ FfmMemoryAccessor.INSTANCE.putLong(src, 555L);
+ FfmMemoryAccessor.INSTANCE.copyMemory(src, dst, 8);
+ assertEquals(555L, FfmMemoryAccessor.INSTANCE.getLong(dst));
+ } finally {
+ FfmMemoryAccessor.INSTANCE.freeMemory(dst);
+ FfmMemoryAccessor.INSTANCE.freeMemory(src);
+ }
+ }
+
+ @Test
+ public void setMemoryFillsBytes() {
+ address = FfmMemoryAccessor.INSTANCE.allocateMemory(4);
+ FfmMemoryAccessor.INSTANCE.setMemory(address, 4, (byte) 0xAB);
+ for (int i = 0; i < 4; i++) {
+ assertEquals((byte) 0xAB, FfmMemoryAccessor.INSTANCE.getByte(address + i));
+ }
+ }
+
+ @Test
+ public void byteArrayIndexedAccessors() {
+ byte[] bytes = new byte[Long.BYTES];
+ ByteBuffer.wrap(bytes).order(ByteOrder.nativeOrder()).putLong(0, 42L);
+ assertEquals(42L, FfmMemoryAccessor.INSTANCE.getLong(bytes, 0));
+
+ byte[] intBytes = new byte[Integer.BYTES * 2];
+ ByteBuffer.wrap(intBytes).order(ByteOrder.nativeOrder()).putInt(0, 123456789);
+ ByteBuffer.wrap(intBytes).order(ByteOrder.nativeOrder()).putInt(Integer.BYTES, -987654321);
+ assertEquals(123456789, FfmMemoryAccessor.INSTANCE.getInt(intBytes, 0));
+ assertEquals(-987654321, FfmMemoryAccessor.INSTANCE.getInt(intBytes, Integer.BYTES));
+ }
+
+ /**
+ * {@link FfmMemoryAccessor#getByteBufferAddress} must return the address of byte 0 of the backing
+ * memory regardless of the buffer's position, matching {@code UnsafeMemoryAccessor}: callers add
+ * {@code position()} themselves. It must also not disturb the caller's buffer state.
+ */
+ @Test
+ public void getByteBufferAddressIsPositionIndependent() {
+ address = FfmMemoryAccessor.INSTANCE.allocateMemory(16);
+ ByteBuffer buf = FfmMemoryAccessor.INSTANCE.directBuffer(address, 16);
+ assertEquals(address, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(buf));
+
+ buf.position(5);
+ buf.limit(12);
+ assertEquals(address, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(buf));
+ assertEquals(5, buf.position());
+ assertEquals(12, buf.limit());
+
+ ByteBuffer slice = buf.slice();
+ assertEquals(address + 5, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(slice));
+ slice.position(3);
+ assertEquals(address + 5, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(slice));
+
+ ByteBuffer readOnly = buf.asReadOnlyBuffer();
+ readOnly.position(7);
+ assertEquals(address, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(readOnly));
+
+ buf.position(12);
+ assertEquals(address, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(buf));
+ buf.limit(16).position(16);
+ assertEquals(address, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(buf));
+ }
+
+ @Test
+ public void directBufferRejectsNegativeCapacity() {
+ assertThrows(
+ IllegalArgumentException.class, () -> FfmMemoryAccessor.INSTANCE.directBuffer(1, -1));
+ }
+
+ @Test
+ public void allocateMemoryRejectsNegativeSize() {
+ assertThrows(
+ IllegalArgumentException.class, () -> FfmMemoryAccessor.INSTANCE.allocateMemory(-1));
+ }
+}
diff --git a/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmMemoryAccessorWithoutAddOpens.java b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmMemoryAccessorWithoutAddOpens.java
new file mode 100644
index 0000000000..af47f53e77
--- /dev/null
+++ b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestFfmMemoryAccessorWithoutAddOpens.java
@@ -0,0 +1,54 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Verifies the direct-buffer paths of {@link FfmMemoryAccessor} work with no {@code --add-opens}
+ * JVM directive at all, i.e. without reflective access into {@code java.nio} internals.
+ *
+ *
This only proves anything when the JVM really is started without {@code
+ * --add-opens=java.base/java.nio=...}, so it runs in a dedicated Surefire execution (see
+ * memory-ffm/pom.xml) that overrides the inherited argLine to be empty. It is excluded from the
+ * default execution, which does inherit that flag.
+ */
+public class TestFfmMemoryAccessorWithoutAddOpens {
+
+ @Test
+ public void directBufferRoundTripsWithoutReflection() {
+ long address = FfmMemoryAccessor.INSTANCE.allocateMemory(8);
+ try {
+ FfmMemoryAccessor.INSTANCE.putLong(address, 42L);
+
+ ByteBuffer buf = FfmMemoryAccessor.INSTANCE.directBuffer(address, 8);
+ assertTrue(buf.isDirect());
+ assertEquals(42L, buf.order(ByteOrder.nativeOrder()).getLong(0));
+ assertEquals(address, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(buf));
+
+ buf.position(4);
+ assertEquals(address, FfmMemoryAccessor.INSTANCE.getByteBufferAddress(buf));
+ } finally {
+ FfmMemoryAccessor.INSTANCE.freeMemory(address);
+ }
+ }
+}
diff --git a/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestMemoryUtilFfmWiring.java b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestMemoryUtilFfmWiring.java
new file mode 100644
index 0000000000..b119576913
--- /dev/null
+++ b/memory/memory-ffm/src/test/java/org/apache/arrow/memory/ffm/TestMemoryUtilFfmWiring.java
@@ -0,0 +1,42 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.arrow.memory.util.MemoryUtil;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Verifies MemoryUtil actually resolves to FfmMemoryAccessor when arrow.memory.accessor.type=FFM is
+ * set. Runs in a dedicated Surefire execution (see memory-ffm/pom.xml) so the property is set
+ * before MemoryUtil's static init, and doesn't leak into this module's other tests, which rely on
+ * calling FfmMemoryAccessor directly rather than through MemoryUtil.
+ */
+public class TestMemoryUtilFfmWiring {
+
+ @Test
+ public void memoryUtilDelegatesToFfmAccessor() {
+ long address = MemoryUtil.allocateMemory(8);
+ try {
+ MemoryUtil.putLong(address, 123456789L);
+ assertEquals(123456789L, MemoryUtil.getLong(address));
+ } finally {
+ MemoryUtil.freeMemory(address);
+ }
+ }
+}
diff --git a/memory/pom.xml b/memory/pom.xml
index 4ea3d1f9ca..e3562f5d6b 100644
--- a/memory/pom.xml
+++ b/memory/pom.xml
@@ -34,4 +34,17 @@ under the License.
memory-netty-buffer-patch
memory-netty
+
+
+
+
+ arrow-memory-ffm
+
+ [22,)
+
+
+ memory-ffm
+
+
+