| 1 | package com.mojang.blaze3d.vertex;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.util.Mth;
|
| 7 | import net.minecraft.util.VisibleForDebug;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 | import org.slf4j.Logger;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public class TlsfAllocator {
|
| 13 | private static final int SECOND_LEVEL_BIN_LOG2 = 3;
|
| 14 | private static final int SECOND_LEVEL_BIN_COUNT = 8;
|
| 15 | private static final int FIRST_LEVEL_INDEX_SHIFT = 8;
|
| 16 | private static final int FIRST_LEVEL_BIN_COUNT = 32;
|
| 17 | private static final int SMALL_BLOCK_SIZE = 256;
|
| 18 | private static final long MAX_ALLOCATION_SIZE = 549755813888L;
|
| 19 | private static final int ALIGN_SIZE = 32;
|
| 20 | private int firstLevelBitmap = 0;
|
| 21 | private final int[] secondLevelBitmap = new int[32];
|
| 22 | private final TlsfAllocator.@Nullable Block[] freeLists = new TlsfAllocator.Block[256];
|
| 23 | private final long totalMemorySize;
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 |
|
| 26 | public TlsfAllocator(final TlsfAllocator.Heap heap) {
|
| 27 | long alignedHeapSize = alignDown(heap.size, 32L);
|
| 28 | TlsfAllocator.Block freeBlock = new TlsfAllocator.Block(alignedHeapSize, heap, 0L, null, null, null, null);
|
| 29 | freeBlock.setFree();
|
| 30 | this.insertFreeBlock(freeBlock);
|
| 31 | long remainingHeapSize = heap.size - alignedHeapSize;
|
| 32 | TlsfAllocator.Block sentinelBlock = new TlsfAllocator.Block(remainingHeapSize, heap, alignedHeapSize, null, null, null, freeBlock);
|
| 33 | sentinelBlock.setUsed();
|
| 34 | freeBlock.nextPhysicalBlock = sentinelBlock;
|
| 35 | this.totalMemorySize = alignedHeapSize;
|
| 36 | }
|
| 37 |
|
| 38 | private TlsfAllocator.@Nullable Block getBlockFromFreeList(final int firstLevelIndex, final int secondLevelIndex) {
|
| 39 | return this.freeLists[firstLevelIndex * 8 + secondLevelIndex];
|
| 40 | }
|
| 41 |
|
| 42 | private void setBlockFreeList(final int firstLevelIndex, final int secondLevelIndex, final TlsfAllocator.@Nullable Block block) {
|
| 43 | this.freeLists[firstLevelIndex * 8 + secondLevelIndex] = block;
|
| 44 | }
|
| 45 |
|
| 46 | private static long alignUp(final long x, final long align) {
|
| 47 | return x + (align - 1L) & ~(align - 1L);
|
| 48 | }
|
| 49 |
|
| 50 | private static long alignDown(final long x, final long align) {
|
| 51 | return x - (x & align - 1L);
|
| 52 | }
|
| 53 |
|
| 54 | private static int findLastSignificantBit(final long x) {
|
| 55 | return 63 - Long.numberOfLeadingZeros(x);
|
| 56 | }
|
| 57 |
|
| 58 | private static int findFirstSignificantBit(final int x) {
|
| 59 | return Integer.numberOfTrailingZeros(x);
|
| 60 | }
|
| 61 |
|
| 62 | private TlsfAllocator.IndexPair getLevelIndex(final long size) {
|
| 63 | if (Long.compareUnsigned(size, 256L) < 0) {
|
| 64 | int firstLevelIndex = 0;
|
| 65 | int secondLevelIndex = (int)Long.divideUnsigned(size, 32L);
|
| 66 | return new TlsfAllocator.IndexPair(0, secondLevelIndex);
|
| 67 | } else {
|
| 68 | int firstLevelIndex = findLastSignificantBit(size);
|
| 69 | int secondLevelIndex = (int)(size >>> firstLevelIndex - 3) ^ 8;
|
| 70 | firstLevelIndex -= 7;
|
| 71 | return new TlsfAllocator.IndexPair(firstLevelIndex, secondLevelIndex);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | private TlsfAllocator.IndexPair mappingSearch(final long size) {
|
| 76 | long roundedSize = size;
|
| 77 | if (Long.compareUnsigned(size, 256L) >= 0) {
|
| 78 | long round = (1L << findLastSignificantBit(size) - 3) - 1L;
|
| 79 | roundedSize += round;
|
| 80 | }
|
| 81 |
|
| 82 | return this.getLevelIndex(roundedSize);
|
| 83 | }
|
| 84 |
|
| 85 | private void insertFreeBlock(final TlsfAllocator.Block block) {
|
| 86 | TlsfAllocator.IndexPair levelIndex = this.getLevelIndex(block.getSize());
|
| 87 | TlsfAllocator.Block currentBlock = this.getBlockFromFreeList(levelIndex.firstLevelIndex, levelIndex.secondLevelIndex);
|
| 88 | if (currentBlock != null) {
|
| 89 | currentBlock.previousFreeBlock = block;
|
| 90 | }
|
| 91 |
|
| 92 | block.nextFreeBlock = currentBlock;
|
| 93 | this.firstLevelBitmap = this.firstLevelBitmap | 1 << levelIndex.firstLevelIndex;
|
| 94 | this.secondLevelBitmap[levelIndex.firstLevelIndex] = this.secondLevelBitmap[levelIndex.firstLevelIndex] | 1 << levelIndex.secondLevelIndex;
|
| 95 | this.setBlockFreeList(levelIndex.firstLevelIndex, levelIndex.secondLevelIndex, block);
|
| 96 | }
|
| 97 |
|
| 98 | private void removeFreeBlock(final TlsfAllocator.Block block, final int firstLevel, final int secondLevel) {
|
| 99 | TlsfAllocator.Block next = block.nextFreeBlock;
|
| 100 | TlsfAllocator.Block previous = block.previousFreeBlock;
|
| 101 | if (previous != null) {
|
| 102 | previous.nextFreeBlock = next;
|
| 103 | }
|
| 104 |
|
| 105 | if (next != null) {
|
| 106 | next.previousFreeBlock = previous;
|
| 107 | }
|
| 108 |
|
| 109 | if (this.getBlockFromFreeList(firstLevel, secondLevel) == block) {
|
| 110 | this.setBlockFreeList(firstLevel, secondLevel, next);
|
| 111 | if (next == null) {
|
| 112 | this.secondLevelBitmap[firstLevel] = this.secondLevelBitmap[firstLevel] & ~(1 << secondLevel);
|
| 113 | if (this.secondLevelBitmap[firstLevel] == 0) {
|
| 114 | this.firstLevelBitmap &= ~(1 << firstLevel);
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | private void trimBlock(final TlsfAllocator.Block block, final long size) {
|
| 121 | if (Long.compareUnsigned(block.getSize(), size + 256L) > 0) {
|
| 122 | long remaining = block.getSize() - size;
|
| 123 | TlsfAllocator.Block remainingBlock = new TlsfAllocator.Block(remaining, block.heap, block.offsetFromHeap + size, null, null, block.nextPhysicalBlock, block);
|
| 124 | remainingBlock.setFree();
|
| 125 | TlsfAllocator.Block next = block.nextPhysicalBlock;
|
| 126 | if (next != null) {
|
| 127 | assert next.previousPhysicalBlock == block;
|
| 128 | next.previousPhysicalBlock = remainingBlock;
|
| 129 | }
|
| 130 |
|
| 131 | block.nextPhysicalBlock = remainingBlock;
|
| 132 | block.setSize(size);
|
| 133 | this.mergeBlockWithNext(remainingBlock);
|
| 134 | this.insertFreeBlock(remainingBlock);
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | public TlsfAllocator.@Nullable Allocation allocate(final long size, final int align) {
|
| 139 | boolean isPowerOfTwo = Mth.isPowerOfTwo(align);
|
| 140 | int sizePadding = isPowerOfTwo && align <= 32 ? 0 : align;
|
| 141 | long alignedSize = alignUp(size + sizePadding, 32L);
|
| 142 | assert alignedSize <= 549755813888L;
|
| 143 | TlsfAllocator.IndexPair levelIndex = this.mappingSearch(alignedSize);
|
| 144 | int firstLevelIndex = levelIndex.firstLevelIndex;
|
| 145 | int secondLevelIndex = levelIndex.secondLevelIndex;
|
| 146 | long firstLevelSize = 1L << firstLevelIndex + 8 - 1;
|
| 147 | long secondLevelInterval = firstLevelSize / 8L;
|
| 148 | long slotSize = firstLevelSize + secondLevelIndex * secondLevelInterval;
|
| 149 | if (firstLevelIndex < 32) {
|
| 150 | int slBitmap = this.secondLevelBitmap[firstLevelIndex] & -1 << secondLevelIndex;
|
| 151 | if (slBitmap == 0) {
|
| 152 | int flBitmap = this.firstLevelBitmap & -1 << firstLevelIndex + 1;
|
| 153 | if (flBitmap == 0) {
|
| 154 | return null;
|
| 155 | }
|
| 156 |
|
| 157 | firstLevelIndex = findFirstSignificantBit(flBitmap);
|
| 158 | slBitmap = this.secondLevelBitmap[firstLevelIndex];
|
| 159 | }
|
| 160 |
|
| 161 | secondLevelIndex = findFirstSignificantBit(slBitmap);
|
| 162 | TlsfAllocator.Block block = this.getBlockFromFreeList(firstLevelIndex, secondLevelIndex);
|
| 163 | assert block != null && block.getSize() >= alignedSize;
|
| 164 | this.removeFreeBlock(block, firstLevelIndex, secondLevelIndex);
|
| 165 | this.trimBlock(block, slotSize);
|
| 166 | block.setUsed();
|
| 167 | long gap = Long.remainderUnsigned(block.offsetFromHeap, align);
|
| 168 | long alignmentOffset = gap == 0L ? 0L : align - gap;
|
| 169 | long allocationOffset = block.offsetFromHeap + alignmentOffset;
|
| 170 | return new TlsfAllocator.Allocation(block, allocationOffset);
|
| 171 | } else {
|
| 172 | return null;
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | private void mergeBlockWithPrevious(final TlsfAllocator.Block block) {
|
| 177 | TlsfAllocator.Block previous = block.previousPhysicalBlock;
|
| 178 | if (previous != null && previous.isFree()) {
|
| 179 | assert previous.offsetFromHeap + previous.getSize() == block.offsetFromHeap;
|
| 180 | assert previous.nextPhysicalBlock == block;
|
| 181 | TlsfAllocator.IndexPair levelIndex = this.getLevelIndex(previous.getSize());
|
| 182 | this.removeFreeBlock(previous, levelIndex.firstLevelIndex, levelIndex.secondLevelIndex);
|
| 183 | TlsfAllocator.Block prevprev = previous.previousPhysicalBlock;
|
| 184 | if (prevprev != null) {
|
| 185 | assert prevprev.nextPhysicalBlock == previous;
|
| 186 | prevprev.nextPhysicalBlock = block;
|
| 187 | }
|
| 188 |
|
| 189 | block.previousPhysicalBlock = prevprev;
|
| 190 | block.setSize(block.getSize() + previous.getSize());
|
| 191 | block.offsetFromHeap = previous.offsetFromHeap;
|
| 192 | previous.previousPhysicalBlock = null;
|
| 193 | previous.nextPhysicalBlock = null;
|
| 194 | previous.nextFreeBlock = null;
|
| 195 | previous.previousFreeBlock = null;
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | private void mergeBlockWithNext(final TlsfAllocator.Block block) {
|
| 200 | TlsfAllocator.Block next = block.nextPhysicalBlock;
|
| 201 | if (next != null && next.isFree()) {
|
| 202 | assert next.offsetFromHeap - block.getSize() == block.offsetFromHeap;
|
| 203 | assert next.previousPhysicalBlock == block;
|
| 204 | TlsfAllocator.IndexPair levelIndex = this.getLevelIndex(next.getSize());
|
| 205 | this.removeFreeBlock(next, levelIndex.firstLevelIndex, levelIndex.secondLevelIndex);
|
| 206 | TlsfAllocator.Block nextnext = next.nextPhysicalBlock;
|
| 207 | if (nextnext != null) {
|
| 208 | assert nextnext.previousPhysicalBlock == next;
|
| 209 | nextnext.previousPhysicalBlock = block;
|
| 210 | }
|
| 211 |
|
| 212 | block.nextPhysicalBlock = nextnext;
|
| 213 | block.setSize(block.getSize() + next.getSize());
|
| 214 | next.previousPhysicalBlock = null;
|
| 215 | next.nextPhysicalBlock = null;
|
| 216 | next.nextFreeBlock = null;
|
| 217 | next.previousFreeBlock = null;
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | public void free(final TlsfAllocator.Allocation allocation) {
|
| 222 | if (!allocation.freed) {
|
| 223 | TlsfAllocator.Block block = allocation.block;
|
| 224 | block.setFree();
|
| 225 | this.mergeBlockWithPrevious(block);
|
| 226 | this.mergeBlockWithNext(block);
|
| 227 | this.insertFreeBlock(block);
|
| 228 | allocation.freed = true;
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | public boolean isCompletelyFree() {
|
| 233 | if (Long.bitCount(this.firstLevelBitmap) == 1) {
|
| 234 | int firstLevelIndex = findFirstSignificantBit(this.firstLevelBitmap);
|
| 235 | int slBitmap = this.secondLevelBitmap[firstLevelIndex];
|
| 236 | if (Long.bitCount(slBitmap) == 1) {
|
| 237 | int secondLevelIndex = findFirstSignificantBit(slBitmap);
|
| 238 | TlsfAllocator.Block freeBlock = this.getBlockFromFreeList(firstLevelIndex, secondLevelIndex);
|
| 239 | return freeBlock != null && freeBlock.getSize() == this.totalMemorySize;
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | return false;
|
| 244 | }
|
| 245 |
|
| 246 | @VisibleForDebug
|
| 247 | public void printAllocatorStatistics(final String name) {
|
| 248 | int freeBlockCount = 0;
|
| 249 | long freeMemorySize = 0L;
|
| 250 | int levelFreeBlockCount = 0;
|
| 251 |
|
| 252 | for (int i = 0; i < 32; i++) {
|
| 253 | for (int j = 0; j < 8; j++) {
|
| 254 | levelFreeBlockCount = 0;
|
| 255 |
|
| 256 | for (TlsfAllocator.Block block = this.getBlockFromFreeList(i, j); block != null; block = block.nextFreeBlock) {
|
| 257 | levelFreeBlockCount++;
|
| 258 | freeMemorySize += block.getSize();
|
| 259 | }
|
| 260 |
|
| 261 | freeBlockCount += levelFreeBlockCount;
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | double unusedPercent = (double)freeMemorySize / this.totalMemorySize * 100.0;
|
| 266 | LOGGER.debug("Uber buffer {}, size: {} -- free-listed memory: {} -- free-block count:{}", name, this.totalMemorySize, unusedPercent, freeBlockCount);
|
| 267 | }
|
| 268 |
|
| 269 | @Environment(EnvType.CLIENT)
|
| 270 | public static class Allocation {
|
| 271 | private final TlsfAllocator.Block block;
|
| 272 | private final long offsetFromHeap;
|
| 273 | private boolean freed = false;
|
| 274 |
|
| 275 | private Allocation(final TlsfAllocator.Block block, final long offsetFromHeap) {
|
| 276 | this.block = block;
|
| 277 | this.offsetFromHeap = offsetFromHeap;
|
| 278 | }
|
| 279 |
|
| 280 | public long getSize() {
|
| 281 | return this.block.getSize();
|
| 282 | }
|
| 283 |
|
| 284 | public TlsfAllocator.Heap getHeap() {
|
| 285 | return this.block.heap;
|
| 286 | }
|
| 287 |
|
| 288 | public long getOffsetFromHeap() {
|
| 289 | return this.offsetFromHeap;
|
| 290 | }
|
| 291 |
|
| 292 | public boolean isFreed() {
|
| 293 | return this.freed;
|
| 294 | }
|
| 295 | }
|
| 296 |
|
| 297 | @Environment(EnvType.CLIENT)
|
| 298 | private static class Block {
|
| 299 | private long size = 0L;
|
| 300 | public final TlsfAllocator.Heap heap;
|
| 301 | public long offsetFromHeap;
|
| 302 | public TlsfAllocator.@Nullable Block nextFreeBlock;
|
| 303 | public TlsfAllocator.@Nullable Block previousFreeBlock;
|
| 304 | public TlsfAllocator.@Nullable Block nextPhysicalBlock;
|
| 305 | public TlsfAllocator.@Nullable Block previousPhysicalBlock;
|
| 306 | private static final int BLOCK_HEADER_FREE_BIT = 1;
|
| 307 |
|
| 308 | private Block(
|
| 309 | final long size,
|
| 310 | final TlsfAllocator.Heap heap,
|
| 311 | final long offsetFromHeap,
|
| 312 | final TlsfAllocator.@Nullable Block nextFreeBlock,
|
| 313 | final TlsfAllocator.@Nullable Block previousFreeBlock,
|
| 314 | final TlsfAllocator.@Nullable Block nextPhysicalBlock,
|
| 315 | final TlsfAllocator.@Nullable Block previousPhysicalBlock
|
| 316 | ) {
|
| 317 | this.heap = heap;
|
| 318 | this.offsetFromHeap = offsetFromHeap;
|
| 319 | this.nextFreeBlock = nextFreeBlock;
|
| 320 | this.previousFreeBlock = previousFreeBlock;
|
| 321 | this.nextPhysicalBlock = nextPhysicalBlock;
|
| 322 | this.previousPhysicalBlock = previousPhysicalBlock;
|
| 323 | this.setSize(size);
|
| 324 | }
|
| 325 |
|
| 326 | private boolean isFree() {
|
| 327 | return (this.size & 1L) == 1L;
|
| 328 | }
|
| 329 |
|
| 330 | private void setFree() {
|
| 331 | this.size |= 1L;
|
| 332 | }
|
| 333 |
|
| 334 | private void setUsed() {
|
| 335 | this.size &= -2L;
|
| 336 | }
|
| 337 |
|
| 338 | private long getSize() {
|
| 339 | return this.size & -2L;
|
| 340 | }
|
| 341 |
|
| 342 | private void setSize(final long size) {
|
| 343 | long oldSize = this.size;
|
| 344 | this.size = size | oldSize & 1L;
|
| 345 | }
|
| 346 | }
|
| 347 |
|
| 348 | @Environment(EnvType.CLIENT)
|
| 349 | public static class Heap {
|
| 350 | private final long size;
|
| 351 |
|
| 352 | public Heap(final long size) {
|
| 353 | this.size = size;
|
| 354 | }
|
| 355 | }
|
| 356 |
|
| 357 | @Environment(EnvType.CLIENT)
|
| 358 | private record IndexPair(int firstLevelIndex, int secondLevelIndex) {
|
| 359 | }
|
| 360 | }
|
| 361 |
|