From 5776783ae54c934335283bf00c47a8c1a2e18d48 Mon Sep 17 00:00:00 2001 From: squid233 <60126026+squid233@users.noreply.github.com> Date: Fri, 23 Aug 2024 16:23:27 +0800 Subject: [PATCH] 0.2.0 --- README.md | 2 +- gradle.properties | 2 +- .../io/github/overrun/memstack/DefaultMemoryStack.java | 5 +++-- src/main/java/io/github/overrun/memstack/MemoryStack.java | 5 +++-- .../io/github/overrun/memstack/test/MemoryStackTest.java | 8 +++++--- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f115f14..496ad6e 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,6 @@ Gradle: ```groovy dependencies { - implementation("io.github.over-run:memstack:0.1.0") + implementation("io.github.over-run:memstack:0.2.0") } ``` diff --git a/gradle.properties b/gradle.properties index 4dbf430..b665cc7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ projGroupId=io.github.over-run projArtifactId=memstack # The project name should only contain lowercase letters, numbers and hyphen. projName=memstack -projVersion=0.1.0 +projVersion=0.2.0 projDesc=Memory stack for FFM API # Uncomment them if you want to publish to maven repository. projUrl=https://github.com/Over-Run/memstack diff --git a/src/main/java/io/github/overrun/memstack/DefaultMemoryStack.java b/src/main/java/io/github/overrun/memstack/DefaultMemoryStack.java index f68dd54..4146ba4 100644 --- a/src/main/java/io/github/overrun/memstack/DefaultMemoryStack.java +++ b/src/main/java/io/github/overrun/memstack/DefaultMemoryStack.java @@ -1,6 +1,7 @@ package io.github.overrun.memstack; import java.lang.foreign.MemorySegment; +import java.util.Arrays; /** * The default implementation of {@link MemoryStack}. @@ -10,7 +11,7 @@ */ public class DefaultMemoryStack implements MemoryStack { private final MemorySegment segment; - private final long[] frames; + private long[] frames; private long offset = 0L; private int frameIndex = 0; @@ -47,7 +48,7 @@ public MemorySegment allocate(long byteSize, long byteAlignment) { @Override public MemoryStack push() { if (frameIndex >= frames.length) { - throw new IndexOutOfBoundsException("stack frame overflow; max frame count: " + frames.length); + frames = Arrays.copyOf(frames, frames.length * 3 / 2); } frames[frameIndex] = offset; frameIndex++; diff --git a/src/main/java/io/github/overrun/memstack/MemoryStack.java b/src/main/java/io/github/overrun/memstack/MemoryStack.java index 3c4d1d7..d602844 100644 --- a/src/main/java/io/github/overrun/memstack/MemoryStack.java +++ b/src/main/java/io/github/overrun/memstack/MemoryStack.java @@ -134,16 +134,17 @@ private static void checkSize(long size, String message) { /** * Remembers the current offset and pushes a frame for next allocations. + *

+ * The memory stack expands the internal frames array by 1.5x if there is not enough space to push. * * @return {@code this} - * @throws IndexOutOfBoundsException if there is not enough frames to push */ MemoryStack push(); /** * Pops to the previous frame and sets the current offset. * - * @throws IndexOutOfBoundsException if there is not enough frames to pop + * @throws IndexOutOfBoundsException if the frame index reached the bottom of the stack */ void pop(); diff --git a/src/test/java/io/github/overrun/memstack/test/MemoryStackTest.java b/src/test/java/io/github/overrun/memstack/test/MemoryStackTest.java index 7496963..c03ba31 100644 --- a/src/test/java/io/github/overrun/memstack/test/MemoryStackTest.java +++ b/src/test/java/io/github/overrun/memstack/test/MemoryStackTest.java @@ -88,12 +88,14 @@ void testAllocate() { } @Test - void testOverflow() { + void testExpand() { MemoryStack stack = MemoryStack.of(); - for (int i = 0; i < stack.frameCount(); i++) { + int count = stack.frameCount(); + for (int i = 0; i < count; i++) { stack.push(); } - assertThrowsExactly(IndexOutOfBoundsException.class, stack::push); + stack.push(); + assertEquals(count * 3 / 2, stack.frameCount()); } @Test