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.
|
|
|
|
|
|
|
|
// See malloc.h for overview.
|
|
|
|
//
|
|
|
|
// TODO(rsc): double-check stats.
|
|
|
|
|
2009-06-30 21:01:50 -06:00
|
|
|
package malloc
|
2008-12-18 16:42:28 -07:00
|
|
|
#include "runtime.h"
|
|
|
|
#include "malloc.h"
|
2009-03-30 01:01:07 -06:00
|
|
|
#include "defs.h"
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
MHeap mheap;
|
|
|
|
MStats mstats;
|
|
|
|
|
|
|
|
// Allocate an object of at least size bytes.
|
|
|
|
// Small objects are allocated from the per-thread cache's free lists.
|
|
|
|
// Large objects (> 32 kB) are allocated straight from the heap.
|
|
|
|
void*
|
2009-12-03 18:22:23 -07:00
|
|
|
mallocgc(uintptr size, uint32 refflag, int32 dogc)
|
2008-12-18 16:42:28 -07:00
|
|
|
{
|
|
|
|
int32 sizeclass;
|
|
|
|
MCache *c;
|
|
|
|
uintptr npages;
|
|
|
|
MSpan *s;
|
|
|
|
void *v;
|
2009-01-26 18:37:05 -07:00
|
|
|
uint32 *ref;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
2010-01-12 11:03:02 -07:00
|
|
|
if(gcwaiting && g != m->g0 && m->locks == 0)
|
2010-01-09 10:47:45 -07:00
|
|
|
gosched();
|
2008-12-19 04:13:39 -07:00
|
|
|
if(m->mallocing)
|
2009-05-29 14:31:53 -06:00
|
|
|
throw("malloc/free - deadlock");
|
2008-12-19 04:13:39 -07:00
|
|
|
m->mallocing = 1;
|
2008-12-18 16:42:28 -07:00
|
|
|
if(size == 0)
|
|
|
|
size = 1;
|
|
|
|
|
2009-12-03 18:22:23 -07:00
|
|
|
mstats.nmalloc++;
|
2008-12-18 16:42:28 -07:00
|
|
|
if(size <= MaxSmallSize) {
|
|
|
|
// Allocate from mcache free lists.
|
|
|
|
sizeclass = SizeToClass(size);
|
|
|
|
size = class_to_size[sizeclass];
|
|
|
|
c = m->mcache;
|
|
|
|
v = MCache_Alloc(c, sizeclass, size);
|
|
|
|
if(v == nil)
|
2008-12-19 04:13:39 -07:00
|
|
|
throw("out of memory");
|
2008-12-18 16:42:28 -07:00
|
|
|
mstats.alloc += size;
|
2008-12-19 04:13:39 -07:00
|
|
|
} else {
|
|
|
|
// TODO(rsc): Report tracebacks for very large allocations.
|
|
|
|
|
|
|
|
// Allocate directly from heap.
|
|
|
|
npages = size >> PageShift;
|
|
|
|
if((size & PageMask) != 0)
|
|
|
|
npages++;
|
|
|
|
s = MHeap_Alloc(&mheap, npages, 0);
|
|
|
|
if(s == nil)
|
|
|
|
throw("out of memory");
|
|
|
|
mstats.alloc += npages<<PageShift;
|
|
|
|
v = (void*)(s->start << PageShift);
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
|
2009-01-26 18:37:05 -07:00
|
|
|
// setup for mark sweep
|
2009-06-04 22:09:06 -06:00
|
|
|
if(!mlookup(v, nil, nil, &ref)) {
|
|
|
|
printf("malloc %D; mlookup failed\n", (uint64)size);
|
|
|
|
throw("malloc mlookup");
|
|
|
|
}
|
2009-12-03 18:22:23 -07:00
|
|
|
*ref = RefNone | refflag;
|
2009-01-26 18:37:05 -07:00
|
|
|
|
2008-12-19 04:13:39 -07:00
|
|
|
m->mallocing = 0;
|
2009-12-03 18:22:23 -07:00
|
|
|
|
|
|
|
if(dogc && mstats.inuse_pages > mstats.next_gc)
|
|
|
|
gc(0);
|
2008-12-19 04:13:39 -07:00
|
|
|
return v;
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
|
2009-01-26 18:37:05 -07:00
|
|
|
void*
|
2009-12-03 18:22:23 -07:00
|
|
|
malloc(uintptr size)
|
2009-01-26 18:37:05 -07:00
|
|
|
{
|
2009-12-03 18:22:23 -07:00
|
|
|
return mallocgc(size, 0, 0);
|
2009-01-26 18:37:05 -07:00
|
|
|
}
|
|
|
|
|
2008-12-18 16:42:28 -07:00
|
|
|
// Free the object whose base pointer is v.
|
|
|
|
void
|
|
|
|
free(void *v)
|
|
|
|
{
|
|
|
|
int32 sizeclass, size;
|
|
|
|
uintptr page, tmp;
|
|
|
|
MSpan *s;
|
|
|
|
MCache *c;
|
2009-01-26 18:37:05 -07:00
|
|
|
uint32 *ref;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
2009-01-09 17:22:13 -07:00
|
|
|
if(v == nil)
|
|
|
|
return;
|
|
|
|
|
2009-05-29 14:31:53 -06:00
|
|
|
if(m->mallocing)
|
|
|
|
throw("malloc/free - deadlock");
|
|
|
|
m->mallocing = 1;
|
|
|
|
|
2009-06-04 22:09:06 -06:00
|
|
|
if(!mlookup(v, nil, nil, &ref))
|
|
|
|
throw("free mlookup");
|
2009-01-26 18:37:05 -07:00
|
|
|
*ref = RefFree;
|
|
|
|
|
2008-12-18 16:42:28 -07:00
|
|
|
// Find size class for v.
|
|
|
|
page = (uintptr)v >> PageShift;
|
|
|
|
sizeclass = MHeapMapCache_GET(&mheap.mapcache, page, tmp);
|
|
|
|
if(sizeclass == 0) {
|
|
|
|
// Missed in cache.
|
|
|
|
s = MHeap_Lookup(&mheap, page);
|
|
|
|
if(s == nil)
|
|
|
|
throw("free - invalid pointer");
|
|
|
|
sizeclass = s->sizeclass;
|
|
|
|
if(sizeclass == 0) {
|
|
|
|
// Large object.
|
|
|
|
mstats.alloc -= s->npages<<PageShift;
|
2009-10-16 00:10:49 -06:00
|
|
|
runtime_memclr(v, s->npages<<PageShift);
|
2008-12-18 16:42:28 -07:00
|
|
|
MHeap_Free(&mheap, s);
|
2009-05-29 14:31:53 -06:00
|
|
|
goto out;
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
MHeapMapCache_SET(&mheap.mapcache, page, sizeclass);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Small object.
|
|
|
|
c = m->mcache;
|
|
|
|
size = class_to_size[sizeclass];
|
2009-10-16 00:10:49 -06:00
|
|
|
runtime_memclr(v, size);
|
2008-12-18 16:42:28 -07:00
|
|
|
mstats.alloc -= size;
|
|
|
|
MCache_Free(c, v, sizeclass, size);
|
2009-05-29 14:31:53 -06:00
|
|
|
|
|
|
|
out:
|
|
|
|
m->mallocing = 0;
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
|
2009-01-26 18:37:05 -07:00
|
|
|
int32
|
|
|
|
mlookup(void *v, byte **base, uintptr *size, uint32 **ref)
|
2008-12-19 04:13:39 -07:00
|
|
|
{
|
2009-02-06 15:41:21 -07:00
|
|
|
uintptr n, nobj, i;
|
2009-02-11 18:54:03 -07:00
|
|
|
byte *p;
|
2008-12-19 04:13:39 -07:00
|
|
|
MSpan *s;
|
|
|
|
|
2009-12-03 18:22:23 -07:00
|
|
|
mstats.nlookup++;
|
2009-01-26 18:37:05 -07:00
|
|
|
s = MHeap_LookupMaybe(&mheap, (uintptr)v>>PageShift);
|
2008-12-19 04:13:39 -07:00
|
|
|
if(s == nil) {
|
2009-01-26 18:37:05 -07:00
|
|
|
if(base)
|
|
|
|
*base = nil;
|
|
|
|
if(size)
|
|
|
|
*size = 0;
|
|
|
|
if(ref)
|
|
|
|
*ref = 0;
|
|
|
|
return 0;
|
2008-12-19 04:13:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
p = (byte*)((uintptr)s->start<<PageShift);
|
|
|
|
if(s->sizeclass == 0) {
|
|
|
|
// Large object.
|
2009-01-26 18:37:05 -07:00
|
|
|
if(base)
|
|
|
|
*base = p;
|
|
|
|
if(size)
|
|
|
|
*size = s->npages<<PageShift;
|
|
|
|
if(ref)
|
|
|
|
*ref = &s->gcref0;
|
|
|
|
return 1;
|
2008-12-19 04:13:39 -07:00
|
|
|
}
|
|
|
|
|
2009-02-06 15:41:21 -07:00
|
|
|
if((byte*)v >= (byte*)s->gcref) {
|
|
|
|
// pointers into the gc ref counts
|
|
|
|
// do not count as pointers.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-19 04:13:39 -07:00
|
|
|
n = class_to_size[s->sizeclass];
|
2009-01-26 18:37:05 -07:00
|
|
|
i = ((byte*)v - p)/n;
|
|
|
|
if(base)
|
|
|
|
*base = p + i*n;
|
|
|
|
if(size)
|
|
|
|
*size = n;
|
2009-02-06 15:41:21 -07:00
|
|
|
nobj = (s->npages << PageShift) / (n + RefcountOverhead);
|
|
|
|
if((byte*)s->gcref < p || (byte*)(s->gcref+nobj) > p+(s->npages<<PageShift)) {
|
2009-02-15 14:41:48 -07:00
|
|
|
printf("odd span state=%d span=%p base=%p sizeclass=%d n=%D size=%D npages=%D\n",
|
2009-03-30 01:01:07 -06:00
|
|
|
s->state, s, p, s->sizeclass, (uint64)nobj, (uint64)n, (uint64)s->npages);
|
2009-02-15 14:41:48 -07:00
|
|
|
printf("s->base sizeclass %d v=%p base=%p gcref=%p blocksize=%D nobj=%D size=%D end=%p end=%p\n",
|
2009-03-30 01:01:07 -06:00
|
|
|
s->sizeclass, v, p, s->gcref, (uint64)s->npages<<PageShift,
|
|
|
|
(uint64)nobj, (uint64)n, s->gcref + nobj, p+(s->npages<<PageShift));
|
2009-01-26 18:37:05 -07:00
|
|
|
throw("bad gcref");
|
|
|
|
}
|
|
|
|
if(ref)
|
|
|
|
*ref = &s->gcref[i];
|
|
|
|
|
|
|
|
return 1;
|
2008-12-19 04:13:39 -07:00
|
|
|
}
|
|
|
|
|
2008-12-18 16:42:28 -07:00
|
|
|
MCache*
|
|
|
|
allocmcache(void)
|
|
|
|
{
|
|
|
|
return FixAlloc_Alloc(&mheap.cachealloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mallocinit(void)
|
|
|
|
{
|
|
|
|
InitSizes();
|
|
|
|
MHeap_Init(&mheap, SysAlloc);
|
|
|
|
m->mcache = allocmcache();
|
|
|
|
|
|
|
|
// See if it works.
|
|
|
|
free(malloc(1));
|
|
|
|
}
|
|
|
|
|
2008-12-19 04:13:39 -07:00
|
|
|
// Runtime stubs.
|
|
|
|
|
|
|
|
void*
|
|
|
|
mal(uint32 n)
|
|
|
|
{
|
2009-12-03 18:22:23 -07:00
|
|
|
return mallocgc(n, 0, 1);
|
2008-12-19 04:13:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stack allocator uses malloc/free most of the time,
|
|
|
|
// but if we're in the middle of malloc and need stack,
|
|
|
|
// we have to do something else to avoid deadlock.
|
|
|
|
// In that case, we fall back on a fixed-size free-list
|
|
|
|
// allocator, assuming that inside malloc all the stack
|
|
|
|
// frames are small, so that all the stack allocations
|
|
|
|
// will be a single size, the minimum (right now, 5k).
|
|
|
|
struct {
|
|
|
|
Lock;
|
|
|
|
FixAlloc;
|
|
|
|
} stacks;
|
|
|
|
|
|
|
|
void*
|
|
|
|
stackalloc(uint32 n)
|
|
|
|
{
|
|
|
|
void *v;
|
2009-01-26 18:37:05 -07:00
|
|
|
uint32 *ref;
|
2008-12-19 04:13:39 -07:00
|
|
|
|
2009-06-15 22:31:56 -06:00
|
|
|
if(m->mallocing || m->gcing) {
|
2008-12-19 04:13:39 -07:00
|
|
|
lock(&stacks);
|
|
|
|
if(stacks.size == 0)
|
2009-01-28 16:22:16 -07:00
|
|
|
FixAlloc_Init(&stacks, n, SysAlloc, nil, nil);
|
2008-12-19 04:13:39 -07:00
|
|
|
if(stacks.size != n) {
|
2009-03-30 01:01:07 -06:00
|
|
|
printf("stackalloc: in malloc, size=%D want %d", (uint64)stacks.size, n);
|
2008-12-19 04:13:39 -07:00
|
|
|
throw("stackalloc");
|
|
|
|
}
|
|
|
|
v = FixAlloc_Alloc(&stacks);
|
|
|
|
unlock(&stacks);
|
|
|
|
return v;
|
|
|
|
}
|
2009-01-26 18:37:05 -07:00
|
|
|
v = malloc(n);
|
2009-06-04 22:09:06 -06:00
|
|
|
if(!mlookup(v, nil, nil, &ref))
|
|
|
|
throw("stackalloc mlookup");
|
2009-01-26 18:37:05 -07:00
|
|
|
*ref = RefStack;
|
|
|
|
return v;
|
2008-12-19 04:13:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
stackfree(void *v)
|
|
|
|
{
|
2009-06-15 22:31:56 -06:00
|
|
|
if(m->mallocing || m->gcing) {
|
2008-12-19 04:13:39 -07:00
|
|
|
lock(&stacks);
|
|
|
|
FixAlloc_Free(&stacks, v);
|
|
|
|
unlock(&stacks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(v);
|
|
|
|
}
|
2009-06-30 21:01:50 -06:00
|
|
|
|
|
|
|
func Alloc(n uintptr) (p *byte) {
|
|
|
|
p = malloc(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
func Free(p *byte) {
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
func Lookup(p *byte) (base *byte, size uintptr) {
|
|
|
|
mlookup(p, &base, &size, nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetStats() (s *MStats) {
|
|
|
|
s = &mstats;
|
|
|
|
}
|
|
|
|
|
|
|
|
func GC() {
|
|
|
|
gc(1);
|
|
|
|
}
|