github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/runtime/malloc.h (about)

     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.
     4  
     5  // Memory allocator, based on tcmalloc.
     6  // http://goog-perftools.sourceforge.net/doc/tcmalloc.html
     7  
     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
    14  // allocators.
    15  //
    16  // The allocator's data structures are:
    17  //
    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.
    25  //
    26  // Allocating a small object proceeds up a hierarchy of caches:
    27  //
    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.
    32  //
    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.
    36  //
    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
    41  //	   the heap.
    42  //
    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.
    47  //
    48  // Freeing a small object proceeds up the same hierarchy:
    49  //
    50  //	1. Look up the size class for the object and add it to
    51  //	   the MCache free list.
    52  //
    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.
    55  //
    56  //	3. If all the objects in a given span have returned to
    57  //	   the MCentral list, return that span to the page heap.
    58  //
    59  //	4. If the heap has too much memory, return some to the
    60  //	   operating system.
    61  //
    62  //	TODO(rsc): Step 4 is not implemented.
    63  //
    64  // Allocating and freeing a large object uses the page heap
    65  // directly, bypassing the MCache and MCentral free lists.
    66  //
    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
    73  // zeroing this way:
    74  //
    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.
    79  //
    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, ...).
    82  
    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;
    90  
    91  enum
    92  {
    93  	PageShift	= 13,
    94  	PageSize	= 1<<PageShift,
    95  	PageMask	= PageSize - 1,
    96  };
    97  typedef	uintptr	PageID;		// address >> PageShift
    98  
    99  enum
   100  {
   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 = 67,
   107  
   108  	// Tunable constants.
   109  	MaxSmallSize = 32<<10,
   110  
   111  	// Tiny allocator parameters, see "Tiny allocator" comment in malloc.goc.
   112  	TinySize = 16,
   113  	TinySizeClass = 2,
   114  
   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
   118  
   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  #ifdef _64BIT
   124  #ifdef GOOS_windows
   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,
   129  #else
   130  	MHeapMap_Bits = 37 - PageShift,
   131  #endif
   132  #else
   133  	MHeapMap_Bits = 32 - PageShift,
   134  #endif
   135  
   136  	// Max number of threads to run garbage collection.
   137  	// 2, 3, and 4 are all plausible maximums depending
   138  	// on the hardware details of the machine.  The garbage
   139  	// collector scales well to 8 cpus.
   140  	MaxGcproc = 8,
   141  };
   142  
   143  // Maximum memory allocation size, a hint for callers.
   144  // This must be a #define instead of an enum because it
   145  // is so large.
   146  #ifdef _64BIT
   147  #define	MaxMem	(1ULL<<(MHeapMap_Bits+PageShift))	/* 128 GB or 32 GB */
   148  #else
   149  #define	MaxMem	((uintptr)-1)
   150  #endif
   151  
   152  // A generic linked list of blocks.  (Typically the block is bigger than sizeof(MLink).)
   153  struct MLink
   154  {
   155  	MLink *next;
   156  };
   157  
   158  // SysAlloc obtains a large chunk of zeroed memory from the
   159  // operating system, typically on the order of a hundred kilobytes
   160  // or a megabyte.
   161  // NOTE: SysAlloc returns OS-aligned memory, but the heap allocator
   162  // may use larger alignment, so the caller must be careful to realign the
   163  // memory obtained by SysAlloc.
   164  //
   165  // SysUnused notifies the operating system that the contents
   166  // of the memory region are no longer needed and can be reused
   167  // for other purposes.
   168  // SysUsed notifies the operating system that the contents
   169  // of the memory region are needed again.
   170  //
   171  // SysFree returns it unconditionally; this is only used if
   172  // an out-of-memory error has been detected midway through
   173  // an allocation.  It is okay if SysFree is a no-op.
   174  //
   175  // SysReserve reserves address space without allocating memory.
   176  // If the pointer passed to it is non-nil, the caller wants the
   177  // reservation there, but SysReserve can still choose another
   178  // location if that one is unavailable.  On some systems and in some
   179  // cases SysReserve will simply check that the address space is
   180  // available and not actually reserve it.  If SysReserve returns
   181  // non-nil, it sets *reserved to true if the address space is
   182  // reserved, false if it has merely been checked.
   183  // NOTE: SysReserve returns OS-aligned memory, but the heap allocator
   184  // may use larger alignment, so the caller must be careful to realign the
   185  // memory obtained by SysAlloc.
   186  //
   187  // SysMap maps previously reserved address space for use.
   188  // The reserved argument is true if the address space was really
   189  // reserved, not merely checked.
   190  //
   191  // SysFault marks a (already SysAlloc'd) region to fault
   192  // if accessed.  Used only for debugging the runtime.
   193  
   194  void*	runtime·SysAlloc(uintptr nbytes, uint64 *stat);
   195  void	runtime·SysFree(void *v, uintptr nbytes, uint64 *stat);
   196  void	runtime·SysUnused(void *v, uintptr nbytes);
   197  void	runtime·SysUsed(void *v, uintptr nbytes);
   198  void	runtime·SysMap(void *v, uintptr nbytes, bool reserved, uint64 *stat);
   199  void*	runtime·SysReserve(void *v, uintptr nbytes, bool *reserved);
   200  void	runtime·SysFault(void *v, uintptr nbytes);
   201  
   202  // FixAlloc is a simple free-list allocator for fixed size objects.
   203  // Malloc uses a FixAlloc wrapped around SysAlloc to manages its
   204  // MCache and MSpan objects.
   205  //
   206  // Memory returned by FixAlloc_Alloc is not zeroed.
   207  // The caller is responsible for locking around FixAlloc calls.
   208  // Callers can keep state in the object but the first word is
   209  // smashed by freeing and reallocating.
   210  struct FixAlloc
   211  {
   212  	uintptr	size;
   213  	void	(*first)(void *arg, byte *p);	// called first time p is returned
   214  	void*	arg;
   215  	MLink*	list;
   216  	byte*	chunk;
   217  	uint32	nchunk;
   218  	uintptr	inuse;	// in-use bytes now
   219  	uint64*	stat;
   220  };
   221  
   222  void	runtime·FixAlloc_Init(FixAlloc *f, uintptr size, void (*first)(void*, byte*), void *arg, uint64 *stat);
   223  void*	runtime·FixAlloc_Alloc(FixAlloc *f);
   224  void	runtime·FixAlloc_Free(FixAlloc *f, void *p);
   225  
   226  
   227  // Statistics.
   228  // Shared with Go: if you edit this structure, also edit type MemStats in mem.go.
   229  struct MStats
   230  {
   231  	// General statistics.
   232  	uint64	alloc;		// bytes allocated and still in use
   233  	uint64	total_alloc;	// bytes allocated (even if freed)
   234  	uint64	sys;		// bytes obtained from system (should be sum of xxx_sys below, no locking, approximate)
   235  	uint64	nlookup;	// number of pointer lookups
   236  	uint64	nmalloc;	// number of mallocs
   237  	uint64	nfree;  // number of frees
   238  
   239  	// Statistics about malloc heap.
   240  	// protected by mheap.Lock
   241  	uint64	heap_alloc;	// bytes allocated and still in use
   242  	uint64	heap_sys;	// bytes obtained from system
   243  	uint64	heap_idle;	// bytes in idle spans
   244  	uint64	heap_inuse;	// bytes in non-idle spans
   245  	uint64	heap_released;	// bytes released to the OS
   246  	uint64	heap_objects;	// total number of allocated objects
   247  
   248  	// Statistics about allocation of low-level fixed-size structures.
   249  	// Protected by FixAlloc locks.
   250  	uint64	stacks_inuse;	// bootstrap stacks
   251  	uint64	stacks_sys;
   252  	uint64	mspan_inuse;	// MSpan structures
   253  	uint64	mspan_sys;
   254  	uint64	mcache_inuse;	// MCache structures
   255  	uint64	mcache_sys;
   256  	uint64	buckhash_sys;	// profiling bucket hash table
   257  	uint64	gc_sys;
   258  	uint64	other_sys;
   259  
   260  	// Statistics about garbage collector.
   261  	// Protected by mheap or stopping the world during GC.
   262  	uint64	next_gc;	// next GC (in heap_alloc time)
   263  	uint64  last_gc;	// last GC (in absolute time)
   264  	uint64	pause_total_ns;
   265  	uint64	pause_ns[256];
   266  	uint32	numgc;
   267  	bool	enablegc;
   268  	bool	debuggc;
   269  
   270  	// Statistics about allocation size classes.
   271  	struct {
   272  		uint32 size;
   273  		uint64 nmalloc;
   274  		uint64 nfree;
   275  	} by_size[NumSizeClasses];
   276  };
   277  
   278  #define mstats runtime·memStats
   279  extern MStats mstats;
   280  void	runtime·updatememstats(GCStats *stats);
   281  
   282  // Size classes.  Computed and initialized by InitSizes.
   283  //
   284  // SizeToClass(0 <= n <= MaxSmallSize) returns the size class,
   285  //	1 <= sizeclass < NumSizeClasses, for n.
   286  //	Size class 0 is reserved to mean "not small".
   287  //
   288  // class_to_size[i] = largest size in class i
   289  // class_to_allocnpages[i] = number of pages to allocate when
   290  //	making new objects in class i
   291  
   292  int32	runtime·SizeToClass(int32);
   293  uintptr	runtime·roundupsize(uintptr);
   294  extern	int32	runtime·class_to_size[NumSizeClasses];
   295  extern	int32	runtime·class_to_allocnpages[NumSizeClasses];
   296  extern	int8	runtime·size_to_class8[1024/8 + 1];
   297  extern	int8	runtime·size_to_class128[(MaxSmallSize-1024)/128 + 1];
   298  extern	void	runtime·InitSizes(void);
   299  
   300  
   301  typedef struct MCacheList MCacheList;
   302  struct MCacheList
   303  {
   304  	MLink *list;
   305  	uint32 nlist;
   306  };
   307  
   308  // Per-thread (in Go, per-P) cache for small objects.
   309  // No locking needed because it is per-thread (per-P).
   310  struct MCache
   311  {
   312  	// The following members are accessed on every malloc,
   313  	// so they are grouped here for better caching.
   314  	int32 next_sample;		// trigger heap sample after allocating this many bytes
   315  	intptr local_cachealloc;	// bytes allocated (or freed) from cache since last lock of heap
   316  	// Allocator cache for tiny objects w/o pointers.
   317  	// See "Tiny allocator" comment in malloc.goc.
   318  	byte*	tiny;
   319  	uintptr	tinysize;
   320  	// The rest is not accessed on every malloc.
   321  	MSpan*	alloc[NumSizeClasses];	// spans to allocate from
   322  	MCacheList free[NumSizeClasses];// lists of explicitly freed objects
   323  	// Local allocator stats, flushed during GC.
   324  	uintptr local_nlookup;		// number of pointer lookups
   325  	uintptr local_largefree;	// bytes freed for large objects (>MaxSmallSize)
   326  	uintptr local_nlargefree;	// number of frees for large objects (>MaxSmallSize)
   327  	uintptr local_nsmallfree[NumSizeClasses];	// number of frees for small objects (<=MaxSmallSize)
   328  };
   329  
   330  MSpan*	runtime·MCache_Refill(MCache *c, int32 sizeclass);
   331  void	runtime·MCache_Free(MCache *c, MLink *p, int32 sizeclass, uintptr size);
   332  void	runtime·MCache_ReleaseAll(MCache *c);
   333  
   334  // MTypes describes the types of blocks allocated within a span.
   335  // The compression field describes the layout of the data.
   336  //
   337  // MTypes_Empty:
   338  //     All blocks are free, or no type information is available for
   339  //     allocated blocks.
   340  //     The data field has no meaning.
   341  // MTypes_Single:
   342  //     The span contains just one block.
   343  //     The data field holds the type information.
   344  //     The sysalloc field has no meaning.
   345  // MTypes_Words:
   346  //     The span contains multiple blocks.
   347  //     The data field points to an array of type [NumBlocks]uintptr,
   348  //     and each element of the array holds the type of the corresponding
   349  //     block.
   350  // MTypes_Bytes:
   351  //     The span contains at most seven different types of blocks.
   352  //     The data field points to the following structure:
   353  //         struct {
   354  //             type  [8]uintptr       // type[0] is always 0
   355  //             index [NumBlocks]byte
   356  //         }
   357  //     The type of the i-th block is: data.type[data.index[i]]
   358  enum
   359  {
   360  	MTypes_Empty = 0,
   361  	MTypes_Single = 1,
   362  	MTypes_Words = 2,
   363  	MTypes_Bytes = 3,
   364  };
   365  struct MTypes
   366  {
   367  	byte	compression;	// one of MTypes_*
   368  	uintptr	data;
   369  };
   370  
   371  enum
   372  {
   373  	KindSpecialFinalizer = 1,
   374  	KindSpecialProfile = 2,
   375  	// Note: The finalizer special must be first because if we're freeing
   376  	// an object, a finalizer special will cause the freeing operation
   377  	// to abort, and we want to keep the other special records around
   378  	// if that happens.
   379  };
   380  
   381  typedef struct Special Special;
   382  struct Special
   383  {
   384  	Special*	next;	// linked list in span
   385  	uint16		offset;	// span offset of object
   386  	byte		kind;	// kind of Special
   387  };
   388  
   389  // The described object has a finalizer set for it.
   390  typedef struct SpecialFinalizer SpecialFinalizer;
   391  struct SpecialFinalizer
   392  {
   393  	Special;
   394  	FuncVal*	fn;
   395  	uintptr		nret;
   396  	Type*		fint;
   397  	PtrType*	ot;
   398  };
   399  
   400  // The described object is being heap profiled.
   401  typedef struct Bucket Bucket; // from mprof.goc
   402  typedef struct SpecialProfile SpecialProfile;
   403  struct SpecialProfile
   404  {
   405  	Special;
   406  	Bucket*	b;
   407  };
   408  
   409  // An MSpan is a run of pages.
   410  enum
   411  {
   412  	MSpanInUse = 0,
   413  	MSpanFree,
   414  	MSpanListHead,
   415  	MSpanDead,
   416  };
   417  struct MSpan
   418  {
   419  	MSpan	*next;		// in a span linked list
   420  	MSpan	*prev;		// in a span linked list
   421  	PageID	start;		// starting page number
   422  	uintptr	npages;		// number of pages in span
   423  	MLink	*freelist;	// list of free objects
   424  	// sweep generation:
   425  	// if sweepgen == h->sweepgen - 2, the span needs sweeping
   426  	// if sweepgen == h->sweepgen - 1, the span is currently being swept
   427  	// if sweepgen == h->sweepgen, the span is swept and ready to use
   428  	// h->sweepgen is incremented by 2 after every GC
   429  	uint32	sweepgen;
   430  	uint16	ref;		// capacity - number of objects in freelist
   431  	uint8	sizeclass;	// size class
   432  	bool	incache;	// being used by an MCache
   433  	uint8	state;		// MSpanInUse etc
   434  	uint8	needzero;	// needs to be zeroed before allocation
   435  	uintptr	elemsize;	// computed from sizeclass or from npages
   436  	int64   unusedsince;	// First time spotted by GC in MSpanFree state
   437  	uintptr npreleased;	// number of pages released to the OS
   438  	byte	*limit;		// end of data in span
   439  	MTypes	types;		// types of allocated objects in this span
   440  	Lock	specialLock;	// guards specials list
   441  	Special	*specials;	// linked list of special records sorted by offset.
   442  	MLink	*freebuf;	// objects freed explicitly, not incorporated into freelist yet
   443  };
   444  
   445  void	runtime·MSpan_Init(MSpan *span, PageID start, uintptr npages);
   446  void	runtime·MSpan_EnsureSwept(MSpan *span);
   447  bool	runtime·MSpan_Sweep(MSpan *span);
   448  
   449  // Every MSpan is in one doubly-linked list,
   450  // either one of the MHeap's free lists or one of the
   451  // MCentral's span lists.  We use empty MSpan structures as list heads.
   452  void	runtime·MSpanList_Init(MSpan *list);
   453  bool	runtime·MSpanList_IsEmpty(MSpan *list);
   454  void	runtime·MSpanList_Insert(MSpan *list, MSpan *span);
   455  void	runtime·MSpanList_InsertBack(MSpan *list, MSpan *span);
   456  void	runtime·MSpanList_Remove(MSpan *span);	// from whatever list it is in
   457  
   458  
   459  // Central list of free objects of a given size.
   460  struct MCentral
   461  {
   462  	Lock;
   463  	int32 sizeclass;
   464  	MSpan nonempty;	// list of spans with a free object
   465  	MSpan empty;	// list of spans with no free objects (or cached in an MCache)
   466  	int32 nfree;	// # of objects available in nonempty spans
   467  };
   468  
   469  void	runtime·MCentral_Init(MCentral *c, int32 sizeclass);
   470  MSpan*	runtime·MCentral_CacheSpan(MCentral *c);
   471  void	runtime·MCentral_UncacheSpan(MCentral *c, MSpan *s);
   472  bool	runtime·MCentral_FreeSpan(MCentral *c, MSpan *s, int32 n, MLink *start, MLink *end);
   473  void	runtime·MCentral_FreeList(MCentral *c, MLink *start); // TODO: need this?
   474  
   475  // Main malloc heap.
   476  // The heap itself is the "free[]" and "large" arrays,
   477  // but all the other global data is here too.
   478  struct MHeap
   479  {
   480  	Lock;
   481  	MSpan free[MaxMHeapList];	// free lists of given length
   482  	MSpan freelarge;		// free lists length >= MaxMHeapList
   483  	MSpan busy[MaxMHeapList];	// busy lists of large objects of given length
   484  	MSpan busylarge;		// busy lists of large objects length >= MaxMHeapList
   485  	MSpan **allspans;		// all spans out there
   486  	MSpan **sweepspans;		// copy of allspans referenced by sweeper
   487  	uint32	nspan;
   488  	uint32	nspancap;
   489  	uint32	sweepgen;		// sweep generation, see comment in MSpan
   490  	uint32	sweepdone;		// all spans are swept
   491  
   492  	// span lookup
   493  	MSpan**	spans;
   494  	uintptr	spans_mapped;
   495  
   496  	// range of addresses we might see in the heap
   497  	byte *bitmap;
   498  	uintptr bitmap_mapped;
   499  	byte *arena_start;
   500  	byte *arena_used;
   501  	byte *arena_end;
   502  	bool arena_reserved;
   503  
   504  	// central free lists for small size classes.
   505  	// the padding makes sure that the MCentrals are
   506  	// spaced CacheLineSize bytes apart, so that each MCentral.Lock
   507  	// gets its own cache line.
   508  	struct {
   509  		MCentral;
   510  		byte pad[CacheLineSize];
   511  	} central[NumSizeClasses];
   512  
   513  	FixAlloc spanalloc;	// allocator for Span*
   514  	FixAlloc cachealloc;	// allocator for MCache*
   515  	FixAlloc specialfinalizeralloc;	// allocator for SpecialFinalizer*
   516  	FixAlloc specialprofilealloc;	// allocator for SpecialProfile*
   517  	Lock speciallock; // lock for sepcial record allocators.
   518  
   519  	// Malloc stats.
   520  	uint64 largefree;	// bytes freed for large objects (>MaxSmallSize)
   521  	uint64 nlargefree;	// number of frees for large objects (>MaxSmallSize)
   522  	uint64 nsmallfree[NumSizeClasses];	// number of frees for small objects (<=MaxSmallSize)
   523  };
   524  extern MHeap runtime·mheap;
   525  
   526  void	runtime·MHeap_Init(MHeap *h);
   527  MSpan*	runtime·MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, bool large, bool needzero);
   528  void	runtime·MHeap_Free(MHeap *h, MSpan *s, int32 acct);
   529  MSpan*	runtime·MHeap_Lookup(MHeap *h, void *v);
   530  MSpan*	runtime·MHeap_LookupMaybe(MHeap *h, void *v);
   531  void	runtime·MGetSizeClassInfo(int32 sizeclass, uintptr *size, int32 *npages, int32 *nobj);
   532  void*	runtime·MHeap_SysAlloc(MHeap *h, uintptr n);
   533  void	runtime·MHeap_MapBits(MHeap *h);
   534  void	runtime·MHeap_MapSpans(MHeap *h);
   535  void	runtime·MHeap_Scavenger(void);
   536  void	runtime·MHeap_SplitSpan(MHeap *h, MSpan *s);
   537  
   538  void*	runtime·mallocgc(uintptr size, uintptr typ, uint32 flag);
   539  void*	runtime·persistentalloc(uintptr size, uintptr align, uint64 *stat);
   540  int32	runtime·mlookup(void *v, byte **base, uintptr *size, MSpan **s);
   541  void	runtime·gc(int32 force);
   542  uintptr	runtime·sweepone(void);
   543  void	runtime·markscan(void *v);
   544  void	runtime·marknogc(void *v);
   545  void	runtime·checkallocated(void *v, uintptr n);
   546  void	runtime·markfreed(void *v);
   547  void	runtime·checkfreed(void *v, uintptr n);
   548  extern	int32	runtime·checking;
   549  void	runtime·markspan(void *v, uintptr size, uintptr n, bool leftover);
   550  void	runtime·unmarkspan(void *v, uintptr size);
   551  void	runtime·purgecachedstats(MCache*);
   552  void*	runtime·cnew(Type*);
   553  void*	runtime·cnewarray(Type*, intgo);
   554  void	runtime·tracealloc(void*, uintptr, uintptr);
   555  void	runtime·tracefree(void*, uintptr);
   556  void	runtime·tracegc(void);
   557  
   558  uintptr	runtime·gettype(void*);
   559  
   560  enum
   561  {
   562  	// flags to malloc
   563  	FlagNoScan	= 1<<0,	// GC doesn't have to scan object
   564  	FlagNoProfiling	= 1<<1,	// must not profile
   565  	FlagNoGC	= 1<<2,	// must not free or scan for pointers
   566  	FlagNoZero	= 1<<3, // don't zero memory
   567  	FlagNoInvokeGC	= 1<<4, // don't invoke GC
   568  };
   569  
   570  void	runtime·MProf_Malloc(void*, uintptr);
   571  void	runtime·MProf_Free(Bucket*, uintptr, bool);
   572  void	runtime·MProf_GC(void);
   573  void	runtime·iterate_memprof(void (*callback)(Bucket*, uintptr, uintptr*, uintptr, uintptr, uintptr));
   574  int32	runtime·gcprocs(void);
   575  void	runtime·helpgc(int32 nproc);
   576  void	runtime·gchelper(void);
   577  void	runtime·createfing(void);
   578  G*	runtime·wakefing(void);
   579  extern bool	runtime·fingwait;
   580  extern bool	runtime·fingwake;
   581  
   582  void	runtime·setprofilebucket(void *p, Bucket *b);
   583  
   584  bool	runtime·addfinalizer(void*, FuncVal *fn, uintptr, Type*, PtrType*);
   585  void	runtime·removefinalizer(void*);
   586  void	runtime·queuefinalizer(byte *p, FuncVal *fn, uintptr nret, Type *fint, PtrType *ot);
   587  
   588  void	runtime·freeallspecials(MSpan *span, void *p, uintptr size);
   589  bool	runtime·freespecial(Special *s, void *p, uintptr size, bool freed);
   590  
   591  enum
   592  {
   593  	TypeInfo_SingleObject = 0,
   594  	TypeInfo_Array = 1,
   595  	TypeInfo_Chan = 2,
   596  
   597  	// Enables type information at the end of blocks allocated from heap	
   598  	DebugTypeAtBlockEnd = 0,
   599  };
   600  
   601  // Information from the compiler about the layout of stack frames.
   602  typedef struct BitVector BitVector;
   603  struct BitVector
   604  {
   605  	int32 n; // # of bits
   606  	uint32 *data;
   607  };
   608  typedef struct StackMap StackMap;
   609  struct StackMap
   610  {
   611  	int32 n; // number of bitmaps
   612  	int32 nbit; // number of bits in each bitmap
   613  	uint32 data[];
   614  };
   615  enum {
   616  	// Pointer map
   617  	BitsPerPointer = 2,
   618  	BitsDead = 0,
   619  	BitsScalar = 1,
   620  	BitsPointer = 2,
   621  	BitsMultiWord = 3,
   622  	// BitsMultiWord will be set for the first word of a multi-word item.
   623  	// When it is set, one of the following will be set for the second word.
   624  	BitsString = 0,
   625  	BitsSlice = 1,
   626  	BitsIface = 2,
   627  	BitsEface = 3,
   628  };
   629  // Returns pointer map data for the given stackmap index
   630  // (the index is encoded in PCDATA_StackMapIndex).
   631  BitVector	runtime·stackmapdata(StackMap *stackmap, int32 n);
   632  
   633  // defined in mgc0.go
   634  void	runtime·gc_m_ptr(Eface*);
   635  void	runtime·gc_g_ptr(Eface*);
   636  void	runtime·gc_itab_ptr(Eface*);
   637  
   638  void	runtime·memorydump(void);
   639  int32	runtime·setgcpercent(int32);
   640  
   641  // Value we use to mark dead pointers when GODEBUG=gcdead=1.
   642  #define PoisonGC ((uintptr)0xf969696969696969ULL)
   643  #define PoisonStack ((uintptr)0x6868686868686868ULL)