RFR: 8347408: Create an internal method handle adapter for system calls with errno [v3]
Shaojin Wen
swen at openjdk.org
Tue May 6 08:50:15 UTC 2025
On Mon, 5 May 2025 17:13:02 GMT, Per Minborg <pminborg at openjdk.org> wrote:
>> As we advance, converting older JDK code to use the relatively new FFM API requires system calls that can provide `errno` and the likes to explicitly allocate a MemorySegment to capture potential error states. This can lead to negative performance implications if not designed carefully and also introduces unnecessary code complexity.
>>
>> Hence, this PR proposes adding a JDK internal method handle adapter that can be used to handle system calls with `errno`, `GetLastError`, and `WSAGetLastError`.
>>
>> It relies on an efficient carrier-thread-local cache of memory regions to allide allocations.
>
> Per Minborg has updated the pull request incrementally with three additional commits since the last revision:
>
> - Fix empty line at the end of a third file
> - Fix empty line at the end of another file
> - Fix empty line at the end of a file
src/java.base/share/classes/jdk/internal/foreign/CaptureStateUtil.java line 390:
> 388: return MhUtil.findVirtual(LOOKUP, Arena.class, "close",
> 389: MethodType.methodType(void.class));
> 390: }
Suggestion:
return switch (index) {
case NON_NEGATIVE_INT -> MhUtil.findStatic(LOOKUP, "nonNegative",
MethodType.methodType(boolean.class, int.class));
case SUCCESS_INT -> MhUtil.findStatic(LOOKUP, "success",
MethodType.methodType(int.class, int.class, MemorySegment.class));
case ERROR_INT -> MhUtil.findStatic(LOOKUP, "error",
MethodType.methodType(int.class, MethodHandle.class, int.class, MemorySegment.class));
case NON_NEGATIVE_LONG -> MhUtil.findStatic(LOOKUP, "nonNegative",
MethodType.methodType(boolean.class, long.class));
case SUCCESS_LONG -> MhUtil.findStatic(LOOKUP, "success",
MethodType.methodType(long.class, long.class, MemorySegment.class));
case ERROR_LONG -> MhUtil.findStatic(LOOKUP, "error",
MethodType.methodType(long.class, MethodHandle.class, long.class, MemorySegment.class));
case ACQUIRE_ARENA -> MhUtil.findStatic(LOOKUP, "acquireArena",
MethodType.methodType(Arena.class));
case ALLOCATE -> MhUtil.findStatic(LOOKUP, "allocate",
MethodType.methodType(MemorySegment.class, Arena.class));
case ARENA_CLOSE -> MhUtil.findVirtual(LOOKUP, Arena.class, "close",
MethodType.methodType(void.class));
default -> throw new InternalError("Unknown index: " + index);
};
Switch statement can be replaced with enhanced 'switch'
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25043#discussion_r2075016584
More information about the core-libs-dev
mailing list