22 constexpr
size_t wordSize =
sizeof(size_t) * 8;
29 return m_MemBlockStrategyType;
32 struct SingleAxisPriorityList::BinTracker
36 BinTracker(
unsigned int maxLifeTime)
37 : m_OccupiedSpaces(1 + maxLifeTime/
wordSize, 0)
41 void AddBlock(MemBlock* block,
const size_t word,
const size_t index)
43 m_OccupiedSpaces[index] = m_OccupiedSpaces[index] | word;
45 m_PlacedBlocks.push_back(block);
46 m_MemSize = std::max(m_MemSize, block->m_MemSize);
50 void AddBlock(MemBlock* block,
51 const size_t startIndex,
52 const size_t endIndex,
53 const size_t firstWord,
54 const size_t lastWord)
56 m_OccupiedSpaces[startIndex] = m_OccupiedSpaces[startIndex] | firstWord;
57 m_OccupiedSpaces[endIndex] = m_OccupiedSpaces[endIndex] | lastWord;
59 for (
size_t i = startIndex +1; i <= endIndex -1; ++i)
61 m_OccupiedSpaces[i] = std::numeric_limits<size_t>::max();
64 m_PlacedBlocks.push_back(block);
65 m_MemSize = std::max(m_MemSize, block->m_MemSize);
69 std::vector<size_t> m_OccupiedSpaces;
70 std::vector<MemBlock*> m_PlacedBlocks;
73 void SingleAxisPriorityList::PlaceBlocks(
const std::list<MemBlock*>& priorityList,
74 std::vector<BinTracker>& placedBlocks,
75 const unsigned int maxLifetime)
78 auto singleWordLoop = [&](MemBlock* curBlock,
const size_t firstWord,
const size_t index)
82 for (
auto& blockList : placedBlocks)
85 if ((blockList.m_OccupiedSpaces[index] & firstWord) == 0)
88 blockList.AddBlock(curBlock, firstWord, index);
96 placedBlocks.emplace_back(BinTracker{maxLifetime});
97 placedBlocks.back().AddBlock(curBlock, firstWord, index);
102 auto doubleWordLoop =[&](MemBlock* curBlock,
103 const size_t firstWord,
104 const size_t firstIndex,
105 const size_t lastWord,
106 const size_t lastIndex)
109 for (
auto& blockList : placedBlocks)
112 if ((blockList.m_OccupiedSpaces[firstIndex] & firstWord) == 0 &&
113 (blockList.m_OccupiedSpaces[lastIndex] & lastWord) == 0)
115 blockList.AddBlock(curBlock, firstIndex, lastIndex, firstWord, lastWord);
123 placedBlocks.emplace_back(BinTracker{maxLifetime});
124 placedBlocks.back().AddBlock(curBlock, firstIndex, lastIndex, firstWord, lastWord);
129 for(
const auto curBlock : priorityList)
136 const size_t firstWordIndex = curBlock->m_StartOfLife/
wordSize;
137 const size_t lastWordIndex = curBlock->m_EndOfLife/
wordSize;
141 size_t remainder = (curBlock->m_StartOfLife - firstWordIndex *
wordSize);
142 size_t firstWord = std::numeric_limits<size_t>::max() >> remainder;
145 if(firstWordIndex == lastWordIndex)
148 remainder += curBlock->m_EndOfLife + 1 - curBlock->m_StartOfLife;
149 firstWord = firstWord >> (
wordSize - remainder);
150 firstWord = firstWord << (
wordSize - remainder);
152 singleWordLoop(curBlock, firstWord, firstWordIndex);
158 remainder = (curBlock->m_EndOfLife + 1 - lastWordIndex *
wordSize);
159 size_t lastWord = std::numeric_limits<size_t>::max() << (
wordSize - remainder);
161 if(firstWordIndex + 1 == lastWordIndex)
163 doubleWordLoop(curBlock, firstWord, firstWordIndex, lastWord, lastWordIndex);
175 for (
auto& blockList : placedBlocks)
178 if ((blockList.m_OccupiedSpaces[firstWordIndex] & firstWord) != 0 ||
179 (blockList.m_OccupiedSpaces[lastWordIndex] & lastWord) != 0)
186 for (
size_t i = firstWordIndex +1; i <= lastWordIndex -1; ++i)
188 if (blockList.m_OccupiedSpaces[i] != 0)
200 blockList.AddBlock(curBlock, firstWordIndex, lastWordIndex, firstWord, lastWord);
208 placedBlocks.emplace_back(BinTracker{maxLifetime});
209 placedBlocks.back().AddBlock(curBlock, firstWordIndex, lastWordIndex, firstWord, lastWord);
216 unsigned int maxLifetime = 0;
217 std::list<MemBlock*> priorityList;
218 for (
auto& block: blocks)
220 maxLifetime = std::max(maxLifetime, block.m_EndOfLife);
221 priorityList.emplace_back(&block);
232 std::vector<BinTracker> placedBlocks;
233 placedBlocks.reserve(maxLifetime);
234 PlaceBlocks(priorityList, placedBlocks, maxLifetime);
236 std::vector<MemBin> bins;
237 bins.reserve(placedBlocks.size());
238 for (
auto blockList: placedBlocks)
241 bin.
m_MemBlocks.reserve(blockList.m_PlacedBlocks.size());
244 for (
auto block : blockList.m_PlacedBlocks)
252 bins.push_back(std::move(bin));