[foreign-jextract] RFR: 8241925: jextract should generate simple allocation, access API for C primitive types

Maurizio Cimadamore mcimadamore at openjdk.java.net
Tue Mar 31 16:08:03 UTC 2020


On Tue, 31 Mar 2020 15:32:22 GMT, Athijegannathan Sundararajan <sundar at openjdk.org> wrote:

> added static method helper classes to handle allocation, access ot C primitive types and C strings.

Some examples still use Foreign.toCString - I think we should remove those helpers from Foreign to avoid confusion.

src/jdk.incubator.jextract/share/classes/jdk/incubator/jextract/tool/resources/Cstring.java.template line 25:

> 24:
> 25:     private static void copyString(MemoryAddress addr, byte[] bytes) {
> 26:         for (int i = 0 ; i < bytes.length; i++) {

This should use a bulk copy

var heapSegment = MemorySegment.of(bytes);
MemoryAddress.copy(heapSegment, addr, bytes.length)

src/jdk.incubator.jextract/share/classes/jdk/incubator/jextract/tool/resources/Cstring.java.template line 59:

> 58:
> 59:     public String toString(MemoryAddress addr) {
> 60:         StringBuilder buf = new StringBuilder();

This should be static

doc/panama_jextract.md line 329:

> 328:         try (AllocationScope scope = AllocationScope.unboundedNativeScope()) {
> 329:             var aPtr = Cdouble.allocateArray(a, scope);
> 330:             var xPtr = Cdouble.allocateArray(x, scope);

Should we allocate arrays directly off heap and avoid the copy? To do that we might need another helper to create
'empty' arrays.

test/jdk/tools/jextract/test8241925/LibTest8241925Test.java line 44:

> 43:     public void test() {
> 44:         try (var scope = AllocationScope.unboundedNativeScope()) {
> 45:             var addr = Cint.allocate(12, scope);

This looks really neat!

src/jdk.incubator.jextract/share/classes/jdk/incubator/jextract/tool/resources/C-X.java.template line 73:

> 72:     private static void copy(MemoryAddress addr, ${CARRIER}[] arr) {
> 73:         for (int i = 0 ; i < arr.length; i++) {
> 74:             arrayHandle.set(addr, (long)i, arr[i]);

This should use bulk copy (see my suggestion on CString::copy)

-------------

PR: https://git.openjdk.java.net/panama-foreign/pull/78


More information about the panama-dev mailing list