< prev index next >

test/jdk/java/foreign/TestScopedOperations.java

Print this page

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @enablePreview
 27  * @requires jdk.foreign.linker != "UNSUPPORTED"
 28  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestScopedOperations
 29  */
 30 
 31 import java.lang.foreign.Arena;
 32 import java.lang.foreign.MemorySegment;
 33 import java.lang.foreign.ValueLayout;
 34 
 35 import org.testng.annotations.DataProvider;
 36 import org.testng.annotations.Test;
 37 
 38 import java.io.File;
 39 import java.io.IOException;
 40 import java.nio.channels.FileChannel;
 41 import java.nio.file.Path;
 42 import java.nio.file.StandardOpenOption;
 43 import java.util.ArrayList;
 44 import java.util.List;
 45 import java.util.concurrent.atomic.AtomicReference;
 46 import java.util.function.Consumer;
 47 import java.util.function.Function;

105 
106     static {
107         // session operations
108         ScopedOperation.ofScope(session -> session.allocate(100, 1), "MemorySession::allocate");
109         ScopedOperation.ofScope(session -> {
110             try (FileChannel fileChannel = FileChannel.open(tempPath, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
111                 fileChannel.map(FileChannel.MapMode.READ_WRITE, 0L, 10L, session);
112             } catch (IOException ex) {
113                 fail();
114             }
115         }, "FileChannel::map");
116         // segment operations
117         ScopedOperation.ofSegment(s -> s.toArray(JAVA_BYTE), "MemorySegment::toArray(BYTE)");
118         ScopedOperation.ofSegment(s -> s.copyFrom(s), "MemorySegment::copyFrom");
119         ScopedOperation.ofSegment(s -> s.mismatch(s), "MemorySegment::mismatch");
120         ScopedOperation.ofSegment(s -> s.fill((byte) 0), "MemorySegment::fill");
121         // allocator operations
122         ScopedOperation.ofScope(a -> a.allocate(1), "Arena::allocate/size");
123         ScopedOperation.ofScope(a -> a.allocate(1, 1), "Arena::allocate/size/align");
124         ScopedOperation.ofScope(a -> a.allocate(JAVA_BYTE), "Arena::allocate/layout");
125         ScopedOperation.ofScope(a -> a.allocate(JAVA_BYTE, (byte) 0), "Arena::allocate/byte");
126         ScopedOperation.ofScope(a -> a.allocate(ValueLayout.JAVA_CHAR, (char) 0), "Arena::allocate/char");
127         ScopedOperation.ofScope(a -> a.allocate(ValueLayout.JAVA_SHORT, (short) 0), "Arena::allocate/short");
128         ScopedOperation.ofScope(a -> a.allocate(ValueLayout.JAVA_INT, 0), "Arena::allocate/int");
129         ScopedOperation.ofScope(a -> a.allocate(ValueLayout.JAVA_FLOAT, 0f), "Arena::allocate/float");
130         ScopedOperation.ofScope(a -> a.allocate(ValueLayout.JAVA_LONG, 0L), "Arena::allocate/long");
131         ScopedOperation.ofScope(a -> a.allocate(ValueLayout.JAVA_DOUBLE, 0d), "Arena::allocate/double");
132         ScopedOperation.ofScope(a -> a.allocateArray(JAVA_BYTE, 1L), "Arena::allocateArray/size");
133         ScopedOperation.ofScope(a -> a.allocateArray(JAVA_BYTE, new byte[]{0}), "Arena::allocateArray/byte");
134         ScopedOperation.ofScope(a -> a.allocateArray(ValueLayout.JAVA_CHAR, new char[]{0}), "Arena::allocateArray/char");
135         ScopedOperation.ofScope(a -> a.allocateArray(ValueLayout.JAVA_SHORT, new short[]{0}), "Arena::allocateArray/short");
136         ScopedOperation.ofScope(a -> a.allocateArray(ValueLayout.JAVA_INT, new int[]{0}), "Arena::allocateArray/int");
137         ScopedOperation.ofScope(a -> a.allocateArray(ValueLayout.JAVA_FLOAT, new float[]{0}), "Arena::allocateArray/float");
138         ScopedOperation.ofScope(a -> a.allocateArray(ValueLayout.JAVA_LONG, new long[]{0}), "Arena::allocateArray/long");
139         ScopedOperation.ofScope(a -> a.allocateArray(ValueLayout.JAVA_DOUBLE, new double[]{0}), "Arena::allocateArray/double");
140     };
141 
142     @DataProvider(name = "scopedOperations")
143     static Object[][] scopedOperations() {
144         return scopedOperations.stream().map(op -> new Object[] { op.name, op }).toArray(Object[][]::new);
145     }
146 
147     static class ScopedOperation<X> implements Consumer<X>, Function<Arena, X> {
148 
149         final Function<Arena, X> factory;
150         final Consumer<X> operation;
151         final String name;
152 
153         private ScopedOperation(Function<Arena, X> factory, Consumer<X> operation, String name) {
154             this.factory = factory;
155             this.operation = operation;
156             this.name = name;
157         }
158 
159         @Override

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test


 26  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestScopedOperations
 27  */
 28 
 29 import java.lang.foreign.Arena;
 30 import java.lang.foreign.MemorySegment;
 31 import java.lang.foreign.ValueLayout;
 32 
 33 import org.testng.annotations.DataProvider;
 34 import org.testng.annotations.Test;
 35 
 36 import java.io.File;
 37 import java.io.IOException;
 38 import java.nio.channels.FileChannel;
 39 import java.nio.file.Path;
 40 import java.nio.file.StandardOpenOption;
 41 import java.util.ArrayList;
 42 import java.util.List;
 43 import java.util.concurrent.atomic.AtomicReference;
 44 import java.util.function.Consumer;
 45 import java.util.function.Function;

103 
104     static {
105         // session operations
106         ScopedOperation.ofScope(session -> session.allocate(100, 1), "MemorySession::allocate");
107         ScopedOperation.ofScope(session -> {
108             try (FileChannel fileChannel = FileChannel.open(tempPath, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
109                 fileChannel.map(FileChannel.MapMode.READ_WRITE, 0L, 10L, session);
110             } catch (IOException ex) {
111                 fail();
112             }
113         }, "FileChannel::map");
114         // segment operations
115         ScopedOperation.ofSegment(s -> s.toArray(JAVA_BYTE), "MemorySegment::toArray(BYTE)");
116         ScopedOperation.ofSegment(s -> s.copyFrom(s), "MemorySegment::copyFrom");
117         ScopedOperation.ofSegment(s -> s.mismatch(s), "MemorySegment::mismatch");
118         ScopedOperation.ofSegment(s -> s.fill((byte) 0), "MemorySegment::fill");
119         // allocator operations
120         ScopedOperation.ofScope(a -> a.allocate(1), "Arena::allocate/size");
121         ScopedOperation.ofScope(a -> a.allocate(1, 1), "Arena::allocate/size/align");
122         ScopedOperation.ofScope(a -> a.allocate(JAVA_BYTE), "Arena::allocate/layout");
123         ScopedOperation.ofScope(a -> a.allocateFrom(JAVA_BYTE, (byte) 0), "Arena::allocate/byte");
124         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_CHAR, (char) 0), "Arena::allocate/char");
125         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_SHORT, (short) 0), "Arena::allocate/short");
126         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_INT, 0), "Arena::allocate/int");
127         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_FLOAT, 0f), "Arena::allocate/float");
128         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_LONG, 0L), "Arena::allocate/long");
129         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_DOUBLE, 0d), "Arena::allocate/double");
130         ScopedOperation.ofScope(a -> a.allocate(JAVA_BYTE, 1L), "Arena::allocate/size");
131         ScopedOperation.ofScope(a -> a.allocateFrom(JAVA_BYTE, new byte[]{0}), "Arena::allocateFrom/byte");
132         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_CHAR, new char[]{0}), "Arena::allocateFrom/char");
133         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_SHORT, new short[]{0}), "Arena::allocateFrom/short");
134         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_INT, new int[]{0}), "Arena::allocateFrom/int");
135         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_FLOAT, new float[]{0}), "Arena::allocateFrom/float");
136         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_LONG, new long[]{0}), "Arena::allocateFrom/long");
137         ScopedOperation.ofScope(a -> a.allocateFrom(ValueLayout.JAVA_DOUBLE, new double[]{0}), "Arena::allocateFrom/double");
138     };
139 
140     @DataProvider(name = "scopedOperations")
141     static Object[][] scopedOperations() {
142         return scopedOperations.stream().map(op -> new Object[] { op.name, op }).toArray(Object[][]::new);
143     }
144 
145     static class ScopedOperation<X> implements Consumer<X>, Function<Arena, X> {
146 
147         final Function<Arena, X> factory;
148         final Consumer<X> operation;
149         final String name;
150 
151         private ScopedOperation(Function<Arena, X> factory, Consumer<X> operation, String name) {
152             this.factory = factory;
153             this.operation = operation;
154             this.name = name;
155         }
156 
157         @Override
< prev index next >