github.com/tickstep/library-go@v0.1.1/cachepool/malloc.go (about)

     1  package cachepool
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  //go:linkname mallocgc runtime.mallocgc
     9  // 函数声明可以省略主体。 这样的声明为Go外部实现的功能(例如汇编例程)提供了签名。这是在汇编中实现函数的方式。
    10  func mallocgc(size uintptr, typ uintptr, needzero bool) unsafe.Pointer
    11  
    12  //go:linkname rawbyteslice runtime.rawbyteslice
    13  func rawbyteslice(size int) (b []byte)
    14  
    15  // RawByteSlice point to runtime.rawbyteslice
    16  func RawByteSlice(size int) (b []byte) {
    17  	return rawbyteslice(size)
    18  }
    19  
    20  // RawMalloc allocates a new slice. The slice is not zeroed.
    21  func RawMalloc(size int) unsafe.Pointer {
    22  	return mallocgc(uintptr(size), 0, false)
    23  }
    24  
    25  // RawMallocByteSlice allocates a new byte slice. The slice is not zeroed.
    26  func RawMallocByteSlice(size int) []byte {
    27  	p := mallocgc(uintptr(size), 0, false)
    28  	b := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    29  		Data: uintptr(p),
    30  		Len:  size,
    31  		Cap:  size,
    32  	}))
    33  	return b
    34  }