Skip to content

Commit

Permalink
Add MemoryStack::malloc(MemoryLayout); fix document in Skip
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Feb 1, 2024
1 parent 755edc3 commit 22f43e6
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 105 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ Import as a Gradle dependency:

```groovy
dependencies {
implementation("io.github.over-run:marshal:0.1.0-alpha.12-jdk22")
implementation("io.github.over-run:marshal:0.1.0-alpha.13-jdk22")
}
```
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=marshal
# The project name should only contain lowercase letters, numbers and hyphen.
projName=marshal
projVersion=0.1.0-alpha.12-jdk22
projVersion=0.1.0-alpha.13-jdk22
projDesc=Marshaler of native libraries
# Uncomment them if you want to publish to maven repository.
projUrl=https://github.com/Over-Run/marshal
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/overrun/marshal/MemoryStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package overrun.marshal;

import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.foreign.*;
import java.util.Arrays;
import java.util.Objects;

Expand Down Expand Up @@ -242,6 +239,18 @@ public MemorySegment malloc(long byteSize, long byteAlignment) {
return segment().asSlice(pointer, byteSize);
}

/**
* Allocates a block of {@code size} bytes of memory on the stack.
* The content of the newly allocated block of memory is not initialized, remaining with
* indeterminate values.
*
* @param layout the layout of the memory segment
* @return the memory segment on the stack for the requested allocation
*/
public MemorySegment malloc(MemoryLayout layout) {
return malloc(layout.byteSize(), layout.byteAlignment());
}

private MemorySegment malloc(long byteSize, ValueLayout valueLayout) {
return malloc(byteSize, valueLayout.byteAlignment());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/overrun/marshal/gen/Skip.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import java.lang.annotation.*;

/**
* Skips generating a marked field or method.
* Skips generating a marked method.
* <h2>Example</h2>
* <pre>{@code
* @Skip
* int LAYOUT;
* default int myFunction() { return 1; }
* }</pre>
*
* @author squid233
Expand Down
132 changes: 132 additions & 0 deletions src/test/java/overrun/marshal/test/DowncallSoutTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrun.marshal.test;

import org.junit.jupiter.api.*;
import overrun.marshal.MemoryStack;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.foreign.Arena;
import java.lang.foreign.SegmentAllocator;

import static org.junit.jupiter.api.Assertions.*;
import static overrun.marshal.test.TestUtil.*;

/**
* Test standard output
*
* @author squid233
* @since 0.1.0
*/
public final class DowncallSoutTest {
private static IDowncall d;
private static PrintStream stdout = null;
private static ByteArrayOutputStream outputStream = null;

@BeforeAll
static void beforeAll() {
d = IDowncall.getInstance(false);
}

@BeforeEach
void setUp() {
stdout = System.out;
outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));
}

@AfterEach
void tearDown() {
System.setOut(stdout);
outputStream.reset();
}

@AfterAll
static void afterAll() throws IOException {
if (outputStream != null) {
outputStream.close();
}
}

@Test
void test() {
d.test();
assertEquals("test", outputStream.toString());
}

@Test
void testWithEntrypoint() {
d.testWithEntrypoint();
assertEquals("test", outputStream.toString());
}

@Test
void testSkip() {
d.testSkip();
assertEquals("testSkip", outputStream.toString());
}

@Test
void testInt() {
d.testInt(42);
assertEquals("42", outputStream.toString());
}

@Test
void testString() {
d.testString(TEST_STRING);
assertEquals(TEST_STRING, outputStream.toString());
}

@Test
void testUTF16String() {
d.testUTF16String(utf16Str(TEST_UTF16_STRING));
assertEquals(TEST_UTF16_STRING, outputStream.toString());
}

@Test
void testCEnum() {
d.testCEnum(MyEnum.A);
d.testCEnum(MyEnum.B);
d.testCEnum(MyEnum.C);
assertEquals("024", outputStream.toString());
}

@Test
void testIntArray() {
d.testIntArray(new int[]{4, 2});
try (Arena arena = Arena.ofConfined()) {
d.testIntArray((SegmentAllocator) arena, new int[]{4, 2});
d.testIntArray(arena, new int[]{4, 2});
}
try (MemoryStack stack = MemoryStack.stackPush()) {
d.testIntArray(stack, new int[]{4, 2});
}
d.testVarArgsJava(2, 4, 2);
d.testVarArgsJava(0);
assertEquals("[4, 2][4, 2][4, 2][4, 2][4, 2][]", outputStream.toString());
}

