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