From 1f2b935073f9408b5a9faf3d9d4c6dafd3e94e99 Mon Sep 17 00:00:00 2001 From: squid233 <60126026+squid233@users.noreply.github.com> Date: Wed, 28 Aug 2024 17:36:22 +0800 Subject: [PATCH] [Core] Add PlatformLayouts --- .../main/java/overrungl/util/MemoryUtil.java | 2 +- .../java/overrungl/util/PlatformLayouts.java | 45 +++++++++++++++++++ .../java/overrungl/glfw/GLFWAllocateFun.java | 3 +- .../overrungl/glfw/GLFWReallocateFun.java | 3 +- .../src/main/java/overrungl/nfd/NFD.java | 4 +- .../overrungl/nfd/NFDOpenDialogNArgs.java | 4 +- .../overrungl/nfd/NFDOpenDialogU8Args.java | 4 +- .../overrungl/nfd/NFDSaveDialogNArgs.java | 4 +- .../overrungl/nfd/NFDSaveDialogU8Args.java | 4 +- .../java/overrungl/nfd/NFDWindowHandle.java | 4 +- 10 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 modules/overrungl.core/src/main/java/overrungl/util/PlatformLayouts.java diff --git a/modules/overrungl.core/src/main/java/overrungl/util/MemoryUtil.java b/modules/overrungl.core/src/main/java/overrungl/util/MemoryUtil.java index 2d823de7..88cbb6cd 100644 --- a/modules/overrungl.core/src/main/java/overrungl/util/MemoryUtil.java +++ b/modules/overrungl.core/src/main/java/overrungl/util/MemoryUtil.java @@ -25,8 +25,8 @@ import static java.lang.foreign.FunctionDescriptor.of; import static java.lang.foreign.ValueLayout.ADDRESS; -import static overrungl.internal.RuntimeHelper.SIZE_T; import static overrungl.internal.RuntimeHelper.SIZE_T_LONG; +import static overrungl.util.PlatformLayouts.SIZE_T; /** * The standard-C memory allocator. diff --git a/modules/overrungl.core/src/main/java/overrungl/util/PlatformLayouts.java b/modules/overrungl.core/src/main/java/overrungl/util/PlatformLayouts.java new file mode 100644 index 00000000..746e2c73 --- /dev/null +++ b/modules/overrungl.core/src/main/java/overrungl/util/PlatformLayouts.java @@ -0,0 +1,45 @@ +/* + * 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 overrungl.util; + +import overrungl.internal.RuntimeHelper; + +import java.lang.foreign.MemoryLayout; + +/** + * Platform-specific value layouts. + * + * @author squid233 + * @since 0.1.0 + */ +public final class PlatformLayouts { + /** + * {@code long} type in C + */ + public static final MemoryLayout LONG = RuntimeHelper.LONG; + /** + * {@code size_t} type in C + */ + public static final MemoryLayout SIZE_T = RuntimeHelper.SIZE_T; + /** + * {@code wchar_t} type in C + */ + public static final MemoryLayout WCHAR_T = RuntimeHelper.WCHAR_T; + + private PlatformLayouts() { + } +} diff --git a/modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFWAllocateFun.java b/modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFWAllocateFun.java index 59ff1257..84870eaf 100644 --- a/modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFWAllocateFun.java +++ b/modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFWAllocateFun.java @@ -19,6 +19,7 @@ import overrun.marshal.Upcall; import overrungl.internal.RuntimeHelper; import overrungl.util.MemoryUtil; +import overrungl.util.PlatformLayouts; import java.lang.foreign.Arena; import java.lang.foreign.FunctionDescriptor; @@ -44,7 +45,7 @@ public interface GLFWAllocateFun extends Upcall { * The type of the upcall. */ Type TYPE = Upcall.type(RuntimeHelper.SIZE_T_LONG ? "invoke" : "invoke_int", - FunctionDescriptor.of(ValueLayout.ADDRESS, RuntimeHelper.SIZE_T, ValueLayout.ADDRESS)); + FunctionDescriptor.of(ValueLayout.ADDRESS, PlatformLayouts.SIZE_T, ValueLayout.ADDRESS)); /** * This function must return either a memory block at least {@code size} bytes long, diff --git a/modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFWReallocateFun.java b/modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFWReallocateFun.java index c89108e5..46410bb1 100644 --- a/modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFWReallocateFun.java +++ b/modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFWReallocateFun.java @@ -19,6 +19,7 @@ import overrun.marshal.Upcall; import overrungl.internal.RuntimeHelper; import overrungl.util.MemoryUtil; +import overrungl.util.PlatformLayouts; import java.lang.foreign.Arena; import java.lang.foreign.FunctionDescriptor; @@ -44,7 +45,7 @@ public interface GLFWReallocateFun extends Upcall { * The type of the upcall. */ Type TYPE = Upcall.type(RuntimeHelper.SIZE_T_LONG ? "invoke" : "invoke_int", - FunctionDescriptor.of(ValueLayout.ADDRESS, RuntimeHelper.SIZE_T, ValueLayout.ADDRESS)); + FunctionDescriptor.of(ValueLayout.ADDRESS, PlatformLayouts.SIZE_T, ValueLayout.ADDRESS)); /** * This function must return a memory block at least {@code size} bytes long, or diff --git a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java index 34ddf5c2..d78475f3 100644 --- a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java +++ b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java @@ -24,7 +24,7 @@ import overrun.marshal.gen.SizedSeg; import overrun.marshal.gen.Skip; import overrungl.NativeType; -import overrungl.internal.RuntimeHelper; +import overrungl.util.PlatformLayouts; import overrungl.util.value.Tuple2; import java.lang.foreign.MemorySegment; @@ -149,7 +149,7 @@ public interface NFD extends DirectAccess { * The type of the path-set size ({@code unsigned long} for Windows and Mac OS X, * {@code unsigned int} for others). */ - ValueLayout PATH_SET_SIZE = NFDInternal.isOsWinOrApple ? (ValueLayout) RuntimeHelper.LONG : JAVA_INT; + ValueLayout PATH_SET_SIZE = NFDInternal.isOsWinOrApple ? (ValueLayout) PlatformLayouts.LONG : JAVA_INT; /** * The instance of NFD. */ diff --git a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDOpenDialogNArgs.java b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDOpenDialogNArgs.java index 7f15cf65..7c79c512 100644 --- a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDOpenDialogNArgs.java +++ b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDOpenDialogNArgs.java @@ -20,7 +20,7 @@ import overrun.marshal.Unmarshal; import overrun.marshal.struct.Struct; import overrun.marshal.struct.StructAllocator; -import overrungl.internal.RuntimeHelper; +import overrungl.util.PlatformLayouts; /** * {@code NFDOpenDialogNArgs} @@ -32,7 +32,7 @@ public interface NFDOpenDialogNArgs extends Struct { StructAllocator OF = new StructAllocator<>(java.lang.invoke.MethodHandles.lookup(), LayoutBuilder.struct() .cAddress("filterList") - .add(RuntimeHelper.SIZE_T, "filterCount") + .add(PlatformLayouts.SIZE_T, "filterCount") .add(Unmarshal.STR_LAYOUT, "defaultPath") .cAddress("parentWindow", NFDWindowHandle.OF.layout()) .build() diff --git a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDOpenDialogU8Args.java b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDOpenDialogU8Args.java index 9f4d39d1..989b59df 100644 --- a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDOpenDialogU8Args.java +++ b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDOpenDialogU8Args.java @@ -20,7 +20,7 @@ import overrun.marshal.Unmarshal; import overrun.marshal.struct.Struct; import overrun.marshal.struct.StructAllocator; -import overrungl.internal.RuntimeHelper; +import overrungl.util.PlatformLayouts; /** * {@code NFDOpenDialogU8Args} @@ -32,7 +32,7 @@ public interface NFDOpenDialogU8Args extends Struct { StructAllocator OF = new StructAllocator<>(java.lang.invoke.MethodHandles.lookup(), LayoutBuilder.struct() .cAddress("filterList") - .add(RuntimeHelper.SIZE_T, "filterCount") + .add(PlatformLayouts.SIZE_T, "filterCount") .add(Unmarshal.STR_LAYOUT, "defaultPath") .cAddress("parentWindow", NFDWindowHandle.OF.layout()) .build() diff --git a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDSaveDialogNArgs.java b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDSaveDialogNArgs.java index 537aa477..f9c93a08 100644 --- a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDSaveDialogNArgs.java +++ b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDSaveDialogNArgs.java @@ -20,7 +20,7 @@ import overrun.marshal.Unmarshal; import overrun.marshal.struct.Struct; import overrun.marshal.struct.StructAllocator; -import overrungl.internal.RuntimeHelper; +import overrungl.util.PlatformLayouts; /** * {@code NFDSaveDialogNArgs} @@ -32,7 +32,7 @@ public interface NFDSaveDialogNArgs extends Struct { StructAllocator OF = new StructAllocator<>(java.lang.invoke.MethodHandles.lookup(), LayoutBuilder.struct() .cAddress("filterList") - .add(RuntimeHelper.SIZE_T, "filterCount") + .add(PlatformLayouts.SIZE_T, "filterCount") .add(Unmarshal.STR_LAYOUT, "defaultPath") .add(Unmarshal.STR_LAYOUT, "defaultName") .cAddress("parentWindow", NFDWindowHandle.OF.layout()) diff --git a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDSaveDialogU8Args.java b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDSaveDialogU8Args.java index 1ba684d1..872e9357 100644 --- a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDSaveDialogU8Args.java +++ b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDSaveDialogU8Args.java @@ -20,7 +20,7 @@ import overrun.marshal.Unmarshal; import overrun.marshal.struct.Struct; import overrun.marshal.struct.StructAllocator; -import overrungl.internal.RuntimeHelper; +import overrungl.util.PlatformLayouts; /** * {@code NFDSaveDialogU8Args} @@ -32,7 +32,7 @@ public interface NFDSaveDialogU8Args extends Struct { StructAllocator OF = new StructAllocator<>(java.lang.invoke.MethodHandles.lookup(), LayoutBuilder.struct() .cAddress("filterList") - .add(RuntimeHelper.SIZE_T, "filterCount") + .add(PlatformLayouts.SIZE_T, "filterCount") .add(Unmarshal.STR_LAYOUT, "defaultPath") .add(Unmarshal.STR_LAYOUT, "defaultName") .cAddress("parentWindow", NFDWindowHandle.OF.layout()) diff --git a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDWindowHandle.java b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDWindowHandle.java index 4206a7c9..89bb5ca0 100644 --- a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDWindowHandle.java +++ b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDWindowHandle.java @@ -19,7 +19,7 @@ import overrun.marshal.LayoutBuilder; import overrun.marshal.struct.Struct; import overrun.marshal.struct.StructAllocator; -import overrungl.internal.RuntimeHelper; +import overrungl.util.PlatformLayouts; import java.lang.foreign.MemorySegment; import java.lang.invoke.MethodHandles; @@ -39,7 +39,7 @@ public interface NFDWindowHandle extends Struct { */ StructAllocator OF = new StructAllocator<>(MethodHandles.lookup(), LayoutBuilder.struct() - .add(RuntimeHelper.SIZE_T, "type") + .add(PlatformLayouts.SIZE_T, "type") .cAddress("handle") .build());