packing: Add an extending count at Extend

This commit is contained in:
Hajime Hoshi 2019-11-17 23:21:50 +09:00
parent 74902d47af
commit 61839506d8
3 changed files with 11 additions and 9 deletions

View File

@ -207,11 +207,14 @@ func walk(n *Node, f func(n *Node) error) error {
return nil
}
func (p *Page) Extend() bool {
func (p *Page) Extend(count int) bool {
if p.size >= p.maxSize {
return false
}
newSize := p.size * 2
newSize := p.size
for i := 0; i < count; i++ {
newSize *= 2
}
edgeNodes := []*Node{}
abort := errors.New("abort")
aborted := false

View File

@ -257,7 +257,7 @@ func TestExtend(t *testing.T) {
p := NewPage(1024, 4096)
s := p.Size()
p.Alloc(s/2, s/2)
p.Extend()
p.Extend(1)
if p.Size() != s*2 {
t.Errorf("p.Size(): got: %d, want: %d", p.Size(), s*2)
}
@ -281,7 +281,7 @@ func TestExtend2(t *testing.T) {
p.Alloc(s/2, s/2)
p.Free(n1)
p.Free(n2)
p.Extend()
p.Extend(1)
if p.Size() != s*2 {
t.Errorf("p.Size(): got: %d, want: %d", p.Size(), s*2)
}

View File

@ -91,16 +91,17 @@ type backend struct {
}
func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) {
// If the region is allocated without any extension, it's fine.
// If the region is allocated without any extension, that's fine.
if n := b.page.Alloc(width, height); n != nil {
return n, true
}
// Simulate the extending the page and calculate the appropriate page size.
// By simulating, we can avoid unnecessary extention of underlying textures.
page := b.page.Clone()
nExtended := 0
for {
if !page.Extend() {
if !page.Extend(1) {
// The page can't be extended any more. Return as failure.
return nil, false
}
@ -111,9 +112,7 @@ func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) {
}
}
for i := 0; i < nExtended; i++ {
b.page.Extend()
}
b.page.Extend(nExtended)
s := b.page.Size()
b.restorable = b.restorable.Extend(s, s)