mirror of
https://github.com/usatiuk/dhfs.git
synced 2025-10-29 04:57:48 +01:00
Utils: UninitializedByteBuffer
This commit is contained in:
@@ -70,6 +70,7 @@ public class DhfsImage implements Future<String> {
|
|||||||
"--add-exports", "java.base/sun.nio.ch=ALL-UNNAMED",
|
"--add-exports", "java.base/sun.nio.ch=ALL-UNNAMED",
|
||||||
"--add-exports", "java.base/jdk.internal.access=ALL-UNNAMED",
|
"--add-exports", "java.base/jdk.internal.access=ALL-UNNAMED",
|
||||||
"--add-opens=java.base/java.nio=ALL-UNNAMED",
|
"--add-opens=java.base/java.nio=ALL-UNNAMED",
|
||||||
|
"--enable-preview",
|
||||||
"-Ddhfs.objects.peerdiscovery.interval=1s",
|
"-Ddhfs.objects.peerdiscovery.interval=1s",
|
||||||
"-Ddhfs.objects.invalidation.delay=100",
|
"-Ddhfs.objects.invalidation.delay=100",
|
||||||
"-Ddhfs.objects.deletion.delay=0",
|
"-Ddhfs.objects.deletion.delay=0",
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ public final class JObjectKeyImpl implements JObjectKey {
|
|||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (_bb != null) return _bb;
|
if (_bb != null) return _bb;
|
||||||
var bytes = value.getBytes(StandardCharsets.ISO_8859_1);
|
var bytes = value.getBytes(StandardCharsets.ISO_8859_1);
|
||||||
var directBb = ByteBuffer.allocateDirect(bytes.length);
|
var directBb = UninitializedByteBuffer.allocate(bytes.length);
|
||||||
directBb.put(bytes);
|
directBb.put(bytes);
|
||||||
directBb.flip();
|
directBb.flip();
|
||||||
_bb = directBb;
|
_bb = directBb;
|
||||||
|
|||||||
@@ -102,6 +102,7 @@
|
|||||||
<arg>-parameters</arg>
|
<arg>-parameters</arg>
|
||||||
<arg>--add-exports</arg>
|
<arg>--add-exports</arg>
|
||||||
<arg>java.base/jdk.internal.access=ALL-UNNAMED</arg>
|
<arg>java.base/jdk.internal.access=ALL-UNNAMED</arg>
|
||||||
|
<arg>--enable-preview</arg>
|
||||||
</compilerArgs>
|
</compilerArgs>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
@@ -119,6 +120,7 @@
|
|||||||
--add-exports java.base/sun.nio.ch=ALL-UNNAMED
|
--add-exports java.base/sun.nio.ch=ALL-UNNAMED
|
||||||
--add-exports java.base/jdk.internal.access=ALL-UNNAMED
|
--add-exports java.base/jdk.internal.access=ALL-UNNAMED
|
||||||
--add-opens=java.base/java.nio=ALL-UNNAMED
|
--add-opens=java.base/java.nio=ALL-UNNAMED
|
||||||
|
--enable-preview
|
||||||
</argLine>
|
</argLine>
|
||||||
<skipTests>${skip.unit}</skipTests>
|
<skipTests>${skip.unit}</skipTests>
|
||||||
<redirectTestOutputToFile>true</redirectTestOutputToFile>
|
<redirectTestOutputToFile>true</redirectTestOutputToFile>
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.usatiuk.utils;
|
||||||
|
|
||||||
|
import java.lang.foreign.*;
|
||||||
|
import java.lang.invoke.MethodHandle;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class UninitializedByteBuffer {
|
||||||
|
private static final Linker LINKER = Linker.nativeLinker();
|
||||||
|
private static final MethodHandle malloc = LINKER.downcallHandle(
|
||||||
|
LINKER.defaultLookup().find("malloc").orElseThrow(),
|
||||||
|
FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG)
|
||||||
|
);
|
||||||
|
private static final MethodHandle free = LINKER.downcallHandle(
|
||||||
|
LINKER.defaultLookup().find("free").orElseThrow(),
|
||||||
|
FunctionDescriptor.ofVoid(ValueLayout.ADDRESS)
|
||||||
|
);
|
||||||
|
|
||||||
|
public static ByteBuffer allocate(int capacity) {
|
||||||
|
UnsafeAccessor.get().getNioAccess().reserveMemory(capacity, capacity);
|
||||||
|
|
||||||
|
MemorySegment segment = null;
|
||||||
|
try {
|
||||||
|
segment = (MemorySegment) malloc.invokeExact((long) capacity);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
Consumer<MemorySegment> cleanup = s -> {
|
||||||
|
try {
|
||||||
|
free.invokeExact(s);
|
||||||
|
UnsafeAccessor.get().getNioAccess().unreserveMemory(capacity, capacity);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var reint = segment.reinterpret(capacity, Arena.ofAuto(), cleanup);
|
||||||
|
return reint.asByteBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long getAddress(ByteBuffer buffer) {
|
||||||
|
return UnsafeAccessor.get().getNioAccess().getBufferAddress(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user