0.2.0-dev Download ZIP
177 lines ·7.2 KB Raw
| 1 | diff --git a/thirdparty/offset_allocator/offsetAllocator.hpp b/thirdparty/offset_allocator/offsetAllocator.hpp |
| 2 | --- a/thirdparty/offset_allocator/offsetAllocator.hpp |
| 3 | +++ b/thirdparty/offset_allocator/offsetAllocator.hpp |
| 4 | @@ -10,7 +10,7 @@ namespace OffsetAllocator |
| 5 | typedef unsigned int uint32; |
| 6 | |
| 7 | // 16 bit offsets mode will halve the metadata storage cost |
| 8 | - // But it only supports up to 65536 maximum allocation count |
| 9 | + // But it only supports up to 65534 maximum allocation count |
| 10 | #ifdef USE_16_BIT_NODE_INDICES |
| 11 | typedef uint16 NodeIndex; |
| 12 | #else |
| 13 | @@ -26,9 +26,10 @@ namespace OffsetAllocator |
| 14 | struct Allocation |
| 15 | { |
| 16 | static constexpr uint32 NO_SPACE = 0xffffffff; |
| 17 | + static constexpr NodeIndex NO_SPACE_METADATA = static_cast<NodeIndex>(-1); |
| 18 | |
| 19 | uint32 offset = NO_SPACE; |
| 20 | - NodeIndex metadata = NO_SPACE; // internal: node index |
| 21 | + NodeIndex metadata = NO_SPACE_METADATA; // internal: node index |
| 22 | }; |
| 23 | |
| 24 | struct StorageReport |
| 25 | @@ -64,12 +65,12 @@ namespace OffsetAllocator |
| 26 | StorageReportFull storageReportFull() const; |
| 27 | |
| 28 | private: |
| 29 | - uint32 insertNodeIntoBin(uint32 size, uint32 dataOffset); |
| 30 | - void removeNodeFromBin(uint32 nodeIndex); |
| 31 | + NodeIndex insertNodeIntoBin(uint32 size, uint32 dataOffset); |
| 32 | + void removeNodeFromBin(NodeIndex nodeIndex); |
| 33 | |
| 34 | struct Node |
| 35 | { |
| 36 | - static constexpr NodeIndex unused = 0xffffffff; |
| 37 | + static constexpr NodeIndex unused = static_cast<NodeIndex>(-1); |
| 38 | |
| 39 | uint32 dataOffset = 0; |
| 40 | uint32 dataSize = 0; |
| 41 | diff --git a/thirdparty/offset_allocator/offsetAllocator.cpp b/thirdparty/offset_allocator/offsetAllocator.cpp |
| 42 | --- a/thirdparty/offset_allocator/offsetAllocator.cpp |
| 43 | +++ b/thirdparty/offset_allocator/offsetAllocator.cpp |
| 44 | @@ -142,7 +142,7 @@ namespace OffsetAllocator |
| 45 | { |
| 46 | if (sizeof(NodeIndex) == 2) |
| 47 | { |
| 48 | - ASSERT(maxAllocs <= 65536); |
| 49 | + ASSERT(maxAllocs <= static_cast<uint32>(Node::unused) - 1); |
| 50 | } |
| 51 | reset(); |
| 52 | } |
| 53 | @@ -187,7 +187,7 @@ namespace OffsetAllocator |
| 54 | // Freelist is a stack. Nodes in inverse order so that [0] pops first. |
| 55 | for (uint32 i = 0; i < m_maxAllocs + 1; i++) |
| 56 | { |
| 57 | - m_freeNodes[i] = m_maxAllocs - i; |
| 58 | + m_freeNodes[i] = static_cast<NodeIndex>(m_maxAllocs - i); |
| 59 | } |
| 60 | |
| 61 | // Start state: Whole storage as one big node |
| 62 | @@ -206,7 +206,7 @@ namespace OffsetAllocator |
| 63 | // Out of allocations? |
| 64 | if (m_freeOffset == Allocation::NO_SPACE) |
| 65 | { |
| 66 | - return {.offset = Allocation::NO_SPACE, .metadata = Allocation::NO_SPACE}; |
| 67 | + return {.offset = Allocation::NO_SPACE, .metadata = Allocation::NO_SPACE_METADATA}; |
| 68 | } |
| 69 | |
| 70 | // Round up to bin index to ensure that alloc >= bin |
| 71 | @@ -233,7 +233,7 @@ namespace OffsetAllocator |
| 72 | // Out of space? |
| 73 | if (topBinIndex == Allocation::NO_SPACE) |
| 74 | { |
| 75 | - return {.offset = Allocation::NO_SPACE, .metadata = Allocation::NO_SPACE}; |
| 76 | + return {.offset = Allocation::NO_SPACE, .metadata = Allocation::NO_SPACE_METADATA}; |
| 77 | } |
| 78 | |
| 79 | // All leaf bins here fit the alloc, since the top bin was rounded up. Start leaf search from bit 0. |
| 80 | @@ -244,7 +244,7 @@ namespace OffsetAllocator |
| 81 | uint32 binIndex = (topBinIndex << TOP_BINS_INDEX_SHIFT) | leafBinIndex; |
| 82 | |
| 83 | // Pop the top node of the bin. Bin top = node.next. |
| 84 | - uint32 nodeIndex = m_binIndices[binIndex]; |
| 85 | + NodeIndex nodeIndex = m_binIndices[binIndex]; |
| 86 | Node& node = m_nodes[nodeIndex]; |
| 87 | uint32 nodeTotalSize = node.dataSize; |
| 88 | node.dataSize = size; |
| 89 | @@ -274,7 +274,7 @@ namespace OffsetAllocator |
| 90 | uint32 reminderSize = nodeTotalSize - size; |
| 91 | if (reminderSize > 0) |
| 92 | { |
| 93 | - uint32 newNodeIndex = insertNodeIntoBin(reminderSize, node.dataOffset + size); |
| 94 | + NodeIndex newNodeIndex = insertNodeIntoBin(reminderSize, node.dataOffset + size); |
| 95 | |
| 96 | // Link nodes next to each other so that we can merge them later if both are free |
| 97 | // And update the old next neighbor to point to the new node (in middle) |
| 98 | @@ -289,10 +289,10 @@ namespace OffsetAllocator |
| 99 | |
| 100 | void Allocator::free(Allocation allocation) |
| 101 | { |
| 102 | - ASSERT(allocation.metadata != Allocation::NO_SPACE); |
| 103 | + ASSERT(allocation.metadata != Allocation::NO_SPACE_METADATA); |
| 104 | if (!m_nodes) return; |
| 105 | |
| 106 | - uint32 nodeIndex = allocation.metadata; |
| 107 | + NodeIndex nodeIndex = allocation.metadata; |
| 108 | Node& node = m_nodes[nodeIndex]; |
| 109 | |
| 110 | // Double delete check |
| 111 | @@ -329,8 +329,8 @@ namespace OffsetAllocator |
| 112 | node.neighborNext = nextNode.neighborNext; |
| 113 | } |
| 114 | |
| 115 | - uint32 neighborNext = node.neighborNext; |
| 116 | - uint32 neighborPrev = node.neighborPrev; |
| 117 | + NodeIndex neighborNext = node.neighborNext; |
| 118 | + NodeIndex neighborPrev = node.neighborPrev; |
| 119 | |
| 120 | // Insert the removed node to freelist |
| 121 | #ifdef DEBUG_VERBOSE |
| 122 | @@ -339,7 +339,7 @@ namespace OffsetAllocator |
| 123 | m_freeNodes[++m_freeOffset] = nodeIndex; |
| 124 | |
| 125 | // Insert the (combined) free node to bin |
| 126 | - uint32 combinedNodeIndex = insertNodeIntoBin(size, offset); |
| 127 | + NodeIndex combinedNodeIndex = insertNodeIntoBin(size, offset); |
| 128 | |
| 129 | // Connect neighbors with the new combined node |
| 130 | if (neighborNext != Node::unused) |
| 131 | @@ -354,7 +354,7 @@ namespace OffsetAllocator |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | - uint32 Allocator::insertNodeIntoBin(uint32 size, uint32 dataOffset) |
| 136 | + NodeIndex Allocator::insertNodeIntoBin(uint32 size, uint32 dataOffset) |
| 137 | { |
| 138 | // Round down to bin index to ensure that bin >= alloc |
| 139 | uint32 binIndex = SmallFloat::uintToFloatRoundDown(size); |
| 140 | @@ -371,8 +371,8 @@ namespace OffsetAllocator |
| 141 | } |
| 142 | |
| 143 | // Take a freelist node and insert on top of the bin linked list (next = old top) |
| 144 | - uint32 topNodeIndex = m_binIndices[binIndex]; |
| 145 | - uint32 nodeIndex = m_freeNodes[m_freeOffset--]; |
| 146 | + NodeIndex topNodeIndex = m_binIndices[binIndex]; |
| 147 | + NodeIndex nodeIndex = m_freeNodes[m_freeOffset--]; |
| 148 | #ifdef DEBUG_VERBOSE |
| 149 | printf("Getting node %u from freelist[%u]\n", nodeIndex, m_freeOffset + 1); |
| 150 | #endif |
| 151 | @@ -388,7 +388,7 @@ namespace OffsetAllocator |
| 152 | return nodeIndex; |
| 153 | } |
| 154 | |
| 155 | - void Allocator::removeNodeFromBin(uint32 nodeIndex) |
| 156 | + void Allocator::removeNodeFromBin(NodeIndex nodeIndex) |
| 157 | { |
| 158 | Node &node = m_nodes[nodeIndex]; |
| 159 | |
| 160 | @@ -440,7 +440,7 @@ namespace OffsetAllocator |
| 161 | |
| 162 | uint32 Allocator::allocationSize(Allocation allocation) const |
| 163 | { |
| 164 | - if (allocation.metadata == Allocation::NO_SPACE) return 0; |
| 165 | + if (allocation.metadata == Allocation::NO_SPACE_METADATA) return 0; |
| 166 | if (!m_nodes) return 0; |
| 167 | |
| 168 | return m_nodes[allocation.metadata].dataSize; |
| 169 | @@ -473,7 +473,7 @@ namespace OffsetAllocator |
| 170 | for (uint32 i = 0; i < NUM_LEAF_BINS; i++) |
| 171 | { |
| 172 | uint32 count = 0; |
| 173 | - uint32 nodeIndex = m_binIndices[i]; |
| 174 | + NodeIndex nodeIndex = m_binIndices[i]; |
| 175 | while (nodeIndex != Node::unused) |
| 176 | { |
| 177 | nodeIndex = m_nodes[nodeIndex].binListNext; |