shareable: Refactoring: Add moveTo

This commit is contained in:
Hajime Hoshi 2018-07-22 06:27:57 +09:00
parent 693116ffd6
commit b2a4ddf853

View File

@ -95,6 +95,15 @@ type Image struct {
countForShare int countForShare int
} }
func (i *Image) moveTo(dst *Image) {
dst.dispose(false)
*dst = *i
// i is no longer available but Dispose must not be called
// since i and dst have the same values like node.
runtime.SetFinalizer(i, nil)
}
func (i *Image) isShared() bool { func (i *Image) isShared() bool {
return i.node != nil return i.node != nil
} }
@ -154,11 +163,8 @@ func (i *Image) forceShared() {
} }
} }
newI.replacePixels(pixels) newI.replacePixels(pixels)
i.dispose(false) newI.moveTo(i)
*i = *newI i.countForShare = 0
// TODO: This is a very tricky hack not to call Dispose twice for the same backend.
// Avoid such hack.
runtime.SetFinalizer(newI, nil)
} }
func (i *Image) region() (x, y, width, height int) { func (i *Image) region() (x, y, width, height int) {