-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It makes little sense to include the full dependency just to allow for memory-mapped byte buffer which we can construct by hand.
- Loading branch information
Showing
3 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
engine/runtime-language-arrow/src/main/java/org/enso/interpreter/arrow/util/MemoryUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |