Skip to content

Commit

Permalink
Extract common logic between register and registerAt in registerAlloc…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
deadalnix committed Dec 12, 2023
1 parent 8965fcd commit 80f3b0e
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions sdlib/d/gc/block.d
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,30 @@ public:
current = index + length;
}

assert(bestIndex < PagesInBlock);
allocatedPages.setRange(bestIndex, pages);
registerAllocation(bestIndex, pages);

// If we allocated from the longest range,
// compute the new longest free range.
if (bestLength == longestLength) {
longestLength = max(longestLength - pages, secondLongestLength);
}

usedCount += pages;
longestFreeRange = longestLength;

return bestIndex;
}

bool reserveAt(uint index, uint pages) {
assert(pages > 0 && pages <= PagesInBlock, "Invalid number of pages!");
assert(index <= PagesInBlock - pages, "Invalid index!");
assert(index < PagesInBlock, "Invalid index!");
assert(pages > 0 && index + pages <= PagesInBlock,
"Invalid number of pages!");

auto freeLength = allocatedPages.findSet(index) - index;
if (freeLength < pages) {
return false;
}

allocatedPages.setRange(index, pages);
usedCount += pages;
registerAllocation(index, pages);

// If not allocated from the longest free range, we're done:
if (freeLength != longestFreeRange) {
Expand All @@ -145,18 +143,28 @@ public:
uint current = 0;
uint length;
while (current < PagesInBlock
&& allocatedPages.nextFreeRange(current, index, length)) {
&& allocatedPages.nextFreeRange(current, current, length)) {
if (length > longestLength) {
longestLength = length;
}

current = index + length;
current += length;
}

longestFreeRange = longestLength;
return true;
}

void registerAllocation(uint index, uint pages) {
assert(index < PagesInBlock, "Invalid index!");
assert(pages > 0 && index + pages <= PagesInBlock,
"Invalid number of pages!");

// Mark the pages as allocated.
usedCount += pages;
allocatedPages.setRange(index, pages);
}

void release(uint index, uint pages) {
// FIXME: in contract.
assert(pages > 0 && pages <= PagesInBlock, "Invalid number of pages!");
Expand Down

0 comments on commit 80f3b0e

Please sign in to comment.