Skip to content

Commit 28b2a78

Browse files
committedJan 16, 2024
7903628: Replace ofAddress method in FI classes with a static invoke method
Reviewed-by: mcimadamore
1 parent 0fb877e commit 28b2a78

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed
 

‎src/main/java/org/openjdk/jextract/impl/FunctionalInterfaceBuilder.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public static void generate(SourceFileBuilder builder, String className, ClassSo
5858
fib.emitFunctionalInterfaceMethod();
5959
fib.emitDescriptorDecl();
6060
fib.emitFunctionalFactories();
61-
fib.emitFunctionalFactoryForPointer();
61+
fib.emitInvoke();
6262
fib.classEnd();
6363
}
6464

6565
private void emitFunctionalInterfaceMethod() {
6666
appendIndentedLines(STR."""
6767
68-
\{methodType.returnType().getSimpleName()} apply(\{paramExprs("")});
68+
\{methodType.returnType().getSimpleName()} apply(\{paramExprs()});
6969
""");
7070
}
7171

@@ -80,22 +80,21 @@ static MemorySegment allocate(\{className()} fi, Arena scope) {
8080
""");
8181
}
8282

83-
private void emitFunctionalFactoryForPointer() {
83+
private void emitInvoke() {
8484
boolean needsAllocator = Utils.isStructOrUnion(funcType.returnType());
85-
String allocArg = needsAllocator ? ", (SegmentAllocator)arena" : "";
85+
String allocParam = needsAllocator ? ", SegmentAllocator alloc" : "";
86+
String allocArg = needsAllocator ? ", alloc" : "";
87+
String paramStr = methodType.parameterCount() != 0 ? STR.",\{paramExprs()}" : "";
8688
appendIndentedLines(STR."""
8789
8890
MethodHandle DOWN$MH = Linker.nativeLinker().downcallHandle($DESC);
8991
90-
static \{className()} ofAddress(MemorySegment addr, Arena arena) {
91-
MemorySegment symbol = addr.reinterpret(arena, null);
92-
return (\{paramExprs("_")}) -> {
93-
try {
94-
\{retExpr()} DOWN$MH.invokeExact(symbol\{allocArg}\{otherArgExprs()});
95-
} catch (Throwable ex$) {
96-
throw new AssertionError("should not reach here", ex$);
97-
}
98-
};
92+
static \{methodType.returnType().getSimpleName()} invoke(MemorySegment funcPtr\{allocParam}\{paramStr}) {
93+
try {
94+
\{retExpr()} DOWN$MH.invokeExact(funcPtr\{allocArg}\{otherArgExprs()});
95+
} catch (Throwable ex$) {
96+
throw new AssertionError("should not reach here", ex$);
97+
}
9998
}
10099
""");
101100
}
@@ -109,13 +108,13 @@ private String parameterName(int i) {
109108
return name.isEmpty()? "_x" + i : name;
110109
}
111110

112-
private String paramExprs(String paramNamePrefix) {
111+
private String paramExprs() {
113112
StringBuilder result = new StringBuilder();
114113
String delim = "";
115114
for (int i = 0 ; i < methodType.parameterCount(); i++) {
116115
result.append(delim).append(methodType.parameterType(i).getSimpleName());
117116
result.append(" ");
118-
result.append(paramNamePrefix).append(parameterName(i));
117+
result.append(parameterName(i));
119118
delim = ", ";
120119
}
121120
return result.toString();
@@ -133,7 +132,7 @@ private String otherArgExprs() {
133132
String argsExprs = "";
134133
if (methodType.parameterCount() > 0) {
135134
argsExprs += ", " + IntStream.range(0, methodType.parameterCount())
136-
.mapToObj(i -> "_" + parameterName(i))
135+
.mapToObj(this::parameterName)
137136
.collect(Collectors.joining(", "));
138137
}
139138
return argsExprs;

‎test/jtreg/generator/allocCallback/TestAllocCallback.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public void testOfAddress() {
5252
try (Arena arena = Arena.ofConfined()) {
5353
var foo = alloc_callback_h.foo$SEGMENT();
5454

55-
var barA = Foo.a.ofAddress(Foo.a(foo), arena).apply();
56-
var barB = Foo.b.ofAddress(Foo.b(foo), arena).apply(100);
55+
var barA = Foo.a.invoke(Foo.a(foo), arena);
56+
var barB = Foo.b.invoke(Foo.b(foo), arena, 100);
5757

5858
assertEquals(Bar.a(barA), 5);
5959
assertEquals(Bar.a(barB), 100);

‎test/jtreg/generator/funcPointerInvokers/TestFuncPointerInvokers.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void testStructFieldFITypedef() {
5454
AtomicInteger val = new AtomicInteger(-1);
5555
MemorySegment bar = Bar.allocate(arena);
5656
Bar.foo(bar, Foo.allocate((i) -> val.set(i), arena));
57-
Foo.ofAddress(Bar.foo(bar), arena).apply(42);
57+
Foo.invoke(Bar.foo(bar), 42);
5858
assertEquals(val.get(), 42);
5959
}
6060
}
@@ -64,7 +64,7 @@ public void testGlobalFITypedef() {
6464
try (Arena arena = Arena.ofConfined()) {
6565
AtomicInteger val = new AtomicInteger(-1);
6666
f(Foo.allocate((i) -> val.set(i), arena));
67-
Foo.ofAddress(f(), arena).apply(42);
67+
Foo.invoke(f(), 42);
6868
assertEquals(val.get(), 42);
6969
}
7070
}
@@ -75,7 +75,7 @@ public void testStructFieldFIFunctionPointer() {
7575
AtomicInteger val = new AtomicInteger(-1);
7676
MemorySegment baz = Baz.allocate(arena);
7777
Baz.fp(baz, Baz.fp.allocate((i) -> val.set(i), arena));
78-
Baz.fp.ofAddress(Baz.fp(baz), arena).apply(42);
78+
Baz.fp.invoke(Baz.fp(baz), 42);
7979
assertEquals(val.get(), 42);
8080
}
8181
}
@@ -85,7 +85,7 @@ public void testGlobalFIFunctionPointer() {
8585
try (Arena arena = Arena.ofConfined()) {
8686
AtomicInteger val = new AtomicInteger(-1);
8787
fp(fp.allocate((i) -> val.set(i), arena));
88-
fp.ofAddress(fp(), arena).apply(42);
88+
fp.invoke(fp(), 42);
8989
assertEquals(val.get(), 42);
9090
}
9191
}
@@ -94,7 +94,7 @@ public void testGlobalFIFunctionPointer() {
9494
public void testGlobalFIFunctionPointerAddress() {
9595
try (Arena arena = Arena.ofConfined()) {
9696
fp_addr(fp_addr.allocate((addr) -> MemorySegment.ofAddress(addr.address() + 1), arena));
97-
assertEquals(fp_addr.ofAddress(fp_addr(), arena).apply(MemorySegment.ofAddress(42)), MemorySegment.ofAddress(43));
97+
assertEquals(fp_addr.invoke(fp_addr(), MemorySegment.ofAddress(42)), MemorySegment.ofAddress(43));
9898
}
9999
}
100100
}

‎test/jtreg/generator/test8261511/Test8261511.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ public class Test8261511 {
5151
public void test() {
5252
try (Arena arena = Arena.ofConfined()) {
5353
var funcPtr = Foo.sum(get_foo(arena));
54-
var sumIface = Foo.sum.ofAddress(funcPtr, arena);
55-
assertEquals(sumIface.apply(15,20), 35);
54+
assertEquals(Foo.sum.invoke(funcPtr, 15, 20), 35);
5655
assertEquals(sum(1.2, 4.5), 5.7, 0.001);
5756
}
5857
}

0 commit comments

Comments
 (0)
Please sign in to comment.