diff options
Diffstat (limited to 'src/pkg/runtime/malloc.h')
-rw-r--r-- | src/pkg/runtime/malloc.h | 132 |
1 files changed, 104 insertions, 28 deletions
diff --git a/src/pkg/runtime/malloc.h b/src/pkg/runtime/malloc.h index d846f6810..38122bf8a 100644 --- a/src/pkg/runtime/malloc.h +++ b/src/pkg/runtime/malloc.h @@ -85,6 +85,8 @@ typedef struct MHeap MHeap; typedef struct MSpan MSpan; typedef struct MStats MStats; typedef struct MLink MLink; +typedef struct MTypes MTypes; +typedef struct GCStats GCStats; enum { @@ -113,21 +115,30 @@ enum HeapAllocChunk = 1<<20, // Chunk size for heap growth // Number of bits in page to span calculations (4k pages). - // On 64-bit, we limit the arena to 16G, so 22 bits suffices. - // On 32-bit, we don't bother limiting anything: 20 bits for 4G. + // On 64-bit, we limit the arena to 128GB, or 37 bits. + // On 32-bit, we don't bother limiting anything, so we use the full 32-bit address. #ifdef _64BIT - MHeapMap_Bits = 22, + MHeapMap_Bits = 37 - PageShift, #else - MHeapMap_Bits = 20, + MHeapMap_Bits = 32 - PageShift, #endif // Max number of threads to run garbage collection. // 2, 3, and 4 are all plausible maximums depending // on the hardware details of the machine. The garbage - // collector scales well to 4 cpus. - MaxGcproc = 4, + // collector scales well to 8 cpus. + MaxGcproc = 8, }; +// Maximum memory allocation size, a hint for callers. +// This must be a #define instead of an enum because it +// is so large. +#ifdef _64BIT +#define MaxMem (1ULL<<(MHeapMap_Bits+PageShift)) /* 128 GB */ +#else +#define MaxMem ((uintptr)-1) +#endif + // A generic linked list of blocks. (Typically the block is bigger than sizeof(MLink).) struct MLink { @@ -188,7 +199,7 @@ void runtime·FixAlloc_Free(FixAlloc *f, void *p); // Statistics. -// Shared with Go: if you edit this structure, also edit extern.go. +// Shared with Go: if you edit this structure, also edit type MemStats in mem.go. struct MStats { // General statistics. @@ -219,7 +230,7 @@ struct MStats uint64 buckhash_sys; // profiling bucket hash table // Statistics about garbage collector. - // Protected by stopping the world during GC. + // Protected by mheap or stopping the world during GC. uint64 next_gc; // next GC (in heap_alloc time) uint64 last_gc; // last GC (in absolute time) uint64 pause_total_ns; @@ -239,7 +250,6 @@ struct MStats #define mstats runtime·memStats /* name shared with Go */ extern MStats mstats; - // Size classes. Computed and initialized by InitSizes. // // SizeToClass(0 <= n <= MaxSmallSize) returns the size class, @@ -273,19 +283,19 @@ struct MCacheList struct MCache { MCacheList list[NumSizeClasses]; - uint64 size; - int64 local_cachealloc; // bytes allocated (or freed) from cache since last lock of heap - int64 local_objects; // objects allocated (or freed) from cache since last lock of heap - int64 local_alloc; // bytes allocated (or freed) since last lock of heap - int64 local_total_alloc; // bytes allocated (even if freed) since last lock of heap - int64 local_nmalloc; // number of mallocs since last lock of heap - int64 local_nfree; // number of frees since last lock of heap - int64 local_nlookup; // number of pointer lookups since last lock of heap + uintptr size; + intptr local_cachealloc; // bytes allocated (or freed) from cache since last lock of heap + intptr local_objects; // objects allocated (or freed) from cache since last lock of heap + intptr local_alloc; // bytes allocated (or freed) since last lock of heap + uintptr local_total_alloc; // bytes allocated (even if freed) since last lock of heap + uintptr local_nmalloc; // number of mallocs since last lock of heap + uintptr local_nfree; // number of frees since last lock of heap + uintptr local_nlookup; // number of pointer lookups since last lock of heap int32 next_sample; // trigger heap sample after allocating this many bytes // Statistics about allocation size classes since last lock of heap struct { - int64 nmalloc; - int64 nfree; + uintptr nmalloc; + uintptr nfree; } local_by_size[NumSizeClasses]; }; @@ -294,6 +304,44 @@ void* runtime·MCache_Alloc(MCache *c, int32 sizeclass, uintptr size, int32 zero void runtime·MCache_Free(MCache *c, void *p, int32 sizeclass, uintptr size); void runtime·MCache_ReleaseAll(MCache *c); +// MTypes describes the types of blocks allocated within a span. +// The compression field describes the layout of the data. +// +// MTypes_Empty: +// All blocks are free, or no type information is available for +// allocated blocks. +// The data field has no meaning. +// MTypes_Single: +// The span contains just one block. +// The data field holds the type information. +// The sysalloc field has no meaning. +// MTypes_Words: +// The span contains multiple blocks. +// The data field points to an array of type [NumBlocks]uintptr, +// and each element of the array holds the type of the corresponding +// block. +// MTypes_Bytes: +// The span contains at most seven different types of blocks. +// The data field points to the following structure: +// struct { +// type [8]uintptr // type[0] is always 0 +// index [NumBlocks]byte +// } +// The type of the i-th block is: data.type[data.index[i]] +enum +{ + MTypes_Empty = 0, + MTypes_Single = 1, + MTypes_Words = 2, + MTypes_Bytes = 3, +}; +struct MTypes +{ + byte compression; // one of MTypes_* + bool sysalloc; // whether (void*)data is from runtime·SysAlloc + uintptr data; +}; + // An MSpan is a run of pages. enum { @@ -306,16 +354,17 @@ struct MSpan { MSpan *next; // in a span linked list MSpan *prev; // in a span linked list - MSpan *allnext; // in the list of all spans PageID start; // starting page number uintptr npages; // number of pages in span MLink *freelist; // list of free objects uint32 ref; // number of allocated objects in this span - uint32 sizeclass; // size class + int32 sizeclass; // size class + uintptr elemsize; // computed from sizeclass or from npages uint32 state; // MSpanInUse etc int64 unusedsince; // First time spotted by GC in MSpanFree state uintptr npreleased; // number of pages released to the OS byte *limit; // end of data in span + MTypes types; // types of allocated objects in this span }; void runtime·MSpan_Init(MSpan *span, PageID start, uintptr npages); @@ -342,6 +391,7 @@ struct MCentral void runtime·MCentral_Init(MCentral *c, int32 sizeclass); int32 runtime·MCentral_AllocList(MCentral *c, int32 n, MLink **first); void runtime·MCentral_FreeList(MCentral *c, int32 n, MLink *first); +void runtime·MCentral_FreeSpan(MCentral *c, MSpan *s, int32 n, MLink *start, MLink *end); // Main malloc heap. // The heap itself is the "free[]" and "large" arrays, @@ -351,7 +401,9 @@ struct MHeap Lock; MSpan free[MaxMHeapList]; // free lists of given length MSpan large; // free lists length >= MaxMHeapList - MSpan *allspans; + MSpan **allspans; + uint32 nspan; + uint32 nspancap; // span lookup MSpan *map[1<<MHeapMap_Bits]; @@ -375,10 +427,10 @@ struct MHeap FixAlloc spanalloc; // allocator for Span* FixAlloc cachealloc; // allocator for MCache* }; -extern MHeap runtime·mheap; +extern MHeap *runtime·mheap; void runtime·MHeap_Init(MHeap *h, void *(*allocator)(uintptr)); -MSpan* runtime·MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, int32 acct); +MSpan* runtime·MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, int32 acct, int32 zeroed); void runtime·MHeap_Free(MHeap *h, MSpan *s, int32 acct); MSpan* runtime·MHeap_Lookup(MHeap *h, void *v); MSpan* runtime·MHeap_LookupMaybe(MHeap *h, void *v); @@ -394,12 +446,18 @@ void runtime·markallocated(void *v, uintptr n, bool noptr); void runtime·checkallocated(void *v, uintptr n); void runtime·markfreed(void *v, uintptr n); void runtime·checkfreed(void *v, uintptr n); -int32 runtime·checking; +extern int32 runtime·checking; void runtime·markspan(void *v, uintptr size, uintptr n, bool leftover); void runtime·unmarkspan(void *v, uintptr size); bool runtime·blockspecial(void*); void runtime·setblockspecial(void*, bool); -void runtime·purgecachedstats(M*); +void runtime·purgecachedstats(MCache*); +void* runtime·cnew(Type*); + +void runtime·settype(void*, uintptr); +void runtime·settype_flush(M*, bool); +void runtime·settype_sysfree(MSpan*); +uintptr runtime·gettype(void*); enum { @@ -412,8 +470,26 @@ enum void runtime·MProf_Malloc(void*, uintptr); void runtime·MProf_Free(void*, uintptr); void runtime·MProf_GC(void); -int32 runtime·helpgc(bool*); +int32 runtime·gcprocs(void); +void runtime·helpgc(int32 nproc); void runtime·gchelper(void); -bool runtime·getfinalizer(void *p, bool del, void (**fn)(void*), int32 *nret); +bool runtime·getfinalizer(void *p, bool del, FuncVal **fn, uintptr *nret); void runtime·walkfintab(void (*fn)(void*)); + +enum +{ + TypeInfo_SingleObject = 0, + TypeInfo_Array = 1, + TypeInfo_Map = 2, + TypeInfo_Chan = 3, + + // Enables type information at the end of blocks allocated from heap + DebugTypeAtBlockEnd = 0, +}; + +// defined in mgc0.go +void runtime·gc_m_ptr(Eface*); +void runtime·gc_itab_ptr(Eface*); + +void runtime·memorydump(void); |