Skip to content

Commit 23c6ff7

Browse files
committed
7903384: sync jextract for jdk 20 panama API changes
Reviewed-by: mcimadamore
1 parent ee2d648 commit 23c6ff7

File tree

78 files changed

+5197
-1849
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+5197
-1849
lines changed

samples/dlopen/Dlopen.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
public class Dlopen {
4040
// implementation of Symbol lookup that loads a given shared object using dlopen
4141
// and looks up symbols using dlsym
42-
private static Function<String, Optional<MemorySegment>> lookup(String libraryName, MemorySession session) {
42+
private static Function<String, Optional<MemorySegment>> lookup(String libraryName, SegmentScope scope) {
4343
try (Arena libArena = Arena.openConfined()) {
4444
var handleAddr = dlopen(libArena.allocateUtf8String(libraryName), RTLD_LOCAL());
4545
if (handleAddr.equals(MemorySegment.NULL)) {
4646
throw new IllegalArgumentException("Cannot find library: " + libraryName);
4747
}
48-
var handle = MemorySegment.ofAddress(handleAddr.address(), 0, session,
48+
var handle = MemorySegment.ofAddress(handleAddr.address(), 0, scope,
4949
() -> dlclose(handleAddr));
5050
return name -> {
5151
try (var arena = Arena.openConfined()) {
@@ -61,7 +61,7 @@ public static void main(String[] args) throws Throwable {
6161
var arg = args.length > 0? args[0] : "Java";
6262
var libName = "libhello.dylib";
6363
try (var arena = Arena.openConfined()) {
64-
var symLookup = lookup(libName, arena.session());
64+
var symLookup = lookup(libName, arena.scope());
6565

6666
var linker = Linker.nativeLinker();
6767
// get method handle for a function from helloLIb

samples/libclang/ASTPrinter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static void main(String[] args) {
7575
level[0]--;
7676

7777
return CXChildVisit_Continue();
78-
}, arena.session());
78+
}, arena.scope());
7979

8080
// get the AST root and visit it
8181
var root = clang_getTranslationUnitCursor(arena, tu);

samples/libffmpeg/LibffmpegMain.java

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.io.IOException;
3333
import java.nio.file.Files;
3434
import java.nio.file.Paths;
35+
import java.util.ArrayList;
36+
import java.util.List;
3537
import java.lang.foreign.*;
3638
import libffmpeg.AVCodecContext;
3739
import libffmpeg.AVFormatContext;
@@ -51,49 +53,69 @@
5153
public class LibffmpegMain {
5254
private static int NUM_FRAMES_TO_CAPTURE = 5;
5355

54-
static class ExitException extends RuntimeException {
55-
final int exitCode;
56-
ExitException(int exitCode, String msg) {
57-
super(msg);
58-
this.exitCode = exitCode;
56+
record Exit(String message, int exitCode) {}
57+
58+
public static void main(String[] args) {
59+
var exit = run(args);
60+
System.err.println(exit.message());
61+
System.exit(exit.exitCode());
62+
}
63+
64+
private static class ArenaCleanup implements AutoCloseable {
65+
private Arena arena = Arena.openConfined();
66+
private final List<Runnable> preCloseActions = new ArrayList<>();
67+
68+
void addCleanup(Runnable runnable) {
69+
preCloseActions.add(runnable);
70+
}
71+
72+
Arena arena() {
73+
return arena;
74+
}
75+
76+
@Override
77+
public void close() {
78+
preCloseActions.forEach(Runnable::run);
79+
System.out.println("cleanup done");
80+
arena.close();
5981
}
6082
}
6183

62-
public static void main(String[] args) {
84+
private static Exit run(String[] args) {
6385
if (args.length != 1) {
64-
System.err.println("please pass a .mp4 file");
65-
System.exit(1);
86+
return new Exit("please pass a .mp4 file", 1);
6687
}
6788

6889
av_register_all();
6990

70-
int exitCode = 0;
7191
var pCodecCtxOrig = NULL;
7292
var pCodecCtx = NULL;
7393
var pFrame = NULL;
7494
var pFrameRGB = NULL;
7595
var buffer = NULL;
7696

77-
try (var session = MemorySession.openConfined()) {
97+
try (var arenaCleanup = new ArenaCleanup()) {
98+
var arena = arenaCleanup.arena();
7899
// AVFormatContext *ppFormatCtx;
79-
var ppFormatCtx = session.allocate(C_POINTER);
100+
var ppFormatCtx = arena.allocate(C_POINTER);
80101
// char* fileName;
81102
var fileName = arena.allocateUtf8String(args[0]);
82103

83104
// open video file
84105
if (avformat_open_input(ppFormatCtx, fileName, NULL, NULL) != 0) {
85-
throw new ExitException(1, "Cannot open " + args[0]);
106+
return new Exit("Cannot open " + args[0], 1);
86107
}
87108
System.out.println("opened " + args[0]);
88109
// AVFormatContext *pFormatCtx;
89110
var pFormatCtx = ppFormatCtx.get(C_POINTER, 0);
90111

91112
// Retrieve stream info
92113
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
93-
throw new ExitException(1, "Could not find stream information");
114+
return new Exit("Could not find stream information", 1);
115+
94116
}
95117

96-
arena.addCloseAction(()-> {
118+
arenaCleanup.addCleanup(() -> {
97119
// Close the video file
98120
avformat_close_input(ppFormatCtx);
99121
});
@@ -129,13 +151,13 @@ public static void main(String[] args) {
129151
}
130152

131153
if (videoStream == -1) {
132-
throw new ExitException(1, "Didn't find a video stream");
154+
return new Exit("Didn't find a video stream", 1);
133155
} else {
134156
System.out.println("Found video stream (index: " + videoStream + ")");
135157
}
136158

137159
if (pCodec.equals(NULL)) {
138-
throw new ExitException(1, "Unsupported codec");
160+
return new Exit("Unsupported codec", 1);
139161
}
140162

141163
// Copy context
@@ -144,12 +166,12 @@ public static void main(String[] args) {
144166
// AVCodecContext *pCodecCtx;
145167
pCodecCtx = avcodec_alloc_context3(pCodec);
146168
if (avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
147-
throw new ExitException(1, "Cannot copy context");
169+
return new Exit("Cannot copy context", 1);
148170
}
149171

150172
// Open codec
151173
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
152-
throw new ExitException(1, "Cannot open codec");
174+
return new Exit("Cannot open codec", 1);
153175
}
154176

155177
// Allocate video frame
@@ -167,13 +189,13 @@ public static void main(String[] args) {
167189

168190

169191
if (pFrame.equals(NULL)) {
170-
throw new ExitException(1, "Cannot allocate frame");
192+
return new Exit("Cannot allocate frame", 1);
171193
}
172194
if (pFrameRGB.equals(NULL)) {
173-
throw new ExitException(1, "Cannot allocate RGB frame");
195+
return new Exit("Cannot allocate RGB frame", 1);
174196
}
175197
if (buffer.equals(NULL)) {
176-
throw new ExitException(1, "cannot allocate buffer");
198+
return new Exit("cannot allocate buffer", 1);
177199
}
178200

179201
// Assign appropriate parts of buffer to image planes in pFrameRGB
@@ -188,9 +210,9 @@ public static void main(String[] args) {
188210

189211
int i = 0;
190212
// ACPacket packet;
191-
var packet = AVPacket.allocate(session);
213+
var packet = AVPacket.allocate(arena);
192214
// int* pFrameFinished;
193-
var pFrameFinished = session.allocate(C_INT);
215+
var pFrameFinished = arena.allocate(C_INT);
194216

195217
while (av_read_frame(pFormatCtx, packet) >= 0) {
196218
// Is this a packet from the video stream?
@@ -210,10 +232,10 @@ public static void main(String[] args) {
210232
// Save the frame to disk
211233
if (++i <= NUM_FRAMES_TO_CAPTURE) {
212234
try {
213-
saveFrame(pFrameRGB, session, width, height, i);
235+
saveFrame(pFrameRGB, arena.scope(), width, height, i);
214236
} catch (Exception exp) {
215237
exp.printStackTrace();
216-
throw new ExitException(1, "save frame failed for frame " + i);
238+
return new Exit("save frame failed for frame " + i, 1);
217239
}
218240
}
219241
}
@@ -222,11 +244,6 @@ public static void main(String[] args) {
222244
// Free the packet that was allocated by av_read_frame
223245
av_free_packet(packet);
224246
}
225-
226-
throw new ExitException(0, "Goodbye!");
227-
} catch (ExitException ee) {
228-
System.err.println(ee.getMessage());
229-
exitCode = ee.exitCode;
230247
} finally {
231248
// clean-up everything
232249

@@ -254,10 +271,10 @@ public static void main(String[] args) {
254271
}
255272
}
256273

257-
System.exit(exitCode);
274+
return new Exit("Goodbye!", 0);
258275
}
259276

260-
private static void saveFrame(MemorySegment frameRGB, MemorySession session,
277+
private static void saveFrame(MemorySegment frameRGB, SegmentScope scope,
261278
int width, int height, int iFrame)
262279
throws IOException {
263280
var header = String.format("P6\n%d %d\n255\n", width, height);
@@ -273,7 +290,7 @@ private static void saveFrame(MemorySegment frameRGB, MemorySession session,
273290
// Write pixel data
274291
for (int y = 0; y < height; y++) {
275292
// frameRGB.data[0] + y*frameRGB.linesize[0] is the pointer. And 3*width size of data
276-
var pixelArray = MemorySegment.ofAddress(pdata.address() + y*linesize, 3*width, session);
293+
var pixelArray = MemorySegment.ofAddress(pdata.address() + y*linesize, 3*width, scope);
277294
// dump the pixel byte buffer to file
278295
os.write(pixelArray.toArray(C_CHAR));
279296
}

samples/libjimage/JImageFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static void main(String[] args) {
6868
System.out.println("package " + package_name.getUtf8String(0));
6969
System.out.println("name " + name.getUtf8String(0));
7070
return 1;
71-
}, arena.session());
71+
}, arena.scope());
7272

7373
JIMAGE_ResourceIterator(jimageFile, visitor, NULL);
7474

samples/libjimage/org/openjdk/Constants$root.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
import java.nio.ByteOrder;
88
import java.lang.foreign.*;
99
import static java.lang.foreign.ValueLayout.*;
10-
public class Constants$root {
10+
final class Constants$root {
1111

12-
static final OfBoolean C_BOOL$LAYOUT = JAVA_BOOLEAN;
13-
static final OfByte C_CHAR$LAYOUT = JAVA_BYTE;
14-
static final OfShort C_SHORT$LAYOUT = JAVA_SHORT.withBitAlignment(16);
15-
static final OfInt C_INT$LAYOUT = JAVA_INT.withBitAlignment(32);
16-
static final OfLong C_LONG$LAYOUT = JAVA_LONG.withBitAlignment(64);
17-
static final OfLong C_LONG_LONG$LAYOUT = JAVA_LONG.withBitAlignment(64);
18-
static final OfFloat C_FLOAT$LAYOUT = JAVA_FLOAT.withBitAlignment(32);
19-
static final OfDouble C_DOUBLE$LAYOUT = JAVA_DOUBLE.withBitAlignment(64);
20-
static final OfAddress C_POINTER$LAYOUT = ADDRESS.withBitAlignment(64).asUnbounded();
12+
// Suppresses default constructor, ensuring non-instantiability.
13+
private Constants$root() {}
14+
static final OfBoolean C_BOOL$LAYOUT = JAVA_BOOLEAN;
15+
static final OfByte C_CHAR$LAYOUT = JAVA_BYTE;
16+
static final OfShort C_SHORT$LAYOUT = JAVA_SHORT;
17+
static final OfInt C_INT$LAYOUT = JAVA_INT;
18+
static final OfLong C_LONG$LAYOUT = JAVA_LONG;
19+
static final OfLong C_LONG_LONG$LAYOUT = JAVA_LONG;
20+
static final OfFloat C_FLOAT$LAYOUT = JAVA_FLOAT;
21+
static final OfDouble C_DOUBLE$LAYOUT = JAVA_DOUBLE;
22+
static final OfAddress C_POINTER$LAYOUT = ADDRESS.withBitAlignment(64).asUnbounded();
2123
}
2224

2325

samples/libjimage/org/openjdk/JImageClose_t.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77
import java.nio.ByteOrder;
88
import java.lang.foreign.*;
99
import static java.lang.foreign.ValueLayout.*;
10+
/**
11+
* {@snippet :
12+
* void (*JImageClose_t)(struct JImageFile* jimage);
13+
* }
14+
*/
1015
public interface JImageClose_t {
1116

1217
void apply(java.lang.foreign.MemorySegment jimage);
13-
static MemorySegment allocate(JImageClose_t fi, MemorySession session) {
14-
return RuntimeHelper.upcallStub(JImageClose_t.class, fi, constants$0.JImageClose_t$FUNC, session);
18+
static MemorySegment allocate(JImageClose_t fi, SegmentScope scope) {
19+
return RuntimeHelper.upcallStub(JImageClose_t.class, fi, constants$0.JImageClose_t$FUNC, scope);
1520
}
16-
static JImageClose_t ofAddress(MemorySegment addr, MemorySession session) {
17-
MemorySegment symbol = MemorySegment.ofAddress(addr.address(), 0, session);
21+
static JImageClose_t ofAddress(MemorySegment addr, SegmentScope scope) {
22+
MemorySegment symbol = MemorySegment.ofAddress(addr.address(), 0, scope);
1823
return (java.lang.foreign.MemorySegment _jimage) -> {
1924
try {
20-
constants$0.JImageClose_t$MH.invokeExact((MemorySegment)symbol, _jimage);
25+
constants$0.JImageClose_t$MH.invokeExact(symbol, _jimage);
2126
} catch (Throwable ex$) {
2227
throw new AssertionError("should not reach here", ex$);
2328
}

samples/libjimage/org/openjdk/JImageFindResource_t.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77
import java.nio.ByteOrder;
88
import java.lang.foreign.*;
99
import static java.lang.foreign.ValueLayout.*;
10+
/**
11+
* {@snippet :
12+
* long long (*JImageFindResource_t)(struct JImageFile* jimage,char* module_name,char* version,char* name,long long* size);
13+
* }
14+
*/
1015
public interface JImageFindResource_t {
1116

1217
long apply(java.lang.foreign.MemorySegment jimage, java.lang.foreign.MemorySegment module_name, java.lang.foreign.MemorySegment version, java.lang.foreign.MemorySegment name, java.lang.foreign.MemorySegment size);
13-
static MemorySegment allocate(JImageFindResource_t fi, MemorySession session) {
14-
return RuntimeHelper.upcallStub(JImageFindResource_t.class, fi, constants$1.JImageFindResource_t$FUNC, session);
18+
static MemorySegment allocate(JImageFindResource_t fi, SegmentScope scope) {
19+
return RuntimeHelper.upcallStub(JImageFindResource_t.class, fi, constants$1.JImageFindResource_t$FUNC, scope);
1520
}
16-
static JImageFindResource_t ofAddress(MemorySegment addr, MemorySession session) {
17-
MemorySegment symbol = MemorySegment.ofAddress(addr.address(), 0, session);
21+
static JImageFindResource_t ofAddress(MemorySegment addr, SegmentScope scope) {
22+
MemorySegment symbol = MemorySegment.ofAddress(addr.address(), 0, scope);
1823
return (java.lang.foreign.MemorySegment _jimage, java.lang.foreign.MemorySegment _module_name, java.lang.foreign.MemorySegment _version, java.lang.foreign.MemorySegment _name, java.lang.foreign.MemorySegment _size) -> {
1924
try {
20-
return (long)constants$1.JImageFindResource_t$MH.invokeExact((MemorySegment)symbol, _jimage, _module_name, _version, _name, _size);
25+
return (long)constants$1.JImageFindResource_t$MH.invokeExact(symbol, _jimage, _module_name, _version, _name, _size);
2126
} catch (Throwable ex$) {
2227
throw new AssertionError("should not reach here", ex$);
2328
}

samples/libjimage/org/openjdk/JImageGetResource_t.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77
import java.nio.ByteOrder;
88
import java.lang.foreign.*;
99
import static java.lang.foreign.ValueLayout.*;
10+
/**
11+
* {@snippet :
12+
* long long (*JImageGetResource_t)(struct JImageFile* jimage,long long location,char* buffer,long long size);
13+
* }
14+
*/
1015
public interface JImageGetResource_t {
1116

1217
long apply(java.lang.foreign.MemorySegment jimage, long location, java.lang.foreign.MemorySegment buffer, long size);
13-
static MemorySegment allocate(JImageGetResource_t fi, MemorySession session) {
14-
return RuntimeHelper.upcallStub(JImageGetResource_t.class, fi, constants$2.JImageGetResource_t$FUNC, session);
18+
static MemorySegment allocate(JImageGetResource_t fi, SegmentScope scope) {
19+
return RuntimeHelper.upcallStub(JImageGetResource_t.class, fi, constants$2.JImageGetResource_t$FUNC, scope);
1520
}
16-
static JImageGetResource_t ofAddress(MemorySegment addr, MemorySession session) {
17-
MemorySegment symbol = MemorySegment.ofAddress(addr.address(), 0, session);
21+
static JImageGetResource_t ofAddress(MemorySegment addr, SegmentScope scope) {
22+
MemorySegment symbol = MemorySegment.ofAddress(addr.address(), 0, scope);
1823
return (java.lang.foreign.MemorySegment _jimage, long _location, java.lang.foreign.MemorySegment _buffer, long _size) -> {
1924
try {
20-
return (long)constants$2.JImageGetResource_t$MH.invokeExact((MemorySegment)symbol, _jimage, _location, _buffer, _size);
25+
return (long)constants$2.JImageGetResource_t$MH.invokeExact(symbol, _jimage, _location, _buffer, _size);
2126
} catch (Throwable ex$) {
2227
throw new AssertionError("should not reach here", ex$);
2328
}

samples/libjimage/org/openjdk/JImageOpen_t.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77
import java.nio.ByteOrder;
88
import java.lang.foreign.*;
99
import static java.lang.foreign.ValueLayout.*;
10+
/**
11+
* {@snippet :
12+
* struct JImageFile* (*JImageOpen_t)(char* name,int* error);
13+
* }
14+
*/
1015
public interface JImageOpen_t {
1116

12-
java.lang.foreign.MemorySegment apply(java.lang.foreign.MemorySegment name, java.lang.foreign.MemorySegment error);
13-
static MemorySegment allocate(JImageOpen_t fi, MemorySession session) {
14-
return RuntimeHelper.upcallStub(JImageOpen_t.class, fi, constants$0.JImageOpen_t$FUNC, session);
17+
java.lang.foreign.MemorySegment apply(java.lang.foreign.MemorySegment jimage, java.lang.foreign.MemorySegment package_name);
18+
static MemorySegment allocate(JImageOpen_t fi, SegmentScope scope) {
19+
return RuntimeHelper.upcallStub(JImageOpen_t.class, fi, constants$0.JImageOpen_t$FUNC, scope);
1520
}
16-
static JImageOpen_t ofAddress(MemorySegment addr, MemorySession session) {
17-
MemorySegment symbol = MemorySegment.ofAddress(addr.address(), 0, session);
18-
return (java.lang.foreign.MemorySegment _name, java.lang.foreign.MemorySegment _error) -> {
21+
static JImageOpen_t ofAddress(MemorySegment addr, SegmentScope scope) {
22+
MemorySegment symbol = MemorySegment.ofAddress(addr.address(), 0, scope);
23+
return (java.lang.foreign.MemorySegment _jimage, java.lang.foreign.MemorySegment _package_name) -> {
1924
try {
20-
return (java.lang.foreign.MemorySegment)constants$0.JImageOpen_t$MH.invokeExact((MemorySegment)symbol, _name, _error);
25+
return (java.lang.foreign.MemorySegment)constants$0.JImageOpen_t$MH.invokeExact(symbol, _jimage, _package_name);
2126
} catch (Throwable ex$) {
2227
throw new AssertionError("should not reach here", ex$);
2328
}

0 commit comments

Comments
 (0)