Skip to content

Commit d9e2876

Browse files
authored
Merge pull request #45 from Over-Run/cleanup
Cleanup code and fix bugs
2 parents 3c93e5e + 0cec23a commit d9e2876

File tree

13 files changed

+53
-210
lines changed

13 files changed

+53
-210
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ jdkEnablePreview=true
1818
jdkEarlyAccessDoc=jdk22
1919
kotlinTargetJdkVersion=21
2020

21-
overrunMarshalVersion=0.1.0-alpha.13-jdk22
21+
overrunMarshalVersion=0.1.0-alpha.15-jdk22
2222
overrunPlatformVersion=1.0.0

modules/overrungl.core/src/main/java/overrungl/util/CheckUtil.java

Lines changed: 0 additions & 110 deletions
This file was deleted.

modules/overrungl.core/src/main/java/overrungl/util/MemoryUtil.java

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
package overrungl.util;
1818

1919
import org.jetbrains.annotations.Nullable;
20+
import overrun.marshal.Unmarshal;
2021
import overrungl.Configurations;
21-
import overrungl.internal.RuntimeHelper;
2222

2323
import java.lang.foreign.*;
2424
import java.lang.invoke.MethodHandle;
2525
import java.util.Objects;
2626

2727
import static java.lang.foreign.FunctionDescriptor.of;
2828
import static java.lang.foreign.ValueLayout.ADDRESS;
29+
import static overrungl.internal.RuntimeHelper.SIZE_T;
2930

3031
/**
3132
* The standard-C memory allocator.
@@ -37,14 +38,13 @@ public final class MemoryUtil {
3738
private static final Linker LINKER = Linker.nativeLinker();
3839
private static final SymbolLookup LOOKUP = LINKER.defaultLookup();
3940
private static final MethodHandle
40-
m_malloc = downcall("malloc", of(ADDRESS, RuntimeHelper.SIZE_T)),
41-
m_calloc = downcall("calloc", of(ADDRESS, RuntimeHelper.SIZE_T, RuntimeHelper.SIZE_T)),
42-
m_realloc = downcall("realloc", of(ADDRESS, ADDRESS, RuntimeHelper.SIZE_T)),
41+
m_malloc = downcall("malloc", of(ADDRESS, SIZE_T)),
42+
m_calloc = downcall("calloc", of(ADDRESS, SIZE_T, SIZE_T)),
43+
m_realloc = downcall("realloc", of(ADDRESS, ADDRESS, SIZE_T)),
4344
m_free = downcall("free", FunctionDescriptor.ofVoid(ADDRESS)),
44-
m_memcpy = downcall("memcpy", of(ADDRESS, ADDRESS, ADDRESS, RuntimeHelper.SIZE_T)),
45-
m_memmove = downcall("memmove", of(ADDRESS, ADDRESS, ADDRESS, RuntimeHelper.SIZE_T)),
46-
m_memset = downcall("memset", of(ADDRESS, ADDRESS, ValueLayout.JAVA_INT, RuntimeHelper.SIZE_T)),
47-
strlen = downcall("strlen", of(RuntimeHelper.SIZE_T, ADDRESS));
45+
m_memcpy = downcall("memcpy", of(ADDRESS, ADDRESS, ADDRESS, SIZE_T)),
46+
m_memmove = downcall("memmove", of(ADDRESS, ADDRESS, ADDRESS, SIZE_T)),
47+
m_memset = downcall("memset", of(ADDRESS, ADDRESS, ValueLayout.JAVA_INT, SIZE_T));
4848
private static final boolean DEBUG = Configurations.DEBUG_MEM_UTIL.get();
4949
/**
5050
* The address of {@code NULL}.
@@ -65,7 +65,7 @@ private MemoryUtil() {
6565
* @param segment the segment.
6666
*/
6767
public static boolean isNullptr(@Nullable MemorySegment segment) {
68-
return segment == null || segment.equals(MemorySegment.NULL);
68+
return Unmarshal.isNullPointer(segment);
6969
}
7070

7171
/**
@@ -325,25 +325,6 @@ public static MemorySegment memset(MemorySegment dest, int c, long count) {
325325
}
326326
}
327327

328-
/**
329-
* Gets the length of a string, by using the current locale or a specified locale.
330-
* <p>
331-
* {@code strlen} interprets the string as a single-byte character string,
332-
* so its return value is always equal to the number of bytes,
333-
* even if the string contains multibyte characters.
334-
*
335-
* @param str Null-terminated string.
336-
* @return the number of characters in <i>{@code str}</i>, excluding the terminal null.
337-
* No return value is reserved to indicate an error.
338-
*/
339-
public static long strlen(MemorySegment str) {
340-
try {
341-
return (long) strlen.invokeExact(str);
342-
} catch (Throwable e) {
343-
throw new AssertionError("should not reach here", e);
344-
}
345-
}
346-
347328
/**
348329
* Creates a segment allocator with the given arena.
349330
*

modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
* var filterItem = NFDNFilterItem.create(allocator,
8585
* new Pair<>("Source code", "java"),
8686
* new Pair<>("Image file", "png,jpg"));
87-
* }
87+
*}
8888
* <p>
8989
* A file filter is a pair of strings comprising the friendly name and the specification
9090
* (multiple file extensions are comma-separated).
@@ -573,7 +573,7 @@ default NFDResult pathSetEnumNextN(@NativeType("nfdpathsetenum_t*") MemorySegmen
573573
if (result == NFDResult.OKAY) {
574574
final MemorySegment path = seg.get(Unmarshal.STR_LAYOUT, 0);
575575
if (!Unmarshal.isNullPointer(path)) {
576-
Unmarshal.copy(path, outPath, NFDInternal.nfdCharset);
576+
outPath[0] = path.getString(0L, NFDInternal.nfdCharset);
577577
pathSetFreePathN(path);
578578
}
579579
}

modules/overrungl.stb/src/main/java/overrungl/stb/STBImage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ default MemorySegment loadFromMemory(MemorySegment buffer, MemorySegment x, Memo
255255
}
256256

257257
@Entrypoint("stbi_load_from_memory")
258-
MemorySegment loadFromMemory(SegmentAllocator allocator, int len, byte[] buffer, @Ref int[] x, @Ref int[] y, @Ref int[] channelsInFile, int desiredChannels);
258+
MemorySegment loadFromMemory(SegmentAllocator allocator, byte[] buffer, int len, @Ref int[] x, @Ref int[] y, @Ref int[] channelsInFile, int desiredChannels);
259259

260260
@Skip
261261
default MemorySegment loadFromMemory(SegmentAllocator allocator, byte[] buffer, @Ref int[] x, @Ref int[] y, @Ref int[] channelsInFile, int desiredChannels) {
262-
return loadFromMemory(allocator, buffer.length, buffer, x, y, channelsInFile, desiredChannels);
262+
return loadFromMemory(allocator, buffer, buffer.length, x, y, channelsInFile, desiredChannels);
263263
}
264264

265265
@Entrypoint("stbi_load_gif_from_memory")

modules/overrungl.stb/src/main/java/overrungl/stb/STBPerlin.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package overrungl.stb;
1818

1919
import overrun.marshal.Downcall;
20+
import overrun.marshal.gen.Entrypoint;
2021

2122
/**
2223
* The STB perlin noise generator.
@@ -51,6 +52,7 @@ public interface STBPerlin {
5152
* @param wrapZ wrap z
5253
* @return the value
5354
*/
55+
@Entrypoint("stb_perlin_noise3")
5456
float noise3(float x, float y, float z, int wrapX, int wrapY, int wrapZ);
5557

5658
/**
@@ -67,6 +69,7 @@ public interface STBPerlin {
6769
* @param seed the seed
6870
* @return the value
6971
*/
72+
@Entrypoint("stb_perlin_noise3_seed")
7073
float noise3seed(float x, float y, float z, int wrapX, int wrapY, int wrapZ, int seed);
7174

7275
/**
@@ -84,6 +87,7 @@ public interface STBPerlin {
8487
* @param octaves = 6 -- number of "octaves" of noise3() to sum
8588
* @return the value
8689
*/
90+
@Entrypoint("stb_perlin_ridge_noise3")
8791
float ridgeNoise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves);
8892

8993
/**
@@ -100,6 +104,7 @@ public interface STBPerlin {
100104
* @param octaves = 6 -- number of "octaves" of noise3() to sum
101105
* @return the value
102106
*/
107+
@Entrypoint("stb_perlin_fbm_noise3")
103108
float fbmNoise3(float x, float y, float z, float lacunarity, float gain, int octaves);
104109

105110
/**
@@ -116,6 +121,7 @@ public interface STBPerlin {
116121
* @param octaves = 6 -- number of "octaves" of noise3() to sum
117122
* @return the value
118123
*/
124+
@Entrypoint("stb_perlin_turbulence_noise3")
119125
float turbulenceNoise3(float x, float y, float z, float lacunarity, float gain, int octaves);
120126

121127
/**
@@ -129,5 +135,6 @@ public interface STBPerlin {
129135
* @param wrapZ wrapZ
130136
* @param seed seed
131137
*/
138+
@Entrypoint("stb_perlin_noise3_wrap_nonpow2")
132139
float noise3wrapNonpow2(float x, float y, float z, int wrapX, int wrapY, int wrapZ, byte seed);
133140
}

modules/samples/src/test/java/overrungl/demo/glfw/GLFWJoystickTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package overrungl.demo.glfw;
1818

19-
import overrungl.glfw.GLFWCallbacks;
19+
import overrun.marshal.Unmarshal;
2020
import overrungl.glfw.GLFW;
21+
import overrungl.glfw.GLFWCallbacks;
2122
import overrungl.glfw.GLFWErrorCallback;
2223
import overrungl.glfw.GLFWGamepadState;
23-
import overrungl.util.CheckUtil;
2424

2525
import java.lang.foreign.Arena;
2626
import java.lang.foreign.MemorySegment;
@@ -48,13 +48,13 @@ public void run() {
4848

4949
private void init() {
5050
GLFWErrorCallback.createPrint().set();
51-
CheckUtil.check(glfw.init(), "Unable to initialize GLFW");
51+
if (!glfw.init()) throw new IllegalStateException("Unable to initialize GLFW");
5252
glfw.defaultWindowHints();
5353
glfw.windowHint(GLFW.VISIBLE, false);
5454
glfw.windowHint(GLFW.RESIZABLE, true);
5555
glfw.windowHint(GLFW.CLIENT_API, GLFW.NO_API);
5656
window = glfw.createWindow(200, 100, "Holder", MemorySegment.NULL, MemorySegment.NULL);
57-
CheckUtil.checkNotNullptr(window, "Failed to create the GLFW window");
57+
if (Unmarshal.isNullPointer(window)) throw new IllegalStateException("Failed to create the GLFW window");
5858
glfw.setKeyCallback(window, (_, key, _, action, _) -> {
5959
if (key == GLFW.KEY_ESCAPE && action == GLFW.RELEASE) {
6060
glfw.setWindowShouldClose(window, true);

modules/samples/src/test/java/overrungl/demo/glfw/GLFWWindowIconTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
package overrungl.demo.glfw;
1818

19+
import overrun.marshal.Unmarshal;
1920
import overrungl.demo.util.IOUtil;
20-
import overrungl.glfw.GLFWCallbacks;
2121
import overrungl.glfw.GLFW;
22+
import overrungl.glfw.GLFWCallbacks;
2223
import overrungl.glfw.GLFWErrorCallback;
2324
import overrungl.glfw.GLFWImage;
2425
import overrungl.opengl.GL;
2526
import overrungl.opengl.GLLoader;
2627
import overrungl.stb.STBImage;
27-
import overrungl.util.CheckUtil;
2828

2929
import java.io.IOException;
3030
import java.lang.foreign.Arena;
@@ -60,20 +60,20 @@ public void run() {
6060

6161
private void init(Arena arena) {
6262
GLFWErrorCallback.createPrint().set();
63-
CheckUtil.check(glfw.init(), "Unable to initialize GLFW");
63+
if (!glfw.init()) throw new IllegalStateException("Unable to initialize GLFW");
6464
glfw.defaultWindowHints();
6565
glfw.windowHint(GLFW.VISIBLE, false);
6666
glfw.windowHint(GLFW.RESIZABLE, true);
6767
window = glfw.createWindow(300, 300, "Hello World!", MemorySegment.NULL, MemorySegment.NULL);
68-
CheckUtil.checkNotNullptr(window, "Failed to create the GLFW window");
68+
if (Unmarshal.isNullPointer(window)) throw new IllegalStateException("Failed to create the GLFW window");
6969

7070
try {
7171
final STBImage stbImage = STBImage.INSTANCE;
7272
var px = arena.allocate(JAVA_INT);
7373
var py = arena.allocate(JAVA_INT);
7474
var pc = arena.allocate(JAVA_INT);
7575
var data = stbImage.loadFromMemory(
76-
IOUtil.ioResourceToSegment(arena, "image.png", 256),
76+
IOUtil.ioResourceToSegment(arena, "image.png"),
7777
px, py, pc, STBImage.RGB_ALPHA
7878
);
7979
final GLFWImage image = new GLFWImage(arena);

modules/samples/src/test/java/overrungl/demo/opengl/GL10Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
package overrungl.demo.opengl;
1818

19+
import overrun.marshal.Unmarshal;
1920
import overrungl.glfw.GLFW;
2021
import overrungl.glfw.GLFWCallbacks;
2122
import overrungl.glfw.GLFWErrorCallback;
2223
import overrungl.opengl.GL;
2324
import overrungl.opengl.GLLegacy;
2425
import overrungl.opengl.GLLoader;
25-
import overrungl.util.CheckUtil;
2626

2727
import java.lang.foreign.MemorySegment;
2828
import java.util.Objects;
@@ -52,12 +52,12 @@ public void run() {
5252

5353
private void init() {
5454
GLFWErrorCallback.createPrint().set();
55-
CheckUtil.check(glfw.init(), "Unable to initialize GLFW");
55+
if (!glfw.init()) throw new IllegalStateException("Unable to initialize GLFW");
5656
glfw.defaultWindowHints();
5757
glfw.windowHint(GLFW.VISIBLE, false);
5858
glfw.windowHint(GLFW.RESIZABLE, true);
5959
window = glfw.createWindow(300, 300, "Hello World!", MemorySegment.NULL, MemorySegment.NULL);
60-
CheckUtil.checkNotNullptr(window, "Failed to create the GLFW window");
60+
if (Unmarshal.isNullPointer(window)) throw new IllegalStateException("Failed to create the GLFW window");
6161
glfw.setKeyCallback(window, (_, key, _, action, _) -> {
6262
if (key == GLFW.KEY_ESCAPE && action == GLFW.RELEASE) {
6363
glfw.setWindowShouldClose(window, true);

0 commit comments

Comments
 (0)