@Test
void testSizedIntArray() {
assertThrowsExactly(IllegalArgumentException.class, () -> d.testSizedIntArray(new int[0]));
assertDoesNotThrow(() -> d.testSizedIntArray(new int[]{4, 2}));
assertEquals("[4, 2]", outputStream.toString());
}
}
101 changes: 4 additions & 97 deletions src/test/java/overrun/marshal/test/DowncallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@

package overrun.marshal.test;

import org.junit.jupiter.api.*;
import overrun.marshal.MemoryStack;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SegmentAllocator;
import java.lang.foreign.ValueLayout;

import static org.junit.jupiter.api.Assertions.*;
import static overrun.marshal.test.TestUtil.*;

/**
* Test downcall
Expand All @@ -38,106 +33,25 @@
*/
public final class DowncallTest {
private static IDowncall d;
private static PrintStream stdout = null;
private static ByteArrayOutputStream outputStream = null;

@BeforeAll
static void beforeAll() {
d = IDowncall.getInstance(false);
}

@BeforeEach
void setUp() {
stdout = System.out;
outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));
}

@AfterEach
void tearDown() {
System.setOut(stdout);
outputStream.reset();
}

@AfterAll
static void afterAll() throws IOException {
if (outputStream != null) {
outputStream.close();
}
}

@Test
void test() {
d.test();
assertEquals("test", outputStream.toString());
}

@Test
void testWithEntrypoint() {
d.testWithEntrypoint();
assertEquals("test", outputStream.toString());
}

@Test
void testSkip() {
d.testSkip();
assertEquals("testSkip", outputStream.toString());
}

@Test
void testDefault() {
assertEquals(42, IDowncall.getInstance(false).testDefault());
assertEquals(84, IDowncall.getInstance(true).testDefault());
}

@Test
void testInt() {
d.testInt(42);
assertEquals("42", outputStream.toString());
}

@Test
void testString() {
d.testString(TEST_STRING);
assertEquals(TEST_STRING, outputStream.toString());
}

@Test
void testUTF16String() {
d.testUTF16String(utf16Str(TEST_UTF16_STRING));
assertEquals(TEST_UTF16_STRING, outputStream.toString());
}

@Test
void testCEnum() {
d.testCEnum(MyEnum.A);
d.testCEnum(MyEnum.B);
d.testCEnum(MyEnum.C);
assertEquals("024", outputStream.toString());
}

@Test
void testUpcall() {
try (Arena arena = Arena.ofConfined()) {
assertEquals(84, d.testUpcall(arena, i -> i * 2));
}
}

@Test
void testIntArray() {
d.testIntArray(new int[]{4, 2});
try (Arena arena = Arena.ofConfined()) {
d.testIntArray((SegmentAllocator) arena, new int[]{4, 2});
d.testIntArray(arena, new int[]{4, 2});
}
try (MemoryStack stack = MemoryStack.stackPush()) {
d.testIntArray(stack, new int[]{4, 2});
}
d.testVarArgsJava(2, 4, 2);
d.testVarArgsJava(0);
assertEquals("[4, 2][4, 2][4, 2][4, 2][4, 2][]", outputStream.toString());
}

@Test
void testStruct() {
try (Arena arena = Arena.ofConfined()) {
Expand All @@ -156,8 +70,8 @@ void testReturnInt() {

@Test
void testReturnString() {
assertEquals(TEST_STRING, d.testReturnString());
assertEquals(TEST_UTF16_STRING, d.testReturnUTF16String());
assertEquals(TestUtil.TEST_STRING, d.testReturnString());
assertEquals(TestUtil.TEST_UTF16_STRING, d.testReturnUTF16String());
}

@Test
Expand Down Expand Up @@ -207,13 +121,6 @@ void testReturnIntArray() {
assertArrayEquals(new int[]{4, 2}, d.testReturnIntArray());
}

@Test
void testSizedIntArray() {
assertThrowsExactly(IllegalArgumentException.class, () -> d.testSizedIntArray(new int[0]));
assertDoesNotThrow(() -> d.testSizedIntArray(new int[]{4, 2}));
assertEquals("[4, 2]", outputStream.toString());
}

@Test
void testReturnSizedSeg() {
final MemorySegment segment = d.testReturnSizedSeg();
Expand Down

0 comments on commit 22f43e6

Please sign in to comment.