Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Rename CStdio to CStdlib #36

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.CStdio;
import overrungl.util.CStdlib;

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' -> CStdio.ADDRESS_UNBOUNDED;
* case 'p' -> CStdlib.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' -> CStdio.ADDRESS_UNBOUNDED;
case 'p' -> CStdlib.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.CStdio;
import overrungl.util.CStdlib;

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 = CStdio.ADDRESS_UNBOUNDED;
public static final AddressLayout ADDRESS_UNBOUNDED = CStdlib.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 CStdio.isNullptr(segment);
return CStdlib.isNullptr(segment);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @author squid233
* @since 0.1.0
*/
public final class CStdio {
public final class CStdlib {
/**
* 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 CStdio() {
private CStdlib() {
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 (CStdio.isNullptr(segment)) throw new IllegalStateException(messageSupplier.get());
if (CStdlib.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 (CStdio.isNullptr(segment)) throw new IllegalStateException(message);
if (CStdlib.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 (CStdio.isNullptr(address)) {
if (CStdlib.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 (CStdio.isNullptr(address)) {
if (CStdlib.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) {
CStdio.checkAlignment(alignment);
CStdlib.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) {
CStdio.checkAlignment(alignment);
CStdlib.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 {
CStdio.checkByteSize(byteSize);
CStdio.checkAlignment(byteAlignment);
CStdlib.checkByteSize(byteSize);
CStdlib.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(CStdio.class.getName()) || name.equals(DebugAllocator.class.getName());
return name.equals(CStdlib.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.CStdio;
import overrungl.util.CStdlib;

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, CStdio.malloc(MAT2F));
return put(mat, CStdlib.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, CStdio.malloc(MAT2D));
return put(mat, CStdlib.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, CStdio.malloc(MAT3X2F));
return put(mat, CStdlib.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, CStdio.malloc(MAT3X2D));
return put(mat, CStdlib.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, CStdio.malloc(MAT3F));
return put(mat, CStdlib.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, CStdio.malloc(MAT3D));
return put(mat, CStdlib.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, CStdio.malloc(MAT4X3F));
return put(mat, CStdlib.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, CStdio.malloc(MAT4X3D));
return put(mat, CStdlib.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, CStdio.malloc(MAT4F));
return put(mat, CStdlib.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, CStdio.malloc(MAT4D));
return put(mat, CStdlib.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.CStdio;
import overrungl.util.CStdlib;

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, CStdio.malloc(VEC2I));
return put(vec, CStdlib.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, CStdio.malloc(VEC2F));
return put(vec, CStdlib.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, CStdio.malloc(VEC2D));
return put(vec, CStdlib.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, CStdio.malloc(VEC3I));
return put(vec, CStdlib.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, CStdio.malloc(VEC3F));
return put(vec, CStdlib.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, CStdio.malloc(VEC3D));
return put(vec, CStdlib.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, CStdio.malloc(VEC4I));
return put(vec, CStdlib.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, CStdio.malloc(VEC4F));
return put(vec, CStdlib.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, CStdio.malloc(VEC4D));
return put(vec, CStdlib.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.CStdio;
import overrungl.util.CStdlib;

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

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

Expand Down