Different string allocation on jdk 21/22 (#106492)

Similar to https://github.com/elastic/elasticsearch/pull/106360, the
methods for allocating a native string changed between Java 21 and 22.
This commit adds another util method to handle the differences and uses
it in the jdk systemd impl.
This commit is contained in:
Ryan Ernst 2024-03-19 10:09:18 -07:00 committed by GitHub
parent e14dd54ae9
commit 6731538bbe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 1 deletions

View file

@ -58,7 +58,7 @@ class JdkSystemdLibrary implements SystemdLibrary {
@Override
public int sd_notify(int unset_environment, String state) {
try (Arena arena = Arena.ofConfined()) {
MemorySegment nativeState = arena.allocateUtf8String(state);
MemorySegment nativeState = MemorySegmentUtil.allocateString(arena, state);
return (int) sd_notify$mh.invokeExact(unset_environment, nativeState);
} catch (Throwable t) {
throw new AssertionError(t);

View file

@ -8,6 +8,7 @@
package org.elasticsearch.nativeaccess.jdk;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
/**
@ -19,5 +20,9 @@ class MemorySegmentUtil {
return segment.getUtf8String(offset);
}
static MemorySegment allocateString(Arena arena, String s) {
return arena.allocateUtf8String(s);
}
private MemorySegmentUtil() {}
}

View file

@ -8,6 +8,7 @@
package org.elasticsearch.nativeaccess.jdk;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
public class MemorySegmentUtil {
@ -16,5 +17,9 @@ public class MemorySegmentUtil {
return segment.getString(offset);
}
static MemorySegment allocateString(Arena arena, String s) {
return arena.allocateFrom(s);
}
private MemorySegmentUtil() {}
}