Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Arena ¶
type Arena struct {
// contains filtered or unexported fields
}
Arena是go1.20引入的arena.Arena的升级替代,创建后不得复制
与go1.20引入的arena.Arena相比,有下列不同
- 可以同时在多个goroutine使用
- 不使用泛型,可以在版本低于go1.20使用
- 只能分配固定大小的Go值
Example ¶
type user struct { a, b, c int } a := NewArena(unsafe.Sizeof(user{})) //分配并使用 b := (*user)(a.Alloc()) b.a = 1 b.c = 2 //使用完毕 a.Free()
Output:
func NewArena ¶
NewArena 创建一组代表一起分配和释放的Go值的集合,内存大小为n*dataSize(n=bufsize/dataSize)
- dataSize 是单个Go值大小,决定 Arena.Alloc 返回的指针指向的内存的长度,单位是字节
- bufsize 是可选的,决定 Arena.Alloc 每次自动扩容分配多大内存,默认为8Mb,必须大于dataSize
func (*Arena) Alloc ¶
Alloc分配一个固定大小的Go值,并返回指针,NewArena 的dataSize是这个固定大小
返回的指针如果访问超过固定大小的内存,行为是未定义且不安全的
func (*Arena) Free ¶
func (a *Arena) Free()
Free 释放所有Go值
从调用的一刻开始,通过 Arena.Alloc 获取的指针,读写内存,行为是未定义且不安全的
Click to show internal directories.
Click to hide internal directories.