From: Olaf Wintermann Date: Thu, 26 Jun 2025 17:47:53 +0000 (+0200) Subject: add CUtils X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=7fb6672a75fc81a1d51542002625fba2b7026b63;p=rssreader.git add CUtils --- diff --git a/ui-java/src/main/java/de/unixwork/ui/CUtils.java b/ui-java/src/main/java/de/unixwork/ui/CUtils.java new file mode 100644 index 0000000..0be062c --- /dev/null +++ b/ui-java/src/main/java/de/unixwork/ui/CUtils.java @@ -0,0 +1,30 @@ +package de.unixwork.ui; + +import java.lang.foreign.Arena; +import java.lang.foreign.MemorySegment; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; + +import static java.lang.foreign.Arena.global; + +public class CUtils { + public static MemorySegment cstring(String s) { + if(s == null) { + return MemorySegment.NULL; + } + ToolkitFuncs tk = ToolkitFuncs.getInstance(); + + byte[] utf8 = s.getBytes(StandardCharsets.UTF_8); + try { + long size = utf8.length + 1; + MemorySegment cstr = (MemorySegment) tk.malloc.invoke(size); + MemorySegment segment = MemorySegment.ofAddress(cstr.address()); + ByteBuffer buf = segment.asByteBuffer(); + buf.put(utf8); + buf.put((byte) 0); + return cstr; + } catch (Throwable e) { + throw new RuntimeException(e); + } + } +} diff --git a/ui-java/src/main/java/de/unixwork/ui/ToolkitFuncs.java b/ui-java/src/main/java/de/unixwork/ui/ToolkitFuncs.java index 8779c34..9dd4ea9 100644 --- a/ui-java/src/main/java/de/unixwork/ui/ToolkitFuncs.java +++ b/ui-java/src/main/java/de/unixwork/ui/ToolkitFuncs.java @@ -40,6 +40,10 @@ public class ToolkitFuncs { public MethodHandle text_set; public MethodHandle text_get; + // some libc stuff + public MethodHandle malloc; + public MethodHandle free; + private ToolkitFuncs(Linker linker, SymbolLookup lib) { // void* func(void*) FunctionDescriptor sigm_m = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS); @@ -47,6 +51,7 @@ public class ToolkitFuncs { FunctionDescriptor sigm_mm = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.ADDRESS); FunctionDescriptor sigv_mm = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.ADDRESS); FunctionDescriptor sigv_m = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS); + FunctionDescriptor sigm_l = FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG); MemorySegment object_get_context_addr = lib.find("ui_object_get_context").orElseThrow(); @@ -82,6 +87,9 @@ public class ToolkitFuncs { MemorySegment text_set_addr = lib.find("ui_text_set").orElseThrow(); MemorySegment text_get_addr = lib.find("ui_text_get").orElseThrow(); + MemorySegment malloc_addr = lib.find("malloc").orElseThrow(); + MemorySegment free_addr = lib.find("free").orElseThrow(); + object_get_context = linker.downcallHandle(object_get_context_addr, sigm_m); // use JAVA_LONG for size_t, because we don't support 32bit platforms @@ -116,6 +124,9 @@ public class ToolkitFuncs { double_get = linker.downcallHandle(double_get_addr, FunctionDescriptor.of(ValueLayout.JAVA_DOUBLE, ValueLayout.ADDRESS)); text_set = linker.downcallHandle(text_set_addr, sigv_mm); text_get = linker.downcallHandle(text_get_addr, sigm_m); + + malloc = linker.downcallHandle(malloc_addr, sigm_l); + free = linker.downcallHandle(free_addr, sigv_m); } static ToolkitFuncs getInstance() {