LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Memory allocator, based on tcmalloc. | 5 // Memory allocator, based on tcmalloc. |
6 // http://goog-perftools.sourceforge.net/doc/tcmalloc.html | 6 // http://goog-perftools.sourceforge.net/doc/tcmalloc.html |
7 | 7 |
8 // The main allocator works in runs of pages. | 8 // The main allocator works in runs of pages. |
9 // Small allocation sizes (up to and including 32 kB) are | 9 // Small allocation sizes (up to and including 32 kB) are |
10 // rounded to one of about 100 size classes, each of which | 10 // rounded to one of about 100 size classes, each of which |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 // On 64-bit, we limit the arena to 16G, so 22 bits suffices. | 116 // On 64-bit, we limit the arena to 16G, so 22 bits suffices. |
117 // On 32-bit, we don't bother limiting anything: 20 bits for 4G. | 117 // On 32-bit, we don't bother limiting anything: 20 bits for 4G. |
118 #ifdef _64BIT | 118 #ifdef _64BIT |
119 MHeapMap_Bits = 22, | 119 MHeapMap_Bits = 22, |
120 #else | 120 #else |
121 MHeapMap_Bits = 20, | 121 MHeapMap_Bits = 20, |
122 #endif | 122 #endif |
123 | 123 |
124 // Max number of threads to run garbage collection. | 124 // Max number of threads to run garbage collection. |
125 // 2, 3, and 4 are all plausible maximums depending | 125 // 2, 3, and 4 are all plausible maximums depending |
126 » // on the hardware details of the machine. The second | 126 » // on the hardware details of the machine. The garbage |
127 » // proc is the one that helps the most (after the first), | 127 » // collector scales well to 4 cpus. |
128 » // so start with just 2 for now. | 128 » MaxGcproc = 4, |
129 » MaxGcproc = 2, | |
130 }; | 129 }; |
131 | 130 |
132 // A generic linked list of blocks. (Typically the block is bigger than sizeof(
MLink).) | 131 // A generic linked list of blocks. (Typically the block is bigger than sizeof(
MLink).) |
133 struct MLink | 132 struct MLink |
134 { | 133 { |
135 MLink *next; | 134 MLink *next; |
136 }; | 135 }; |
137 | 136 |
138 // SysAlloc obtains a large chunk of zeroed memory from the | 137 // SysAlloc obtains a large chunk of zeroed memory from the |
139 // operating system, typically on the order of a hundred kilobytes | 138 // operating system, typically on the order of a hundred kilobytes |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 bool debuggc; | 229 bool debuggc; |
231 | 230 |
232 // Statistics about allocation size classes. | 231 // Statistics about allocation size classes. |
233 struct { | 232 struct { |
234 uint32 size; | 233 uint32 size; |
235 uint64 nmalloc; | 234 uint64 nmalloc; |
236 uint64 nfree; | 235 uint64 nfree; |
237 } by_size[NumSizeClasses]; | 236 } by_size[NumSizeClasses]; |
238 }; | 237 }; |
239 | 238 |
240 #define mstats runtime·MemStats»/* name shared with Go */ | 239 #define mstats runtime·memStats»/* name shared with Go */ |
241 extern MStats mstats; | 240 extern MStats mstats; |
242 | 241 |
243 | 242 |
244 // Size classes. Computed and initialized by InitSizes. | 243 // Size classes. Computed and initialized by InitSizes. |
245 // | 244 // |
246 // SizeToClass(0 <= n <= MaxSmallSize) returns the size class, | 245 // SizeToClass(0 <= n <= MaxSmallSize) returns the size class, |
247 // 1 <= sizeclass < NumSizeClasses, for n. | 246 // 1 <= sizeclass < NumSizeClasses, for n. |
248 // Size class 0 is reserved to mean "not small". | 247 // Size class 0 is reserved to mean "not small". |
249 // | 248 // |
250 // class_to_size[i] = largest size in class i | 249 // class_to_size[i] = largest size in class i |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 { | 306 { |
308 MSpan *next; // in a span linked list | 307 MSpan *next; // in a span linked list |
309 MSpan *prev; // in a span linked list | 308 MSpan *prev; // in a span linked list |
310 MSpan *allnext; // in the list of all spans | 309 MSpan *allnext; // in the list of all spans |
311 PageID start; // starting page number | 310 PageID start; // starting page number |
312 uintptr npages; // number of pages in span | 311 uintptr npages; // number of pages in span |
313 MLink *freelist; // list of free objects | 312 MLink *freelist; // list of free objects |
314 uint32 ref; // number of allocated objects in this span | 313 uint32 ref; // number of allocated objects in this span |
315 uint32 sizeclass; // size class | 314 uint32 sizeclass; // size class |
316 uint32 state; // MSpanInUse etc | 315 uint32 state; // MSpanInUse etc |
317 » int64 unusedsince;» // First time spotted by gc in MSpanFree state | 316 » int64 unusedsince;» // First time spotted by GC in MSpanFree state |
318 uintptr npreleased; // number of pages released to the OS | 317 uintptr npreleased; // number of pages released to the OS |
319 byte *limit; // end of data in span | 318 byte *limit; // end of data in span |
320 }; | 319 }; |
321 | 320 |
322 void runtime·MSpan_Init(MSpan *span, PageID start, uintptr npages); | 321 void runtime·MSpan_Init(MSpan *span, PageID start, uintptr npages); |
323 | 322 |
324 // Every MSpan is in one doubly-linked list, | 323 // Every MSpan is in one doubly-linked list, |
325 // either one of the MHeap's free lists or one of the | 324 // either one of the MHeap's free lists or one of the |
326 // MCentral's span lists. We use empty MSpan structures as list heads. | 325 // MCentral's span lists. We use empty MSpan structures as list heads. |
327 void runtime·MSpanList_Init(MSpan *list); | 326 void runtime·MSpanList_Init(MSpan *list); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 extern MHeap runtime·mheap; | 378 extern MHeap runtime·mheap; |
380 | 379 |
381 void runtime·MHeap_Init(MHeap *h, void *(*allocator)(uintptr)); | 380 void runtime·MHeap_Init(MHeap *h, void *(*allocator)(uintptr)); |
382 MSpan* runtime·MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, int32 acct
); | 381 MSpan* runtime·MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, int32 acct
); |
383 void runtime·MHeap_Free(MHeap *h, MSpan *s, int32 acct); | 382 void runtime·MHeap_Free(MHeap *h, MSpan *s, int32 acct); |
384 MSpan* runtime·MHeap_Lookup(MHeap *h, void *v); | 383 MSpan* runtime·MHeap_Lookup(MHeap *h, void *v); |
385 MSpan* runtime·MHeap_LookupMaybe(MHeap *h, void *v); | 384 MSpan* runtime·MHeap_LookupMaybe(MHeap *h, void *v); |
386 void runtime·MGetSizeClassInfo(int32 sizeclass, uintptr *size, int32 *npages,
int32 *nobj); | 385 void runtime·MGetSizeClassInfo(int32 sizeclass, uintptr *size, int32 *npages,
int32 *nobj); |
387 void* runtime·MHeap_SysAlloc(MHeap *h, uintptr n); | 386 void* runtime·MHeap_SysAlloc(MHeap *h, uintptr n); |
388 void runtime·MHeap_MapBits(MHeap *h); | 387 void runtime·MHeap_MapBits(MHeap *h); |
389 void» runtime·MHeap_Scavenger(); | 388 void» runtime·MHeap_Scavenger(void); |
390 | 389 |
391 void* runtime·mallocgc(uintptr size, uint32 flag, int32 dogc, int32 zeroed); | 390 void* runtime·mallocgc(uintptr size, uint32 flag, int32 dogc, int32 zeroed); |
392 int32 runtime·mlookup(void *v, byte **base, uintptr *size, MSpan **s); | 391 int32 runtime·mlookup(void *v, byte **base, uintptr *size, MSpan **s); |
393 void runtime·gc(int32 force); | 392 void runtime·gc(int32 force); |
394 void runtime·markallocated(void *v, uintptr n, bool noptr); | 393 void runtime·markallocated(void *v, uintptr n, bool noptr); |
395 void runtime·checkallocated(void *v, uintptr n); | 394 void runtime·checkallocated(void *v, uintptr n); |
396 void runtime·markfreed(void *v, uintptr n); | 395 void runtime·markfreed(void *v, uintptr n); |
397 void runtime·checkfreed(void *v, uintptr n); | 396 void runtime·checkfreed(void *v, uintptr n); |
398 int32 runtime·checking; | 397 int32 runtime·checking; |
399 void runtime·markspan(void *v, uintptr size, uintptr n, bool leftover); | 398 void runtime·markspan(void *v, uintptr size, uintptr n, bool leftover); |
(...skipping 19 matching lines...) Expand all Loading... |
419 // Must match definition in extern.go. | 418 // Must match definition in extern.go. |
420 enum { | 419 enum { |
421 MProf_None = 0, | 420 MProf_None = 0, |
422 MProf_Sample = 1, | 421 MProf_Sample = 1, |
423 MProf_All = 2, | 422 MProf_All = 2, |
424 }; | 423 }; |
425 extern int32 runtime·malloc_profile; | 424 extern int32 runtime·malloc_profile; |
426 | 425 |
427 bool runtime·getfinalizer(void *p, bool del, void (**fn)(void*), int32 *nret)
; | 426 bool runtime·getfinalizer(void *p, bool del, void (**fn)(void*), int32 *nret)
; |
428 void runtime·walkfintab(void (*fn)(void*)); | 427 void runtime·walkfintab(void (*fn)(void*)); |
LEFT | RIGHT |