Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move MemoryAddress::copy (jextract version) #171

Closed
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -23,11 +23,6 @@ public class C-X {
private static final VarHandle handle = LAYOUT.varHandle(CARRIER);
private static final VarHandle arrayHandle = arrayHandle(LAYOUT, CARRIER);

private static void copy(MemoryAddress addr, ${CARRIER}[] arr) {
var heapSegment = MemorySegment.ofArray(arr);
addr.segment().copyFrom(heapSegment);
}

public static ${CARRIER} get(MemoryAddress addr) {
return (${CARRIER}) handle.get(addr);
}
@@ -69,14 +64,14 @@ public class C-X {
public static MemorySegment allocateArray(${CARRIER}[] arr) {
var arrLayout = MemoryLayout.ofSequence(arr.length, LAYOUT);
var seg = MemorySegment.allocateNative(arrLayout);
copy(seg.baseAddress(), arr);
seg.copyFrom(MemorySegment.ofArray(arr));
return seg;
}

public static MemoryAddress allocateArray(${CARRIER}[] arr, NativeAllocationScope scope) {
var arrLayout = MemoryLayout.ofSequence(arr.length, LAYOUT);
var addr = scope.allocate(arrLayout);
copy(addr, arr);
addr.segment().copyFrom(MemorySegment.ofArray(arr));
return addr;
}

@@ -19,12 +19,6 @@ public final class Cstring {
}
private final static VarHandle byteArrHandle = arrayHandle(C_CHAR, byte.class);

private static void copy(MemoryAddress addr, byte[] bytes) {
var heapSegment = MemorySegment.ofArray(bytes);
addr.segment().copyFrom(heapSegment);
byteArrHandle.set(addr, (long)bytes.length, (byte)0);
}

private static MemorySegment toCString(byte[] bytes) {
MemoryLayout strLayout = MemoryLayout.ofSequence(bytes.length + 1, C_CHAR);
MemorySegment segment = MemorySegment.allocateNative(strLayout);
@@ -36,7 +30,7 @@ public final class Cstring {
private static MemoryAddress toCString(byte[] bytes, NativeAllocationScope scope) {
MemoryLayout strLayout = MemoryLayout.ofSequence(bytes.length + 1, C_CHAR);
MemoryAddress addr = scope.allocate(strLayout);
copy(addr, bytes);
addr.segment().copyFrom(MemorySegment.ofArray(bytes));
return addr;
}

@@ -48,6 +42,15 @@ public final class Cstring {
copy(addr, str.getBytes(charset));
}

//where
private static void copy(MemoryAddress addr, byte[] bytes) {
var heapSegment = MemorySegment.ofArray(bytes);
addr.segment()
.asSlice(addr.segmentOffset(), bytes.length)
.copyFrom(heapSegment);
byteArrHandle.set(addr, (long)bytes.length, (byte)0);
}

public static MemorySegment toCString(String str) {
return toCString(str.getBytes());
}
ProTip! Use n and p to navigate between commits in a pull request.