Skip to content

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Aug 23, 2024
1 parent 2e0a074 commit 5776783
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
```
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.overrun.memstack;

import java.lang.foreign.MemorySegment;
import java.util.Arrays;

/**
* The default implementation of {@link MemoryStack}.
Expand All @@ -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;

Expand Down Expand Up @@ -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++;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/github/overrun/memstack/MemoryStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,17 @@ private static void checkSize(long size, String message) {

/**
* Remembers the current offset and pushes a frame for next allocations.
* <p>
* 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5776783

Please sign in to comment.