Skip to content

Commit

Permalink
8289570: SegmentAllocator:allocateUtf8String(String str) default beha…
Browse files Browse the repository at this point in the history
…vior mismatch to spec

Reviewed-by: alanb, psandoz
  • Loading branch information
mcimadamore committed Jul 1, 2022
1 parent 20124ac commit 8e01ffb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Expand Up @@ -146,7 +146,7 @@ public static void copy(MemorySegment addr, byte[] bytes) {
}

public static MemorySegment toCString(byte[] bytes, SegmentAllocator allocator) {
MemorySegment addr = allocator.allocate(bytes.length + 1, 1L);
MemorySegment addr = allocator.allocate(bytes.length + 1);
copy(addr, bytes);
return addr;
}
Expand Down
19 changes: 19 additions & 0 deletions test/jdk/java/foreign/TestSegmentAllocators.java
Expand Up @@ -185,6 +185,25 @@ public MemorySegment allocateArray(MemoryLayout elementLayout, long count) {
assertEquals(calls.get(), 7);
}

@Test
public void testStringAllocateDelegation() {
AtomicInteger calls = new AtomicInteger();
SegmentAllocator allocator = new SegmentAllocator() {
@Override
public MemorySegment allocate(long bytesSize, long bytesAlignment) {
return MemorySegment.allocateNative(bytesSize, bytesAlignment, MemorySession.openImplicit());
}

@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) {
Expand Down

0 comments on commit 8e01ffb

Please sign in to comment.