Skip to content

Commit

Permalink
Migrated things to JDK 21
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMosigItemis committed Oct 13, 2023
1 parent 7aa1df5 commit e1d2b25
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
with:
distribution: 'temurin'
java-package: 'jdk'
java-version: '20'
java-version: '21'
check-latest: true
server-id: 'ossrh'
server-username: MAVEN_USERNAME
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/snapshot-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
distribution: 'temurin'
java-package: 'jdk'
java-version: '20'
java-version: '21'
check-latest: true
cache: 'maven'
- name: Build & Test
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/itemis/fluffyj/memory/FluffyMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public static FluffyMemoryDereferencer dereference(final MemorySegment nativePtr
* @return - Entry point for easy dereferenciation.
*/
public static FluffyMemoryDereferencer dereference(final long address) {
final var nativeSeg = MemorySegment.ofAddress(address);
return dereference(nativeSeg);
return dereference(MemorySegment.ofAddress(address));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public BinderStage andNoReturnType() {

@Override
public MemorySegment bindTo(final Arena arena) {
requireNonNull(arena, "scope");
requireNonNull(arena, "arena");

if (extractMethodCandidates().stream().anyMatch(Method::isSynthetic)) {
throwCannotBindSynthMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.itemis.fluffyj.memory.internal.StringSegment;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;

/**
* Helps with wrapping "untyped" off heap memory areas ("segments") into {@link FluffySegment}s.
Expand Down Expand Up @@ -61,7 +60,7 @@ public <T> FluffyScalarSegment<T> as(final Class<? extends T> type) {
}

public <T> FluffyMemoryScalarPointerAllocator<T> asPointerOf(final Class<? extends T> type) {
return new FluffyMemoryTypedPointerBuilder(nativeSegment.get(ValueLayout.ADDRESS, 0).address()).as(type);
return new FluffyMemoryTypedPointerBuilder(nativeSegment.address()).as(type);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public Byte[] dereference() {

@Override
public MemorySegment rawDereference() {
return addressSeg.reinterpret(byteSize, arena, null);
return MemorySegment.ofAddress(getRawValue()).reinterpret(byteSize, arena, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public Integer dereference() {

@Override
public MemorySegment rawDereference() {
return addressSeg.reinterpret(JAVA_INT.byteSize(), arena, null);
return MemorySegment.ofAddress(getRawValue()).reinterpret(JAVA_INT.byteSize(), arena, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public Long dereference() {

@Override
public MemorySegment rawDereference() {
return addressSeg.reinterpret(JAVA_LONG.byteSize(), arena, null);
return MemorySegment.ofAddress(getRawValue()).reinterpret(JAVA_LONG.byteSize(), arena, null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.itemis.fluffyj.memory.internal;

import static java.lang.foreign.ValueLayout.ADDRESS;
import static java.lang.foreign.ValueLayout.JAVA_LONG;
import static java.util.Objects.requireNonNull;

Expand All @@ -23,8 +22,7 @@ public class PointerOfString implements FluffyScalarPointer<String> {
public PointerOfString(final long addressPointedTo, final Arena arena) {
this.arena = requireNonNull(arena, "arena");

backingSeg = arena.allocate(JAVA_LONG);
backingSeg.set(JAVA_LONG, 0, addressPointedTo);
backingSeg = arena.allocate(JAVA_LONG, addressPointedTo);
}

@Override
Expand All @@ -49,7 +47,7 @@ public long getRawValue() {

@Override
public MemorySegment getValue() {
return backingSeg.reinterpret(JAVA_LONG.byteSize(), arena, null);
return MemorySegment.ofAddress(getRawValue());
}

@Override
Expand All @@ -59,6 +57,6 @@ public String dereference() {

@Override
public MemorySegment rawDereference() {
return backingSeg.get(ADDRESS, 0);
return MemorySegment.ofAddress(getRawValue()).reinterpret(Long.MAX_VALUE, arena, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final long getRawValue() {

@Override
public MemorySegment getValue() {
return addressSeg;
return MemorySegment.ofAddress(getRawValue());
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/itemis/fluffyj/memory/ClassBasicsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ void constructor_with_null_arg_yields_npe() {
assertNullArgNotAccepted(() -> new FluffyMemoryScalarSegmentAllocator<>(null), "initialValue");
assertNullArgNotAccepted(() -> new FluffyMemoryVectorSegmentAllocator<>(null), "initialValue");
assertNullArgNotAccepted(() -> new FluffyMemorySegmentWrapper(null), "nativeSegment");
assertNullArgNotAccepted(() -> new PointerOfInt(NULL.address(), null) {}, "scope");
assertNullArgNotAccepted(() -> new PointerOfLong(NULL.address(), null) {}, "scope");
assertNullArgNotAccepted(() -> new PointerOfBlob(NULL.address(), A_LONG, null) {}, "scope");
assertNullArgNotAccepted(() -> new PointerOfString(NULL.address(), null) {}, "scope");
assertNullArgNotAccepted(() -> new PointerOfInt(NULL.address(), null) {}, "arena");
assertNullArgNotAccepted(() -> new PointerOfLong(NULL.address(), null) {}, "arena");
assertNullArgNotAccepted(() -> new PointerOfBlob(NULL.address(), A_LONG, null) {}, "arena");
assertNullArgNotAccepted(() -> new PointerOfString(NULL.address(), null) {}, "arena");
assertNullArgNotAccepted(() -> new PointerOfByte(NULL.address(), null) {}, "arena");
assertNullArgNotAccepted(() -> new FluffyMemoryDereferencer(null, Arena.global()), "wrapper");
assertNullArgNotAccepted(() -> new FluffyMemoryDereferencer(wrap(NULL), null), "nativePtr");
assertNullArgNotAccepted(() -> new FluffyMemoryDereferencer(wrap(NULL), null), "arena");
}
}
29 changes: 17 additions & 12 deletions src/test/java/com/itemis/fluffyj/memory/PointerBasicsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.jupiter.params.provider.MethodSource;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.util.stream.Stream;

class PointerBasicsTest extends MemoryScopeEnabledTest {
Expand Down Expand Up @@ -78,33 +79,37 @@ void can_create_scoped_empty_pointer() {
}

@Test
void empty_pointer_creation_does_not_accept_null_scope() {
assertNullArgNotAccepted(() -> pointer().allocate(null), "scope");
void empty_pointer_creation_does_not_accept_null_arena() {
assertNullArgNotAccepted(() -> pointer().allocate(null), "arena");
}

@ParameterizedTest
@MethodSource("typeMappings")
void dereference_should_support_known_types(final MemorySegment dataSeg, final Class<?> inputType,
void dereference_should_support_known_types(final MemorySegment ptrSeg, final Class<?> inputType,
final Class<?> expectedOutputType) {
final var actualOutputType = FluffyMemory.dereference(dataSeg).as(inputType);
final var dereferencedValue = FluffyMemory.dereference(ptrSeg).as(inputType);

assertThat(actualOutputType.getClass()).isEqualTo(expectedOutputType);
assertThat(dereferencedValue.getClass()).isEqualTo(expectedOutputType);
}

private static Stream<Arguments> typeMappings() {
final var longSeg = global().allocate(JAVA_LONG, 123L);
final var longSegPtr = global().allocate(ValueLayout.JAVA_LONG, longSeg.address());
final var intSeg = global().allocate(JAVA_INT, 123);
final var intSegPtr = global().allocate(ValueLayout.JAVA_LONG, intSeg.address());
final var byteSeg = global().allocate(JAVA_BYTE, (byte) 123);
final var byteSegPtr = global().allocate(ValueLayout.JAVA_LONG, byteSeg.address());
final var stringSeg = global().allocateUtf8String("123");
final var stringSegPtr = global().allocate(ValueLayout.JAVA_LONG, stringSeg.address());

return Stream.of(
Arguments.of(longSeg, long.class, Long.class),
Arguments.of(longSeg, Long.class, Long.class),
Arguments.of(intSeg, int.class, Integer.class),
Arguments.of(intSeg, Integer.class, Integer.class),
Arguments.of(byteSeg, byte.class, Byte.class),
Arguments.of(byteSeg, Byte.class, Byte.class),
Arguments.of(stringSeg, String.class, String.class));
Arguments.of(longSegPtr, long.class, Long.class),
Arguments.of(longSegPtr, Long.class, Long.class),
Arguments.of(intSegPtr, int.class, Integer.class),
Arguments.of(intSegPtr, Integer.class, Integer.class),
Arguments.of(byteSegPtr, byte.class, Byte.class),
Arguments.of(byteSegPtr, Byte.class, Byte.class),
Arguments.of(stringSegPtr, String.class, String.class));
}

private static final class MyType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ void rawDereference_returns_segment_pointed_to() {
final var expectedVal = new Random().nextInt();
final var nativeSeg = arena.allocate(ValueLayout.JAVA_INT, expectedVal);
final var underTestAsFfmSeg =
underTest.getValue();
underTest.address().reinterpret(ValueLayout.JAVA_LONG.byteSize(), arena, null);
underTestAsFfmSeg.set(ValueLayout.JAVA_LONG, 0, nativeSeg.address());
underTestAsFfmSeg.get(ValueLayout.JAVA_LONG, 0);

final var result = underTest.rawDereference();

assertThat(result.get(ValueLayout.JAVA_INT, 0)).isEqualTo(expectedVal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,10 @@ void rawDereference_returns_seg_of_data_pointed_to() {
@Test
void wrapped_pointer_works() {
final var nativeSeg = allocateNativeSeg(firstTestValue.rawValue());
// NativeSeg muss einen Wert enthalten, der mindestens Adresslänge hat, also mindestens ein
// long.
final var result = wrap(nativeSeg).asPointerOf(testValueType).allocate(arena);
final var result =
wrap(MemorySegment.ofAddress(nativeSeg.address())).asPointerOf(testValueType).allocate(arena);
assertThat(result).isNotNull();
assertThat(result.getRawValue()).isEqualTo(firstTestValue.typedValue());
assertThat(result.getRawValue()).isEqualTo(nativeSeg.address());
}

@Test
Expand Down

0 comments on commit e1d2b25

Please sign in to comment.