Move to global cleaner + enable tracking for all but memory buffers

This commit is contained in:
mcrcortex
2025-06-27 22:35:52 +10:00
parent 51f54c6edd
commit 726517a8b6
6 changed files with 23 additions and 14 deletions

View File

@@ -7,10 +7,10 @@ import me.cortex.voxy.common.world.SaveLoadSystem;
import java.lang.ref.Cleaner;
import static me.cortex.voxy.common.util.GlobalCleaner.CLEANER;
import static org.lwjgl.util.zstd.Zstd.*;
public class ZSTDCompressor implements StorageCompressor {
private static final Cleaner CLEANER = Cleaner.create();
private record Ref(long ptr) {}
private static Ref createCleanableCompressionContext() {

View File

@@ -0,0 +1,7 @@
package me.cortex.voxy.common.util;
import java.lang.ref.Cleaner;
public class GlobalCleaner {
public static final Cleaner CLEANER = Cleaner.create();
}

View File

@@ -1,5 +1,6 @@
package me.cortex.voxy.common.util;
import me.cortex.voxy.commonImpl.VoxyCommon;
import org.lwjgl.system.MemoryUtil;
import java.nio.ByteBuffer;
@@ -7,6 +8,8 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
public class MemoryBuffer extends TrackedObject {
private static final boolean TRACK_MEMORY_BUFFERS = VoxyCommon.isVerificationFlagOn("trackBuffers");
public final long address;
public final long size;
private final boolean freeable;
@@ -21,7 +24,7 @@ public class MemoryBuffer extends TrackedObject {
}
private MemoryBuffer(boolean track, long address, long size, boolean freeable) {
super(track);
super(track && TRACK_MEMORY_BUFFERS);
this.tracked = track;
this.size = size;
this.address = address;

View File

@@ -2,8 +2,9 @@ package me.cortex.voxy.common.util;
import java.lang.ref.Cleaner;
import static me.cortex.voxy.common.util.GlobalCleaner.CLEANER;
public class ThreadLocalMemoryBuffer {
private static final Cleaner CLEANER = Cleaner.create();
private static MemoryBuffer createMemoryBuffer(long size) {
var buffer = new MemoryBuffer(size);
var ref = MemoryBuffer.createUntrackedUnfreeableRawFrom(buffer.address, buffer.size);

View File

@@ -5,9 +5,11 @@ import me.cortex.voxy.commonImpl.VoxyCommon;
import java.lang.ref.Cleaner;
import static me.cortex.voxy.common.util.GlobalCleaner.CLEANER;
public abstract class TrackedObject {
//TODO: maybe make this false? for performance overhead?
public static final boolean TRACK_OBJECT_ALLOCATIONS = VoxyCommon.isVerificationFlagOn("ensureTrackedObjectsAreFreed");
public static final boolean TRACK_OBJECT_ALLOCATIONS = VoxyCommon.isVerificationFlagOn("ensureTrackedObjectsAreFreed", true);
public static final boolean TRACK_OBJECT_ALLOCATION_STACKS = VoxyCommon.isVerificationFlagOn("trackObjectAllocationStacks");
private final Ref ref;
@@ -49,14 +51,6 @@ public abstract class TrackedObject {
public record Ref(Cleaner.Cleanable cleanable, boolean[] freedRef) {}
private static final Cleaner cleaner;
static {
if (TRACK_OBJECT_ALLOCATIONS) {
cleaner = Cleaner.create();
} else {
cleaner = null;
}
}
public static Ref register(boolean track, Object obj) {
boolean[] freed = new boolean[1];
Cleaner.Cleanable cleanable = null;
@@ -69,7 +63,7 @@ public abstract class TrackedObject {
} else {
trace = null;
}
cleanable = cleaner.register(obj, () -> {
cleanable = CLEANER.register(obj, () -> {
if (!freed[0]) {
Logger.error("Object named: " + clazz + " was not freed, location at:\n", trace==null?"Enable allocation stack tracing":trace);
}

View File

@@ -31,7 +31,11 @@ public class VoxyCommon implements ModInitializer {
//This is hardcoded like this because people do not understand what they are doing
public static boolean isVerificationFlagOn(String name) {
return System.getProperty("voxy."+name, "false").equals("true");
return isVerificationFlagOn(name, false);
}
public static boolean isVerificationFlagOn(String name, boolean defaultOn) {
return System.getProperty("voxy."+name, defaultOn?"true":"false").equals("true");
}
public static void breakpoint() {