File size: 2,295 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 javax.annotation.Nullable;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.slf4j.Logger;

@OnlyIn(Dist.CLIENT)
public class SectionBufferBuilderPool {
    private static final Logger LOGGER = LogUtils.getLogger();
    private final Queue<SectionBufferBuilderPack> freeBuffers;
    private volatile int freeBufferCount;

    private SectionBufferBuilderPool(List<SectionBufferBuilderPack> p_312374_) {
        this.freeBuffers = Queues.newArrayDeque(p_312374_);
        this.freeBufferCount = this.freeBuffers.size();
    }

    public static SectionBufferBuilderPool allocate(int p_310783_) {
        int i = Math.max(1, (int)((double)Runtime.getRuntime().maxMemory() * 0.3) / SectionBufferBuilderPack.TOTAL_BUFFERS_SIZE);
        int j = Math.max(1, Math.min(p_310783_, i));
        List<SectionBufferBuilderPack> list = new ArrayList<>(j);

        try {
            for (int k = 0; k < j; k++) {
                list.add(new SectionBufferBuilderPack());
            }
        } catch (OutOfMemoryError outofmemoryerror) {
            LOGGER.warn("Allocated only {}/{} buffers", list.size(), j);
            int l = Math.min(list.size() * 2 / 3, list.size() - 1);

            for (int i1 = 0; i1 < l; i1++) {
                list.remove(list.size() - 1).close();
            }
        }

        return new SectionBufferBuilderPool(list);
    }

    @Nullable
    public SectionBufferBuilderPack acquire() {
        SectionBufferBuilderPack sectionbufferbuilderpack = this.freeBuffers.poll();
        if (sectionbufferbuilderpack != null) {
            this.freeBufferCount = this.freeBuffers.size();
            return sectionbufferbuilderpack;
        } else {
            return null;
        }
    }

    public void release(SectionBufferBuilderPack p_310220_) {
        this.freeBuffers.add(p_310220_);
        this.freeBufferCount = this.freeBuffers.size();
    }

    public boolean isEmpty() {
        return this.freeBuffers.isEmpty();
    }

    public int getFreeBufferCount() {
        return this.freeBufferCount;
    }
}