1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
/*
* Copyright (c) 2020, 2023, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
/*
* @test
* @enablePreview
* @modules java.base/jdk.internal.foreign
* @run testng/othervm TestSegmentAllocators
*/
import java.lang.foreign.*;
import jdk.internal.foreign.MappedMemorySegmentImpl;
import jdk.internal.foreign.NativeMemorySegmentImpl;
import org.testng.annotations.*;
import java.lang.foreign.Arena;
import java.lang.invoke.VarHandle;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
import java.util.function.Function;
import static org.testng.Assert.*;
public class TestSegmentAllocators {
final static int ELEMS = 128;
@Test(dataProvider = "scalarAllocations")
@SuppressWarnings("unchecked")
public <Z, L extends ValueLayout> void testAllocation(Z value, AllocationFactory allocationFactory, L layout, AllocationFunction<Z, L> allocationFunction, Function<MemoryLayout, VarHandle> handleFactory) {
layout = (L)layout.withByteAlignment(layout.byteSize());
L[] layouts = (L[])new ValueLayout[] {
layout,
layout.withByteAlignment(layout.byteAlignment() * 2),
layout.withByteAlignment(layout.byteAlignment() * 4),
layout.withByteAlignment(layout.byteAlignment() * 8)
};
for (L alignedLayout : layouts) {
List<MemorySegment> addressList = new ArrayList<>();
int elems = ELEMS / ((int)alignedLayout.byteAlignment() / (int)layout.byteAlignment());
Arena[] arenas = {
Arena.ofConfined(),
Arena.ofShared()
};
for (Arena arena : arenas) {
try (arena) {
SegmentAllocator allocator = allocationFactory.allocator(alignedLayout.byteSize() * ELEMS, arena);
for (int i = 0; i < elems; i++) {
MemorySegment address = allocationFunction.allocate(allocator, alignedLayout, value);
assertEquals(address.byteSize(), alignedLayout.byteSize());
addressList.add(address);
VarHandle handle = handleFactory.apply(alignedLayout);
assertEquals(value, handle.get(address));
}
boolean isBound = allocationFactory.isBound();
try {
allocationFunction.allocate(allocator, alignedLayout, value);
assertFalse(isBound);
} catch (IndexOutOfBoundsException ex) {
//failure is expected if bound
assertTrue(isBound);
}
}
// addresses should be invalid now
for (MemorySegment address : addressList) {
assertFalse(address.scope().isAlive());
}
}
}
}
static final int SIZE_256M = 1024 * 1024 * 256;
@Test
public void testBigAllocationInUnboundedSession() {
try (Arena arena = Arena.ofConfined()) {
for (int i = 8 ; i < SIZE_256M ; i *= 8) {
SegmentAllocator allocator = SegmentAllocator.slicingAllocator(arena.allocate(i * 2 + 1));
MemorySegment address = allocator.allocate(i, i);
//check size
assertEquals(address.byteSize(), i);
//check alignment
assertEquals(address.address() % i, 0);
}
}
}
@Test
public void testTooBigForBoundedArena() {
try (Arena arena = Arena.ofConfined()) {
SegmentAllocator allocator = SegmentAllocator.slicingAllocator(arena.allocate(10));
assertThrows(IndexOutOfBoundsException.class, () -> allocator.allocate(12));
allocator.allocate(5);
}
}
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
public void testBadAllocationSize(SegmentAllocator allocator) {
allocator.allocate(-1);
}
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
public void testBadAllocationAlignZero(SegmentAllocator allocator) {
allocator.allocate(1, 0);
}
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
public void testBadAllocationAlignNeg(SegmentAllocator allocator) {
allocator.allocate(1, -1);
}
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
public void testBadAllocationAlignNotPowerTwo(SegmentAllocator allocator) {
allocator.allocate(1, 3);
}
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
public void testBadAllocationArrayNegSize(SegmentAllocator allocator) {
allocator.allocateArray(ValueLayout.JAVA_BYTE, -1);
}
@Test(dataProvider = "allocators", expectedExceptions = IllegalArgumentException.class)
public void testBadAllocationArrayOverflow(SegmentAllocator allocator) {
allocator.allocateArray(ValueLayout.JAVA_LONG, Long.MAX_VALUE);
}
@Test(expectedExceptions = OutOfMemoryError.class)
public void testBadArenaNullReturn() {
try (Arena arena = Arena.ofConfined()) {
arena.allocate(Long.MAX_VALUE, 2);
}
}
@Test
public void testArrayAllocateDelegation() {
AtomicInteger calls = new AtomicInteger();
SegmentAllocator allocator = new SegmentAllocator() {
@Override
public MemorySegment allocate(long bytesSize, long byteAlignment) {
return null;
}
@Override
public MemorySegment allocateArray(MemoryLayout elementLayout, long count) {
calls.incrementAndGet();
return null;
};
};
allocator.allocateArray(ValueLayout.JAVA_BYTE);
allocator.allocateArray(ValueLayout.JAVA_SHORT);
allocator.allocateArray(ValueLayout.JAVA_CHAR);
allocator.allocateArray(ValueLayout.JAVA_INT);
allocator.allocateArray(ValueLayout.JAVA_FLOAT);
allocator.allocateArray(ValueLayout.JAVA_LONG);
allocator.allocateArray(ValueLayout.JAVA_DOUBLE);
assertEquals(calls.get(), 7);
}
@Test
public void testStringAllocateDelegation() {
AtomicInteger calls = new AtomicInteger();
SegmentAllocator allocator = new SegmentAllocator() {
@Override
public MemorySegment allocate(long byteSize, long byteAlignment) {
return Arena.ofAuto().allocate(byteSize, byteAlignment);
}
@Override
public MemorySegment allocate(long size) {
calls.incrementAndGet();
return allocate(size, 1);
};
};
allocator.allocateUtf8String("Hello");
assertEquals(calls.get(), 1);
}
@Test(dataProvider = "arrayAllocations")
public <Z> void testArray(AllocationFactory allocationFactory, ValueLayout layout, AllocationFunction<Object, ValueLayout> allocationFunction, ToArrayHelper<Z> arrayHelper) {
Z arr = arrayHelper.array();
Arena[] arenas = {
Arena.ofConfined(),
Arena.ofShared()
};
for (Arena arena : arenas) {
try (arena) {
SegmentAllocator allocator = allocationFactory.allocator(100, arena);
MemorySegment address = allocationFunction.allocate(allocator, layout, arr);
Z found = arrayHelper.toArray(address, layout);
assertEquals(found, arr);
}
}
}
@Test(dataProvider = "arrayAllocations")
public <Z> void testPredicatesAndCommands(AllocationFactory allocationFactory, ValueLayout layout, AllocationFunction<Object, ValueLayout> allocationFunction, ToArrayHelper<Z> arrayHelper) {
Z arr = arrayHelper.array();
Arena[] arenas = {
Arena.ofConfined(),
Arena.ofShared()
};
for (Arena arena : arenas) {
try (arena) {
SegmentAllocator allocator = allocationFactory.allocator(100, arena);
MemorySegment segment = allocationFunction.allocate(allocator, layout, arr);
assertThrows(UnsupportedOperationException.class, segment::load);
assertThrows(UnsupportedOperationException.class, segment::unload);
assertThrows(UnsupportedOperationException.class, segment::isLoaded);
assertThrows(UnsupportedOperationException.class, segment::force);
assertFalse(segment.isMapped());
assertEquals(segment.isNative(), segment instanceof NativeMemorySegmentImpl);
}
}
}
@DataProvider(name = "scalarAllocations")
static Object[][] scalarAllocations() {
List<Object[]> scalarAllocations = new ArrayList<>();
for (AllocationFactory factory : AllocationFactory.values()) {
scalarAllocations.add(new Object[] { (byte)42, factory, ValueLayout.JAVA_BYTE,
(AllocationFunction.OfByte) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { (short)42, factory, ValueLayout.JAVA_SHORT.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfShort) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { (char)42, factory, ValueLayout.JAVA_CHAR.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfChar) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { 42, factory,
ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfInt) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { 42f, factory, ValueLayout.JAVA_FLOAT.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfFloat) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { 42L, factory, ValueLayout.JAVA_LONG.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfLong) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { 42d, factory, ValueLayout.JAVA_DOUBLE.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfDouble) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { MemorySegment.ofAddress(42), factory, ValueLayout.ADDRESS.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfAddress) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { (short)42, factory, ValueLayout.JAVA_SHORT.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfShort) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { (char)42, factory, ValueLayout.JAVA_CHAR.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfChar) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { 42, factory,
ValueLayout.JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfInt) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { 42f, factory, ValueLayout.JAVA_FLOAT.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfFloat) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { 42L, factory, ValueLayout.JAVA_LONG.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfLong) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { 42d, factory, ValueLayout.JAVA_DOUBLE.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfDouble) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
scalarAllocations.add(new Object[] { MemorySegment.ofAddress(42), factory, ValueLayout.ADDRESS.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfAddress) SegmentAllocator::allocate,
(Function<MemoryLayout, VarHandle>)l -> l.varHandle() });
}
return scalarAllocations.toArray(Object[][]::new);
}
@DataProvider(name = "arrayAllocations")
static Object[][] arrayAllocations() {
List<Object[]> arrayAllocations = new ArrayList<>();
for (AllocationFactory factory : AllocationFactory.values()) {
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_BYTE,
(AllocationFunction.OfByteArray) SegmentAllocator::allocateArray,
ToArrayHelper.toByteArray });
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_CHAR.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfCharArray) SegmentAllocator::allocateArray,
ToArrayHelper.toCharArray });
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_SHORT.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfShortArray) SegmentAllocator::allocateArray,
ToArrayHelper.toShortArray });
arrayAllocations.add(new Object[] { factory,
ValueLayout.JAVA_INT.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfIntArray) SegmentAllocator::allocateArray,
ToArrayHelper.toIntArray });
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_FLOAT.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfFloatArray) SegmentAllocator::allocateArray,
ToArrayHelper.toFloatArray });
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_LONG.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfLongArray) SegmentAllocator::allocateArray,
ToArrayHelper.toLongArray });
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_DOUBLE.withOrder(ByteOrder.LITTLE_ENDIAN),
(AllocationFunction.OfDoubleArray) SegmentAllocator::allocateArray,
ToArrayHelper.toDoubleArray });
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_CHAR.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfCharArray) SegmentAllocator::allocateArray,
ToArrayHelper.toCharArray });
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_SHORT.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfShortArray) SegmentAllocator::allocateArray,
ToArrayHelper.toShortArray });
arrayAllocations.add(new Object[] { factory,
ValueLayout.JAVA_INT.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfIntArray) SegmentAllocator::allocateArray,
ToArrayHelper.toIntArray });
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_FLOAT.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfFloatArray) SegmentAllocator::allocateArray,
ToArrayHelper.toFloatArray });
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_LONG.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfLongArray) SegmentAllocator::allocateArray,
ToArrayHelper.toLongArray });
arrayAllocations.add(new Object[] { factory, ValueLayout.JAVA_DOUBLE.withOrder(ByteOrder.BIG_ENDIAN),
(AllocationFunction.OfDoubleArray) SegmentAllocator::allocateArray,
ToArrayHelper.toDoubleArray });
};
return arrayAllocations.toArray(Object[][]::new);
}
interface AllocationFunction<X, L extends ValueLayout> {
MemorySegment allocate(SegmentAllocator allocator, L layout, X value);
interface OfByte extends AllocationFunction<Byte, ValueLayout.OfByte> { }
interface OfBoolean extends AllocationFunction<Boolean, ValueLayout.OfBoolean> { }
interface OfChar extends AllocationFunction<Character, ValueLayout.OfChar> { }
interface OfShort extends AllocationFunction<Short, ValueLayout.OfShort> { }
interface OfInt extends AllocationFunction<Integer, ValueLayout.OfInt> { }
interface OfFloat extends AllocationFunction<Float, ValueLayout.OfFloat> { }
interface OfLong extends AllocationFunction<Long, ValueLayout.OfLong> { }
interface OfDouble extends AllocationFunction<Double, ValueLayout.OfDouble> { }
interface OfAddress extends AllocationFunction<MemorySegment, AddressLayout> { }
interface OfByteArray extends AllocationFunction<byte[], ValueLayout.OfByte> { }
interface OfCharArray extends AllocationFunction<char[], ValueLayout.OfChar> { }
interface OfShortArray extends AllocationFunction<short[], ValueLayout.OfShort> { }
interface OfIntArray extends AllocationFunction<int[], ValueLayout.OfInt> { }
interface OfFloatArray extends AllocationFunction<float[], ValueLayout.OfFloat> { }
interface OfLongArray extends AllocationFunction<long[], ValueLayout.OfLong> { }
interface OfDoubleArray extends AllocationFunction<double[], ValueLayout.OfDouble> { }
}
enum AllocationFactory {
SLICING(true, (size, arena) -> {
return SegmentAllocator.slicingAllocator(arena.allocate(size, 1));
});
private final boolean isBound;
private final BiFunction<Long, Arena, SegmentAllocator> factory;
AllocationFactory(boolean isBound, BiFunction<Long, Arena, SegmentAllocator> factory) {
this.isBound = isBound;
this.factory = factory;
}
SegmentAllocator allocator(long size, Arena arena) {
return factory.apply(size, arena);
}
public boolean isBound() {
return isBound;
}
}
interface ToArrayHelper<T> {
T array();
T toArray(MemorySegment segment, ValueLayout layout);
ToArrayHelper<byte[]> toByteArray = new ToArrayHelper<>() {
@Override
public byte[] array() {
return new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
}
@Override
public byte[] toArray(MemorySegment segment, ValueLayout layout) {
ByteBuffer buffer = segment.asByteBuffer().order(layout.order());
byte[] found = new byte[buffer.limit()];
buffer.get(found);
return found;
}
};
ToArrayHelper<char[]> toCharArray = new ToArrayHelper<>() {
@Override
public char[] array() {
return new char[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
}
@Override
public char[] toArray(MemorySegment segment, ValueLayout layout) {
CharBuffer buffer = segment.asByteBuffer().order(layout.order()).asCharBuffer();
char[] found = new char[buffer.limit()];
buffer.get(found);
return found;
}
};
ToArrayHelper<short[]> toShortArray = new ToArrayHelper<>() {
@Override
public short[] array() {
return new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
}
@Override
public short[] toArray(MemorySegment segment, ValueLayout layout) {
ShortBuffer buffer = segment.asByteBuffer().order(layout.order()).asShortBuffer();
short[] found = new short[buffer.limit()];
buffer.get(found);
return found;
}
};
ToArrayHelper<int[]> toIntArray = new ToArrayHelper<>() {
@Override
public int[] array() {
return new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
}
@Override
public int[] toArray(MemorySegment segment, ValueLayout layout) {
IntBuffer buffer = segment.asByteBuffer().order(layout.order()).asIntBuffer();
int[] found = new int[buffer.limit()];
buffer.get(found);
return found;
}
};
ToArrayHelper<float[]> toFloatArray = new ToArrayHelper<>() {
@Override
public float[] array() {
return new float[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
}
@Override
public float[] toArray(MemorySegment segment, ValueLayout layout) {
FloatBuffer buffer = segment.asByteBuffer().order(layout.order()).asFloatBuffer();
float[] found = new float[buffer.limit()];
buffer.get(found);
return found;
}
};
ToArrayHelper<long[]> toLongArray = new ToArrayHelper<>() {
@Override
public long[] array() {
return new long[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
}
@Override
public long[] toArray(MemorySegment segment, ValueLayout layout) {
LongBuffer buffer = segment.asByteBuffer().order(layout.order()).asLongBuffer();
long[] found = new long[buffer.limit()];
buffer.get(found);
return found;
}
};
ToArrayHelper<double[]> toDoubleArray = new ToArrayHelper<>() {
@Override
public double[] array() {
return new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
}
@Override
public double[] toArray(MemorySegment segment, ValueLayout layout) {
DoubleBuffer buffer = segment.asByteBuffer().order(layout.order()).asDoubleBuffer();
double[] found = new double[buffer.limit()];
buffer.get(found);
return found;
}
};
}
@DataProvider(name = "allocators")
static Object[][] allocators() {
return new Object[][] {
{ SegmentAllocator.prefixAllocator(Arena.global().allocate(10, 1)) },
};
}
}
|