From 5b051535844d6d8dbc2f6aad791e90d71f20496c Mon Sep 17 00:00:00 2001 From: squid233 <60126026+squid233@users.noreply.github.com> Date: Sun, 6 Oct 2024 21:15:27 +0800 Subject: [PATCH] Add CanonicalLayouts --- .github/workflows/gradle.yml | 2 +- .github/workflows/javadoc.yml | 2 +- README.md | 2 +- build.gradle.kts | 3 +- gradle.properties | 4 +- .../overrun/marshal/CanonicalLayouts.java | 54 +++++++++++++++++++ src/main/java/overrun/marshal/Unmarshal.java | 2 +- 7 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 src/main/java/overrun/marshal/CanonicalLayouts.java diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 029ec75..5fa5215 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -19,7 +19,7 @@ jobs: strategy: matrix: java: [ - 23-ea + 23 ] os: [ ubuntu-latest, windows-latest ] runs-on: ${{ matrix.os }} diff --git a/.github/workflows/javadoc.yml b/.github/workflows/javadoc.yml index 5090a4c..f73845e 100644 --- a/.github/workflows/javadoc.yml +++ b/.github/workflows/javadoc.yml @@ -25,7 +25,7 @@ jobs: uses: actions/setup-java@v4 with: java-version: | - 23-ea + 23 distribution: 'temurin' - name: Grant execute permission for gradlew if: ${{ runner.os != 'Windows' }} diff --git a/README.md b/README.md index a71f0d2..b1aaf37 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Import as a Gradle dependency: ```groovy dependencies { - implementation("io.github.over-run:marshal:0.1.0-alpha.34-jdk23") + implementation("io.github.over-run:marshal:0.1.0-alpha.35-jdk23") } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 2c2abbd..c6cb2eb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -91,7 +91,7 @@ allprojects { dependencies { // add your dependencies - compileOnly("org.jetbrains:annotations:24.1.0") + compileOnly("org.jetbrains:annotations:25.0.0") testImplementation("org.junit.jupiter:junit-jupiter:$junitVersion") testImplementation("io.github.over-run:platform:$platformVersion") api("io.github.over-run:memstack:$memstackVersion") @@ -172,6 +172,7 @@ tasks.withType { } else { links("https://download.java.net/java/early_access/$jdkEarlyAccessDoc/docs/api/") } + links("https://over-run.github.io/memstack/") if (jdkEnablePreview.toBoolean()) { addBooleanOption("-enable-preview", true) addStringOption("source", jdkVersion) diff --git a/gradle.properties b/gradle.properties index 34b2a21..e883a71 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ projGroupId=io.github.over-run projArtifactId=marshal # The project name should only contain lowercase letters, numbers and hyphen. projName=marshal -projVersion=0.1.0-alpha.34-jdk23 +projVersion=0.1.0-alpha.35-jdk23 projDesc=Marshaler of native libraries # Uncomment them if you want to publish to maven repository. projUrl=https://github.com/Over-Run/marshal @@ -32,7 +32,7 @@ jdkEnablePreview=true # javadoc link of JDK early access build # https://download.java.net/java/early_access/$jdkEarlyAccessDoc/docs/api/ # Uncomment it if you need to use EA build of JDK. -jdkEarlyAccessDoc=jdk23 +#jdkEarlyAccessDoc=jdk23 junitVersion=5.11.0 memstackVersion=0.3.0 diff --git a/src/main/java/overrun/marshal/CanonicalLayouts.java b/src/main/java/overrun/marshal/CanonicalLayouts.java new file mode 100644 index 0000000..ffe57b6 --- /dev/null +++ b/src/main/java/overrun/marshal/CanonicalLayouts.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2024 Overrun Organization + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + */ + +package overrun.marshal; + +import java.lang.foreign.Linker; +import java.lang.foreign.MemoryLayout; +import java.util.Map; + +/** + * Common C memory layouts obtained with {@link Linker#canonicalLayouts()}. + * + * @author squid233 + * @since 0.1.0 + */ +public final class CanonicalLayouts { + private static final Map canonicalLayouts = Linker.nativeLinker().canonicalLayouts(); + /// Specified canonical layouts + public static final MemoryLayout BOOL = get("bool"), + CHAR = get("char"), + SHORT = get("short"), + INT = get("int"), + FLOAT = get("float"), + LONG = get("long"), + LONG_LONG = get("long long"), + DOUBLE = get("double"), + VOID_POINTER = get("void*"), + SIZE_T = get("size_t"), + WCHAR_T = get("wchar_t"); + + private CanonicalLayouts() { + } + + /// Gets the layout from {@link Linker#canonicalLayouts()} with the given name. + /// + /// @param name the name of the layout + /// @return the layout + public static MemoryLayout get(String name) { + return canonicalLayouts.get(name); + } +} diff --git a/src/main/java/overrun/marshal/Unmarshal.java b/src/main/java/overrun/marshal/Unmarshal.java index 4daaeba..31d57db 100644 --- a/src/main/java/overrun/marshal/Unmarshal.java +++ b/src/main/java/overrun/marshal/Unmarshal.java @@ -57,7 +57,7 @@ private Unmarshal() { * @param segment the native segment */ public static boolean isNullPointer(@Nullable MemorySegment segment) { - return segment == null || segment == MemorySegment.NULL || segment.address() == 0L; + return segment == null || segment.equals(MemorySegment.NULL); } /**