共计 1443 个字符,预计需要花费 4 分钟才能阅读完成。
不太懂的 API
persistentalloc
sysAlloc
mfixalloc.go
// 固定大小的对象分配器。返回的内存未归零。//
// See malloc.go for overview.
package runtime
import "unsafe"
//FixAlloc 是用于固定大小对象的简略自在列表分配器。//Malloc 应用包裹在 sysAlloc 四周的 FixAlloc 来治理其 MCache 和 MSpan 对象。//FixAlloc_Alloc 返回的内存未归零。// 调用方负责锁定 FixAlloc 调用。// 调用者能够将状态保留在对象中,但通过开释和重新分配能够粉碎第一个单词。type fixalloc struct {
size uintptr
first unsafe.Pointer // go func(unsafe.pointer, unsafe.pointer); f(arg, p) called first time p is returned,对内存进行初始化
arg unsafe.Pointer
list *mlink // 缓存开释数据
chunk *byte // 每次申请 64K 数据
nchunk uint32 // trunk 残余数据大小
inuse uintptr // 应用中的数据
stat *uint64
}
type mlink struct {next *mlink}
func fixAlloc_Init(f *fixalloc, size uintptr, first func(unsafe.Pointer, unsafe.Pointer), arg unsafe.Pointer, stat *uint64) {
f.size = size
f.first = *(*unsafe.Pointer)(unsafe.Pointer(&first))
f.arg = arg
f.list = nil
f.chunk = nil
f.nchunk = 0
f.inuse = 0
f.stat = stat
}
func fixAlloc_Alloc(f *fixalloc) unsafe.Pointer {
if f.size == 0 {print("runtime: use of FixAlloc_Alloc before FixAlloc_Initn")
throw("runtime: internal error")
}
if f.list != nil {v := unsafe.Pointer(f.list)
f.list = f.list.next
f.inuse += f.size
return v
}
if uintptr(f.nchunk) < f.size {f.chunk = (*uint8)(persistentalloc(_FixAllocChunk, 0, f.stat))
f.nchunk = _FixAllocChunk
}
v := (unsafe.Pointer)(f.chunk)
if f.first != nil {fn := *(*func(unsafe.Pointer, unsafe.Pointer))(unsafe.Pointer(&f.first))
fn(f.arg, v)
}
f.chunk = (*byte)(add(unsafe.Pointer(f.chunk), f.size))
f.nchunk -= uint32(f.size)
f.inuse += f.size
return v
}
func fixAlloc_Free(f *fixalloc, p unsafe.Pointer) {
f.inuse -= f.size
v := (*mlink)(p)
v.next = f.list
f.list = v
}
正文完