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 all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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);
MemoryAddress.copy(heapSegment.baseAddress(), addr, arr.length * sizeof());
}

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;
}

@@ -91,7 +86,7 @@ public class C-X {
throw new UnsupportedOperationException("segment cannot contain integral number of elements");
}
${CARRIER}[] array = new ${CARRIER}[(int) (segSize / elemSize)];
MemoryAddress.copy(seg.baseAddress(), MemorySegment.ofArray(array).baseAddress(), array.length * elemSize);
MemorySegment.ofArray(array).copyFrom(seg);
return array;
}
}
@@ -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);
MemoryAddress.copy(heapSegment.baseAddress(), addr, bytes.length);
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());
}