Skip to content

Commit

Permalink
[Core] Add PlatformLayouts
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Aug 28, 2024
1 parent 766007b commit 1f2b935
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,7 +45,7 @@ public interface GLFWAllocateFun extends Upcall {
* The type of the upcall.
*/
Type<GLFWAllocateFun> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,7 +45,7 @@ public interface GLFWReallocateFun extends Upcall {
* The type of the upcall.
*/
Type<GLFWReallocateFun> 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
Expand Down
4 changes: 2 additions & 2 deletions modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -32,7 +32,7 @@ public interface NFDOpenDialogNArgs extends Struct<NFDOpenDialogNArgs> {
StructAllocator<NFDOpenDialogNArgs> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -32,7 +32,7 @@ public interface NFDOpenDialogU8Args extends Struct<NFDOpenDialogU8Args> {
StructAllocator<NFDOpenDialogU8Args> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -32,7 +32,7 @@ public interface NFDSaveDialogNArgs extends Struct<NFDSaveDialogNArgs> {
StructAllocator<NFDSaveDialogNArgs> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -32,7 +32,7 @@ public interface NFDSaveDialogU8Args extends Struct<NFDSaveDialogU8Args> {
StructAllocator<NFDSaveDialogU8Args> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +39,7 @@ public interface NFDWindowHandle extends Struct<NFDWindowHandle> {
*/
StructAllocator<NFDWindowHandle> OF = new StructAllocator<>(MethodHandles.lookup(),
LayoutBuilder.struct()
.add(RuntimeHelper.SIZE_T, "type")
.add(PlatformLayouts.SIZE_T, "type")
.cAddress("handle")
.build());

Expand Down

0 comments on commit 1f2b935

Please sign in to comment.