RFR: 8347408: Create an internal method handle adapter for system calls with errno [v3]

Shaojin Wen swen at openjdk.org
Tue May 6 09:28:13 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 103:

> 101:             }
> 102:             return returnType;
> 103:         }

Suggestion:

        static Class<?> returnType(MethodHandle target) {
            // Implicit null check
            final MethodType type = target.type();
            final Class<?> returnType = type.returnType();

            if (!(returnType.equals(int.class) || returnType.equals(long.class))) {
                throw illegalArgDoesNot(target, "return an int or a long");
            }
            if (type.parameterCount() == 0 || type.parameterType(0) != MemorySegment.class) {
                throw illegalArgDoesNot(target, "have a MemorySegment as the first parameter");
            }
            return returnType;
        }


If returnType uses a local variable, type also uses a local variable.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/25043#discussion_r2075081366


More information about the core-libs-dev mailing list