Skip to content
0.2.0-dev Download ZIP
93 lines ·4.1 KB Raw
1diff --git a/thirdparty/offset_allocator/offsetAllocator.cpp b/thirdparty/offset_allocator/offsetAllocator.cpp
2--- a/thirdparty/offset_allocator/offsetAllocator.cpp
3+++ b/thirdparty/offset_allocator/offsetAllocator.cpp
4@@ -73,7 +73,7 @@ namespace OffsetAllocator
5 exp = mantissaStartBit + 1;
6 mantissa = (size >> mantissaStartBit) & MANTISSA_MASK;
7
8- uint32 lowBitsMask = (1 << mantissaStartBit) - 1;
9+ uint32 lowBitsMask = (uint32(1) << mantissaStartBit) - 1;
10
11 // Round up!
12 if ((size & lowBitsMask) != 0)
13@@ -126,7 +126,8 @@ namespace OffsetAllocator
14 // Utility functions
15 uint32 findLowestSetBitAfter(uint32 bitMask, uint32 startBitIndex)
16 {
17- uint32 maskBeforeStartIndex = (1 << startBitIndex) - 1;
18+ if (startBitIndex >= 32) return Allocation::NO_SPACE;
19+ uint32 maskBeforeStartIndex = (uint32(1) << startBitIndex) - 1;
20 uint32 maskAfterStartIndex = ~maskBeforeStartIndex;
21 uint32 bitsAfter = bitMask & maskAfterStartIndex;
22 if (bitsAfter == 0) return Allocation::NO_SPACE;
23@@ -212,6 +213,7 @@ namespace OffsetAllocator
24 // Round up to bin index to ensure that alloc >= bin
25 // Gives us min bin index that fits the size
26 uint32 minBinIndex = SmallFloat::uintToFloatRoundUp(size);
27+ if (minBinIndex == 240) minBinIndex = 239; // 4 GiB bin overflows uint32
28
29 uint32 minTopBinIndex = minBinIndex >> TOP_BINS_INDEX_SHIFT;
30 uint32 minLeafBinIndex = minBinIndex & LEAF_BINS_INDEX_MASK;
31@@ -220,7 +222,7 @@ namespace OffsetAllocator
32 uint32 leafBinIndex = Allocation::NO_SPACE;
33
34 // If top bin exists, scan its leaf bin. This can fail (NO_SPACE).
35- if (m_usedBinsTop & (1 << topBinIndex))
36+ if (m_usedBinsTop & (uint32(1) << topBinIndex))
37 {
38 leafBinIndex = findLowestSetBitAfter(m_usedBins[topBinIndex], minLeafBinIndex);
39 }
40@@ -247,6 +249,10 @@ namespace OffsetAllocator
41 NodeIndex nodeIndex = m_binIndices[binIndex];
42 Node& node = m_nodes[nodeIndex];
43 uint32 nodeTotalSize = node.dataSize;
44+ if (nodeTotalSize < size)
45+ {
46+ return {.offset = Allocation::NO_SPACE, .metadata = Allocation::NO_SPACE_METADATA};
47+ }
48 node.dataSize = size;
49 node.used = true;
50 m_binIndices[binIndex] = node.binListNext;
51@@ -260,13 +266,13 @@ namespace OffsetAllocator
52 if (m_binIndices[binIndex] == Node::unused)
53 {
54 // Remove a leaf bin mask bit
55- m_usedBins[topBinIndex] &= ~(1 << leafBinIndex);
56+ m_usedBins[topBinIndex] &= ~(uint32(1) << leafBinIndex);
57
58 // All leaf bins empty?
59 if (m_usedBins[topBinIndex] == 0)
60 {
61 // Remove a top bin mask bit
62- m_usedBinsTop &= ~(1 << topBinIndex);
63+ m_usedBinsTop &= ~(uint32(1) << topBinIndex);
64 }
65 }
66
67@@ -366,8 +372,8 @@ namespace OffsetAllocator
68 if (m_binIndices[binIndex] == Node::unused)
69 {
70 // Set bin mask bits
71- m_usedBins[topBinIndex] |= 1 << leafBinIndex;
72- m_usedBinsTop |= 1 << topBinIndex;
73+ m_usedBins[topBinIndex] |= uint32(1) << leafBinIndex;
74+ m_usedBinsTop |= uint32(1) << topBinIndex;
75 }
76
77 // Take a freelist node and insert on top of the bin linked list (next = old top)
78@@ -415,13 +421,13 @@ namespace OffsetAllocator
79 if (m_binIndices[binIndex] == Node::unused)
80 {
81 // Remove a leaf bin mask bit
82- m_usedBins[topBinIndex] &= ~(1 << leafBinIndex);
83+ m_usedBins[topBinIndex] &= ~(uint32(1) << leafBinIndex);
84
85 // All leaf bins empty?
86 if (m_usedBins[topBinIndex] == 0)
87 {
88 // Remove a top bin mask bit
89- m_usedBinsTop &= ~(1 << topBinIndex);
90+ m_usedBinsTop &= ~(uint32(1) << topBinIndex);
91 }
92 }
93 }