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.
|
|
|
|
|
|
|
|
// Central free lists.
|
|
|
|
//
|
|
|
|
// See malloc.h for an overview.
|
|
|
|
//
|
|
|
|
// The MCentral doesn't actually contain the list of free objects; the MSpan does.
|
|
|
|
// Each MCentral is two lists of MSpans: those with free objects (c->nonempty)
|
|
|
|
// and those that are completely allocated (c->empty).
|
|
|
|
//
|
|
|
|
// TODO(rsc): tcmalloc uses a "transfer cache" to split the list
|
|
|
|
// into sections of class_to_transfercount[sizeclass] objects
|
|
|
|
// so that it is faster to move those lists between MCaches and MCentrals.
|
|
|
|
|
|
|
|
#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 bool MCentral_Grow(MCentral *c);
|
|
|
|
static void* MCentral_Alloc(MCentral *c);
|
|
|
|
static void MCentral_Free(MCentral *c, void *v);
|
|
|
|
|
|
|
|
// Initialize a single central free 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·MCentral_Init(MCentral *c, int32 sizeclass)
|
2008-12-18 16:42:28 -07:00
|
|
|
{
|
|
|
|
c->sizeclass = sizeclass;
|
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(&c->nonempty);
|
|
|
|
runtime·MSpanList_Init(&c->empty);
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate up to n objects from the central free list.
|
|
|
|
// Return the number of objects allocated.
|
|
|
|
// The objects are linked together by their first words.
|
|
|
|
// On return, *pstart points at the first object and *pend at the last.
|
|
|
|
int32
|
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_AllocList(MCentral *c, int32 n, MLink **pfirst)
|
2008-12-18 16:42:28 -07:00
|
|
|
{
|
2008-12-19 04:13:39 -07:00
|
|
|
MLink *first, *last, *v;
|
2008-12-18 16:42:28 -07:00
|
|
|
int32 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·lock(c);
|
2008-12-18 16:42:28 -07:00
|
|
|
// Replenish central list if empty.
|
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(&c->nonempty)) {
|
2008-12-18 16:42:28 -07:00
|
|
|
if(!MCentral_Grow(c)) {
|
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(c);
|
2008-12-19 04:13:39 -07:00
|
|
|
*pfirst = nil;
|
2008-12-18 16:42:28 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy from list, up to n.
|
2008-12-19 04:13:39 -07:00
|
|
|
// First one is guaranteed to work, because we just grew the list.
|
|
|
|
first = MCentral_Alloc(c);
|
|
|
|
last = first;
|
|
|
|
for(i=1; i<n && (v = MCentral_Alloc(c)) != nil; i++) {
|
|
|
|
last->next = v;
|
|
|
|
last = v;
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
2008-12-19 04:13:39 -07:00
|
|
|
last->next = nil;
|
2008-12-18 16:42:28 -07:00
|
|
|
c->nfree -= 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·unlock(c);
|
2008-12-19 04:13:39 -07:00
|
|
|
*pfirst = first;
|
2008-12-18 16:42:28 -07:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper: allocate one object from the central free list.
|
|
|
|
static void*
|
|
|
|
MCentral_Alloc(MCentral *c)
|
|
|
|
{
|
|
|
|
MSpan *s;
|
2008-12-19 04:13:39 -07:00
|
|
|
MLink *v;
|
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
|
|
|
if(runtime·MSpanList_IsEmpty(&c->nonempty))
|
2008-12-18 16:42:28 -07:00
|
|
|
return nil;
|
|
|
|
s = c->nonempty.next;
|
2008-12-19 04:13:39 -07:00
|
|
|
s->ref++;
|
2008-12-18 16:42:28 -07:00
|
|
|
v = s->freelist;
|
2008-12-19 04:13:39 -07:00
|
|
|
s->freelist = v->next;
|
2008-12-18 16:42:28 -07:00
|
|
|
if(s->freelist == nil) {
|
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);
|
|
|
|
runtime·MSpanList_Insert(&c->empty, s);
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free n objects back into the central free 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·MCentral_FreeList(MCentral *c, int32 n, MLink *start)
|
2008-12-18 16:42:28 -07:00
|
|
|
{
|
2008-12-19 04:13:39 -07:00
|
|
|
MLink *v, *next;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
2008-12-19 04:13:39 -07:00
|
|
|
// Assume next == nil marks end of list.
|
2008-12-18 16:42:28 -07:00
|
|
|
// n and end would be useful if we implemented
|
|
|
|
// the transfer cache optimization in the TODO above.
|
|
|
|
USED(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
|
|
|
runtime·lock(c);
|
2008-12-18 16:42:28 -07:00
|
|
|
for(v=start; v; v=next) {
|
2008-12-19 04:13:39 -07:00
|
|
|
next = v->next;
|
2008-12-18 16:42:28 -07:00
|
|
|
MCentral_Free(c, v);
|
|
|
|
}
|
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(c);
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper: free one object back into the central free list.
|
|
|
|
static void
|
|
|
|
MCentral_Free(MCentral *c, void *v)
|
|
|
|
{
|
|
|
|
MSpan *s;
|
2011-02-02 21:03:47 -07:00
|
|
|
MLink *p;
|
2010-02-10 01:00:12 -07:00
|
|
|
int32 size;
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
// Find span for v.
|
2011-01-28 13:03:26 -07:00
|
|
|
s = runtime·MHeap_Lookup(&runtime·mheap, v);
|
2008-12-18 16:42:28 -07:00
|
|
|
if(s == nil || 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·throw("invalid free");
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
// Move to nonempty if necessary.
|
|
|
|
if(s->freelist == nil) {
|
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);
|
|
|
|
runtime·MSpanList_Insert(&c->nonempty, s);
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add v back to s's free list.
|
2008-12-19 04:13:39 -07:00
|
|
|
p = v;
|
|
|
|
p->next = s->freelist;
|
|
|
|
s->freelist = p;
|
2008-12-18 16:42:28 -07:00
|
|
|
c->nfree++;
|
|
|
|
|
|
|
|
// If s is completely freed, return it to the heap.
|
|
|
|
if(--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
|
|
|
size = runtime·class_to_size[c->sizeclass];
|
|
|
|
runtime·MSpanList_Remove(s);
|
2011-02-02 21:03:47 -07:00
|
|
|
runtime·unmarkspan((byte*)(s->start<<PageShift), s->npages<<PageShift);
|
|
|
|
*(uintptr*)(s->start<<PageShift) = 1; // needs zeroing
|
2008-12-19 04:13:39 -07:00
|
|
|
s->freelist = nil;
|
2010-02-10 01:00:12 -07:00
|
|
|
c->nfree -= (s->npages << PageShift) / size;
|
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(c);
|
|
|
|
runtime·MHeap_Free(&runtime·mheap, s, 0);
|
|
|
|
runtime·lock(c);
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-12 02:01:24 -06:00
|
|
|
// Free n objects from a span s back into the central free list c.
|
|
|
|
// Called from GC.
|
|
|
|
void
|
|
|
|
runtime·MCentral_FreeSpan(MCentral *c, MSpan *s, int32 n, MLink *start, MLink *end)
|
|
|
|
{
|
|
|
|
int32 size;
|
|
|
|
|
|
|
|
runtime·lock(c);
|
|
|
|
|
|
|
|
// Move to nonempty if necessary.
|
|
|
|
if(s->freelist == nil) {
|
|
|
|
runtime·MSpanList_Remove(s);
|
|
|
|
runtime·MSpanList_Insert(&c->nonempty, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the objects back to s's free list.
|
|
|
|
end->next = s->freelist;
|
|
|
|
s->freelist = start;
|
|
|
|
s->ref -= n;
|
|
|
|
c->nfree += n;
|
|
|
|
|
|
|
|
// If s is completely freed, return it to the heap.
|
|
|
|
if(s->ref == 0) {
|
|
|
|
size = runtime·class_to_size[c->sizeclass];
|
|
|
|
runtime·MSpanList_Remove(s);
|
|
|
|
*(uintptr*)(s->start<<PageShift) = 1; // needs zeroing
|
|
|
|
s->freelist = nil;
|
|
|
|
c->nfree -= (s->npages << PageShift) / size;
|
|
|
|
runtime·unlock(c);
|
|
|
|
runtime·unmarkspan((byte*)(s->start<<PageShift), s->npages<<PageShift);
|
|
|
|
runtime·MHeap_Free(&runtime·mheap, s, 0);
|
|
|
|
} else {
|
|
|
|
runtime·unlock(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-10 15:59:39 -07:00
|
|
|
void
|
2011-02-02 21:03:47 -07:00
|
|
|
runtime·MGetSizeClassInfo(int32 sizeclass, uintptr *sizep, int32 *npagesp, int32 *nobj)
|
2010-02-10 15:59:39 -07:00
|
|
|
{
|
|
|
|
int32 size;
|
|
|
|
int32 npages;
|
|
|
|
|
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
|
|
|
npages = runtime·class_to_allocnpages[sizeclass];
|
|
|
|
size = runtime·class_to_size[sizeclass];
|
2010-02-10 15:59:39 -07:00
|
|
|
*npagesp = npages;
|
|
|
|
*sizep = size;
|
2011-02-02 21:03:47 -07:00
|
|
|
*nobj = (npages << PageShift) / size;
|
2010-02-10 15:59:39 -07:00
|
|
|
}
|
|
|
|
|
2008-12-18 16:42:28 -07:00
|
|
|
// Fetch a new span from the heap and
|
|
|
|
// carve into objects for the free list.
|
|
|
|
static bool
|
|
|
|
MCentral_Grow(MCentral *c)
|
|
|
|
{
|
2011-02-02 21:03:47 -07:00
|
|
|
int32 i, n, npages;
|
|
|
|
uintptr size;
|
2008-12-19 04:13:39 -07:00
|
|
|
MLink **tailp, *v;
|
2009-01-26 18:37:05 -07:00
|
|
|
byte *p;
|
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·unlock(c);
|
|
|
|
runtime·MGetSizeClassInfo(c->sizeclass, &size, &npages, &n);
|
|
|
|
s = runtime·MHeap_Alloc(&runtime·mheap, npages, c->sizeclass, 0);
|
2008-12-18 16:42:28 -07:00
|
|
|
if(s == nil) {
|
|
|
|
// TODO(rsc): Log out of memory
|
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(c);
|
2008-12-18 16:42:28 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Carve span into sequence of blocks.
|
2008-12-19 04:13:39 -07:00
|
|
|
tailp = &s->freelist;
|
2008-12-18 16:42:28 -07:00
|
|
|
p = (byte*)(s->start << PageShift);
|
2011-02-02 21:03:47 -07:00
|
|
|
s->limit = p + size*n;
|
2009-01-26 18:37:05 -07:00
|
|
|
for(i=0; i<n; i++) {
|
2008-12-19 04:13:39 -07:00
|
|
|
v = (MLink*)p;
|
|
|
|
*tailp = v;
|
|
|
|
tailp = &v->next;
|
2009-01-26 18:37:05 -07:00
|
|
|
p += size;
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
2008-12-19 04:13:39 -07:00
|
|
|
*tailp = nil;
|
2011-02-02 21:03:47 -07:00
|
|
|
runtime·markspan((byte*)(s->start<<PageShift), size, n, size*n < (s->npages<<PageShift));
|
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(c);
|
2008-12-18 16:42:28 -07:00
|
|
|
c->nfree += 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
|
|
|
runtime·MSpanList_Insert(&c->nonempty, s);
|
2008-12-18 16:42:28 -07:00
|
|
|
return true;
|
|
|
|
}
|