Donate power-of-2-aligned ranges instead of just slicing at the mid-point.

Also fix up space tests to match.
This commit is contained in:
Bryan Boreham committed 2016-05-13 16:27:28 +01:00
1 parent f37e742536
commit 64dc7a1f69
5 files changed
+99 -37

No files matched your search

+11 -5
View File
@@ -122,8 +122,11 @@ func (s *Space) biggestFreeRange(r address.Range) (biggest address.Range) {
biggestSize := address.Count(0)
s.walkFree(r, func(chunk address.Range) bool {
if size := chunk.Size(); size >= biggestSize {
biggest = chunk
biggestSize = size
chunk = chunk.BiggestPow2AlignedRange()
if size = chunk.Size(); size >= biggestSize {
biggest = chunk
biggestSize = size
}
}
return false
})
@@ -137,9 +140,12 @@ func (s *Space) Donate(r address.Range) (address.Range, bool) {
return address.Range{}, false
}
// Donate half of that biggest free range. Note size/2 rounds down, so
// the resulting donation size rounds up, and in particular can't be empty.
biggest.Start = address.Add(biggest.Start, address.Offset(biggest.Size()/2))
// Donate no more than half what we have available
if biggest.Size() > s.NumFreeAddressesInRange(r)/2 {
// Note size/2 rounds down, so the resulting donation size
// rounds up, and in particular can't be empty.
biggest.Start = address.Add(biggest.Start, address.Offset(biggest.Size()/2))
}
s.ours = subtract(s.ours, biggest.Start, biggest.End)
s.free = subtract(s.free, biggest.Start, biggest.End)