From e6918187568dbd01842d8d1d2c808ce16a894239 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 13:54:28 +0200 Subject: Adding upstream version 18.2.2. Signed-off-by: Daniel Baumann --- src/arrow/java/memory/memory-unsafe/pom.xml | 33 ++++++++++ .../memory/DefaultAllocationManagerFactory.java | 37 ++++++++++++ .../arrow/memory/UnsafeAllocationManager.java | 70 ++++++++++++++++++++++ .../arrow/memory/TestAllocationManagerUnsafe.java | 41 +++++++++++++ .../arrow/memory/TestUnsafeAllocationManager.java | 68 +++++++++++++++++++++ 5 files changed, 249 insertions(+) create mode 100644 src/arrow/java/memory/memory-unsafe/pom.xml create mode 100644 src/arrow/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java create mode 100644 src/arrow/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/UnsafeAllocationManager.java create mode 100644 src/arrow/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestAllocationManagerUnsafe.java create mode 100644 src/arrow/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestUnsafeAllocationManager.java (limited to 'src/arrow/java/memory/memory-unsafe') diff --git a/src/arrow/java/memory/memory-unsafe/pom.xml b/src/arrow/java/memory/memory-unsafe/pom.xml new file mode 100644 index 000000000..6358784d5 --- /dev/null +++ b/src/arrow/java/memory/memory-unsafe/pom.xml @@ -0,0 +1,33 @@ + + + + + arrow-memory + org.apache.arrow + 6.0.1 + + 4.0.0 + + arrow-memory-unsafe + Arrow Memory - Unsafe + Allocator and utils for allocating memory in Arrow based on sun.misc.Unsafe + + + + + org.apache.arrow + arrow-memory-core + ${project.version} + + + + diff --git a/src/arrow/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java b/src/arrow/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/DefaultAllocationManagerFactory.java new file mode 100644 index 000000000..720c3d02d --- /dev/null +++ b/src/arrow/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/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; + +/** + * The default Allocation Manager Factory for a module. + * + */ +public class DefaultAllocationManagerFactory implements AllocationManager.Factory { + + public static final AllocationManager.Factory FACTORY = UnsafeAllocationManager.FACTORY; + + @Override + public AllocationManager create(BufferAllocator accountingAllocator, long size) { + return FACTORY.create(accountingAllocator, size); + } + + @Override + public ArrowBuf empty() { + return UnsafeAllocationManager.FACTORY.empty(); + } +} diff --git a/src/arrow/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/UnsafeAllocationManager.java b/src/arrow/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/UnsafeAllocationManager.java new file mode 100644 index 000000000..b10aba359 --- /dev/null +++ b/src/arrow/java/memory/memory-unsafe/src/main/java/org/apache/arrow/memory/UnsafeAllocationManager.java @@ -0,0 +1,70 @@ +/* + * 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; + +import org.apache.arrow.memory.util.MemoryUtil; + +/** + * Allocation manager based on unsafe API. + */ +public final class UnsafeAllocationManager extends AllocationManager { + + private static final ArrowBuf EMPTY = new ArrowBuf(ReferenceManager.NO_OP, + null, + 0, + MemoryUtil.UNSAFE.allocateMemory(0) + ); + + public static final AllocationManager.Factory FACTORY = new Factory() { + @Override + public AllocationManager create(BufferAllocator accountingAllocator, long size) { + return new UnsafeAllocationManager(accountingAllocator, size); + } + + @Override + public ArrowBuf empty() { + return EMPTY; + } + }; + + private final long allocatedSize; + + private final long allocatedAddress; + + UnsafeAllocationManager(BufferAllocator accountingAllocator, long requestedSize) { + super(accountingAllocator); + allocatedAddress = MemoryUtil.UNSAFE.allocateMemory(requestedSize); + allocatedSize = requestedSize; + } + + @Override + public long getSize() { + return allocatedSize; + } + + @Override + protected long memoryAddress() { + return allocatedAddress; + } + + @Override + protected void release0() { + MemoryUtil.UNSAFE.freeMemory(allocatedAddress); + } + +} diff --git a/src/arrow/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestAllocationManagerUnsafe.java b/src/arrow/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestAllocationManagerUnsafe.java new file mode 100644 index 000000000..33abe92e5 --- /dev/null +++ b/src/arrow/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestAllocationManagerUnsafe.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; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * Test cases for {@link AllocationManager}. + */ +public class TestAllocationManagerUnsafe { + + @Test + public void testAllocationManagerType() { + + // test unsafe allocation manager type + System.setProperty( + DefaultAllocationManagerOption.ALLOCATION_MANAGER_TYPE_PROPERTY_NAME, "Unsafe"); + DefaultAllocationManagerOption.AllocationManagerType mgrType = + DefaultAllocationManagerOption.getDefaultAllocationManagerType(); + + assertEquals(DefaultAllocationManagerOption.AllocationManagerType.Unsafe, mgrType); + + } +} diff --git a/src/arrow/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestUnsafeAllocationManager.java b/src/arrow/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestUnsafeAllocationManager.java new file mode 100644 index 000000000..c15882a37 --- /dev/null +++ b/src/arrow/java/memory/memory-unsafe/src/test/java/org/apache/arrow/memory/TestUnsafeAllocationManager.java @@ -0,0 +1,68 @@ +/* + * 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Test cases for {@link UnsafeAllocationManager}. + */ +public class TestUnsafeAllocationManager { + + private BaseAllocator createUnsafeAllocator() { + return new RootAllocator(BaseAllocator.configBuilder().allocationManagerFactory(UnsafeAllocationManager.FACTORY) + .build()); + } + + private void readWriteArrowBuf(ArrowBuf buffer) { + // write buffer + for (long i = 0; i < buffer.capacity() / 8; i++) { + buffer.setLong(i * 8, i); + } + + // read buffer + for (long i = 0; i < buffer.capacity() / 8; i++) { + long val = buffer.getLong(i * 8); + assertEquals(i, val); + } + } + + /** + * Test the memory allocation for {@link UnsafeAllocationManager}. + */ + @Test + public void testBufferAllocation() { + final long bufSize = 4096L; + try (BaseAllocator allocator = createUnsafeAllocator(); + ArrowBuf buffer = allocator.buffer(bufSize)) { + assertTrue(buffer.getReferenceManager() instanceof BufferLedger); + BufferLedger bufferLedger = (BufferLedger) buffer.getReferenceManager(); + + // make sure we are using unsafe allocation manager + AllocationManager allocMgr = bufferLedger.getAllocationManager(); + assertTrue(allocMgr instanceof UnsafeAllocationManager); + UnsafeAllocationManager unsafeMgr = (UnsafeAllocationManager) allocMgr; + + assertEquals(bufSize, unsafeMgr.getSize()); + readWriteArrowBuf(buffer); + } + } +} -- cgit v1.2.3