< prev index next > test/jdk/java/foreign/NativeTestHelper.java
Print this page
public static boolean isPointer(MemoryLayout layout) {
return layout instanceof ValueLayout valueLayout && valueLayout.carrier() == MemorySegment.class;
}
// the constants below are useful aliases for C types. The type/carrier association is only valid for 64-bit platforms.
/**
* The layout for the {@code bool} C type
*/
! public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
/**
* The layout for the {@code char} C type
*/
! public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;
/**
* The layout for the {@code short} C type
*/
! public static final ValueLayout.OfShort C_SHORT = ValueLayout.JAVA_SHORT;
/**
* The layout for the {@code int} C type
*/
! public static final ValueLayout.OfInt C_INT = ValueLayout.JAVA_INT;
/**
* The layout for the {@code long long} C type.
*/
! public static final ValueLayout.OfLong C_LONG_LONG = ValueLayout.JAVA_LONG;
/**
* The layout for the {@code float} C type
*/
! public static final ValueLayout.OfFloat C_FLOAT = ValueLayout.JAVA_FLOAT;
/**
* The layout for the {@code double} C type
*/
! public static final ValueLayout.OfDouble C_DOUBLE = ValueLayout.JAVA_DOUBLE;
/**
* The {@code T*} native type.
*/
! public static final AddressLayout C_POINTER = ValueLayout.ADDRESS
! .withTargetLayout(MemoryLayout.sequenceLayout(C_CHAR));
!
! public static final Linker LINKER = Linker.nativeLinker();
private static final MethodHandle FREE = LINKER.downcallHandle(
LINKER.defaultLookup().find("free").get(), FunctionDescriptor.ofVoid(C_POINTER));
private static final MethodHandle MALLOC = LINKER.downcallHandle(
public static boolean isPointer(MemoryLayout layout) {
return layout instanceof ValueLayout valueLayout && valueLayout.carrier() == MemorySegment.class;
}
+ public static final Linker LINKER = Linker.nativeLinker();
+
// the constants below are useful aliases for C types. The type/carrier association is only valid for 64-bit platforms.
/**
* The layout for the {@code bool} C type
*/
! public static final ValueLayout.OfBoolean C_BOOL = (ValueLayout.OfBoolean) LINKER.canonicalLayouts().get("bool");
/**
* The layout for the {@code char} C type
*/
! public static final ValueLayout.OfByte C_CHAR = (ValueLayout.OfByte) LINKER.canonicalLayouts().get("char");
/**
* The layout for the {@code short} C type
*/
! public static final ValueLayout.OfShort C_SHORT = (ValueLayout.OfShort) LINKER.canonicalLayouts().get("short");
/**
* The layout for the {@code int} C type
*/
! public static final ValueLayout.OfInt C_INT = (ValueLayout.OfInt) LINKER.canonicalLayouts().get("int");
/**
* The layout for the {@code long long} C type.
*/
! public static final ValueLayout.OfLong C_LONG_LONG = (ValueLayout.OfLong) LINKER.canonicalLayouts().get("long long");
/**
* The layout for the {@code float} C type
*/
! public static final ValueLayout.OfFloat C_FLOAT = (ValueLayout.OfFloat) LINKER.canonicalLayouts().get("float");
/**
* The layout for the {@code double} C type
*/
! public static final ValueLayout.OfDouble C_DOUBLE = (ValueLayout.OfDouble) LINKER.canonicalLayouts().get("double");
/**
* The {@code T*} native type.
*/
! public static final AddressLayout C_POINTER = ((AddressLayout) LINKER.canonicalLayouts().get("void*"))
! .withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, C_CHAR));
! /**
! * The layout for the {@code size_t} C type
+ */
+ public static final ValueLayout C_SIZE_T = (ValueLayout) LINKER.canonicalLayouts().get("size_t");
+
+ // Common layout shared by some tests
+ // struct S_PDI { void* p0; double p1; int p2; };
+ public static final MemoryLayout S_PDI_LAYOUT = switch ((int) ValueLayout.ADDRESS.byteSize()) {
+ case 8 -> MemoryLayout.structLayout(
+ C_POINTER.withName("p0"),
+ C_DOUBLE.withName("p1"),
+ C_INT.withName("p2"),
+ MemoryLayout.paddingLayout(4));
+ case 4 -> MemoryLayout.structLayout(
+ C_POINTER.withName("p0"),
+ C_DOUBLE.withName("p1"),
+ C_INT.withName("p2"));
+ default -> throw new UnsupportedOperationException("Unsupported address size");
+ };
private static final MethodHandle FREE = LINKER.downcallHandle(
LINKER.defaultLookup().find("free").get(), FunctionDescriptor.ofVoid(C_POINTER));
private static final MethodHandle MALLOC = LINKER.downcallHandle(
slice.copyFrom((MemorySegment) fieldValue.value());
return actual -> fieldCheck.accept(slicer.apply((MemorySegment) actual));
} else {
VarHandle accessor = containerLayout.varHandle(fieldPath);
//set value
! accessor.set(container, fieldValue.value());
! return actual -> fieldCheck.accept(accessor.get((MemorySegment) actual));
}
}
private static UnaryOperator<MemorySegment> slicer(MemoryLayout containerLayout, MemoryLayout.PathElement fieldPath) {
MethodHandle slicer = containerLayout.sliceHandle(fieldPath);
return container -> {
try {
! return (MemorySegment) slicer.invokeExact(container);
} catch (Throwable e) {
throw new IllegalStateException(e);
}
};
}
slice.copyFrom((MemorySegment) fieldValue.value());
return actual -> fieldCheck.accept(slicer.apply((MemorySegment) actual));
} else {
VarHandle accessor = containerLayout.varHandle(fieldPath);
//set value
! accessor.set(container, 0L, fieldValue.value());
! return actual -> fieldCheck.accept(accessor.get((MemorySegment) actual, 0L));
}
}
private static UnaryOperator<MemorySegment> slicer(MemoryLayout containerLayout, MemoryLayout.PathElement fieldPath) {
MethodHandle slicer = containerLayout.sliceHandle(fieldPath);
return container -> {
try {
! return (MemorySegment) slicer.invokeExact(container, 0L);
} catch (Throwable e) {
throw new IllegalStateException(e);
}
};
}
< prev index next >