mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 23:34:52 +02:00
8303582: Reduce duplication in jdk/java/foreign tests
Reviewed-by: mcimadamore
This commit is contained in:
parent
8e2014527e
commit
dccfe8a2ee
13 changed files with 230 additions and 364 deletions
|
@ -29,17 +29,22 @@ package jdk.internal.foreign;
|
|||
import java.lang.foreign.MemoryLayout;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.foreign.SegmentAllocator;
|
||||
import java.lang.foreign.StructLayout;
|
||||
import java.lang.foreign.ValueLayout;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
import jdk.internal.access.SharedSecrets;
|
||||
import jdk.internal.foreign.abi.SharedUtils;
|
||||
import jdk.internal.vm.annotation.ForceInline;
|
||||
import sun.invoke.util.Wrapper;
|
||||
|
||||
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
|
||||
|
||||
/**
|
||||
|
@ -199,4 +204,41 @@ public final class Utils {
|
|||
throw new IllegalArgumentException("Invalid alignment constraint : " + byteAlignment);
|
||||
}
|
||||
}
|
||||
|
||||
private static long computePadding(long offset, long align) {
|
||||
boolean isAligned = offset == 0 || offset % align == 0;
|
||||
if (isAligned) {
|
||||
return 0;
|
||||
} else {
|
||||
long gap = offset % align;
|
||||
return align - gap;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return return a struct layout constructed from the given elements, with padding
|
||||
* computed automatically so that they are naturally aligned}.
|
||||
*
|
||||
* @param elements the structs' fields
|
||||
*/
|
||||
public static StructLayout computePaddedStructLayout(MemoryLayout... elements) {
|
||||
long offset = 0L;
|
||||
List<MemoryLayout> layouts = new ArrayList<>();
|
||||
long align = 0;
|
||||
for (MemoryLayout l : elements) {
|
||||
long padding = computePadding(offset, l.bitAlignment());
|
||||
if (padding != 0) {
|
||||
layouts.add(MemoryLayout.paddingLayout(padding));
|
||||
offset += padding;
|
||||
}
|
||||
layouts.add(l);
|
||||
align = Math.max(align, l.bitAlignment());
|
||||
offset += l.bitSize();
|
||||
}
|
||||
long padding = computePadding(offset, align);
|
||||
if (padding != 0) {
|
||||
layouts.add(MemoryLayout.paddingLayout(padding));
|
||||
}
|
||||
return MemoryLayout.structLayout(layouts.toArray(MemoryLayout[]::new));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue