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.
|
|
|
|
|
|
|
|
|
|
// Page heap.
|
|
|
|
|
//
|
|
|
|
|
// See malloc.h for overview.
|
|
|
|
|
//
|
|
|
|
|
// When a MSpan is in the heap free list, state == MSpanFree
|
|
|
|
|
// and heapmap(s->start) == span, heapmap(s->start+s->npages-1) == span.
|
|
|
|
|
//
|
|
|
|
|
// When a MSpan is allocated, state == MSpanInUse
|
|
|
|
|
// and heapmap(i) == span for all s->start <= i < s->start+s->npages.
|
|
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
|
|
static MSpan *MHeap_AllocLocked(MHeap*, uintptr, int32);
|
|
|
|
|
static bool MHeap_Grow(MHeap*, uintptr);
|
|
|
|
|
static void MHeap_FreeLocked(MHeap*, MSpan*);
|
|
|
|
|
static MSpan *MHeap_AllocLarge(MHeap*, uintptr);
|
|
|
|
|
static MSpan *BestFit(MSpan*, uintptr, MSpan*);
|
|
|
|
|
|
2009-01-28 16:22:16 -07:00
|
|
|
|
static void
|
|
|
|
|
RecordSpan(void *vh, byte *p)
|
|
|
|
|
{
|
|
|
|
|
MHeap *h;
|
|
|
|
|
MSpan *s;
|
2012-04-09 03:05:43 -06:00
|
|
|
|
MSpan **all;
|
|
|
|
|
uint32 cap;
|
2009-01-28 16:22:16 -07:00
|
|
|
|
|
|
|
|
|
h = vh;
|
|
|
|
|
s = (MSpan*)p;
|
2012-04-09 03:05:43 -06:00
|
|
|
|
if(h->nspan >= h->nspancap) {
|
|
|
|
|
cap = 64*1024/sizeof(all[0]);
|
|
|
|
|
if(cap < h->nspancap*3/2)
|
|
|
|
|
cap = h->nspancap*3/2;
|
|
|
|
|
all = (MSpan**)runtime·SysAlloc(cap*sizeof(all[0]));
|
2013-02-28 22:21:08 -07:00
|
|
|
|
if(all == nil)
|
|
|
|
|
runtime·throw("runtime: cannot allocate memory");
|
2012-04-09 03:05:43 -06:00
|
|
|
|
if(h->allspans) {
|
|
|
|
|
runtime·memmove(all, h->allspans, h->nspancap*sizeof(all[0]));
|
|
|
|
|
runtime·SysFree(h->allspans, h->nspancap*sizeof(all[0]));
|
|
|
|
|
}
|
|
|
|
|
h->allspans = all;
|
|
|
|
|
h->nspancap = cap;
|
|
|
|
|
}
|
|
|
|
|
h->allspans[h->nspan++] = s;
|
2009-01-28 16:22:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-18 16:42:28 -07:00
|
|
|
|
// Initialize the heap; fetch memory using alloc.
|
|
|
|
|
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·MHeap_Init(MHeap *h, void *(*alloc)(uintptr))
|
2008-12-18 16:42:28 -07:00
|
|
|
|
{
|
2009-01-13 10:55:24 -07:00
|
|
|
|
uint32 i;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
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·FixAlloc_Init(&h->spanalloc, sizeof(MSpan), alloc, RecordSpan, h);
|
|
|
|
|
runtime·FixAlloc_Init(&h->cachealloc, sizeof(MCache), alloc, nil, nil);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
// h->mapcache needs no init
|
|
|
|
|
for(i=0; i<nelem(h->free); i++)
|
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·MSpanList_Init(&h->free[i]);
|
|
|
|
|
runtime·MSpanList_Init(&h->large);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
for(i=0; i<nelem(h->central); i++)
|
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·MCentral_Init(&h->central[i], i);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allocate a new span of npage pages from the heap
|
|
|
|
|
// and record its size class in the HeapMap and HeapMapCache.
|
|
|
|
|
MSpan*
|
2012-05-02 08:01:11 -06:00
|
|
|
|
runtime·MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, int32 acct, int32 zeroed)
|
2008-12-18 16:42:28 -07:00
|
|
|
|
{
|
|
|
|
|
MSpan *s;
|
|
|
|
|
|
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·lock(h);
|
2013-05-22 12:22:57 -06:00
|
|
|
|
mstats.heap_alloc += m->mcache->local_cachealloc;
|
|
|
|
|
m->mcache->local_cachealloc = 0;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
s = MHeap_AllocLocked(h, npage, sizeclass);
|
2010-03-08 15:15:44 -07:00
|
|
|
|
if(s != nil) {
|
2010-03-29 14:06:26 -06:00
|
|
|
|
mstats.heap_inuse += npage<<PageShift;
|
2010-09-07 07:57:22 -06:00
|
|
|
|
if(acct) {
|
|
|
|
|
mstats.heap_objects++;
|
2010-03-08 15:15:44 -07:00
|
|
|
|
mstats.heap_alloc += npage<<PageShift;
|
2010-09-07 07:57:22 -06:00
|
|
|
|
}
|
2010-03-08 15:15:44 -07:00
|
|
|
|
}
|
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·unlock(h);
|
2012-05-02 08:01:11 -06:00
|
|
|
|
if(s != nil && *(uintptr*)(s->start<<PageShift) != 0 && zeroed)
|
|
|
|
|
runtime·memclr((byte*)(s->start<<PageShift), s->npages<<PageShift);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static MSpan*
|
|
|
|
|
MHeap_AllocLocked(MHeap *h, uintptr npage, int32 sizeclass)
|
|
|
|
|
{
|
|
|
|
|
uintptr n;
|
|
|
|
|
MSpan *s, *t;
|
2011-01-28 13:03:26 -07:00
|
|
|
|
PageID p;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
|
|
// Try in fixed-size lists up to max.
|
|
|
|
|
for(n=npage; n < nelem(h->free); n++) {
|
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
|
|
|
|
if(!runtime·MSpanList_IsEmpty(&h->free[n])) {
|
2008-12-18 16:42:28 -07:00
|
|
|
|
s = h->free[n].next;
|
|
|
|
|
goto HaveSpan;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Best fit in list of large spans.
|
|
|
|
|
if((s = MHeap_AllocLarge(h, npage)) == nil) {
|
|
|
|
|
if(!MHeap_Grow(h, npage))
|
|
|
|
|
return nil;
|
|
|
|
|
if((s = MHeap_AllocLarge(h, npage)) == nil)
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HaveSpan:
|
|
|
|
|
// Mark span in use.
|
|
|
|
|
if(s->state != MSpanFree)
|
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·throw("MHeap_AllocLocked - MSpan not free");
|
2008-12-18 16:42:28 -07:00
|
|
|
|
if(s->npages < npage)
|
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·throw("MHeap_AllocLocked - bad npages");
|
|
|
|
|
runtime·MSpanList_Remove(s);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
s->state = MSpanInUse;
|
2011-09-14 09:29:01 -06:00
|
|
|
|
mstats.heap_idle -= s->npages<<PageShift;
|
2012-02-16 11:30:04 -07:00
|
|
|
|
mstats.heap_released -= s->npreleased<<PageShift;
|
2013-04-16 10:08:06 -06:00
|
|
|
|
if(s->npreleased > 0) {
|
|
|
|
|
// We have called runtime·SysUnused with these pages, and on
|
|
|
|
|
// Unix systems it called madvise. At this point at least
|
|
|
|
|
// some BSD-based kernels will return these pages either as
|
|
|
|
|
// zeros or with the old data. For our caller, the first word
|
|
|
|
|
// in the page indicates whether the span contains zeros or
|
|
|
|
|
// not (this word was set when the span was freed by
|
|
|
|
|
// MCentral_Free or runtime·MCentral_FreeSpan). If the first
|
|
|
|
|
// page in the span is returned as zeros, and some subsequent
|
|
|
|
|
// page is returned with the old data, then we will be
|
|
|
|
|
// returning a span that is assumed to be all zeros, but the
|
|
|
|
|
// actual data will not be all zeros. Avoid that problem by
|
|
|
|
|
// explicitly marking the span as not being zeroed, just in
|
|
|
|
|
// case. The beadbead constant we use here means nothing, it
|
|
|
|
|
// is just a unique constant not seen elsewhere in the
|
|
|
|
|
// runtime, as a clue in case it turns up unexpectedly in
|
|
|
|
|
// memory or in a stack trace.
|
|
|
|
|
*(uintptr*)(s->start<<PageShift) = (uintptr)0xbeadbeadbeadbeadULL;
|
|
|
|
|
}
|
2012-02-16 11:30:04 -07:00
|
|
|
|
s->npreleased = 0;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
|
|
if(s->npages > npage) {
|
|
|
|
|
// Trim extra and put it back in the heap.
|
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
|
|
|
|
t = runtime·FixAlloc_Alloc(&h->spanalloc);
|
2010-03-29 14:06:26 -06:00
|
|
|
|
mstats.mspan_inuse = h->spanalloc.inuse;
|
|
|
|
|
mstats.mspan_sys = h->spanalloc.sys;
|
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·MSpan_Init(t, s->start + npage, s->npages - npage);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
s->npages = npage;
|
2011-01-28 13:03:26 -07:00
|
|
|
|
p = t->start;
|
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
|
p -= ((uintptr)h->arena_start>>PageShift);
|
|
|
|
|
if(p > 0)
|
|
|
|
|
h->map[p-1] = s;
|
|
|
|
|
h->map[p] = t;
|
|
|
|
|
h->map[p+t->npages-1] = t;
|
|
|
|
|
*(uintptr*)(t->start<<PageShift) = *(uintptr*)(s->start<<PageShift); // copy "needs zeroing" mark
|
2008-12-18 16:42:28 -07:00
|
|
|
|
t->state = MSpanInUse;
|
|
|
|
|
MHeap_FreeLocked(h, t);
|
2013-01-28 10:53:35 -07:00
|
|
|
|
t->unusedsince = s->unusedsince; // preserve age
|
2008-12-18 16:42:28 -07:00
|
|
|
|
}
|
2013-01-28 10:53:35 -07:00
|
|
|
|
s->unusedsince = 0;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
2010-02-10 22:23:08 -07:00
|
|
|
|
// Record span info, because gc needs to be
|
2008-12-18 16:42:28 -07:00
|
|
|
|
// able to map interior pointer to containing span.
|
|
|
|
|
s->sizeclass = sizeclass;
|
2012-09-24 18:08:05 -06:00
|
|
|
|
s->elemsize = (sizeclass==0 ? s->npages<<PageShift : runtime·class_to_size[sizeclass]);
|
|
|
|
|
s->types.compression = MTypes_Empty;
|
2011-01-28 13:03:26 -07:00
|
|
|
|
p = s->start;
|
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
|
p -= ((uintptr)h->arena_start>>PageShift);
|
2008-12-19 04:13:39 -07:00
|
|
|
|
for(n=0; n<npage; n++)
|
2011-01-28 13:03:26 -07:00
|
|
|
|
h->map[p+n] = s;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allocate a span of exactly npage pages from the list of large spans.
|
|
|
|
|
static MSpan*
|
|
|
|
|
MHeap_AllocLarge(MHeap *h, uintptr npage)
|
|
|
|
|
{
|
|
|
|
|
return BestFit(&h->large, npage, nil);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search list for smallest span with >= npage pages.
|
|
|
|
|
// If there are multiple smallest spans, take the one
|
|
|
|
|
// with the earliest starting address.
|
|
|
|
|
static MSpan*
|
|
|
|
|
BestFit(MSpan *list, uintptr npage, MSpan *best)
|
|
|
|
|
{
|
|
|
|
|
MSpan *s;
|
|
|
|
|
|
|
|
|
|
for(s=list->next; s != list; s=s->next) {
|
|
|
|
|
if(s->npages < npage)
|
|
|
|
|
continue;
|
|
|
|
|
if(best == nil
|
|
|
|
|
|| s->npages < best->npages
|
|
|
|
|
|| (s->npages == best->npages && s->start < best->start))
|
|
|
|
|
best = s;
|
|
|
|
|
}
|
|
|
|
|
return best;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to add at least npage pages of memory to the heap,
|
|
|
|
|
// returning whether it worked.
|
|
|
|
|
static bool
|
|
|
|
|
MHeap_Grow(MHeap *h, uintptr npage)
|
|
|
|
|
{
|
|
|
|
|
uintptr ask;
|
|
|
|
|
void *v;
|
|
|
|
|
MSpan *s;
|
2011-01-28 13:03:26 -07:00
|
|
|
|
PageID p;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
|
|
// Ask for a big chunk, to reduce the number of mappings
|
|
|
|
|
// the operating system needs to track; also amortizes
|
|
|
|
|
// the overhead of an operating system mapping.
|
2010-12-15 09:49:23 -07:00
|
|
|
|
// Allocate a multiple of 64kB (16 pages).
|
2009-09-22 17:28:32 -06:00
|
|
|
|
npage = (npage+15)&~15;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
ask = npage<<PageShift;
|
runtime: allow use of >512 MB on 32-bit platforms
runtime: memory allocated by OS not in usable range
runtime: out of memory: cannot allocate 1114112-byte block (2138832896 in use)
throw: out of memory
runtime.throw+0x40 /Users/rsc/g/go/src/pkg/runtime/runtime.c:102
runtime.throw(0x1fffd, 0x101)
runtime.mallocgc+0x2af /Users/rsc/g/go/src/pkg/runtime/malloc.c:60
runtime.mallocgc(0x100004, 0x0, 0x1, 0x1, 0xc093, ...)
runtime.mal+0x40 /Users/rsc/g/go/src/pkg/runtime/malloc.c:289
runtime.mal(0x100004, 0x20bc4)
runtime.new+0x26 /Users/rsc/g/go/src/pkg/runtime/malloc.c:296
runtime.new(0x100004, 0x8fe84000, 0x20bc4)
main.main+0x29 /Users/rsc/x.go:11
main.main()
runtime.mainstart+0xf /Users/rsc/g/go/src/pkg/runtime/386/asm.s:93
runtime.mainstart()
runtime.goexit /Users/rsc/g/go/src/pkg/runtime/proc.c:178
runtime.goexit()
----- goroutine created by -----
_rt0_386+0xbf /Users/rsc/g/go/src/pkg/runtime/386/asm.s:80
R=iant, r
CC=golang-dev
https://golang.org/cl/4444073
2011-04-27 21:20:53 -06:00
|
|
|
|
if(ask < HeapAllocChunk)
|
2008-12-18 16:42:28 -07:00
|
|
|
|
ask = HeapAllocChunk;
|
|
|
|
|
|
2011-01-28 13:03:26 -07:00
|
|
|
|
v = runtime·MHeap_SysAlloc(h, ask);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
if(v == nil) {
|
|
|
|
|
if(ask > (npage<<PageShift)) {
|
|
|
|
|
ask = npage<<PageShift;
|
2011-01-28 13:03:26 -07:00
|
|
|
|
v = runtime·MHeap_SysAlloc(h, ask);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
}
|
2011-04-26 06:25:40 -06:00
|
|
|
|
if(v == nil) {
|
runtime: allow use of >512 MB on 32-bit platforms
runtime: memory allocated by OS not in usable range
runtime: out of memory: cannot allocate 1114112-byte block (2138832896 in use)
throw: out of memory
runtime.throw+0x40 /Users/rsc/g/go/src/pkg/runtime/runtime.c:102
runtime.throw(0x1fffd, 0x101)
runtime.mallocgc+0x2af /Users/rsc/g/go/src/pkg/runtime/malloc.c:60
runtime.mallocgc(0x100004, 0x0, 0x1, 0x1, 0xc093, ...)
runtime.mal+0x40 /Users/rsc/g/go/src/pkg/runtime/malloc.c:289
runtime.mal(0x100004, 0x20bc4)
runtime.new+0x26 /Users/rsc/g/go/src/pkg/runtime/malloc.c:296
runtime.new(0x100004, 0x8fe84000, 0x20bc4)
main.main+0x29 /Users/rsc/x.go:11
main.main()
runtime.mainstart+0xf /Users/rsc/g/go/src/pkg/runtime/386/asm.s:93
runtime.mainstart()
runtime.goexit /Users/rsc/g/go/src/pkg/runtime/proc.c:178
runtime.goexit()
----- goroutine created by -----
_rt0_386+0xbf /Users/rsc/g/go/src/pkg/runtime/386/asm.s:80
R=iant, r
CC=golang-dev
https://golang.org/cl/4444073
2011-04-27 21:20:53 -06:00
|
|
|
|
runtime·printf("runtime: out of memory: cannot allocate %D-byte block (%D in use)\n", (uint64)ask, mstats.heap_sys);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
return false;
|
2011-04-26 06:25:40 -06:00
|
|
|
|
}
|
2008-12-18 16:42:28 -07:00
|
|
|
|
}
|
2010-03-29 18:30:07 -06:00
|
|
|
|
mstats.heap_sys += ask;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
2008-12-19 04:13:39 -07:00
|
|
|
|
// Create a fake "in use" span and free it, so that the
|
|
|
|
|
// right coalescing happens.
|
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
|
|
|
|
s = runtime·FixAlloc_Alloc(&h->spanalloc);
|
2010-03-29 14:06:26 -06:00
|
|
|
|
mstats.mspan_inuse = h->spanalloc.inuse;
|
|
|
|
|
mstats.mspan_sys = h->spanalloc.sys;
|
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·MSpan_Init(s, (uintptr)v>>PageShift, ask>>PageShift);
|
2011-01-28 13:03:26 -07:00
|
|
|
|
p = s->start;
|
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
|
p -= ((uintptr)h->arena_start>>PageShift);
|
|
|
|
|
h->map[p] = s;
|
|
|
|
|
h->map[p + s->npages - 1] = s;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
s->state = MSpanInUse;
|
|
|
|
|
MHeap_FreeLocked(h, s);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-28 13:03:26 -07:00
|
|
|
|
// Look up the span at the given address.
|
|
|
|
|
// Address is guaranteed to be in map
|
2009-01-26 18:37:05 -07:00
|
|
|
|
// and is guaranteed to be start or end of span.
|
2008-12-18 16:42:28 -07:00
|
|
|
|
MSpan*
|
2011-01-28 13:03:26 -07:00
|
|
|
|
runtime·MHeap_Lookup(MHeap *h, void *v)
|
2008-12-18 16:42:28 -07:00
|
|
|
|
{
|
2011-01-28 13:03:26 -07:00
|
|
|
|
uintptr p;
|
|
|
|
|
|
|
|
|
|
p = (uintptr)v;
|
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
|
p -= (uintptr)h->arena_start;
|
|
|
|
|
return h->map[p >> PageShift];
|
2008-12-18 16:42:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
2011-01-28 13:03:26 -07:00
|
|
|
|
// Look up the span at the given address.
|
|
|
|
|
// Address is *not* guaranteed to be in map
|
2009-01-26 18:37:05 -07:00
|
|
|
|
// and may be anywhere in the span.
|
|
|
|
|
// Map entries for the middle of a span are only
|
|
|
|
|
// valid for allocated spans. Free spans may have
|
|
|
|
|
// other garbage in their middles, so we have to
|
|
|
|
|
// check for that.
|
|
|
|
|
MSpan*
|
2011-01-28 13:03:26 -07:00
|
|
|
|
runtime·MHeap_LookupMaybe(MHeap *h, void *v)
|
2009-01-26 18:37:05 -07:00
|
|
|
|
{
|
|
|
|
|
MSpan *s;
|
2011-01-28 13:03:26 -07:00
|
|
|
|
PageID p, q;
|
2009-01-26 18:37:05 -07:00
|
|
|
|
|
2011-01-28 13:03:26 -07:00
|
|
|
|
if((byte*)v < h->arena_start || (byte*)v >= h->arena_used)
|
|
|
|
|
return nil;
|
|
|
|
|
p = (uintptr)v>>PageShift;
|
|
|
|
|
q = p;
|
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
|
q -= (uintptr)h->arena_start >> PageShift;
|
|
|
|
|
s = h->map[q];
|
2009-01-26 18:37:05 -07:00
|
|
|
|
if(s == nil || p < s->start || p - s->start >= s->npages)
|
|
|
|
|
return nil;
|
2009-02-11 18:54:03 -07:00
|
|
|
|
if(s->state != MSpanInUse)
|
|
|
|
|
return nil;
|
2009-01-26 18:37:05 -07:00
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-18 16:42:28 -07:00
|
|
|
|
// Free the span back into the heap.
|
|
|
|
|
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·MHeap_Free(MHeap *h, MSpan *s, int32 acct)
|
2008-12-18 16:42:28 -07:00
|
|
|
|
{
|
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·lock(h);
|
2013-05-22 12:22:57 -06:00
|
|
|
|
mstats.heap_alloc += m->mcache->local_cachealloc;
|
|
|
|
|
m->mcache->local_cachealloc = 0;
|
2010-03-29 14:06:26 -06:00
|
|
|
|
mstats.heap_inuse -= s->npages<<PageShift;
|
2010-09-07 07:57:22 -06:00
|
|
|
|
if(acct) {
|
2010-03-08 15:15:44 -07:00
|
|
|
|
mstats.heap_alloc -= s->npages<<PageShift;
|
2010-09-07 07:57:22 -06:00
|
|
|
|
mstats.heap_objects--;
|
|
|
|
|
}
|
2008-12-18 16:42:28 -07:00
|
|
|
|
MHeap_FreeLocked(h, s);
|
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·unlock(h);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
MHeap_FreeLocked(MHeap *h, MSpan *s)
|
|
|
|
|
{
|
2011-01-28 13:03:26 -07:00
|
|
|
|
uintptr *sp, *tp;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
MSpan *t;
|
2011-01-28 13:03:26 -07:00
|
|
|
|
PageID p;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
2012-09-24 18:08:05 -06:00
|
|
|
|
if(s->types.sysalloc)
|
|
|
|
|
runtime·settype_sysfree(s);
|
|
|
|
|
s->types.compression = MTypes_Empty;
|
|
|
|
|
|
2008-12-19 04:13:39 -07:00
|
|
|
|
if(s->state != MSpanInUse || s->ref != 0) {
|
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·printf("MHeap_FreeLocked - span %p ptr %p state %d ref %d\n", s, s->start<<PageShift, s->state, s->ref);
|
|
|
|
|
runtime·throw("MHeap_FreeLocked - invalid free");
|
2008-12-19 04:13:39 -07:00
|
|
|
|
}
|
2011-09-14 09:29:01 -06:00
|
|
|
|
mstats.heap_idle += s->npages<<PageShift;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
s->state = MSpanFree;
|
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·MSpanList_Remove(s);
|
2011-01-28 13:03:26 -07:00
|
|
|
|
sp = (uintptr*)(s->start<<PageShift);
|
2013-01-28 10:53:35 -07:00
|
|
|
|
// Stamp newly unused spans. The scavenger will use that
|
|
|
|
|
// info to potentially give back some pages to the OS.
|
|
|
|
|
s->unusedsince = runtime·nanotime();
|
|
|
|
|
s->npreleased = 0;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
|
|
// Coalesce with earlier, later spans.
|
2011-01-28 13:03:26 -07:00
|
|
|
|
p = s->start;
|
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
|
p -= (uintptr)h->arena_start >> PageShift;
|
|
|
|
|
if(p > 0 && (t = h->map[p-1]) != nil && t->state != MSpanInUse) {
|
|
|
|
|
tp = (uintptr*)(t->start<<PageShift);
|
|
|
|
|
*tp |= *sp; // propagate "needs zeroing" mark
|
2008-12-18 16:42:28 -07:00
|
|
|
|
s->start = t->start;
|
|
|
|
|
s->npages += t->npages;
|
2012-02-16 11:30:04 -07:00
|
|
|
|
s->npreleased = t->npreleased; // absorb released pages
|
2011-01-28 13:03:26 -07:00
|
|
|
|
p -= t->npages;
|
|
|
|
|
h->map[p] = s;
|
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·MSpanList_Remove(t);
|
2009-01-28 16:22:16 -07:00
|
|
|
|
t->state = MSpanDead;
|
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·FixAlloc_Free(&h->spanalloc, t);
|
2010-03-29 14:06:26 -06:00
|
|
|
|
mstats.mspan_inuse = h->spanalloc.inuse;
|
|
|
|
|
mstats.mspan_sys = h->spanalloc.sys;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
}
|
2011-01-28 13:03:26 -07:00
|
|
|
|
if(p+s->npages < nelem(h->map) && (t = h->map[p+s->npages]) != nil && t->state != MSpanInUse) {
|
|
|
|
|
tp = (uintptr*)(t->start<<PageShift);
|
|
|
|
|
*sp |= *tp; // propagate "needs zeroing" mark
|
2008-12-18 16:42:28 -07:00
|
|
|
|
s->npages += t->npages;
|
2012-02-16 11:30:04 -07:00
|
|
|
|
s->npreleased += t->npreleased;
|
2011-01-28 13:03:26 -07:00
|
|
|
|
h->map[p + s->npages - 1] = s;
|
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·MSpanList_Remove(t);
|
2009-01-28 16:22:16 -07:00
|
|
|
|
t->state = MSpanDead;
|
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·FixAlloc_Free(&h->spanalloc, t);
|
2010-03-29 14:06:26 -06:00
|
|
|
|
mstats.mspan_inuse = h->spanalloc.inuse;
|
|
|
|
|
mstats.mspan_sys = h->spanalloc.sys;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert s into appropriate list.
|
|
|
|
|
if(s->npages < nelem(h->free))
|
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·MSpanList_Insert(&h->free[s->npages], s);
|
2008-12-18 16:42:28 -07:00
|
|
|
|
else
|
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·MSpanList_Insert(&h->large, s);
|
2012-02-16 11:30:04 -07:00
|
|
|
|
}
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
2012-10-16 04:41:32 -06:00
|
|
|
|
static void
|
|
|
|
|
forcegchelper(Note *note)
|
|
|
|
|
{
|
|
|
|
|
runtime·gc(1);
|
|
|
|
|
runtime·notewakeup(note);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-03 22:00:55 -07:00
|
|
|
|
static uintptr
|
|
|
|
|
scavengelist(MSpan *list, uint64 now, uint64 limit)
|
|
|
|
|
{
|
|
|
|
|
uintptr released, sumreleased;
|
|
|
|
|
MSpan *s;
|
|
|
|
|
|
|
|
|
|
if(runtime·MSpanList_IsEmpty(list))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
sumreleased = 0;
|
|
|
|
|
for(s=list->next; s != list; s=s->next) {
|
|
|
|
|
if((now - s->unusedsince) > limit) {
|
|
|
|
|
released = (s->npages - s->npreleased) << PageShift;
|
|
|
|
|
mstats.heap_released += released;
|
|
|
|
|
sumreleased += released;
|
|
|
|
|
s->npreleased = s->npages;
|
|
|
|
|
runtime·SysUnused((void*)(s->start << PageShift), s->npages << PageShift);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sumreleased;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uintptr
|
|
|
|
|
scavenge(uint64 now, uint64 limit)
|
|
|
|
|
{
|
|
|
|
|
uint32 i;
|
|
|
|
|
uintptr sumreleased;
|
|
|
|
|
MHeap *h;
|
|
|
|
|
|
2013-02-15 12:27:03 -07:00
|
|
|
|
h = runtime·mheap;
|
2013-02-03 22:00:55 -07:00
|
|
|
|
sumreleased = 0;
|
|
|
|
|
for(i=0; i < nelem(h->free); i++)
|
|
|
|
|
sumreleased += scavengelist(&h->free[i], now, limit);
|
|
|
|
|
sumreleased += scavengelist(&h->large, now, limit);
|
|
|
|
|
return sumreleased;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 15:01:13 -07:00
|
|
|
|
static FuncVal forcegchelperv = {(void(*)(void))forcegchelper};
|
|
|
|
|
|
2012-02-16 11:30:04 -07:00
|
|
|
|
// Release (part of) unused memory to OS.
|
2012-03-01 09:48:17 -07:00
|
|
|
|
// Goroutine created at startup.
|
2012-02-16 11:30:04 -07:00
|
|
|
|
// Loop forever.
|
|
|
|
|
void
|
|
|
|
|
runtime·MHeap_Scavenger(void)
|
|
|
|
|
{
|
|
|
|
|
MHeap *h;
|
|
|
|
|
uint64 tick, now, forcegc, limit;
|
2013-02-03 22:00:55 -07:00
|
|
|
|
uint32 k;
|
|
|
|
|
uintptr sumreleased;
|
2012-02-16 11:30:04 -07:00
|
|
|
|
byte *env;
|
|
|
|
|
bool trace;
|
2012-10-16 04:41:32 -06:00
|
|
|
|
Note note, *notep;
|
2012-02-16 11:30:04 -07:00
|
|
|
|
|
2013-03-12 07:21:44 -06:00
|
|
|
|
g->issystem = true;
|
|
|
|
|
g->isbackground = true;
|
|
|
|
|
|
2012-02-16 11:30:04 -07:00
|
|
|
|
// If we go two minutes without a garbage collection, force one to run.
|
|
|
|
|
forcegc = 2*60*1e9;
|
|
|
|
|
// If a span goes unused for 5 minutes after a garbage collection,
|
|
|
|
|
// we hand it back to the operating system.
|
|
|
|
|
limit = 5*60*1e9;
|
|
|
|
|
// Make wake-up period small enough for the sampling to be correct.
|
2012-02-17 12:13:16 -07:00
|
|
|
|
if(forcegc < limit)
|
|
|
|
|
tick = forcegc/2;
|
|
|
|
|
else
|
|
|
|
|
tick = limit/2;
|
2012-02-16 11:30:04 -07:00
|
|
|
|
|
|
|
|
|
trace = false;
|
|
|
|
|
env = runtime·getenv("GOGCTRACE");
|
|
|
|
|
if(env != nil)
|
|
|
|
|
trace = runtime·atoi(env) > 0;
|
|
|
|
|
|
2013-02-15 12:27:03 -07:00
|
|
|
|
h = runtime·mheap;
|
2012-02-16 11:30:04 -07:00
|
|
|
|
for(k=0;; k++) {
|
|
|
|
|
runtime·noteclear(¬e);
|
2013-02-20 09:21:45 -07:00
|
|
|
|
runtime·entersyscallblock();
|
2012-02-16 11:30:04 -07:00
|
|
|
|
runtime·notetsleep(¬e, tick);
|
|
|
|
|
runtime·exitsyscall();
|
|
|
|
|
|
|
|
|
|
runtime·lock(h);
|
|
|
|
|
now = runtime·nanotime();
|
|
|
|
|
if(now - mstats.last_gc > forcegc) {
|
|
|
|
|
runtime·unlock(h);
|
2012-10-16 04:41:32 -06:00
|
|
|
|
// The scavenger can not block other goroutines,
|
|
|
|
|
// otherwise deadlock detector can fire spuriously.
|
|
|
|
|
// GC blocks other goroutines via the runtime·worldsema.
|
|
|
|
|
runtime·noteclear(¬e);
|
|
|
|
|
notep = ¬e;
|
2013-02-21 15:01:13 -07:00
|
|
|
|
runtime·newproc1(&forcegchelperv, (byte*)¬ep, sizeof(notep), 0, runtime·MHeap_Scavenger);
|
2013-02-20 09:21:45 -07:00
|
|
|
|
runtime·entersyscallblock();
|
2012-10-16 04:41:32 -06:00
|
|
|
|
runtime·notesleep(¬e);
|
|
|
|
|
runtime·exitsyscall();
|
2013-01-28 10:53:35 -07:00
|
|
|
|
if(trace)
|
|
|
|
|
runtime·printf("scvg%d: GC forced\n", k);
|
2012-02-16 11:30:04 -07:00
|
|
|
|
runtime·lock(h);
|
|
|
|
|
now = runtime·nanotime();
|
|
|
|
|
}
|
2013-02-03 22:00:55 -07:00
|
|
|
|
sumreleased = scavenge(now, limit);
|
2012-02-16 11:30:04 -07:00
|
|
|
|
runtime·unlock(h);
|
|
|
|
|
|
|
|
|
|
if(trace) {
|
|
|
|
|
if(sumreleased > 0)
|
|
|
|
|
runtime·printf("scvg%d: %p MB released\n", k, sumreleased>>20);
|
|
|
|
|
runtime·printf("scvg%d: inuse: %D, idle: %D, sys: %D, released: %D, consumed: %D (MB)\n",
|
|
|
|
|
k, mstats.heap_inuse>>20, mstats.heap_idle>>20, mstats.heap_sys>>20,
|
|
|
|
|
mstats.heap_released>>20, (mstats.heap_sys - mstats.heap_released)>>20);
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-18 16:42:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-03 22:00:55 -07:00
|
|
|
|
void
|
|
|
|
|
runtime∕debug·freeOSMemory(void)
|
|
|
|
|
{
|
|
|
|
|
runtime·gc(1);
|
2013-02-15 12:27:03 -07:00
|
|
|
|
runtime·lock(runtime·mheap);
|
2013-02-03 22:00:55 -07:00
|
|
|
|
scavenge(~(uintptr)0, 0);
|
2013-02-15 12:27:03 -07:00
|
|
|
|
runtime·unlock(runtime·mheap);
|
2013-02-03 22:00:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-18 16:42:28 -07:00
|
|
|
|
// Initialize a new span with the given start and npages.
|
|
|
|
|
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·MSpan_Init(MSpan *span, PageID start, uintptr npages)
|
2008-12-18 16:42:28 -07:00
|
|
|
|
{
|
|
|
|
|
span->next = nil;
|
|
|
|
|
span->prev = nil;
|
|
|
|
|
span->start = start;
|
|
|
|
|
span->npages = npages;
|
|
|
|
|
span->freelist = nil;
|
|
|
|
|
span->ref = 0;
|
|
|
|
|
span->sizeclass = 0;
|
2012-09-24 18:08:05 -06:00
|
|
|
|
span->elemsize = 0;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
span->state = 0;
|
2012-02-16 11:30:04 -07:00
|
|
|
|
span->unusedsince = 0;
|
|
|
|
|
span->npreleased = 0;
|
2012-09-24 18:08:05 -06:00
|
|
|
|
span->types.compression = MTypes_Empty;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize an empty doubly-linked list.
|
|
|
|
|
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·MSpanList_Init(MSpan *list)
|
2008-12-18 16:42:28 -07:00
|
|
|
|
{
|
2009-01-28 16:22:16 -07:00
|
|
|
|
list->state = MSpanListHead;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
list->next = list;
|
|
|
|
|
list->prev = list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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·MSpanList_Remove(MSpan *span)
|
2008-12-18 16:42:28 -07:00
|
|
|
|
{
|
|
|
|
|
if(span->prev == nil && span->next == nil)
|
|
|
|
|
return;
|
|
|
|
|
span->prev->next = span->next;
|
|
|
|
|
span->next->prev = span->prev;
|
|
|
|
|
span->prev = nil;
|
|
|
|
|
span->next = nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
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·MSpanList_IsEmpty(MSpan *list)
|
2008-12-18 16:42:28 -07:00
|
|
|
|
{
|
|
|
|
|
return list->next == list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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·MSpanList_Insert(MSpan *list, MSpan *span)
|
2008-12-18 16:42:28 -07:00
|
|
|
|
{
|
2011-02-02 21:03:47 -07:00
|
|
|
|
if(span->next != nil || span->prev != nil) {
|
|
|
|
|
runtime·printf("failed MSpanList_Insert %p %p %p\n", span, span->next, span->prev);
|
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·throw("MSpanList_Insert");
|
2011-02-02 21:03:47 -07:00
|
|
|
|
}
|
2008-12-18 16:42:28 -07:00
|
|
|
|
span->next = list->next;
|
|
|
|
|
span->prev = list;
|
|
|
|
|
span->next->prev = span;
|
|
|
|
|
span->prev->next = span;
|
|
|
|
|
}
|
2011-02-02 21:03:47 -07:00
|
|
|
|
|
|
|
|
|
|