packing: Add comments

This commit is contained in:
Hajime Hoshi 2018-03-08 00:18:28 +09:00
parent 1dd32066d2
commit 86be32dfb5
2 changed files with 20 additions and 13 deletions

View File

@ -231,19 +231,28 @@ func (p *Page) Extend() bool {
newSize := p.size * 2
edgeNodes := []*Node{}
abort := errors.New("abort")
aborted := false
canExtendNodes := true
_ = walk(p.root, func(n *Node) error {
if n.x+n.width < p.size && n.y+n.height < p.size {
return nil
}
if n.used {
aborted = true
canExtendNodes = false
return abort
}
edgeNodes = append(edgeNodes, n)
return nil
})
if aborted {
if canExtendNodes {
for _, n := range edgeNodes {
if n.x+n.width == p.size {
n.width += newSize - p.size
}
if n.y+n.height == p.size {
n.height += newSize - p.size
}
}
} else {
leftUpper := p.root
leftLower := &Node{
x: 0,
@ -278,15 +287,6 @@ func (p *Page) Extend() bool {
}
left.parent = p.root
right.parent = p.root
} else {
for _, n := range edgeNodes {
if n.x+n.width == p.size {
n.width += newSize - p.size
}
if n.y+n.height == p.size {
n.height += newSize - p.size
}
}
}
p.size = newSize

View File

@ -211,6 +211,7 @@ func TestExtend(t *testing.T) {
p := &Page{}
s := p.Size()
p.Alloc(s/2, s/2)
// Now Extend extends the page by exntending the existing edge nodes.
p.Extend()
if p.Alloc(s*3/2, s*2) == nil {
t.Fail()
@ -223,7 +224,7 @@ func TestExtend(t *testing.T) {
}
}
func TestExtend2(t *testing.T) {
func TestExtendAddingNewNodes(t *testing.T) {
p := &Page{}
s := p.Size()
p.Alloc(s/2, s/2)
@ -232,7 +233,13 @@ func TestExtend2(t *testing.T) {
p.Alloc(s/2, s/2)
p.Free(n1)
p.Free(n2)
// There is an allocation at lower left.
// In this case, Extend doesn't extend the existing edge nodes, but
// instead adds new nodes.
p.Extend()
if p.Alloc(s*3/2, s*2) != nil {
t.Fail()
}
if p.Alloc(s, s*2) == nil {
t.Fail()
}