Skip to content

Commit

Permalink
packing: Add an extending count at Extend
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Nov 17, 2019
1 parent 74902d4 commit 6183950
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
7 changes: 5 additions & 2 deletions internal/packing/packing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/packing/packing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
9 changes: 4 additions & 5 deletions internal/shareable/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)

Expand Down

0 comments on commit 6183950

Please sign in to comment.