Skip to content

Commit 0db4702

Browse files
author
Darragh Clarke
committed
8368877: Generate Jextract bindings for Kqueue
Reviewed-by: alanb, aefimov
1 parent 28460ca commit 0db4702

File tree

11 files changed

+1343
-0
lines changed

11 files changed

+1343
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
// Generated by jextract
27+
28+
package jdk.internal.ffi.generated.errno;
29+
30+
import java.lang.foreign.*;
31+
import java.lang.invoke.*;
32+
import java.util.*;
33+
import java.util.stream.*;
34+
35+
import static java.lang.foreign.ValueLayout.*;
36+
37+
@SuppressWarnings("restricted")
38+
public class errno_h$shared {
39+
40+
errno_h$shared() {
41+
// Should not be called directly
42+
}
43+
44+
public static final OfBoolean C_BOOL = (OfBoolean) Linker.nativeLinker().canonicalLayouts().get("bool");
45+
public static final OfByte C_CHAR =(OfByte)Linker.nativeLinker().canonicalLayouts().get("char");
46+
public static final OfShort C_SHORT = (OfShort) Linker.nativeLinker().canonicalLayouts().get("short");
47+
public static final OfInt C_INT = (OfInt) Linker.nativeLinker().canonicalLayouts().get("int");
48+
public static final OfLong C_LONG_LONG = (OfLong) Linker.nativeLinker().canonicalLayouts().get("long long");
49+
public static final OfFloat C_FLOAT = (OfFloat) Linker.nativeLinker().canonicalLayouts().get("float");
50+
public static final OfDouble C_DOUBLE = (OfDouble) Linker.nativeLinker().canonicalLayouts().get("double");
51+
public static final AddressLayout C_POINTER = ((AddressLayout) Linker.nativeLinker().canonicalLayouts().get("void*"))
52+
.withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, C_CHAR));
53+
public static final OfLong C_LONG = (OfLong) Linker.nativeLinker().canonicalLayouts().get("long");
54+
55+
static final boolean TRACE_DOWNCALLS = Boolean.getBoolean("jextract.trace.downcalls");
56+
57+
static void traceDowncall(String name, Object... args) {
58+
String traceArgs = Arrays.stream(args)
59+
.map(Object::toString)
60+
.collect(Collectors.joining(", "));
61+
System.out.printf("%s(%s)\n", name, traceArgs);
62+
}
63+
64+
static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fdesc) {
65+
try {
66+
return MethodHandles.lookup().findVirtual(fi, name, fdesc.toMethodType());
67+
} catch (ReflectiveOperationException ex) {
68+
throw new AssertionError(ex);
69+
}
70+
}
71+
72+
static MemoryLayout align(MemoryLayout layout, long align) {
73+
return switch (layout) {
74+
case PaddingLayout p -> p;
75+
case ValueLayout v -> v.withByteAlignment(align);
76+
case GroupLayout g -> {
77+
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
78+
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
79+
yield g instanceof StructLayout ?
80+
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
81+
}
82+
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
83+
};
84+
}
85+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
// Generated by jextract
27+
28+
package jdk.internal.ffi.generated.errno;
29+
30+
import java.lang.foreign.*;
31+
import java.lang.invoke.*;
32+
33+
@SuppressWarnings("restricted")
34+
public class errno_h extends errno_h$shared {
35+
36+
errno_h() {
37+
// Should not be called directly
38+
}
39+
40+
static final Arena LIBRARY_ARENA = Arena.ofAuto();
41+
42+
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.loaderLookup()
43+
.or(Linker.nativeLinker().defaultLookup());
44+
45+
private static final int EINTR = (int)4L;
46+
/**
47+
* {@snippet lang=c :
48+
* #define EINTR 4
49+
* }
50+
*/
51+
public static int EINTR() {
52+
return EINTR;
53+
}
54+
55+
private static class strerror_r {
56+
public static final FunctionDescriptor DESC = FunctionDescriptor.of(
57+
C_INT,
58+
C_INT,
59+
C_POINTER,
60+
C_LONG
61+
);
62+
63+
public static final MemorySegment ADDR = SYMBOL_LOOKUP.findOrThrow("strerror_r");
64+
65+
public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
66+
}
67+
68+
/**
69+
* Function descriptor for:
70+
* {@snippet lang=c :
71+
* int strerror_r(int __errnum, char *__strerrbuf, size_t __buflen)
72+
* }
73+
*/
74+
public static FunctionDescriptor strerror_r$descriptor() {
75+
return strerror_r.DESC;
76+
}
77+
78+
/**
79+
* Downcall method handle for:
80+
* {@snippet lang=c :
81+
* int strerror_r(int __errnum, char *__strerrbuf, size_t __buflen)
82+
* }
83+
*/
84+
public static MethodHandle strerror_r$handle() {
85+
return strerror_r.HANDLE;
86+
}
87+
88+
/**
89+
* Address for:
90+
* {@snippet lang=c :
91+
* int strerror_r(int __errnum, char *__strerrbuf, size_t __buflen)
92+
* }
93+
*/
94+
public static MemorySegment strerror_r$address() {
95+
return strerror_r.ADDR;
96+
}
97+
98+
/**
99+
* {@snippet lang=c :
100+
* int strerror_r(int __errnum, char *__strerrbuf, size_t __buflen)
101+
* }
102+
*/
103+
public static int strerror_r(int __errnum, MemorySegment __strerrbuf, long __buflen) {
104+
var mh$ = strerror_r.HANDLE;
105+
try {
106+
if (TRACE_DOWNCALLS) {
107+
traceDowncall("strerror_r", __errnum, __strerrbuf, __buflen);
108+
}
109+
return (int)mh$.invokeExact(__errnum, __strerrbuf, __buflen);
110+
} catch (Error | RuntimeException ex) {
111+
throw ex;
112+
} catch (Throwable ex$) {
113+
throw new AssertionError("should not reach here", ex$);
114+
}
115+
}
116+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
/**
27+
* Defines native structures for errno APIs.
28+
* Generated with the following jextract command:
29+
* {@snippet lang = "Shell Script":
30+
*
31+
* HEADER_NAME=errno.h
32+
* echo "#include <errno.h>" > $HEADER_NAME
33+
* echo "#include <string.h>" >> $HEADER_NAME
34+
*
35+
* jextract --target-package jdk.internal.ffi.generated.errno \
36+
* --include-constant EINTR \
37+
* --include-function strerror_r \
38+
* $HEADER_NAME
39+
* }
40+
*
41+
*/
42+
43+
package jdk.internal.ffi.generated.errno;

0 commit comments

Comments
 (0)