Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
8289188: SegmentAllocator:allocateArray(*) default behavior mismatch …
Browse files Browse the repository at this point in the history
…to spec

Reviewed-by: alanb
  • Loading branch information
mcimadamore committed Jun 27, 2022
1 parent 699ad45 commit 2c8ada6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
Expand Up @@ -282,12 +282,13 @@ default MemorySegment allocateArray(ValueLayout.OfDouble elementLayout, double..

private <Z> MemorySegment copyArrayWithSwapIfNeeded(Z array, ValueLayout elementLayout,
Function<Z, MemorySegment> heapSegmentFactory) {
Objects.requireNonNull(array);
Objects.requireNonNull(elementLayout);
int size = Array.getLength(array);
MemorySegment addr = allocate(MemoryLayout.sequenceLayout(size, elementLayout));
MemorySegment.copy(heapSegmentFactory.apply(array), elementLayout, 0,
addr, elementLayout.withOrder(ByteOrder.nativeOrder()), 0, size);
int size = array == null ? 0 : Array.getLength(array);
MemorySegment addr = allocateArray(elementLayout, size);
if (size > 0) {
MemorySegment.copy(heapSegmentFactory.apply(array), elementLayout, 0,
addr, elementLayout.withOrder(ByteOrder.nativeOrder()), 0, size);
}
return addr;
}

Expand Down
16 changes: 15 additions & 1 deletion test/jdk/java/foreign/TestNulls.java
Expand Up @@ -121,7 +121,21 @@ public class TestNulls {
"java.lang.foreign.ValueLayout$OfLong/withAttribute(java.lang.String,java.lang.constant.Constable)/1/0",
"java.lang.foreign.ValueLayout$OfDouble/withAttribute(java.lang.String,java.lang.constant.Constable)/1/0",
"java.lang.foreign.GroupLayout/withAttribute(java.lang.String,java.lang.constant.Constable)/1/0",
"java.lang.foreign.FunctionDescriptor/withAttribute(java.lang.String,java.lang.constant.Constable)/1/0"
"java.lang.foreign.FunctionDescriptor/withAttribute(java.lang.String,java.lang.constant.Constable)/1/0",
"java.lang.foreign.SegmentAllocator/allocateArray(java.lang.foreign.ValueLayout$OfByte,byte[])/1/0",
"java.lang.foreign.SegmentAllocator/allocateArray(java.lang.foreign.ValueLayout$OfShort,short[])/1/0",
"java.lang.foreign.SegmentAllocator/allocateArray(java.lang.foreign.ValueLayout$OfChar,char[])/1/0",
"java.lang.foreign.SegmentAllocator/allocateArray(java.lang.foreign.ValueLayout$OfInt,int[])/1/0",
"java.lang.foreign.SegmentAllocator/allocateArray(java.lang.foreign.ValueLayout$OfFloat,float[])/1/0",
"java.lang.foreign.SegmentAllocator/allocateArray(java.lang.foreign.ValueLayout$OfLong,long[])/1/0",
"java.lang.foreign.SegmentAllocator/allocateArray(java.lang.foreign.ValueLayout$OfDouble,double[])/1/0",
"java.lang.foreign.MemorySession/allocateArray(java.lang.foreign.ValueLayout$OfByte,byte[])/1/0",
"java.lang.foreign.MemorySession/allocateArray(java.lang.foreign.ValueLayout$OfShort,short[])/1/0",
"java.lang.foreign.MemorySession/allocateArray(java.lang.foreign.ValueLayout$OfChar,char[])/1/0",
"java.lang.foreign.MemorySession/allocateArray(java.lang.foreign.ValueLayout$OfInt,int[])/1/0",
"java.lang.foreign.MemorySession/allocateArray(java.lang.foreign.ValueLayout$OfFloat,float[])/1/0",
"java.lang.foreign.MemorySession/allocateArray(java.lang.foreign.ValueLayout$OfLong,long[])/1/0",
"java.lang.foreign.MemorySession/allocateArray(java.lang.foreign.ValueLayout$OfDouble,double[])/1/0"
);

static final Set<String> OBJECT_METHODS = Stream.of(Object.class.getMethods())
Expand Down
27 changes: 27 additions & 0 deletions test/jdk/java/foreign/TestSegmentAllocators.java
Expand Up @@ -43,6 +43,7 @@
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 java.util.stream.IntStream;
Expand Down Expand Up @@ -159,6 +160,32 @@ public void testBadAllocationAlignNotPowerTwo(SegmentAllocator allocator) {
allocator.allocate(1, 3);
}

@Test
public void testArrayAllocateDelegation() {
AtomicInteger calls = new AtomicInteger();
SegmentAllocator allocator = new SegmentAllocator() {
@Override
public MemorySegment allocate(long bytesSize, long bytesAlignment) {
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(dataProvider = "arrayAllocations")
public <Z> void testArray(AllocationFactory allocationFactory, ValueLayout layout, AllocationFunction<Object, ValueLayout> allocationFunction, ToArrayHelper<Z> arrayHelper) {
Z arr = arrayHelper.array();
Expand Down

1 comment on commit 2c8ada6

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.