2008-12-18 16:42:28 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-05-22 03:29:17 -06:00
|
|
|
// Per-P malloc cache for small objects.
|
2008-12-18 16:42:28 -07:00
|
|
|
//
|
|
|
|
// See malloc.h for an overview.
|
|
|
|
|
|
|
|
#include "runtime.h"
|
2011-12-16 13:33:58 -07:00
|
|
|
#include "arch_GOARCH.h"
|
2008-12-18 16:42:28 -07:00
|
|
|
#include "malloc.h"
|
|
|
|
|
2013-05-28 01:05:55 -06:00
|
|
|
void
|
|
|
|
runtime·MCache_Refill(MCache *c, int32 sizeclass)
|
2008-12-18 16:42:28 -07:00
|
|
|
{
|
|
|
|
MCacheList *l;
|
|
|
|
|
2013-05-28 01:05:55 -06:00
|
|
|
// Replenish using central lists.
|
2008-12-18 16:42:28 -07:00
|
|
|
l = &c->list[sizeclass];
|
2013-05-28 01:05:55 -06:00
|
|
|
if(l->list)
|
|
|
|
runtime·throw("MCache_Refill: the list is not empty");
|
|
|
|
l->nlist = runtime·MCentral_AllocList(&runtime·mheap->central[sizeclass], &l->list);
|
|
|
|
if(l->list == nil)
|
|
|
|
runtime·throw("out of memory");
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
|
2008-12-19 04:13:39 -07:00
|
|
|
// Take n elements off l and return them to the central free list.
|
|
|
|
static void
|
2013-05-22 03:29:17 -06:00
|
|
|
ReleaseN(MCacheList *l, int32 n, int32 sizeclass)
|
2008-12-19 04:13:39 -07:00
|
|
|
{
|
|
|
|
MLink *first, **lp;
|
|
|
|
int32 i;
|
|
|
|
|
|
|
|
// Cut off first n elements.
|
|
|
|
first = l->list;
|
|
|
|
lp = &l->list;
|
|
|
|
for(i=0; i<n; i++)
|
|
|
|
lp = &(*lp)->next;
|
|
|
|
l->list = *lp;
|
|
|
|
*lp = nil;
|
|
|
|
l->nlist -= n;
|
|
|
|
|
|
|
|
// Return them to central free list.
|
2013-05-22 03:29:17 -06:00
|
|
|
runtime·MCentral_FreeList(&runtime·mheap->central[sizeclass], first);
|
2008-12-19 04:13:39 -07:00
|
|
|
}
|
|
|
|
|
2008-12-18 16:42:28 -07:00
|
|
|
void
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·MCache_Free(MCache *c, void *v, int32 sizeclass, uintptr size)
|
2008-12-18 16:42:28 -07:00
|
|
|
{
|
|
|
|
MCacheList *l;
|
2008-12-19 04:13:39 -07:00
|
|
|
MLink *p;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
// Put back on list.
|
|
|
|
l = &c->list[sizeclass];
|
2008-12-19 04:13:39 -07:00
|
|
|
p = v;
|
|
|
|
p->next = l->list;
|
2008-12-18 16:42:28 -07:00
|
|
|
l->list = p;
|
|
|
|
l->nlist++;
|
2011-07-18 12:52:57 -06:00
|
|
|
c->local_cachealloc -= size;
|
2010-09-07 07:57:22 -06:00
|
|
|
c->local_objects--;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
2013-05-22 03:29:17 -06:00
|
|
|
// We transfer span at a time from MCentral to MCache,
|
|
|
|
// if we have 2 times more than that, release a half back.
|
|
|
|
if(l->nlist >= 2*(runtime·class_to_allocnpages[sizeclass]<<PageShift)/size)
|
|
|
|
ReleaseN(l, l->nlist/2, sizeclass);
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
|
2010-03-08 15:15:44 -07:00
|
|
|
void
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·MCache_ReleaseAll(MCache *c)
|
2010-03-08 15:15:44 -07:00
|
|
|
{
|
|
|
|
int32 i;
|
|
|
|
MCacheList *l;
|
|
|
|
|
|
|
|
for(i=0; i<NumSizeClasses; i++) {
|
|
|
|
l = &c->list[i];
|
2013-05-22 03:29:17 -06:00
|
|
|
if(l->list) {
|
|
|
|
runtime·MCentral_FreeList(&runtime·mheap->central[i], l->list);
|
|
|
|
l->list = nil;
|
|
|
|
l->nlist = 0;
|
|
|
|
}
|
2010-03-08 15:15:44 -07:00
|
|
|
}
|
|
|
|
}
|