github.com/iikira/iikira-go-utils@v0.0.0-20230610031953-f2cb11cde33a/utils/cachepool/malloc.go (about)

     1  package cachepool
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  //go:linkname mallocgc runtime.mallocgc
     9  func mallocgc(size uintptr, typ uintptr, needzero bool) unsafe.Pointer
    10  
    11  //go:linkname rawbyteslice runtime.rawbyteslice
    12  func rawbyteslice(size int) (b []byte)
    13  
    14  // RawByteSlice point to runtime.rawbyteslice
    15  func RawByteSlice(size int) (b []byte) {
    16  	return rawbyteslice(size)
    17  }
    18  
    19  // RawMalloc allocates a new slice. The slice is not zeroed.
    20  func RawMalloc(size int) unsafe.Pointer {
    21  	return mallocgc(uintptr(size), 0, false)
    22  }
    23  
    24  // RawMallocByteSlice allocates a new byte slice. The slice is not zeroed.
    25  func RawMallocByteSlice(size int) []byte {
    26  	p := mallocgc(uintptr(size), 0, false)
    27  	b := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    28  		Data: uintptr(p),
    29  		Len:  size,
    30  		Cap:  size,
    31  	}))
    32  	return b
    33  }