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 // Memory allocator, based on tcmalloc.
6 // http://goog-perftools.sourceforge.net/doc/tcmalloc.html
8 // The main allocator works in runs of pages.
9 // Small allocation sizes (up to and including 32 kB) are
10 // rounded to one of about 100 size classes, each of which
11 // has its own free list of objects of exactly that size.
12 // Any free page of memory can be split into a set of objects
13 // of one size class, which are then managed using free list
16 // The allocator's data structures are:
18 // FixAlloc: a free-list allocator for fixed-size objects,
19 // used to manage storage used by the allocator.
20 // MHeap: the malloc heap, managed at page (4096-byte) granularity.
21 // MSpan: a run of pages managed by the MHeap.
22 // MCentral: a shared free list for a given size class.
23 // MCache: a per-thread (in Go, per-P) cache for small objects.
24 // MStats: allocation statistics.
26 // Allocating a small object proceeds up a hierarchy of caches:
28 // 1. Round the size up to one of the small size classes
29 // and look in the corresponding MCache free list.
30 // If the list is not empty, allocate an object from it.
31 // This can all be done without acquiring a lock.
33 // 2. If the MCache free list is empty, replenish it by
34 // taking a bunch of objects from the MCentral free list.
35 // Moving a bunch amortizes the cost of acquiring the MCentral lock.
37 // 3. If the MCentral free list is empty, replenish it by
38 // allocating a run of pages from the MHeap and then
39 // chopping that memory into a objects of the given size.
40 // Allocating many objects amortizes the cost of locking
43 // 4. If the MHeap is empty or has no page runs large enough,
44 // allocate a new group of pages (at least 1MB) from the
45 // operating system. Allocating a large run of pages
46 // amortizes the cost of talking to the operating system.
48 // Freeing a small object proceeds up the same hierarchy:
50 // 1. Look up the size class for the object and add it to
51 // the MCache free list.
53 // 2. If the MCache free list is too long or the MCache has
54 // too much memory, return some to the MCentral free lists.
56 // 3. If all the objects in a given span have returned to
57 // the MCentral list, return that span to the page heap.
59 // 4. If the heap has too much memory, return some to the
62 // TODO(rsc): Step 4 is not implemented.
64 // Allocating and freeing a large object uses the page heap
65 // directly, bypassing the MCache and MCentral free lists.
67 // The small objects on the MCache and MCentral free lists
68 // may or may not be zeroed. They are zeroed if and only if
69 // the second word of the object is zero. A span in the
70 // page heap is zeroed unless s->needzero is set. When a span
71 // is allocated to break into small objects, it is zeroed if needed
72 // and s->needzero is set. There are two main benefits to delaying the
75 // 1. stack frames allocated from the small object lists
76 // or the page heap can avoid zeroing altogether.
77 // 2. the cost of zeroing when reusing a small object is
78 // charged to the mutator, not the garbage collector.
80 // This C code was written with an eye toward translating to Go
81 // in the future. Methods have the form Type_Method(Type *t, ...).
83 typedef struct MCentral MCentral
;
84 typedef struct MHeap MHeap
;
85 typedef struct mspan MSpan
;
86 typedef struct mstats MStats
;
87 typedef struct mlink MLink
;
88 typedef struct mtypes MTypes
;
89 typedef struct gcstats GCStats
;
94 PageSize
= 1<<PageShift
,
95 PageMask
= PageSize
- 1,
97 typedef uintptr PageID
; // address >> PageShift
101 // Computed constant. The definition of MaxSmallSize and the
102 // algorithm in msize.c produce some number of different allocation
103 // size classes. _NumSizeClasses is that number. It's needed here
104 // because there are static arrays of this length; when msize runs its
105 // size choosing algorithm it double-checks that NumSizeClasses agrees.
106 // _NumSizeClasses is defined in runtime2.go as 67.
108 // Tunable constants.
109 MaxSmallSize
= 32<<10,
111 // Tiny allocator parameters, see "Tiny allocator" comment in malloc.goc.
115 FixAllocChunk
= 16<<10, // Chunk size for FixAlloc
116 MaxMHeapList
= 1<<(20 - PageShift
), // Maximum page length for fixed-size list in MHeap.
117 HeapAllocChunk
= 1<<20, // Chunk size for heap growth
119 // Number of bits in page to span calculations (4k pages).
120 // On Windows 64-bit we limit the arena to 32GB or 35 bits (see below for reason).
121 // On other 64-bit platforms, we limit the arena to 128GB, or 37 bits.
122 // On 32-bit, we don't bother limiting anything, so we use the full 32-bit address.
123 #if __SIZEOF_POINTER__ == 8
125 // Windows counts memory used by page table into committed memory
126 // of the process, so we can't reserve too much memory.
127 // See http://golang.org/issue/5402 and http://golang.org/issue/5236.
128 MHeapMap_Bits
= 35 - PageShift
,
130 MHeapMap_Bits
= 37 - PageShift
,
133 MHeapMap_Bits
= 32 - PageShift
,
137 // Maximum memory allocation size, a hint for callers.
138 // This must be a #define instead of an enum because it
140 #if __SIZEOF_POINTER__ == 8
141 #define MaxMem (1ULL<<(MHeapMap_Bits+PageShift)) /* 128 GB or 32 GB */
143 #define MaxMem ((uintptr)-1)
145 // SysAlloc obtains a large chunk of zeroed memory from the
146 // operating system, typically on the order of a hundred kilobytes
148 // NOTE: SysAlloc returns OS-aligned memory, but the heap allocator
149 // may use larger alignment, so the caller must be careful to realign the
150 // memory obtained by SysAlloc.
152 // SysUnused notifies the operating system that the contents
153 // of the memory region are no longer needed and can be reused
154 // for other purposes.
155 // SysUsed notifies the operating system that the contents
156 // of the memory region are needed again.
158 // SysFree returns it unconditionally; this is only used if
159 // an out-of-memory error has been detected midway through
160 // an allocation. It is okay if SysFree is a no-op.
162 // SysReserve reserves address space without allocating memory.
163 // If the pointer passed to it is non-nil, the caller wants the
164 // reservation there, but SysReserve can still choose another
165 // location if that one is unavailable. On some systems and in some
166 // cases SysReserve will simply check that the address space is
167 // available and not actually reserve it. If SysReserve returns
168 // non-nil, it sets *reserved to true if the address space is
169 // reserved, false if it has merely been checked.
170 // NOTE: SysReserve returns OS-aligned memory, but the heap allocator
171 // may use larger alignment, so the caller must be careful to realign the
172 // memory obtained by SysAlloc.
174 // SysMap maps previously reserved address space for use.
175 // The reserved argument is true if the address space was really
176 // reserved, not merely checked.
178 // SysFault marks a (already SysAlloc'd) region to fault
179 // if accessed. Used only for debugging the runtime.
181 void* runtime_SysAlloc(uintptr nbytes
, uint64
*stat
)
182 __asm__ (GOSYM_PREFIX
"runtime.sysAlloc");
183 void runtime_SysFree(void *v
, uintptr nbytes
, uint64
*stat
)
184 __asm__ (GOSYM_PREFIX
"runtime.sysFree");
185 void runtime_SysUnused(void *v
, uintptr nbytes
);
186 void runtime_SysUsed(void *v
, uintptr nbytes
);
187 void runtime_SysMap(void *v
, uintptr nbytes
, bool reserved
, uint64
*stat
);
188 void* runtime_SysReserve(void *v
, uintptr nbytes
, bool *reserved
);
189 void runtime_SysFault(void *v
, uintptr nbytes
);
191 // FixAlloc is a simple free-list allocator for fixed size objects.
192 // Malloc uses a FixAlloc wrapped around SysAlloc to manages its
193 // MCache and MSpan objects.
195 // Memory returned by FixAlloc_Alloc is not zeroed.
196 // The caller is responsible for locking around FixAlloc calls.
197 // Callers can keep state in the object but the first word is
198 // smashed by freeing and reallocating.
202 void (*first
)(void *arg
, byte
*p
); // called first time p is returned
207 uintptr inuse
; // in-use bytes now
211 void runtime_FixAlloc_Init(FixAlloc
*f
, uintptr size
, void (*first
)(void*, byte
*), void *arg
, uint64
*stat
);
212 void* runtime_FixAlloc_Alloc(FixAlloc
*f
);
213 void runtime_FixAlloc_Free(FixAlloc
*f
, void *p
);
215 extern MStats
*mstats(void)
216 __asm__ (GOSYM_PREFIX
"runtime.getMstats");
217 void runtime_updatememstats(GCStats
*stats
)
218 __asm__ (GOSYM_PREFIX
"runtime.updatememstats");
220 // Size classes. Computed and initialized by InitSizes.
222 // SizeToClass(0 <= n <= MaxSmallSize) returns the size class,
223 // 1 <= sizeclass < _NumSizeClasses, for n.
224 // Size class 0 is reserved to mean "not small".
226 // class_to_size[i] = largest size in class i
227 // class_to_allocnpages[i] = number of pages to allocate when
228 // making new objects in class i
230 int32
runtime_SizeToClass(int32
);
231 uintptr
runtime_roundupsize(uintptr
)
232 __asm__(GOSYM_PREFIX
"runtime.roundupsize");
233 extern int32 runtime_class_to_size
[_NumSizeClasses
];
234 extern int32 runtime_class_to_allocnpages
[_NumSizeClasses
];
235 extern int8 runtime_size_to_class8
[1024/8 + 1];
236 extern int8 runtime_size_to_class128
[(MaxSmallSize
-1024)/128 + 1];
237 extern void runtime_InitSizes(void);
240 typedef struct mcachelist MCacheList
;
242 MSpan
* runtime_MCache_Refill(MCache
*c
, int32 sizeclass
);
243 void runtime_MCache_Free(MCache
*c
, MLink
*p
, int32 sizeclass
, uintptr size
);
244 void runtime_MCache_ReleaseAll(MCache
*c
);
246 // MTypes describes the types of blocks allocated within a span.
247 // The compression field describes the layout of the data.
250 // All blocks are free, or no type information is available for
252 // The data field has no meaning.
254 // The span contains just one block.
255 // The data field holds the type information.
256 // The sysalloc field has no meaning.
258 // The span contains multiple blocks.
259 // The data field points to an array of type [NumBlocks]uintptr,
260 // and each element of the array holds the type of the corresponding
263 // The span contains at most seven different types of blocks.
264 // The data field points to the following structure:
266 // type [8]uintptr // type[0] is always 0
267 // index [NumBlocks]byte
269 // The type of the i-th block is: data.type[data.index[i]]
280 KindSpecialFinalizer
= 1,
281 KindSpecialProfile
= 2,
282 // Note: The finalizer special must be first because if we're freeing
283 // an object, a finalizer special will cause the freeing operation
284 // to abort, and we want to keep the other special records around
288 typedef struct special Special
;
290 // The described object has a finalizer set for it.
291 typedef struct SpecialFinalizer SpecialFinalizer
;
292 struct SpecialFinalizer
300 // The described object is being heap profiled.
301 typedef struct bucket Bucket
; // from mprof.go
302 typedef struct SpecialProfile SpecialProfile
;
303 struct SpecialProfile
309 // An MSpan is a run of pages.
318 void runtime_MSpan_Init(MSpan
*span
, PageID start
, uintptr npages
);
319 void runtime_MSpan_EnsureSwept(MSpan
*span
);
320 bool runtime_MSpan_Sweep(MSpan
*span
);
322 // Every MSpan is in one doubly-linked list,
323 // either one of the MHeap's free lists or one of the
324 // MCentral's span lists. We use empty MSpan structures as list heads.
325 void runtime_MSpanList_Init(MSpan
*list
);
326 bool runtime_MSpanList_IsEmpty(MSpan
*list
);
327 void runtime_MSpanList_Insert(MSpan
*list
, MSpan
*span
);
328 void runtime_MSpanList_InsertBack(MSpan
*list
, MSpan
*span
);
329 void runtime_MSpanList_Remove(MSpan
*span
); // from whatever list it is in
332 // Central list of free objects of a given size.
337 MSpan nonempty
; // list of spans with a free object
338 MSpan mempty
; // list of spans with no free objects (or cached in an MCache)
339 int32 nfree
; // # of objects available in nonempty spans
342 void runtime_MCentral_Init(MCentral
*c
, int32 sizeclass
);
343 MSpan
* runtime_MCentral_CacheSpan(MCentral
*c
);
344 void runtime_MCentral_UncacheSpan(MCentral
*c
, MSpan
*s
);
345 bool runtime_MCentral_FreeSpan(MCentral
*c
, MSpan
*s
, int32 n
, MLink
*start
, MLink
*end
);
346 void runtime_MCentral_FreeList(MCentral
*c
, MLink
*start
); // TODO: need this?
349 // The heap itself is the "free[]" and "large" arrays,
350 // but all the other global data is here too.
354 MSpan free
[MaxMHeapList
]; // free lists of given length
355 MSpan freelarge
; // free lists length >= MaxMHeapList
356 MSpan busy
[MaxMHeapList
]; // busy lists of large objects of given length
357 MSpan busylarge
; // busy lists of large objects length >= MaxMHeapList
358 MSpan
**allspans
; // all spans out there
359 MSpan
**sweepspans
; // copy of allspans referenced by sweeper
362 uint32 sweepgen
; // sweep generation, see comment in MSpan
363 uint32 sweepdone
; // all spans are swept
367 uintptr spans_mapped
;
369 // range of addresses we might see in the heap
371 uintptr bitmap_mapped
;
377 // central free lists for small size classes.
378 // the padding makes sure that the MCentrals are
379 // spaced CacheLineSize bytes apart, so that each MCentral.Lock
380 // gets its own cache line.
384 } central
[_NumSizeClasses
];
386 FixAlloc spanalloc
; // allocator for Span*
387 FixAlloc cachealloc
; // allocator for MCache*
388 FixAlloc specialfinalizeralloc
; // allocator for SpecialFinalizer*
389 FixAlloc specialprofilealloc
; // allocator for SpecialProfile*
390 Lock speciallock
; // lock for sepcial record allocators.
393 uint64 largefree
; // bytes freed for large objects (>MaxSmallSize)
394 uint64 nlargefree
; // number of frees for large objects (>MaxSmallSize)
395 uint64 nsmallfree
[_NumSizeClasses
]; // number of frees for small objects (<=MaxSmallSize)
397 extern MHeap runtime_mheap
;
399 void runtime_MHeap_Init(MHeap
*h
);
400 MSpan
* runtime_MHeap_Alloc(MHeap
*h
, uintptr npage
, int32 sizeclass
, bool large
, bool needzero
);
401 void runtime_MHeap_Free(MHeap
*h
, MSpan
*s
, int32 acct
);
402 MSpan
* runtime_MHeap_Lookup(MHeap
*h
, void *v
);
403 MSpan
* runtime_MHeap_LookupMaybe(MHeap
*h
, void *v
);
404 void runtime_MGetSizeClassInfo(int32 sizeclass
, uintptr
*size
, int32
*npages
, int32
*nobj
);
405 void* runtime_MHeap_SysAlloc(MHeap
*h
, uintptr n
);
406 void runtime_MHeap_MapBits(MHeap
*h
);
407 void runtime_MHeap_MapSpans(MHeap
*h
);
408 void runtime_MHeap_Scavenger(void*);
409 void runtime_MHeap_SplitSpan(MHeap
*h
, MSpan
*s
);
411 void* runtime_mallocgc(uintptr size
, uintptr typ
, uint32 flag
);
412 void* runtime_persistentalloc(uintptr size
, uintptr align
, uint64
*stat
)
413 __asm__(GOSYM_PREFIX
"runtime.persistentalloc");
414 int32
runtime_mlookup(void *v
, byte
**base
, uintptr
*size
, MSpan
**s
);
415 void runtime_gc(int32 force
);
416 uintptr
runtime_sweepone(void);
417 void runtime_markscan(void *v
);
418 void runtime_marknogc(void *v
);
419 void runtime_checkallocated(void *v
, uintptr n
);
420 void runtime_markfreed(void *v
);
421 void runtime_checkfreed(void *v
, uintptr n
);
422 extern int32 runtime_checking
;
423 void runtime_markspan(void *v
, uintptr size
, uintptr n
, bool leftover
);
424 void runtime_unmarkspan(void *v
, uintptr size
);
425 void runtime_purgecachedstats(MCache
*);
426 void* runtime_cnew(const Type
*)
427 __asm__(GOSYM_PREFIX
"runtime.newobject");
428 void* runtime_cnewarray(const Type
*, intgo
)
429 __asm__(GOSYM_PREFIX
"runtime.newarray");
430 void runtime_tracealloc(void*, uintptr
, uintptr
)
431 __asm__ (GOSYM_PREFIX
"runtime.tracealloc");
432 void runtime_tracefree(void*, uintptr
)
433 __asm__ (GOSYM_PREFIX
"runtime.tracefree");
434 void runtime_tracegc(void)
435 __asm__ (GOSYM_PREFIX
"runtime.tracegc");
437 uintptr
runtime_gettype(void*);
442 FlagNoScan
= 1<<0, // GC doesn't have to scan object
443 FlagNoProfiling
= 1<<1, // must not profile
444 FlagNoGC
= 1<<2, // must not free or scan for pointers
445 FlagNoZero
= 1<<3, // don't zero memory
446 FlagNoInvokeGC
= 1<<4, // don't invoke GC
449 typedef struct Obj Obj
;
452 byte
*p
; // data pointer
453 uintptr n
; // size of data in bytes
454 uintptr ti
; // type info
457 void runtime_MProf_Malloc(void*, uintptr
)
458 __asm__ (GOSYM_PREFIX
"runtime.mProf_Malloc");
459 void runtime_MProf_Free(Bucket
*, uintptr
, bool)
460 __asm__ (GOSYM_PREFIX
"runtime.mProf_Free");
461 void runtime_MProf_GC(void)
462 __asm__ (GOSYM_PREFIX
"runtime.mProf_GC");
463 void runtime_iterate_memprof(FuncVal
* callback
)
464 __asm__ (GOSYM_PREFIX
"runtime.iterate_memprof");
465 int32
runtime_gcprocs(void)
466 __asm__ (GOSYM_PREFIX
"runtime.gcprocs");
467 void runtime_helpgc(int32 nproc
)
468 __asm__ (GOSYM_PREFIX
"runtime.helpgc");
469 void runtime_gchelper(void)
470 __asm__ (GOSYM_PREFIX
"runtime.gchelper");
471 void runtime_createfing(void);
472 G
* runtime_wakefing(void)
473 __asm__ (GOSYM_PREFIX
"runtime.wakefing");
474 extern bool runtime_fingwait
;
475 extern bool runtime_fingwake
;
477 void runtime_setprofilebucket(void *p
, Bucket
*b
)
478 __asm__ (GOSYM_PREFIX
"runtime.setprofilebucket");
480 struct __go_func_type
;
481 struct __go_ptr_type
;
482 bool runtime_addfinalizer(void *p
, FuncVal
*fn
, const struct __go_func_type
*, const struct __go_ptr_type
*);
483 void runtime_removefinalizer(void*);
484 void runtime_queuefinalizer(void *p
, FuncVal
*fn
, const struct __go_func_type
*ft
, const struct __go_ptr_type
*ot
);
486 void runtime_freeallspecials(MSpan
*span
, void *p
, uintptr size
);
487 bool runtime_freespecial(Special
*s
, void *p
, uintptr size
, bool freed
);
491 TypeInfo_SingleObject
= 0,
495 // Enables type information at the end of blocks allocated from heap
496 DebugTypeAtBlockEnd
= 0,
499 // Information from the compiler about the layout of stack frames.
500 typedef struct BitVector BitVector
;
503 int32 n
; // # of bits
506 typedef struct StackMap StackMap
;
509 int32 n
; // number of bitmaps
510 int32 nbit
; // number of bits in each bitmap
520 // BitsMultiWord will be set for the first word of a multi-word item.
521 // When it is set, one of the following will be set for the second word.
527 // Returns pointer map data for the given stackmap index
528 // (the index is encoded in PCDATA_StackMapIndex).
529 BitVector
runtime_stackmapdata(StackMap
*stackmap
, int32 n
);
531 // defined in mgc0.go
532 void runtime_gc_m_ptr(Eface
*);
533 void runtime_gc_g_ptr(Eface
*);
534 void runtime_gc_itab_ptr(Eface
*);
536 void runtime_memorydump(void);
537 int32
runtime_setgcpercent(int32
)
538 __asm__ (GOSYM_PREFIX
"runtime.setgcpercent");
540 // Value we use to mark dead pointers when GODEBUG=gcdead=1.
541 #define PoisonGC ((uintptr)0xf969696969696969ULL)
542 #define PoisonStack ((uintptr)0x6868686868686868ULL)