From 70ffe740c6c042ff093bcdc5f7fcbb71e24ccb8a Mon Sep 17 00:00:00 2001 From: ah-OOG-ah <75745146+ah-OOG-ah@users.noreply.github.com> Date: Tue, 26 Nov 2024 19:12:27 -0500 Subject: [PATCH] Add stack.malloc --- .../client/lwjgl3/CompatMemoryUtil.java | 18 ++++++++++++++++++ .../gtnhlib/client/lwjgl3/MemoryStack.java | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/lwjgl3/CompatMemoryUtil.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/lwjgl3/CompatMemoryUtil.java index 5fd3100..0219a40 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/lwjgl3/CompatMemoryUtil.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/lwjgl3/CompatMemoryUtil.java @@ -33,6 +33,8 @@ public class CompatMemoryUtil { static final sun.misc.Unsafe UNSAFE; + static final ByteOrder NATIVE_ORDER = ByteOrder.nativeOrder(); + private static final Class BUFFER_BYTE; private static final Class BUFFER_INT; private static final Class BUFFER_FLOAT; @@ -341,6 +343,22 @@ private static long getCapacityOffset() { return getFieldOffsetInt(bb, MAGIC_CAPACITY); } + static ByteBuffer wrapBufferByte(long address, int capacity) { + ByteBuffer buffer; + try { + buffer = (ByteBuffer)UNSAFE.allocateInstance(BUFFER_BYTE); + } catch (InstantiationException e) { + throw new UnsupportedOperationException(e); + } + + UNSAFE.putLong(buffer, ADDRESS, address); + UNSAFE.putInt(buffer, MARK, -1); + UNSAFE.putInt(buffer, LIMIT, capacity); + UNSAFE.putInt(buffer, CAPACITY, capacity); + + return buffer.order(NATIVE_ORDER); + } + static IntBuffer wrapBufferInt(long address, int capacity) { IntBuffer buffer; try { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/client/lwjgl3/MemoryStack.java b/src/main/java/com/gtnewhorizon/gtnhlib/client/lwjgl3/MemoryStack.java index 2c1ba65..0b7b79a 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/client/lwjgl3/MemoryStack.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/client/lwjgl3/MemoryStack.java @@ -11,6 +11,7 @@ import static com.gtnewhorizon.gtnhlib.client.lwjgl3.CompatMemoryUtil.memPutLong; import static com.gtnewhorizon.gtnhlib.client.lwjgl3.CompatMemoryUtil.memPutShort; import static com.gtnewhorizon.gtnhlib.client.lwjgl3.CompatMemoryUtil.memSet; +import static com.gtnewhorizon.gtnhlib.client.lwjgl3.CompatMemoryUtil.wrapBufferByte; import static com.gtnewhorizon.gtnhlib.client.lwjgl3.CompatMemoryUtil.wrapBufferFloat; import static com.gtnewhorizon.gtnhlib.client.lwjgl3.CompatMemoryUtil.wrapBufferInt; @@ -329,6 +330,23 @@ public long ncalloc(int alignment, int num, int size) { // ------------------------------------------------- + /** + * Allocates an aligned {@link ByteBuffer} on the stack. + * + * @param alignment the required buffer alignment + * @param size the number of elements in the buffer + * + * @return the allocated buffer + */ + public ByteBuffer malloc(int alignment, int size) { + if (/*DEBUG*/ false) { + checkAlignment(alignment); + } + return wrapBufferByte(nmalloc(alignment, size), size); + } + + // ------------------------------------------------- + /** Unsafe version of {@link #shorts(short)}. */ public long nshort(short value) { long a = nmalloc(2, 2);