2025-11-24 22:52:51 +03:00

73 lines
2.5 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.google.common.collect.Queues
* com.mojang.logging.LogUtils
* org.jspecify.annotations.Nullable
* org.slf4j.Logger
*/
package net.minecraft.client.renderer;
import com.google.common.collect.Queues;
import com.mojang.logging.LogUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import net.minecraft.client.renderer.SectionBufferBuilderPack;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
public class SectionBufferBuilderPool {
private static final Logger LOGGER = LogUtils.getLogger();
private final Queue<SectionBufferBuilderPack> freeBuffers;
private volatile int freeBufferCount;
private SectionBufferBuilderPool(List<SectionBufferBuilderPack> buffers) {
this.freeBuffers = Queues.newArrayDeque(buffers);
this.freeBufferCount = this.freeBuffers.size();
}
public static SectionBufferBuilderPool allocate(int maxWorkers) {
int maxBuffers = Math.max(1, (int)((double)Runtime.getRuntime().maxMemory() * 0.3) / SectionBufferBuilderPack.TOTAL_BUFFERS_SIZE);
int targetBufferCount = Math.max(1, Math.min(maxWorkers, maxBuffers));
ArrayList<SectionBufferBuilderPack> buffers = new ArrayList<SectionBufferBuilderPack>(targetBufferCount);
try {
for (int i = 0; i < targetBufferCount; ++i) {
buffers.add(new SectionBufferBuilderPack());
}
}
catch (OutOfMemoryError e) {
LOGGER.warn("Allocated only {}/{} buffers", (Object)buffers.size(), (Object)targetBufferCount);
int buffersToDrop = Math.min(buffers.size() * 2 / 3, buffers.size() - 1);
for (int i = 0; i < buffersToDrop; ++i) {
((SectionBufferBuilderPack)buffers.remove(buffers.size() - 1)).close();
}
}
return new SectionBufferBuilderPool(buffers);
}
public @Nullable SectionBufferBuilderPack acquire() {
SectionBufferBuilderPack buffer = this.freeBuffers.poll();
if (buffer != null) {
this.freeBufferCount = this.freeBuffers.size();
return buffer;
}
return null;
}
public void release(SectionBufferBuilderPack buffer) {
this.freeBuffers.add(buffer);
this.freeBufferCount = this.freeBuffers.size();
}
public boolean isEmpty() {
return this.freeBuffers.isEmpty();
}
public int getFreeBufferCount() {
return this.freeBufferCount;
}
}