Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/main/java/org/openjdk/jextract/impl/StructBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -359,18 +359,18 @@ private void emitReinterpret() {

/**
* Reinterprets {@code addr} using target {@code arena} and {@code cleanupAction} (if any).
* The returned segment has size {@code layout().byteSize()}
* The returned segment has size {@code elementCount * layout().byteSize()}
*/
public static MemorySegment reinterpret(MemorySegment addr, Arena arena, Consumer<MemorySegment> cleanup) {
return reinterpret(addr, 1, arena, cleanup);
public static MemorySegment reinterpret(MemorySegment addr, long elementCount, Arena arena, Consumer<MemorySegment> cleanup) {
return addr.reinterpret(layout().byteSize() * elementCount, arena, cleanup);
}

/**
* Reinterprets {@code addr} using target {@code arena} and {@code cleanupAction} (if any).
* Reinterprets {@code addr} using the existing scope.
* The returned segment has size {@code elementCount * layout().byteSize()}
*/
public static MemorySegment reinterpret(MemorySegment addr, long elementCount, Arena arena, Consumer<MemorySegment> cleanup) {
return addr.reinterpret(layout().byteSize() * elementCount, arena, cleanup);
public static MemorySegment reinterpret(MemorySegment addr, long elementCount) {
return addr.reinterpret(layout().byteSize() * elementCount);
}
""");
}
Expand Down
17 changes: 15 additions & 2 deletions test/jtreg/generator/reinterpret/TestReinterpret.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -45,7 +45,7 @@ public class TestReinterpret {
public void testSingleStruct() {
try (Arena arena = Arena.ofConfined()) {
MemorySegment addr = make(14, 99);
MemorySegment seg = Point.reinterpret(addr, arena, reinterpret_h::freePoint);
MemorySegment seg = Point.reinterpret(addr, 1, arena, reinterpret_h::freePoint);
assertEquals(Point.x(seg), 14);
assertEquals(Point.y(seg), 99);
}
Expand All @@ -64,4 +64,17 @@ public void testStructArray() {
}
}
}

@Test
public void testReinterpretScope() {
try (Arena arena = Arena.ofConfined()) {
MemorySegment addr = make(14, 99);
MemorySegment seg = Point.reinterpret(addr, 1);
assertEquals(Point.x(seg), 14);
assertEquals(Point.y(seg), 99);

MemorySegment array = Point.reinterpret(addr, 2);
assertEquals(array.byteSize(), Point.layout().byteSize() * 2);
}
}
}
4 changes: 2 additions & 2 deletions test/jtreg/generator/test8257892/LibUnsupportedTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -62,7 +62,7 @@ public void testAllocateFoo() {
@Test
public void testGetFoo() {
try (Arena arena = Arena.ofConfined()) {
var seg = Foo.reinterpret(getFoo(), arena, null);
var seg = Foo.reinterpret(getFoo(), 1, arena, null);
Foo.i(seg, 42);
Foo.c(seg, (byte)'j');
assertEquals(Foo.i(seg), 42);
Expand Down