上卷 程序设计
中卷 标准库
- bufio 1.18
- bytes 1.18
- io 1.18
- container 1.18
- encoding 1.18
- crypto 1.18
- hash 1.18
- index 1.18
- sort 1.18
- context 1.18
- database 1.18
- connection
- query
- queryrow
- exec
- prepare
- transaction
- scan & null
- context
- tcp
- udp
- http
- server
- handler
- client
- h2、tls
- url
- rpc
- exec
- signal
- embed 1.18
- plugin 1.18
- reflect 1.18
- runtime 1.18
- KeepAlived
- ReadMemStats
- SetFinalizer
- Stack
- sync 1.18
- atomic
- mutex
- rwmutex
- waitgroup
- cond
- once
- map
- pool
- copycheck
- nocopy
- unsafe 1.18
- fmt 1.18
- log 1.18
- math 1.18
- time 1.18
- timer
下卷 运行时
源码剖析
附录
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
4.6.2 分配
新建 G 时,其初始栈大小为 2 KB。
// stack.go _StackMin = 2048
// proc.go func newproc1(fn *funcval, callergp *g, callerpc uintptr) *g { newg := gfget(_p_) if newg == nil { newg = malg(_StackMin) } }
// proc.go // Allocate a new g, with a stack big enough for stacksize bytes. func malg(stacksize int32) *g { newg := new(g) if stacksize >= 0 { stacksize = round2(_StackSystem + stacksize) systemstack(func() { newg.stack = stackalloc(uint32(stacksize)) }) newg.stackguard0 = newg.stack.lo + _StackGuard newg.stackguard1 = ^uintptr(0) // Clear the bottom word of the stack. We record g // there on gsignal stack during VDSO on ARM and ARM64. *(*uintptr)(unsafe.Pointer(newg.stack.lo)) = 0 } return newg }
// proc.go // Get from gfree list. func gfget(_p_ *p) *g { gp := _p_.gFree.pop() if gp == nil { return nil } if gp.stack.lo == 0 { // Stack was deallocated in gfput. Allocate a new one. systemstack(func() { gp.stack = stackalloc(_FixedStack) }) gp.stackguard0 = gp.stack.lo + _StackGuard } return gp }
分配操作优先从本地缓存提取。如果超出限制,则直接找全局大块缓存。
// mcache.go type mcache struct { stackcache [_NumStackOrders]stackfreelist } type stackfreelist struct { list gclinkptr // linked list of free stacks size uintptr // total size of stacks in list }
// stack.go // Global pool of large stack spans. var stackLarge struct { lock mutex free [heapAddrBits - pageShift]mSpanList // free lists by log_2(s.npages) }
如缓存都不足以满足需求,则从堆分配。
// malloc.go // Per-P, per order stack segment cache size. _StackCacheSize = 32 * 1024
// stack.go // stackalloc allocates an n byte stack. func stackalloc(n uint32) stack { // Small stacks are allocated with a fixed-size free-list allocator. // If we need a stack of a bigger size, we fall back on allocating // a dedicated span. var v unsafe.Pointer // 没有超出本地缓存限制。 if n < _FixedStack<<_NumStackOrders && n < _StackCacheSize { // 计算等级。 order := uint8(0) n2 := n for n2 > _FixedStack { order++ n2 >>= 1 } var x gclinkptr if stackNoCache != 0 || thisg.m.p == 0 || thisg.m.preemptoff != "" { // 全局 pool 分配。 lock(&stackpool[order].item.mu) x = stackpoolalloc(order) unlock(&stackpool[order].item.mu) } else { // 本地 cache,对应等级链表提取。 c := thisg.m.p.ptr().mcache x = c.stackcache[order].list // 扩容后重试。 if x.ptr() == nil { stackcacherefill(c, order) x = c.stackcache[order].list } c.stackcache[order].list = x.ptr().next c.stackcache[order].size -= uintptr(n) } v = unsafe.Pointer(x) } else { // 大块。 var s *mspan npage := uintptr(n) >> _PageShift log2npage := stacklog2(npage) // 从大块缓存获取。 lock(&stackLarge.lock) if !stackLarge.free[log2npage].isEmpty() { s = stackLarge.free[log2npage].first stackLarge.free[log2npage].remove(s) } unlock(&stackLarge.lock) // 如果没有大块缓存,则从堆分配。 if s == nil { s = mheap_.allocManual(npage, spanAllocStack) osStackAlloc(s) s.elemsize = uintptr(n) } v = unsafe.Pointer(s.base()) } // stack { lo, hi } return stack{uintptr(v), uintptr(v) + uintptr(n)} }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论