"runtime"
"sync"
"syscall"
- "unsafe"
)
const (
type splicePipe struct {
splicePipeFields
-
- // We want to use a finalizer, so ensure that the size is
- // large enough to not use the tiny allocator.
- _ [24 - unsafe.Sizeof(splicePipeFields{})%24]byte
+ cleanup runtime.Cleanup
}
// splicePipePool caches pipes to avoid high-frequency construction and destruction of pipe buffers.
if p == nil {
return nil
}
- runtime.SetFinalizer(p, destroyPipe)
+
+ p.cleanup = runtime.AddCleanup(p, func(spf splicePipeFields) {
+ destroyPipe(&splicePipe{splicePipeFields: spf})
+ }, p.splicePipeFields)
return p
}
// If there is still data left in the pipe,
// then close and discard it instead of putting it back into the pool.
if p.data != 0 {
- runtime.SetFinalizer(p, nil)
+ p.cleanup.Stop()
destroyPipe(p)
return
}