MINOR: Move runtime.SetFinalizer into freeXXX
This commit is contained in:
parent
cd0565716f
commit
8031bb85f5
12
ts/scalar.go
12
ts/scalar.go
|
@ -17,8 +17,10 @@ type Scalar struct {
|
||||||
|
|
||||||
// free releases C allocated memory.
|
// free releases C allocated memory.
|
||||||
func freeCScalar(x *Scalar) error {
|
func freeCScalar(x *Scalar) error {
|
||||||
nbytes := x.nbytes()
|
if gotch.Debug {
|
||||||
atomic.AddInt64(&AllocatedMem, -nbytes)
|
nbytes := x.nbytes()
|
||||||
|
atomic.AddInt64(&AllocatedMem, -nbytes)
|
||||||
|
}
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
delete(ExistingScalars, x.name)
|
delete(ExistingScalars, x.name)
|
||||||
lock.Unlock()
|
lock.Unlock()
|
||||||
|
@ -53,8 +55,10 @@ func newScalar(cscalar lib.Cscalar, nameOpt ...string) *Scalar {
|
||||||
}
|
}
|
||||||
|
|
||||||
atomic.AddInt64(&ScalarCount, 1)
|
atomic.AddInt64(&ScalarCount, 1)
|
||||||
nbytes := x.nbytes()
|
if gotch.Debug {
|
||||||
atomic.AddInt64(&AllocatedMem, nbytes)
|
nbytes := x.nbytes()
|
||||||
|
atomic.AddInt64(&AllocatedMem, nbytes)
|
||||||
|
}
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
ExistingScalars[x.name] = struct{}{}
|
ExistingScalars[x.name] = struct{}{}
|
||||||
lock.Unlock()
|
lock.Unlock()
|
||||||
|
|
14
ts/tensor.go
14
ts/tensor.go
|
@ -67,8 +67,10 @@ func newTensor(ctensor lib.Ctensor, nameOpt ...string) *Tensor {
|
||||||
x.d = new(bigStruct)
|
x.d = new(bigStruct)
|
||||||
|
|
||||||
atomic.AddInt64(&TensorCount, 1)
|
atomic.AddInt64(&TensorCount, 1)
|
||||||
nbytes := x.nbytes()
|
if gotch.Debug {
|
||||||
atomic.AddInt64(&AllocatedMem, nbytes)
|
nbytes := x.nbytes()
|
||||||
|
atomic.AddInt64(&AllocatedMem, nbytes)
|
||||||
|
}
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
if _, ok := ExistingTensors[name]; ok {
|
if _, ok := ExistingTensors[name]; ok {
|
||||||
name = fmt.Sprintf("%s_%09d", name, TensorCount)
|
name = fmt.Sprintf("%s_%09d", name, TensorCount)
|
||||||
|
@ -170,6 +172,10 @@ func freeCTensor(ts *Tensor) error {
|
||||||
// IMPORTANT. make it nil so won't double free.
|
// IMPORTANT. make it nil so won't double free.
|
||||||
ts.ctensor = nil
|
ts.ctensor = nil
|
||||||
|
|
||||||
|
// Clear SetFinalizer on ts so no double free tensor.
|
||||||
|
// Ref. https://pkg.go.dev/runtime#SetFinalizer
|
||||||
|
runtime.SetFinalizer(ts, nil)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1190,10 +1196,6 @@ func (ts *Tensor) Drop() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear SetFinalizer on ts so no double free tensor.
|
|
||||||
// Ref. https://pkg.go.dev/runtime#SetFinalizer
|
|
||||||
runtime.SetFinalizer(ts, nil)
|
|
||||||
|
|
||||||
ts.calledFrom = "ts.Drop()"
|
ts.calledFrom = "ts.Drop()"
|
||||||
return freeCTensor(ts)
|
return freeCTensor(ts)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user