Skip to content

Commit

Permalink
Merge branch 'main' into opengl
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Oct 21, 2023
2 parents 12a4195 + e2689a8 commit 41b07e2
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 44 deletions.
19 changes: 19 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Security Policy

## Supported Versions

We are currently working on 0.x versions
and only the latest version will be supported.

| Version | Supported |
| ------- | ------------------ |
| 0.1.x | :white_check_mark: |

## Reporting a Vulnerability

You can report a vulnerability through [creating an issue](https://github.com/Over-Run/overrungl/security/advisories/new).

By reporting the vulnerability, you acknowledge that you have read and agree our [Code of Conduct](CODE_OF_CONDUCT.md).

We don't accept any vulnerability in unsupported versions.
Please keep using the latest version.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package overrungl;

import overrungl.internal.Exceptions;
import overrungl.util.MemoryUtil;
import overrungl.util.CStdio;

import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.ValueLayout;
Expand All @@ -38,7 +38,7 @@
* case 'F' -> JAVA_FLOAT;
* case 'D' -> JAVA_DOUBLE;
* case 'P' -> ADDRESS;
* case 'p' -> MemoryUtil.ADDRESS_UNBOUNDED;
* case 'p' -> CStdio.ADDRESS_UNBOUNDED;
* default -> throw new IllegalArgumentException();
* }}
*
Expand Down Expand Up @@ -123,7 +123,7 @@ public static ValueLayout ofValue(char c) throws IllegalArgumentException {
case 'F' -> JAVA_FLOAT;
case 'D' -> JAVA_DOUBLE;
case 'P' -> ADDRESS;
case 'p' -> MemoryUtil.ADDRESS_UNBOUNDED;
case 'p' -> CStdio.ADDRESS_UNBOUNDED;
default ->
throw Exceptions.IAE. "Invalid argument c: expected one of B, S, I, J, C, Z, F, D, P or p; got '\{ c }'" ;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import overrungl.FunctionDescriptors;
import overrungl.os.Architecture;
import overrungl.os.Platform;
import overrungl.util.MemoryUtil;
import overrungl.util.CStdio;

import java.io.File;
import java.lang.foreign.*;
Expand Down Expand Up @@ -50,7 +50,7 @@ public final class RuntimeHelper {
/**
* An unbounded address layout.
*/
public static final AddressLayout ADDRESS_UNBOUNDED = MemoryUtil.ADDRESS_UNBOUNDED;
public static final AddressLayout ADDRESS_UNBOUNDED = CStdio.ADDRESS_UNBOUNDED;
private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);

/**
Expand Down Expand Up @@ -194,7 +194,7 @@ public static SymbolLookup load(String module, String basename, String version)
* @param segment the segment.
*/
public static boolean isNullptr(@Nullable MemorySegment segment) {
return MemoryUtil.isNullptr(segment);
return CStdio.isNullptr(segment);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import static overrungl.FunctionDescriptors.*;

/**
* The standard-C memory allocator.
* The standard-C library functions.
*
* @author squid233
* @since 0.1.0
*/
public final class MemoryUtil {
public final class CStdio {
/**
* An unbounded address layout.
*/
Expand All @@ -57,7 +57,7 @@ private static MethodHandle downcall(String name, FunctionDescriptors function)
return RuntimeHelper.downcallThrow(LOOKUP.find(name), function);
}

private MemoryUtil() {
private CStdio() {
throw new IllegalStateException("Do not construct instance");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static void checkNotNullptr(MemorySegment segment) throws IllegalStateExc
* @see #checkNotNullptr(MemorySegment, String)
*/
public static void checkNotNullptr(MemorySegment segment, Supplier<String> messageSupplier) throws IllegalStateException {
if (MemoryUtil.isNullptr(segment)) throw new IllegalStateException(messageSupplier.get());
if (CStdio.isNullptr(segment)) throw new IllegalStateException(messageSupplier.get());
}

/**
Expand All @@ -105,6 +105,6 @@ public static void checkNotNullptr(MemorySegment segment, Supplier<String> messa
* @see #checkNotNullptr(MemorySegment, Supplier)
*/
public static void checkNotNullptr(MemorySegment segment, String message) throws IllegalStateException {
if (MemoryUtil.isNullptr(segment)) throw new IllegalStateException(message);
if (CStdio.isNullptr(segment)) throw new IllegalStateException(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private DebugAllocator() {
}

static long track(long address, long size) {
if (MemoryUtil.isNullptr(address)) {
if (CStdio.isNullptr(address)) {
Thread t = Thread.currentThread();
THREADS.putIfAbsent(t.threadId(), t.getName());

Expand Down Expand Up @@ -122,7 +122,7 @@ private static void trackAbortPrint(Allocation allocation, String name, String a
}

static long untrack(long address) {
if (MemoryUtil.isNullptr(address)) {
if (CStdio.isNullptr(address)) {
return 0L;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private void checkPointer(long pointer) {
*/
public MemorySegment malloc(long alignment, long size) {
if (DEBUG) {
MemoryUtil.checkAlignment(alignment);
CStdio.checkAlignment(alignment);
}

// Align address to the specified alignment
Expand All @@ -338,7 +338,7 @@ public MemorySegment malloc(long alignment, long size) {
*/
public MemorySegment ncalloc(long alignment, long num, long size) {
if (DEBUG) {
MemoryUtil.checkAlignment(alignment);
CStdio.checkAlignment(alignment);
}

long bytes = num * size;
Expand Down Expand Up @@ -559,8 +559,8 @@ public MemorySegment callocPointer() {

@Override
public MemorySegment allocate(long byteSize, long byteAlignment) throws IllegalArgumentException {
MemoryUtil.checkByteSize(byteSize);
MemoryUtil.checkAlignment(byteAlignment);
CStdio.checkByteSize(byteSize);
CStdio.checkAlignment(byteAlignment);
return calloc(byteAlignment, byteSize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static Object[] stackWalkGetTrace() {
.skip(2)
.dropWhile(f -> {
String name = f.getClassName();
return name.equals(MemoryUtil.class.getName()) || name.equals(DebugAllocator.class.getName());
return name.equals(CStdio.class.getName()) || name.equals(DebugAllocator.class.getName());
})
.toArray(StackWalker.StackFrame[]::new)
);
Expand Down
22 changes: 11 additions & 11 deletions modules/overrungl.joml/src/main/java/overrungl/joml/Matrixn.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.joml.*;
import overrungl.util.MemoryStack;
import overrungl.util.MemoryUtil;
import overrungl.util.CStdio;

import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
Expand Down Expand Up @@ -197,7 +197,7 @@ public static MemorySegment allocate(SegmentAllocator allocator, Matrix4dc mat)
* @return the memory address
*/
public static MemorySegment malloc(Matrix2fc mat) {
return put(mat, MemoryUtil.malloc(MAT2F));
return put(mat, CStdio.malloc(MAT2F));
}

/**
Expand All @@ -207,7 +207,7 @@ public static MemorySegment malloc(Matrix2fc mat) {
* @return the memory address
*/
public static MemorySegment malloc(Matrix2dc mat) {
return put(mat, MemoryUtil.malloc(MAT2D));
return put(mat, CStdio.malloc(MAT2D));
}

/**
Expand All @@ -217,7 +217,7 @@ public static MemorySegment malloc(Matrix2dc mat) {
* @return the memory address
*/
public static MemorySegment malloc(Matrix3x2fc mat) {
return put(mat, MemoryUtil.malloc(MAT3X2F));
return put(mat, CStdio.malloc(MAT3X2F));
}

/**
Expand All @@ -227,7 +227,7 @@ public static MemorySegment malloc(Matrix3x2fc mat) {
* @return the memory address
*/
public static MemorySegment malloc(Matrix3x2dc mat) {
return put(mat, MemoryUtil.malloc(MAT3X2D));
return put(mat, CStdio.malloc(MAT3X2D));
}

/**
Expand All @@ -237,7 +237,7 @@ public static MemorySegment malloc(Matrix3x2dc mat) {
* @return the memory address
*/
public static MemorySegment malloc(Matrix3fc mat) {
return put(mat, MemoryUtil.malloc(MAT3F));
return put(mat, CStdio.malloc(MAT3F));
}

/**
Expand All @@ -247,7 +247,7 @@ public static MemorySegment malloc(Matrix3fc mat) {
* @return the memory address
*/
public static MemorySegment malloc(Matrix3dc mat) {
return put(mat, MemoryUtil.malloc(MAT3D));
return put(mat, CStdio.malloc(MAT3D));
}

/**
Expand All @@ -257,7 +257,7 @@ public static MemorySegment malloc(Matrix3dc mat) {
* @return the memory address
*/
public static MemorySegment malloc(Matrix4x3fc mat) {
return put(mat, MemoryUtil.malloc(MAT4X3F));
return put(mat, CStdio.malloc(MAT4X3F));
}

/**
Expand All @@ -267,7 +267,7 @@ public static MemorySegment malloc(Matrix4x3fc mat) {
* @return the memory address
*/
public static MemorySegment malloc(Matrix4x3dc mat) {
return put(mat, MemoryUtil.malloc(MAT4X3D));
return put(mat, CStdio.malloc(MAT4X3D));
}

/**
Expand All @@ -277,7 +277,7 @@ public static MemorySegment malloc(Matrix4x3dc mat) {
* @return the memory address
*/
public static MemorySegment malloc(Matrix4fc mat) {
return put(mat, MemoryUtil.malloc(MAT4F));
return put(mat, CStdio.malloc(MAT4F));
}

/**
Expand All @@ -287,7 +287,7 @@ public static MemorySegment malloc(Matrix4fc mat) {
* @return the memory address
*/
public static MemorySegment malloc(Matrix4dc mat) {
return put(mat, MemoryUtil.malloc(MAT4D));
return put(mat, CStdio.malloc(MAT4D));
}

/**
Expand Down
20 changes: 10 additions & 10 deletions modules/overrungl.joml/src/main/java/overrungl/joml/Vectorn.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.joml.*;
import overrungl.util.MemoryStack;
import overrungl.util.MemoryUtil;
import overrungl.util.CStdio;

import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
Expand Down Expand Up @@ -181,7 +181,7 @@ public static MemorySegment allocate(SegmentAllocator allocator, Vector4dc vec)
* @return the memory address
*/
public static MemorySegment malloc(Vector2ic vec) {
return put(vec, MemoryUtil.malloc(VEC2I));
return put(vec, CStdio.malloc(VEC2I));
}

/**
Expand All @@ -191,7 +191,7 @@ public static MemorySegment malloc(Vector2ic vec) {
* @return the memory address
*/
public static MemorySegment malloc(Vector2fc vec) {
return put(vec, MemoryUtil.malloc(VEC2F));
return put(vec, CStdio.malloc(VEC2F));
}

/**
Expand All @@ -201,7 +201,7 @@ public static MemorySegment malloc(Vector2fc vec) {
* @return the memory address
*/
public static MemorySegment malloc(Vector2dc vec) {
return put(vec, MemoryUtil.malloc(VEC2D));
return put(vec, CStdio.malloc(VEC2D));
}

/**
Expand All @@ -211,7 +211,7 @@ public static MemorySegment malloc(Vector2dc vec) {
* @return the memory address
*/
public static MemorySegment malloc(Vector3ic vec) {
return put(vec, MemoryUtil.malloc(VEC3I));
return put(vec, CStdio.malloc(VEC3I));
}

/**
Expand All @@ -221,7 +221,7 @@ public static MemorySegment malloc(Vector3ic vec) {
* @return the memory address
*/
public static MemorySegment malloc(Vector3fc vec) {
return put(vec, MemoryUtil.malloc(VEC3F));
return put(vec, CStdio.malloc(VEC3F));
}

/**
Expand All @@ -231,7 +231,7 @@ public static MemorySegment malloc(Vector3fc vec) {
* @return the memory address
*/
public static MemorySegment malloc(Vector3dc vec) {
return put(vec, MemoryUtil.malloc(VEC3D));
return put(vec, CStdio.malloc(VEC3D));
}

/**
Expand All @@ -241,7 +241,7 @@ public static MemorySegment malloc(Vector3dc vec) {
* @return the memory address
*/
public static MemorySegment malloc(Vector4ic vec) {
return put(vec, MemoryUtil.malloc(VEC4I));
return put(vec, CStdio.malloc(VEC4I));
}

/**
Expand All @@ -251,7 +251,7 @@ public static MemorySegment malloc(Vector4ic vec) {
* @return the memory address
*/
public static MemorySegment malloc(Vector4fc vec) {
return put(vec, MemoryUtil.malloc(VEC4F));
return put(vec, CStdio.malloc(VEC4F));
}

/**
Expand All @@ -261,7 +261,7 @@ public static MemorySegment malloc(Vector4fc vec) {
* @return the memory address
*/
public static MemorySegment malloc(Vector4dc vec) {
return put(vec, MemoryUtil.malloc(VEC4D));
return put(vec, CStdio.malloc(VEC4D));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import overrungl.util.MemoryUtil;
import overrungl.util.CStdio;

/**
* Tests memory util
Expand Down Expand Up @@ -66,21 +66,21 @@ public MemorySegment testArenaOutside() {
public MemorySegment memoryUtilMalloc() {
MemorySegment seg = null;
try {
seg = MemoryUtil.malloc(size);
seg = CStdio.malloc(size);
return seg;
} finally {
MemoryUtil.free(seg);
CStdio.free(seg);
}
}

@Benchmark
public MemorySegment memoryUtilCalloc() {
MemorySegment seg = null;
try {
seg = MemoryUtil.calloc(1, size);
seg = CStdio.calloc(1, size);
return seg;
} finally {
MemoryUtil.free(seg);
CStdio.free(seg);
}
}

Expand Down

0 comments on commit 41b07e2

Please sign in to comment.