github.com/GoWebProd/gip@v0.0.0-20230623090727-b60d41d5d320/safe/cleanup.go (about)

     1  package safe
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  
     7  	"github.com/GoWebProd/gip/rtime"
     8  )
     9  
    10  var cleanSlice = make([]byte, 1024)
    11  
    12  func Cleanup[T any](v *T) {
    13  	var (
    14  		data []byte
    15  		t T
    16  	)
    17  
    18  	size := int(unsafe.Sizeof(t))
    19  	h := (*reflect.SliceHeader)(unsafe.Pointer(&data))
    20  	h.Len = size
    21  	h.Cap = size
    22  	h.Data = uintptr(rtime.Noescape(v))
    23  
    24  	for i := 0; i < size; i += 1024 {
    25  		copy(data[i:], cleanSlice)
    26  	}
    27  }