diff --git a/build.sbt b/build.sbt index b0daf37921f8..c45fe805bdaa 100644 --- a/build.sbt +++ b/build.sbt @@ -1396,8 +1396,7 @@ lazy val `runtime-language-arrow` = inConfig(Compile)(truffleRunOptionsSettings), instrumentationSettings, libraryDependencies ++= Seq( - "org.graalvm.truffle" % "truffle-api" % graalMavenPackagesVersion % "provided", - "org.apache.arrow" % "arrow-memory-core" % apacheArrowVersion + "org.graalvm.truffle" % "truffle-api" % graalMavenPackagesVersion % "provided" ) ) @@ -1886,7 +1885,7 @@ lazy val `engine-runner` = project "com.sun.jna.internal.Cleaner", "com.sun.jna.Structure$FFIType", "akka.http", - "org.apache.arrow.memory.util.MemoryUtil" + "org.enso.interpreter.arrow.util.MemoryUtil" ) ) .dependsOn(assembly) diff --git a/engine/runtime-language-arrow/src/main/java/org/enso/interpreter/arrow/runtime/ArrowCastToFixedSizeArrayFactory.java b/engine/runtime-language-arrow/src/main/java/org/enso/interpreter/arrow/runtime/ArrowCastToFixedSizeArrayFactory.java index 0c60bebe0b52..cd34f1945015 100644 --- a/engine/runtime-language-arrow/src/main/java/org/enso/interpreter/arrow/runtime/ArrowCastToFixedSizeArrayFactory.java +++ b/engine/runtime-language-arrow/src/main/java/org/enso/interpreter/arrow/runtime/ArrowCastToFixedSizeArrayFactory.java @@ -11,10 +11,11 @@ import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.library.ExportLibrary; import com.oracle.truffle.api.library.ExportMessage; -import java.nio.*; import java.util.BitSet; -import org.apache.arrow.memory.util.MemoryUtil; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; import org.enso.interpreter.arrow.ArrowParser; +import org.enso.interpreter.arrow.util.MemoryUtil; @ExportLibrary(InteropLibrary.class) public class ArrowCastToFixedSizeArrayFactory implements TruffleObject { diff --git a/engine/runtime-language-arrow/src/main/java/org/enso/interpreter/arrow/util/MemoryUtil.java b/engine/runtime-language-arrow/src/main/java/org/enso/interpreter/arrow/util/MemoryUtil.java new file mode 100644 index 000000000000..d79798730829 --- /dev/null +++ b/engine/runtime-language-arrow/src/main/java/org/enso/interpreter/arrow/util/MemoryUtil.java @@ -0,0 +1,48 @@ +package org.enso.interpreter.arrow.util; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.nio.ByteBuffer; + +public class MemoryUtil { + + private static Constructor byteBufferConstr = null; + + static { + + ByteBuffer buffer = null; + try { + buffer = ByteBuffer.allocateDirect(1); + byteBufferConstr = buffer.getClass().getDeclaredConstructor(long.class, long.class); + byteBufferConstr.setAccessible(true); + } catch (NoSuchMethodException e) { + System.err.println("Unable to find a constructor for ByteBuffer created directly from a memory address: " + e.getMessage()); + } finally { + if (buffer != null) { + buffer.clear(); + } + } + } + + /** + * Create a ByteBuffer directly from a (allocated) memory address and its size without copying. + * + * @param address address in memory to the start of the allocated chunk of memory + * @param capacity size in bytes of the allocated chunk of memory + * @return ByteBuffer instance + */ + public static ByteBuffer directBuffer(long address, long capacity) { + if (byteBufferConstr != null) { + try { + return (ByteBuffer) byteBufferConstr.newInstance(address, capacity); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + } + throw new RuntimeException("constructor for a ByteBuffer created from a memory address is missing"); + } +}