Skip to content

Instantly share code, notes, and snippets.

@denismakogon
Created September 13, 2022 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denismakogon/e75482b24e890cdadd20231fcbf340a2 to your computer and use it in GitHub Desktop.
Save denismakogon/e75482b24e890cdadd20231fcbf340a2 to your computer and use it in GitHub Desktop.
package com.hol.panama.lab_5_optional;
import com.hol.panama.stdlib.stdio.stdio_h;
import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import static java.lang.foreign.ValueLayout.ADDRESS;
import static java.lang.foreign.ValueLayout.JAVA_INT;
public class Task {
private static final FunctionDescriptor printfDescriptor =
FunctionDescriptor.of(JAVA_INT, ADDRESS);
static final Linker linker = Linker.nativeLinker();
private static final SymbolLookup linkerLookup = linker.defaultLookup();
private static final SymbolLookup systemLookup = SymbolLookup.loaderLookup();
private static final SymbolLookup symbolLookup = name ->
systemLookup.lookup(name).or(() -> linkerLookup.lookup(name));
static final MemorySegment printfHandle = symbolLookup.lookup("printf").orElseThrow();
static MethodHandle specializedPrintf(MemoryLayout... varargLayouts) {
FunctionDescriptor specialized = printfDescriptor.asVariadic(varargLayouts);
return linker.downcallHandle(printfHandle, specialized);
}
public static final MethodHandle WithInt = specializedPrintf(JAVA_INT);
public static final MethodHandle WithString = specializedPrintf(ADDRESS);
public static final MethodHandle WithStringAndInt = specializedPrintf(ADDRESS, JAVA_INT);
public static void main(String[] args) throws Throwable {
var one = "My name is %s.\n";
var two = "Denis";
var three = "I'm %dyo old.\n";
var four = 31;
var five = one + three;
try (var ms = MemorySession.openConfined()) {
var oneSegment = ms.allocateUtf8String(one);
var twoSegment = ms.allocateUtf8String(two);
var threeSegment = ms.allocateUtf8String(three);
var fiveSegment = ms.allocateUtf8String(five);
WithString.invoke(oneSegment.address(), twoSegment);
WithInt.invoke(threeSegment.address(), four);
WithStringAndInt.invoke(fiveSegment.address(), twoSegment, four);
stdio_h.printf(oneSegment.address(), twoSegment.address());
stdio_h.printf(threeSegment.address(), four);
stdio_h.printf(fiveSegment.address(), twoSegment.address(), four);
var allocator = SegmentAllocator.newNativeArena(
one.length() + 1 + two.length() + 1 +
three.length() + 1 + five.length() + 1,
ms
);
oneSegment = allocator.allocateUtf8String(one);
twoSegment = allocator.allocateUtf8String(two);
threeSegment = allocator.allocateUtf8String(three);
fiveSegment = allocator.allocateUtf8String(five);
stdio_h.printf(oneSegment.address(), twoSegment.address());
stdio_h.printf(threeSegment.address(), four);
stdio_h.printf(fiveSegment.address(), twoSegment.address(), four);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment