Skip to content

Commit

Permalink
Merge pull request #45 from Over-Run/cleanup
Browse files Browse the repository at this point in the history
Cleanup code and fix bugs
  • Loading branch information
squid233 committed Feb 1, 2024
2 parents 3c93e5e + 0cec23a commit d9e2876
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 210 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ jdkEnablePreview=true
jdkEarlyAccessDoc=jdk22
kotlinTargetJdkVersion=21

overrunMarshalVersion=0.1.0-alpha.13-jdk22
overrunMarshalVersion=0.1.0-alpha.15-jdk22
overrunPlatformVersion=1.0.0
110 changes: 0 additions & 110 deletions modules/overrungl.core/src/main/java/overrungl/util/CheckUtil.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
package overrungl.util;

import org.jetbrains.annotations.Nullable;
import overrun.marshal.Unmarshal;
import overrungl.Configurations;
import overrungl.internal.RuntimeHelper;

import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import java.util.Objects;

import static java.lang.foreign.FunctionDescriptor.of;
import static java.lang.foreign.ValueLayout.ADDRESS;
import static overrungl.internal.RuntimeHelper.SIZE_T;

/**
* The standard-C memory allocator.
Expand All @@ -37,14 +38,13 @@ public final class MemoryUtil {
private static final Linker LINKER = Linker.nativeLinker();
private static final SymbolLookup LOOKUP = LINKER.defaultLookup();
private static final MethodHandle
m_malloc = downcall("malloc", of(ADDRESS, RuntimeHelper.SIZE_T)),
m_calloc = downcall("calloc", of(ADDRESS, RuntimeHelper.SIZE_T, RuntimeHelper.SIZE_T)),
m_realloc = downcall("realloc", of(ADDRESS, ADDRESS, RuntimeHelper.SIZE_T)),
m_malloc = downcall("malloc", of(ADDRESS, SIZE_T)),
m_calloc = downcall("calloc", of(ADDRESS, SIZE_T, SIZE_T)),
m_realloc = downcall("realloc", of(ADDRESS, ADDRESS, SIZE_T)),
m_free = downcall("free", FunctionDescriptor.ofVoid(ADDRESS)),
m_memcpy = downcall("memcpy", of(ADDRESS, ADDRESS, ADDRESS, RuntimeHelper.SIZE_T)),
m_memmove = downcall("memmove", of(ADDRESS, ADDRESS, ADDRESS, RuntimeHelper.SIZE_T)),
m_memset = downcall("memset", of(ADDRESS, ADDRESS, ValueLayout.JAVA_INT, RuntimeHelper.SIZE_T)),
strlen = downcall("strlen", of(RuntimeHelper.SIZE_T, ADDRESS));
m_memcpy = downcall("memcpy", of(ADDRESS, ADDRESS, ADDRESS, SIZE_T)),
m_memmove = downcall("memmove", of(ADDRESS, ADDRESS, ADDRESS, SIZE_T)),
m_memset = downcall("memset", of(ADDRESS, ADDRESS, ValueLayout.JAVA_INT, SIZE_T));
private static final boolean DEBUG = Configurations.DEBUG_MEM_UTIL.get();
/**
* The address of {@code NULL}.
Expand All @@ -65,7 +65,7 @@ private MemoryUtil() {
* @param segment the segment.
*/
public static boolean isNullptr(@Nullable MemorySegment segment) {
return segment == null || segment.equals(MemorySegment.NULL);
return Unmarshal.isNullPointer(segment);
}

/**
Expand Down Expand Up @@ -325,25 +325,6 @@ public static MemorySegment memset(MemorySegment dest, int c, long count) {
}
}

/**
* Gets the length of a string, by using the current locale or a specified locale.
* <p>
* {@code strlen} interprets the string as a single-byte character string,
* so its return value is always equal to the number of bytes,
* even if the string contains multibyte characters.
*
* @param str Null-terminated string.
* @return the number of characters in <i>{@code str}</i>, excluding the terminal null.
* No return value is reserved to indicate an error.
*/
public static long strlen(MemorySegment str) {
try {
return (long) strlen.invokeExact(str);
} catch (Throwable e) {
throw new AssertionError("should not reach here", e);
}
}

/**
* Creates a segment allocator with the given arena.
*
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 @@ -84,7 +84,7 @@
* var filterItem = NFDNFilterItem.create(allocator,
* new Pair<>("Source code", "java"),
* new Pair<>("Image file", "png,jpg"));
* }
*}
* <p>
* A file filter is a pair of strings comprising the friendly name and the specification
* (multiple file extensions are comma-separated).
Expand Down Expand Up @@ -573,7 +573,7 @@ default NFDResult pathSetEnumNextN(@NativeType("nfdpathsetenum_t*") MemorySegmen
if (result == NFDResult.OKAY) {
final MemorySegment path = seg.get(Unmarshal.STR_LAYOUT, 0);
if (!Unmarshal.isNullPointer(path)) {
Unmarshal.copy(path, outPath, NFDInternal.nfdCharset);
outPath[0] = path.getString(0L, NFDInternal.nfdCharset);
pathSetFreePathN(path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ default MemorySegment loadFromMemory(MemorySegment buffer, MemorySegment x, Memo
}

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

@Skip
default MemorySegment loadFromMemory(SegmentAllocator allocator, byte[] buffer, @Ref int[] x, @Ref int[] y, @Ref int[] channelsInFile, int desiredChannels) {
return loadFromMemory(allocator, buffer.length, buffer, x, y, channelsInFile, desiredChannels);
return loadFromMemory(allocator, buffer, buffer.length, x, y, channelsInFile, desiredChannels);
}

@Entrypoint("stbi_load_gif_from_memory")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package overrungl.stb;

import overrun.marshal.Downcall;
import overrun.marshal.gen.Entrypoint;

/**
* The STB perlin noise generator.
Expand Down Expand Up @@ -51,6 +52,7 @@ public interface STBPerlin {
* @param wrapZ wrap z
* @return the value
*/
@Entrypoint("stb_perlin_noise3")
float noise3(float x, float y, float z, int wrapX, int wrapY, int wrapZ);

/**
Expand All @@ -67,6 +69,7 @@ public interface STBPerlin {
* @param seed the seed
* @return the value
*/
@Entrypoint("stb_perlin_noise3_seed")
float noise3seed(float x, float y, float z, int wrapX, int wrapY, int wrapZ, int seed);

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

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

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

/**
Expand All @@ -129,5 +135,6 @@ public interface STBPerlin {
* @param wrapZ wrapZ
* @param seed seed
*/
@Entrypoint("stb_perlin_noise3_wrap_nonpow2")
float noise3wrapNonpow2(float x, float y, float z, int wrapX, int wrapY, int wrapZ, byte seed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package overrungl.demo.glfw;

import overrungl.glfw.GLFWCallbacks;
import overrun.marshal.Unmarshal;
import overrungl.glfw.GLFW;
import overrungl.glfw.GLFWCallbacks;
import overrungl.glfw.GLFWErrorCallback;
import overrungl.glfw.GLFWGamepadState;
import overrungl.util.CheckUtil;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
Expand Down Expand Up @@ -48,13 +48,13 @@ public void run() {

private void init() {
GLFWErrorCallback.createPrint().set();
CheckUtil.check(glfw.init(), "Unable to initialize GLFW");
if (!glfw.init()) throw new IllegalStateException("Unable to initialize GLFW");
glfw.defaultWindowHints();
glfw.windowHint(GLFW.VISIBLE, false);
glfw.windowHint(GLFW.RESIZABLE, true);
glfw.windowHint(GLFW.CLIENT_API, GLFW.NO_API);
window = glfw.createWindow(200, 100, "Holder", MemorySegment.NULL, MemorySegment.NULL);
CheckUtil.checkNotNullptr(window, "Failed to create the GLFW window");
if (Unmarshal.isNullPointer(window)) throw new IllegalStateException("Failed to create the GLFW window");
glfw.setKeyCallback(window, (_, key, _, action, _) -> {
if (key == GLFW.KEY_ESCAPE && action == GLFW.RELEASE) {
glfw.setWindowShouldClose(window, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package overrungl.demo.glfw;

import overrun.marshal.Unmarshal;
import overrungl.demo.util.IOUtil;
import overrungl.glfw.GLFWCallbacks;
import overrungl.glfw.GLFW;
import overrungl.glfw.GLFWCallbacks;
import overrungl.glfw.GLFWErrorCallback;
import overrungl.glfw.GLFWImage;
import overrungl.opengl.GL;
import overrungl.opengl.GLLoader;
import overrungl.stb.STBImage;
import overrungl.util.CheckUtil;

import java.io.IOException;
import java.lang.foreign.Arena;
Expand Down Expand Up @@ -60,20 +60,20 @@ public void run() {

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

try {
final STBImage stbImage = STBImage.INSTANCE;
var px = arena.allocate(JAVA_INT);
var py = arena.allocate(JAVA_INT);
var pc = arena.allocate(JAVA_INT);
var data = stbImage.loadFromMemory(
IOUtil.ioResourceToSegment(arena, "image.png", 256),
IOUtil.ioResourceToSegment(arena, "image.png"),
px, py, pc, STBImage.RGB_ALPHA
);
final GLFWImage image = new GLFWImage(arena);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package overrungl.demo.opengl;

import overrun.marshal.Unmarshal;
import overrungl.glfw.GLFW;
import overrungl.glfw.GLFWCallbacks;
import overrungl.glfw.GLFWErrorCallback;
import overrungl.opengl.GL;
import overrungl.opengl.GLLegacy;
import overrungl.opengl.GLLoader;
import overrungl.util.CheckUtil;

import java.lang.foreign.MemorySegment;
import java.util.Objects;
Expand Down Expand Up @@ -52,12 +52,12 @@ public void run() {

private void init() {
GLFWErrorCallback.createPrint().set();
CheckUtil.check(glfw.init(), "Unable to initialize GLFW");
if (!glfw.init()) throw new IllegalStateException("Unable to initialize GLFW");
glfw.defaultWindowHints();
glfw.windowHint(GLFW.VISIBLE, false);
glfw.windowHint(GLFW.RESIZABLE, true);
window = glfw.createWindow(300, 300, "Hello World!", MemorySegment.NULL, MemorySegment.NULL);
CheckUtil.checkNotNullptr(window, "Failed to create the GLFW window");
if (Unmarshal.isNullPointer(window)) throw new IllegalStateException("Failed to create the GLFW window");
glfw.setKeyCallback(window, (_, key, _, action, _) -> {
if (key == GLFW.KEY_ESCAPE && action == GLFW.RELEASE) {
glfw.setWindowShouldClose(window, true);
Expand Down
Loading

0 comments on commit d9e2876

Please sign in to comment.