Skip to content

Commit

Permalink
Removal of SegmentAllocator (#31)
Browse files Browse the repository at this point in the history
- [Core] Remove MemoryUtil::segmentAllocator
- [OpenGL] Remove allocator from GL20C::getProgramInfoLog and ::getShaderInfoLog
  • Loading branch information
squid233 authored Aug 9, 2023
1 parent 381c743 commit 727bbf7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,6 @@ private MemoryUtil() {
throw new IllegalStateException("Do not construct instance");
}

/**
* {@return a segment allocator of this}
* The returned memory must be released <strong>explicitly</strong> by {@link #free(MemorySegment)}.
*/
public static SegmentAllocator segmentAllocator() {
class Holder {
private static final SegmentAllocator ALLOCATOR = (byteSize, byteAlignment) -> calloc(byteSize / byteAlignment, byteAlignment);
}
return Holder.ALLOCATOR;
}

/**
* {@return {@code true} if <i>{@code segment}</i> is a null pointer}
*
Expand Down
59 changes: 41 additions & 18 deletions modules/overrungl.opengl/src/main/java/overrungl/opengl/GL20C.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import overrungl.internal.RuntimeHelper;
import overrungl.util.MemoryStack;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SegmentAllocator;

Expand Down Expand Up @@ -455,18 +456,29 @@ public static void getProgramInfoLog(int program, int bufSize, MemorySegment len
}
}

public static String getProgramInfoLog(SegmentAllocator allocator, int program, int bufSize, int @Nullable [] length) {
var pLen = length != null ? allocator.allocate(JAVA_INT) : MemorySegment.NULL;
var pLog = allocator.allocateArray(JAVA_BYTE, bufSize);
getProgramInfoLog(program, bufSize, pLen, pLog);
if (length != null && length.length > 0) {
length[0] = pLen.get(JAVA_INT, 0);
public static String getProgramInfoLog(int program, int bufSize, int @Nullable [] length) {
final MemoryStack stack = MemoryStack.stackGet();
final long stackPointer = stack.getPointer();
try {
var pLen = length != null ? stack.calloc(JAVA_INT) : MemorySegment.NULL;
final Arena allocator = stack.getPointer() < bufSize ? Arena.ofConfined() : stack;
try {
var pLog = allocator.allocateArray(JAVA_BYTE, bufSize);
getProgramInfoLog(program, bufSize, pLen, pLog);
if (length != null && length.length > 0) {
length[0] = pLen.get(JAVA_INT, 0);
}
return pLog.getUtf8String(0);
} finally {
if (!(allocator instanceof MemoryStack)) allocator.close();
}
} finally {
stack.setPointer(stackPointer);
}
return pLog.getUtf8String(0);
}

public static String getProgramInfoLog(SegmentAllocator allocator, int program) {
return getProgramInfoLog(allocator, program, getProgrami(program, INFO_LOG_LENGTH), null);
public static String getProgramInfoLog(int program) {
return getProgramInfoLog(program, getProgrami(program, INFO_LOG_LENGTH), null);
}

public static void getProgramiv(int program, int pname, MemorySegment params) {
Expand Down Expand Up @@ -505,18 +517,29 @@ public static void getShaderInfoLog(int shader, int bufSize, MemorySegment lengt
}
}

public static String getShaderInfoLog(SegmentAllocator allocator, int shader, int bufSize, int @Nullable [] length) {
var pLen = length != null ? allocator.allocate(JAVA_INT) : MemorySegment.NULL;
var pLog = allocator.allocateArray(JAVA_BYTE, bufSize);
getShaderInfoLog(shader, bufSize, pLen, pLog);
if (length != null && length.length > 0) {
length[0] = pLen.get(JAVA_INT, 0);
public static String getShaderInfoLog(int shader, int bufSize, int @Nullable [] length) {
final MemoryStack stack = MemoryStack.stackGet();
final long stackPointer = stack.getPointer();
try {
var pLen = length != null ? stack.callocInt() : MemorySegment.NULL;
final Arena allocator = stack.getPointer() < bufSize ? Arena.ofConfined() : stack;
try {
var pLog = allocator.allocateArray(JAVA_BYTE, bufSize);
getShaderInfoLog(shader, bufSize, pLen, pLog);
if (length != null && length.length > 0) {
length[0] = pLen.get(JAVA_INT, 0);
}
return pLog.getUtf8String(0);
} finally {
if (!(allocator instanceof MemoryStack)) allocator.close();
}
} finally {
stack.setPointer(stackPointer);
}
return pLog.getUtf8String(0);
}

public static String getShaderInfoLog(SegmentAllocator allocator, int shader) {
return getShaderInfoLog(allocator, shader, getShaderi(shader, INFO_LOG_LENGTH), null);
public static String getShaderInfoLog(int shader) {
return getShaderInfoLog(shader, getShaderi(shader, INFO_LOG_LENGTH), null);
}

public static void getShaderSource(int shader, int bufSize, MemorySegment length, MemorySegment source) {
Expand Down

0 comments on commit 727bbf7

Please sign in to comment.