Remove buffer builder leak fix since you can now close them properly
This commit is contained in:
parent
48b492b906
commit
967d39997f
|
|
@ -1,57 +0,0 @@
|
||||||
package org.embeddedt.modernfix.common.mixin.bugfix.buffer_builder_leak;
|
|
||||||
|
|
||||||
import com.mojang.blaze3d.vertex.BufferBuilder;
|
|
||||||
import org.embeddedt.modernfix.ModernFix;
|
|
||||||
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
|
|
||||||
import org.embeddedt.modernfix.render.UnsafeBufferHelper;
|
|
||||||
import org.spongepowered.asm.mixin.Dynamic;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.Shadow;
|
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
@Mixin(value = BufferBuilder.class, priority = 1500)
|
|
||||||
@ClientOnlyMixin
|
|
||||||
public class BufferBuilderMixin {
|
|
||||||
@Shadow private ByteBuffer buffer;
|
|
||||||
@Shadow private boolean closed;
|
|
||||||
|
|
||||||
private static boolean leakReported = false;
|
|
||||||
|
|
||||||
private boolean mfix$shouldFree = true;
|
|
||||||
|
|
||||||
@Dynamic
|
|
||||||
@Inject(method = "flywheel$injectForRender", at = @At("RETURN"), remap = false, require = 0)
|
|
||||||
private void preventFree(CallbackInfo ci) {
|
|
||||||
mfix$shouldFree = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ensure UnsafeBufferHelper is classloaded early, to avoid Forge's event transformer showing an error in the log.
|
|
||||||
*/
|
|
||||||
@Inject(method = "<clinit>", at = @At(value = "RETURN"))
|
|
||||||
private static void initUnsafeBufferHelper(CallbackInfo ci) {
|
|
||||||
UnsafeBufferHelper.init();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void finalize() throws Throwable {
|
|
||||||
try {
|
|
||||||
ByteBuffer buf = buffer;
|
|
||||||
// can be null if a mod already tried to free the buffer
|
|
||||||
if(!this.closed && buf != null && mfix$shouldFree) {
|
|
||||||
if(!leakReported) {
|
|
||||||
leakReported = true;
|
|
||||||
ModernFix.LOGGER.warn("One or more BufferBuilders have been leaked, ModernFix will attempt to correct this.");
|
|
||||||
}
|
|
||||||
UnsafeBufferHelper.free(buf);
|
|
||||||
buffer = null;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
super.finalize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
package org.embeddedt.modernfix.render;
|
|
||||||
|
|
||||||
import org.embeddedt.modernfix.ModernFix;
|
|
||||||
import org.lwjgl.system.MemoryUtil;
|
|
||||||
import sun.misc.Unsafe;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper that frees ByteBuffers allocated by BufferBuilders, and nulls out the address pointer
|
|
||||||
* to prevent double frees.
|
|
||||||
*
|
|
||||||
* @author Moulberry
|
|
||||||
*/
|
|
||||||
public class UnsafeBufferHelper {
|
|
||||||
private static final MemoryUtil.MemoryAllocator ALLOCATOR = MemoryUtil.getAllocator(false);
|
|
||||||
|
|
||||||
private static sun.misc.Unsafe UNSAFE = null;
|
|
||||||
private static long ADDRESS = -1;
|
|
||||||
|
|
||||||
static {
|
|
||||||
try {
|
|
||||||
final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
|
||||||
theUnsafe.setAccessible(true);
|
|
||||||
UNSAFE = (Unsafe)theUnsafe.get(null);
|
|
||||||
|
|
||||||
final Field addressField = MemoryUtil.class.getDeclaredField("ADDRESS");
|
|
||||||
addressField.setAccessible(true);
|
|
||||||
ADDRESS = addressField.getLong(null);
|
|
||||||
} catch(Throwable t) {
|
|
||||||
ModernFix.LOGGER.error("Could load unsafe/buffer address", t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void init() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void free(ByteBuffer buf) {
|
|
||||||
if(UNSAFE != null && ADDRESS >= 0) {
|
|
||||||
// set the address to 0 to prevent double free
|
|
||||||
long address = UNSAFE.getAndSetLong(buf, ADDRESS, 0);
|
|
||||||
if(address != 0) {
|
|
||||||
ALLOCATOR.free(address);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ALLOCATOR.free(MemoryUtil.memAddress0(buf));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user