github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/runtime/mcache.c (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  // Per-thread (in Go, per-M) malloc cache for small objects.
     6  //
     7  // See malloc.h for an overview.
     8  
     9  #include "runtime.h"
    10  #include "arch_GOARCH.h"
    11  #include "malloc.h"
    12  
    13  void*
    14  runtime·MCache_Alloc(MCache *c, int32 sizeclass, uintptr size, int32 zeroed)
    15  {
    16  	MCacheList *l;
    17  	MLink *first, *v;
    18  	int32 n;
    19  
    20  	// Allocate from list.
    21  	l = &c->list[sizeclass];
    22  	if(l->list == nil) {
    23  		// Replenish using central lists.
    24  		n = runtime·MCentral_AllocList(&runtime·mheap->central[sizeclass],
    25  			runtime·class_to_transfercount[sizeclass], &first);
    26  		if(n == 0)
    27  			runtime·throw("out of memory");
    28  		l->list = first;
    29  		l->nlist = n;
    30  		c->size += n*size;
    31  	}
    32  	v = l->list;
    33  	l->list = v->next;
    34  	l->nlist--;
    35  	if(l->nlist < l->nlistmin)
    36  		l->nlistmin = l->nlist;
    37  	c->size -= size;
    38  
    39  	// v is zeroed except for the link pointer
    40  	// that we used above; zero that.
    41  	v->next = nil;
    42  	if(zeroed) {
    43  		// block is zeroed iff second word is zero ...
    44  		if(size > sizeof(uintptr) && ((uintptr*)v)[1] != 0)
    45  			runtime·memclr((byte*)v, size);
    46  	}
    47  	c->local_cachealloc += size;
    48  	c->local_objects++;
    49  	return v;
    50  }
    51  
    52  // Take n elements off l and return them to the central free list.
    53  static void
    54  ReleaseN(MCache *c, MCacheList *l, int32 n, int32 sizeclass)
    55  {
    56  	MLink *first, **lp;
    57  	int32 i;
    58  
    59  	// Cut off first n elements.
    60  	first = l->list;
    61  	lp = &l->list;
    62  	for(i=0; i<n; i++)
    63  		lp = &(*lp)->next;
    64  	l->list = *lp;
    65  	*lp = nil;
    66  	l->nlist -= n;
    67  	if(l->nlist < l->nlistmin)
    68  		l->nlistmin = l->nlist;
    69  	c->size -= n*runtime·class_to_size[sizeclass];
    70  
    71  	// Return them to central free list.
    72  	runtime·MCentral_FreeList(&runtime·mheap->central[sizeclass], n, first);
    73  }
    74  
    75  void
    76  runtime·MCache_Free(MCache *c, void *v, int32 sizeclass, uintptr size)
    77  {
    78  	int32 i, n;
    79  	MCacheList *l;
    80  	MLink *p;
    81  
    82  	// Put back on list.
    83  	l = &c->list[sizeclass];
    84  	p = v;
    85  	p->next = l->list;
    86  	l->list = p;
    87  	l->nlist++;
    88  	c->size += size;
    89  	c->local_cachealloc -= size;
    90  	c->local_objects--;
    91  
    92  	if(l->nlist >= MaxMCacheListLen) {
    93  		// Release a chunk back.
    94  		ReleaseN(c, l, runtime·class_to_transfercount[sizeclass], sizeclass);
    95  	}
    96  
    97  	if(c->size >= MaxMCacheSize) {
    98  		// Scavenge.
    99  		for(i=0; i<NumSizeClasses; i++) {
   100  			l = &c->list[i];
   101  			n = l->nlistmin;
   102  
   103  			// n is the minimum number of elements we've seen on
   104  			// the list since the last scavenge.  If n > 0, it means that
   105  			// we could have gotten by with n fewer elements
   106  			// without needing to consult the central free list.
   107  			// Move toward that situation by releasing n/2 of them.
   108  			if(n > 0) {
   109  				if(n > 1)
   110  					n /= 2;
   111  				ReleaseN(c, l, n, i);
   112  			}
   113  			l->nlistmin = l->nlist;
   114  		}
   115  	}
   116  }
   117  
   118  void
   119  runtime·MCache_ReleaseAll(MCache *c)
   120  {
   121  	int32 i;
   122  	MCacheList *l;
   123  
   124  	for(i=0; i<NumSizeClasses; i++) {
   125  		l = &c->list[i];
   126  		ReleaseN(c, l, l->nlist, i);
   127  		l->nlistmin = 0;
   128  	}
   129  }