1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Per-P malloc cache for small objects.
7 // See malloc.h for an overview.
13 // dummy MSpan that contains no free objects.
16 func allocmcache() *mcache {
18 c := (*mcache)(fixAlloc_Alloc(&mheap_.cachealloc))
20 memclr(unsafe.Pointer(c), unsafe.Sizeof(*c))
21 for i := 0; i < _NumSizeClasses; i++ {
22 c.alloc[i] = &emptymspan
25 // Set first allocation sample size.
26 rate := MemProfileRate
27 if rate > 0x3fffffff { // make 2*rate not overflow
31 c.next_sample = int32(int(fastrand1()) % (2 * rate))
37 func freemcache(c *mcache) {
42 // NOTE(rsc,rlh): If gcworkbuffree comes back, we need to coordinate
43 // with the stealing of gcworkbufs during garbage collection to avoid
44 // a race where the workbuf is double-freed.
45 // gcworkbuffree(c.gcworkbuf)
49 fixAlloc_Free(&mheap_.cachealloc, unsafe.Pointer(c))
54 // Gets a span that has a free object in it and assigns it
55 // to be the cached span for the given sizeclass. Returns this span.
56 func mCache_Refill(c *mcache, sizeclass int32) *mspan {
60 // Return the current cached span to the central lists.
61 s := c.alloc[sizeclass]
62 if s.freelist != nil {
63 gothrow("refill on a nonempty span")
69 // Get a new cached span from the central lists.
70 s = mCentral_CacheSpan(&mheap_.central[sizeclass].mcentral)
72 gothrow("out of memory")
74 if s.freelist == nil {
75 println(s.ref, (s.npages<<_PageShift)/s.elemsize)
78 c.alloc[sizeclass] = s
83 func mCache_ReleaseAll(c *mcache) {
84 for i := 0; i < _NumSizeClasses; i++ {
87 mCentral_UncacheSpan(&mheap_.central[i].mcentral, s)
88 c.alloc[i] = &emptymspan