2010-12-03 05:34:57 +01: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)
|
compiler, runtime: replace hashmap code with Go 1.7 hashmap
This change removes the gccgo-specific hashmap code and replaces it with
the hashmap code from the Go 1.7 runtime. The Go 1.7 hashmap code is
more efficient, does a better job on details like when to update a key,
and provides some support against denial-of-service attacks.
The compiler is changed to call the new hashmap functions instead of the
old ones.
The compiler now tracks which types are reflexive and which require
updating when used as a map key, and records the information in map type
descriptors.
Map_index_expression is simplified. The special case for a map index on
the right hand side of a tuple expression has been unnecessary for some
time, and is removed. The support for specially marking a map index as
an lvalue is removed, in favor of lowering an assignment to a map index
into a function call. The long-obsolete support for a map index of a
pointer to a map is removed.
The __go_new_map_big function (known to the compiler as
Runtime::MAKEMAPBIG) is no longer needed, as the new runtime.makemap
function takes an int64 hint argument.
The old map descriptor type and supporting expression is removed.
The compiler was still supporting the long-obsolete syntax `m[k] = 0,
false` to delete a value from a map. That is now removed, requiring a
change to one of the gccgo-specific tests.
The builtin len function applied to a map or channel p is now compiled
as `p == nil ? 0 : *(*int)(p)`. The __go_chan_len function (known to
the compiler as Runtime::CHAN_LEN) is removed.
Support for a shared zero value for maps to large value types is
introduced, along the lines of the gc compiler. The zero value is
handled as a common variable.
The hash function is changed to take a seed argument, changing the
runtime hash functions and the compiler-generated hash functions.
Unlike the gc compiler, both the hash and equal functions continue to
take the type length.
Types that can not be compared now store nil for the hash and equal
functions, rather than pointing to functions that throw. Interface hash
and comparison functions now check explicitly for nil. This matches the
gc compiler and permits a simple implementation for ismapkey.
The compiler is changed to permit marking struct and array types as
incomparable, meaning that they have no hash or equal function. We use
this for thunk types, removing the existing special code to avoid
generating hash/equal functions for them.
The C runtime code adds memclr, memequal, and memmove functions.
The hashmap code uses go:linkname comments to make the functions
visible, as otherwise the compiler would discard them.
The hashmap code comments out the unused reference to the address of the
first parameter in the race code, as otherwise the compiler thinks that
the parameter escapes and copies it onto the heap. This is probably not
needed when we enable escape analysis.
Several runtime map tests that ere previously skipped for gccgo are now
run.
The Go runtime picks up type kind information and stubs. The type kind
information causes the generated runtime header file to define some
constants, including `empty`, and the C code is adjusted accordingly.
A Go-callable version of runtime.throw, that takes a Go string, is
added to be called from the hashmap code.
Reviewed-on: https://go-review.googlesource.com/29447
* go.go-torture/execute/map-1.go: Replace old map deletion syntax
with call to builtin delete function.
From-SVN: r240334
2016-09-21 22:58:51 +02:00
|
|
|
// and those that are completely allocated (c->mempty).
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
|
|
|
// 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-10-27 01:57:58 +02:00
|
|
|
#include "arch.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "malloc.h"
|
|
|
|
|
|
|
|
static bool MCentral_Grow(MCentral *c);
|
2014-07-19 10:53:52 +02:00
|
|
|
static void MCentral_Free(MCentral *c, MLink *v);
|
|
|
|
static void MCentral_ReturnToHeap(MCentral *c, MSpan *s);
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Initialize a single central free list.
|
|
|
|
void
|
|
|
|
runtime_MCentral_Init(MCentral *c, int32 sizeclass)
|
|
|
|
{
|
|
|
|
c->sizeclass = sizeclass;
|
|
|
|
runtime_MSpanList_Init(&c->nonempty);
|
compiler, runtime: replace hashmap code with Go 1.7 hashmap
This change removes the gccgo-specific hashmap code and replaces it with
the hashmap code from the Go 1.7 runtime. The Go 1.7 hashmap code is
more efficient, does a better job on details like when to update a key,
and provides some support against denial-of-service attacks.
The compiler is changed to call the new hashmap functions instead of the
old ones.
The compiler now tracks which types are reflexive and which require
updating when used as a map key, and records the information in map type
descriptors.
Map_index_expression is simplified. The special case for a map index on
the right hand side of a tuple expression has been unnecessary for some
time, and is removed. The support for specially marking a map index as
an lvalue is removed, in favor of lowering an assignment to a map index
into a function call. The long-obsolete support for a map index of a
pointer to a map is removed.
The __go_new_map_big function (known to the compiler as
Runtime::MAKEMAPBIG) is no longer needed, as the new runtime.makemap
function takes an int64 hint argument.
The old map descriptor type and supporting expression is removed.
The compiler was still supporting the long-obsolete syntax `m[k] = 0,
false` to delete a value from a map. That is now removed, requiring a
change to one of the gccgo-specific tests.
The builtin len function applied to a map or channel p is now compiled
as `p == nil ? 0 : *(*int)(p)`. The __go_chan_len function (known to
the compiler as Runtime::CHAN_LEN) is removed.
Support for a shared zero value for maps to large value types is
introduced, along the lines of the gc compiler. The zero value is
handled as a common variable.
The hash function is changed to take a seed argument, changing the
runtime hash functions and the compiler-generated hash functions.
Unlike the gc compiler, both the hash and equal functions continue to
take the type length.
Types that can not be compared now store nil for the hash and equal
functions, rather than pointing to functions that throw. Interface hash
and comparison functions now check explicitly for nil. This matches the
gc compiler and permits a simple implementation for ismapkey.
The compiler is changed to permit marking struct and array types as
incomparable, meaning that they have no hash or equal function. We use
this for thunk types, removing the existing special code to avoid
generating hash/equal functions for them.
The C runtime code adds memclr, memequal, and memmove functions.
The hashmap code uses go:linkname comments to make the functions
visible, as otherwise the compiler would discard them.
The hashmap code comments out the unused reference to the address of the
first parameter in the race code, as otherwise the compiler thinks that
the parameter escapes and copies it onto the heap. This is probably not
needed when we enable escape analysis.
Several runtime map tests that ere previously skipped for gccgo are now
run.
The Go runtime picks up type kind information and stubs. The type kind
information causes the generated runtime header file to define some
constants, including `empty`, and the C code is adjusted accordingly.
A Go-callable version of runtime.throw, that takes a Go string, is
added to be called from the hashmap code.
Reviewed-on: https://go-review.googlesource.com/29447
* go.go-torture/execute/map-1.go: Replace old map deletion syntax
with call to builtin delete function.
From-SVN: r240334
2016-09-21 22:58:51 +02:00
|
|
|
runtime_MSpanList_Init(&c->mempty);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// Allocate a span to use in an MCache.
|
|
|
|
MSpan*
|
|
|
|
runtime_MCentral_CacheSpan(MCentral *c)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
2013-01-29 21:52:43 +01:00
|
|
|
MSpan *s;
|
2013-11-06 20:49:01 +01:00
|
|
|
int32 cap, n;
|
2014-06-07 00:37:27 +02:00
|
|
|
uint32 sg;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
runtime_lock(c);
|
2014-06-07 00:37:27 +02:00
|
|
|
sg = runtime_mheap.sweepgen;
|
|
|
|
retry:
|
|
|
|
for(s = c->nonempty.next; s != &c->nonempty; s = s->next) {
|
|
|
|
if(s->sweepgen == sg-2 && runtime_cas(&s->sweepgen, sg-2, sg-1)) {
|
2010-12-03 05:34:57 +01:00
|
|
|
runtime_unlock(c);
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_MSpan_Sweep(s);
|
|
|
|
runtime_lock(c);
|
|
|
|
// the span could have been moved to heap, retry
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
if(s->sweepgen == sg-1) {
|
|
|
|
// the span is being swept by background sweeper, skip
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// we have a nonempty span that does not require sweeping, allocate from it
|
|
|
|
goto havespan;
|
|
|
|
}
|
|
|
|
|
compiler, runtime: replace hashmap code with Go 1.7 hashmap
This change removes the gccgo-specific hashmap code and replaces it with
the hashmap code from the Go 1.7 runtime. The Go 1.7 hashmap code is
more efficient, does a better job on details like when to update a key,
and provides some support against denial-of-service attacks.
The compiler is changed to call the new hashmap functions instead of the
old ones.
The compiler now tracks which types are reflexive and which require
updating when used as a map key, and records the information in map type
descriptors.
Map_index_expression is simplified. The special case for a map index on
the right hand side of a tuple expression has been unnecessary for some
time, and is removed. The support for specially marking a map index as
an lvalue is removed, in favor of lowering an assignment to a map index
into a function call. The long-obsolete support for a map index of a
pointer to a map is removed.
The __go_new_map_big function (known to the compiler as
Runtime::MAKEMAPBIG) is no longer needed, as the new runtime.makemap
function takes an int64 hint argument.
The old map descriptor type and supporting expression is removed.
The compiler was still supporting the long-obsolete syntax `m[k] = 0,
false` to delete a value from a map. That is now removed, requiring a
change to one of the gccgo-specific tests.
The builtin len function applied to a map or channel p is now compiled
as `p == nil ? 0 : *(*int)(p)`. The __go_chan_len function (known to
the compiler as Runtime::CHAN_LEN) is removed.
Support for a shared zero value for maps to large value types is
introduced, along the lines of the gc compiler. The zero value is
handled as a common variable.
The hash function is changed to take a seed argument, changing the
runtime hash functions and the compiler-generated hash functions.
Unlike the gc compiler, both the hash and equal functions continue to
take the type length.
Types that can not be compared now store nil for the hash and equal
functions, rather than pointing to functions that throw. Interface hash
and comparison functions now check explicitly for nil. This matches the
gc compiler and permits a simple implementation for ismapkey.
The compiler is changed to permit marking struct and array types as
incomparable, meaning that they have no hash or equal function. We use
this for thunk types, removing the existing special code to avoid
generating hash/equal functions for them.
The C runtime code adds memclr, memequal, and memmove functions.
The hashmap code uses go:linkname comments to make the functions
visible, as otherwise the compiler would discard them.
The hashmap code comments out the unused reference to the address of the
first parameter in the race code, as otherwise the compiler thinks that
the parameter escapes and copies it onto the heap. This is probably not
needed when we enable escape analysis.
Several runtime map tests that ere previously skipped for gccgo are now
run.
The Go runtime picks up type kind information and stubs. The type kind
information causes the generated runtime header file to define some
constants, including `empty`, and the C code is adjusted accordingly.
A Go-callable version of runtime.throw, that takes a Go string, is
added to be called from the hashmap code.
Reviewed-on: https://go-review.googlesource.com/29447
* go.go-torture/execute/map-1.go: Replace old map deletion syntax
with call to builtin delete function.
From-SVN: r240334
2016-09-21 22:58:51 +02:00
|
|
|
for(s = c->mempty.next; s != &c->mempty; s = s->next) {
|
2014-06-07 00:37:27 +02:00
|
|
|
if(s->sweepgen == sg-2 && runtime_cas(&s->sweepgen, sg-2, sg-1)) {
|
|
|
|
// we have an empty span that requires sweeping,
|
|
|
|
// sweep it and see if we can free some space in it
|
|
|
|
runtime_MSpanList_Remove(s);
|
|
|
|
// swept spans are at the end of the list
|
compiler, runtime: replace hashmap code with Go 1.7 hashmap
This change removes the gccgo-specific hashmap code and replaces it with
the hashmap code from the Go 1.7 runtime. The Go 1.7 hashmap code is
more efficient, does a better job on details like when to update a key,
and provides some support against denial-of-service attacks.
The compiler is changed to call the new hashmap functions instead of the
old ones.
The compiler now tracks which types are reflexive and which require
updating when used as a map key, and records the information in map type
descriptors.
Map_index_expression is simplified. The special case for a map index on
the right hand side of a tuple expression has been unnecessary for some
time, and is removed. The support for specially marking a map index as
an lvalue is removed, in favor of lowering an assignment to a map index
into a function call. The long-obsolete support for a map index of a
pointer to a map is removed.
The __go_new_map_big function (known to the compiler as
Runtime::MAKEMAPBIG) is no longer needed, as the new runtime.makemap
function takes an int64 hint argument.
The old map descriptor type and supporting expression is removed.
The compiler was still supporting the long-obsolete syntax `m[k] = 0,
false` to delete a value from a map. That is now removed, requiring a
change to one of the gccgo-specific tests.
The builtin len function applied to a map or channel p is now compiled
as `p == nil ? 0 : *(*int)(p)`. The __go_chan_len function (known to
the compiler as Runtime::CHAN_LEN) is removed.
Support for a shared zero value for maps to large value types is
introduced, along the lines of the gc compiler. The zero value is
handled as a common variable.
The hash function is changed to take a seed argument, changing the
runtime hash functions and the compiler-generated hash functions.
Unlike the gc compiler, both the hash and equal functions continue to
take the type length.
Types that can not be compared now store nil for the hash and equal
functions, rather than pointing to functions that throw. Interface hash
and comparison functions now check explicitly for nil. This matches the
gc compiler and permits a simple implementation for ismapkey.
The compiler is changed to permit marking struct and array types as
incomparable, meaning that they have no hash or equal function. We use
this for thunk types, removing the existing special code to avoid
generating hash/equal functions for them.
The C runtime code adds memclr, memequal, and memmove functions.
The hashmap code uses go:linkname comments to make the functions
visible, as otherwise the compiler would discard them.
The hashmap code comments out the unused reference to the address of the
first parameter in the race code, as otherwise the compiler thinks that
the parameter escapes and copies it onto the heap. This is probably not
needed when we enable escape analysis.
Several runtime map tests that ere previously skipped for gccgo are now
run.
The Go runtime picks up type kind information and stubs. The type kind
information causes the generated runtime header file to define some
constants, including `empty`, and the C code is adjusted accordingly.
A Go-callable version of runtime.throw, that takes a Go string, is
added to be called from the hashmap code.
Reviewed-on: https://go-review.googlesource.com/29447
* go.go-torture/execute/map-1.go: Replace old map deletion syntax
with call to builtin delete function.
From-SVN: r240334
2016-09-21 22:58:51 +02:00
|
|
|
runtime_MSpanList_InsertBack(&c->mempty, s);
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_unlock(c);
|
|
|
|
runtime_MSpan_Sweep(s);
|
|
|
|
runtime_lock(c);
|
|
|
|
// the span could be moved to nonempty or heap, retry
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
if(s->sweepgen == sg-1) {
|
|
|
|
// the span is being swept by background sweeper, skip
|
|
|
|
continue;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2014-06-07 00:37:27 +02:00
|
|
|
// already swept empty span,
|
|
|
|
// all subsequent ones must also be either swept or in process of sweeping
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replenish central list if empty.
|
|
|
|
if(!MCentral_Grow(c)) {
|
|
|
|
runtime_unlock(c);
|
2014-07-19 10:53:52 +02:00
|
|
|
return nil;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
goto retry;
|
2014-06-07 00:37:27 +02:00
|
|
|
|
|
|
|
havespan:
|
2013-01-29 21:52:43 +01:00
|
|
|
cap = (s->npages << PageShift) / s->elemsize;
|
2013-11-06 20:49:01 +01:00
|
|
|
n = cap - s->ref;
|
2014-07-19 10:53:52 +02:00
|
|
|
if(n == 0)
|
|
|
|
runtime_throw("empty span");
|
|
|
|
if(s->freelist == nil)
|
|
|
|
runtime_throw("freelist empty");
|
2013-01-29 21:52:43 +01:00
|
|
|
c->nfree -= n;
|
2013-11-06 20:49:01 +01:00
|
|
|
runtime_MSpanList_Remove(s);
|
compiler, runtime: replace hashmap code with Go 1.7 hashmap
This change removes the gccgo-specific hashmap code and replaces it with
the hashmap code from the Go 1.7 runtime. The Go 1.7 hashmap code is
more efficient, does a better job on details like when to update a key,
and provides some support against denial-of-service attacks.
The compiler is changed to call the new hashmap functions instead of the
old ones.
The compiler now tracks which types are reflexive and which require
updating when used as a map key, and records the information in map type
descriptors.
Map_index_expression is simplified. The special case for a map index on
the right hand side of a tuple expression has been unnecessary for some
time, and is removed. The support for specially marking a map index as
an lvalue is removed, in favor of lowering an assignment to a map index
into a function call. The long-obsolete support for a map index of a
pointer to a map is removed.
The __go_new_map_big function (known to the compiler as
Runtime::MAKEMAPBIG) is no longer needed, as the new runtime.makemap
function takes an int64 hint argument.
The old map descriptor type and supporting expression is removed.
The compiler was still supporting the long-obsolete syntax `m[k] = 0,
false` to delete a value from a map. That is now removed, requiring a
change to one of the gccgo-specific tests.
The builtin len function applied to a map or channel p is now compiled
as `p == nil ? 0 : *(*int)(p)`. The __go_chan_len function (known to
the compiler as Runtime::CHAN_LEN) is removed.
Support for a shared zero value for maps to large value types is
introduced, along the lines of the gc compiler. The zero value is
handled as a common variable.
The hash function is changed to take a seed argument, changing the
runtime hash functions and the compiler-generated hash functions.
Unlike the gc compiler, both the hash and equal functions continue to
take the type length.
Types that can not be compared now store nil for the hash and equal
functions, rather than pointing to functions that throw. Interface hash
and comparison functions now check explicitly for nil. This matches the
gc compiler and permits a simple implementation for ismapkey.
The compiler is changed to permit marking struct and array types as
incomparable, meaning that they have no hash or equal function. We use
this for thunk types, removing the existing special code to avoid
generating hash/equal functions for them.
The C runtime code adds memclr, memequal, and memmove functions.
The hashmap code uses go:linkname comments to make the functions
visible, as otherwise the compiler would discard them.
The hashmap code comments out the unused reference to the address of the
first parameter in the race code, as otherwise the compiler thinks that
the parameter escapes and copies it onto the heap. This is probably not
needed when we enable escape analysis.
Several runtime map tests that ere previously skipped for gccgo are now
run.
The Go runtime picks up type kind information and stubs. The type kind
information causes the generated runtime header file to define some
constants, including `empty`, and the C code is adjusted accordingly.
A Go-callable version of runtime.throw, that takes a Go string, is
added to be called from the hashmap code.
Reviewed-on: https://go-review.googlesource.com/29447
* go.go-torture/execute/map-1.go: Replace old map deletion syntax
with call to builtin delete function.
From-SVN: r240334
2016-09-21 22:58:51 +02:00
|
|
|
runtime_MSpanList_InsertBack(&c->mempty, s);
|
2014-07-19 10:53:52 +02:00
|
|
|
s->incache = true;
|
|
|
|
runtime_unlock(c);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return span from an MCache.
|
|
|
|
void
|
|
|
|
runtime_MCentral_UncacheSpan(MCentral *c, MSpan *s)
|
|
|
|
{
|
|
|
|
MLink *v;
|
|
|
|
int32 cap, n;
|
|
|
|
|
|
|
|
runtime_lock(c);
|
|
|
|
|
|
|
|
s->incache = false;
|
|
|
|
|
|
|
|
// Move any explicitly freed items from the freebuf to the freelist.
|
|
|
|
while((v = s->freebuf) != nil) {
|
|
|
|
s->freebuf = v->next;
|
|
|
|
runtime_markfreed(v);
|
|
|
|
v->next = s->freelist;
|
|
|
|
s->freelist = v;
|
|
|
|
s->ref--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s->ref == 0) {
|
|
|
|
// Free back to heap. Unlikely, but possible.
|
|
|
|
MCentral_ReturnToHeap(c, s); // unlocks c
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cap = (s->npages << PageShift) / s->elemsize;
|
|
|
|
n = cap - s->ref;
|
|
|
|
if(n > 0) {
|
|
|
|
c->nfree += n;
|
|
|
|
runtime_MSpanList_Remove(s);
|
|
|
|
runtime_MSpanList_Insert(&c->nonempty, s);
|
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
runtime_unlock(c);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// Free the list of objects back into the central free list c.
|
|
|
|
// Called from runtime_free.
|
2010-12-03 05:34:57 +01:00
|
|
|
void
|
2013-11-06 20:49:01 +01:00
|
|
|
runtime_MCentral_FreeList(MCentral *c, MLink *start)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
2013-11-06 20:49:01 +01:00
|
|
|
MLink *next;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
runtime_lock(c);
|
2013-11-06 20:49:01 +01:00
|
|
|
for(; start != nil; start = next) {
|
|
|
|
next = start->next;
|
|
|
|
MCentral_Free(c, start);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
runtime_unlock(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper: free one object back into the central free list.
|
2014-07-19 10:53:52 +02:00
|
|
|
// Caller must hold lock on c on entry. Holds lock on exit.
|
2010-12-03 05:34:57 +01:00
|
|
|
static void
|
2014-07-19 10:53:52 +02:00
|
|
|
MCentral_Free(MCentral *c, MLink *v)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
MSpan *s;
|
|
|
|
|
|
|
|
// Find span for v.
|
2013-11-06 20:49:01 +01:00
|
|
|
s = runtime_MHeap_Lookup(&runtime_mheap, v);
|
2010-12-03 05:34:57 +01:00
|
|
|
if(s == nil || s->ref == 0)
|
|
|
|
runtime_throw("invalid free");
|
2014-07-19 10:53:52 +02:00
|
|
|
if(s->sweepgen != runtime_mheap.sweepgen)
|
|
|
|
runtime_throw("free into unswept span");
|
|
|
|
|
|
|
|
// If the span is currently being used unsynchronized by an MCache,
|
|
|
|
// we can't modify the freelist. Add to the freebuf instead. The
|
|
|
|
// items will get moved to the freelist when the span is returned
|
|
|
|
// by the MCache.
|
|
|
|
if(s->incache) {
|
|
|
|
v->next = s->freebuf;
|
|
|
|
s->freebuf = v;
|
|
|
|
return;
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// Move span to nonempty if necessary.
|
2010-12-03 05:34:57 +01:00
|
|
|
if(s->freelist == nil) {
|
|
|
|
runtime_MSpanList_Remove(s);
|
|
|
|
runtime_MSpanList_Insert(&c->nonempty, s);
|
|
|
|
}
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// Add the object to span's free list.
|
|
|
|
runtime_markfreed(v);
|
|
|
|
v->next = s->freelist;
|
|
|
|
s->freelist = v;
|
|
|
|
s->ref--;
|
2010-12-03 05:34:57 +01:00
|
|
|
c->nfree++;
|
|
|
|
|
|
|
|
// If s is completely freed, return it to the heap.
|
2014-07-19 10:53:52 +02:00
|
|
|
if(s->ref == 0) {
|
|
|
|
MCentral_ReturnToHeap(c, s); // unlocks c
|
2010-12-03 05:34:57 +01:00
|
|
|
runtime_lock(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// Free n objects from a span s back into the central free list c.
|
2014-06-07 00:37:27 +02:00
|
|
|
// Called during sweep.
|
2014-07-19 10:53:52 +02:00
|
|
|
// Returns true if the span was returned to heap. Sets sweepgen to
|
|
|
|
// the latest generation.
|
2014-06-07 00:37:27 +02:00
|
|
|
bool
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_MCentral_FreeSpan(MCentral *c, MSpan *s, int32 n, MLink *start, MLink *end)
|
|
|
|
{
|
2014-07-19 10:53:52 +02:00
|
|
|
if(s->incache)
|
|
|
|
runtime_throw("freespan into cached span");
|
2012-10-23 06:31:11 +02:00
|
|
|
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;
|
2014-07-19 10:53:52 +02:00
|
|
|
|
|
|
|
// delay updating sweepgen until here. This is the signal that
|
|
|
|
// the span may be used in an MCache, so it must come after the
|
|
|
|
// linked list operations above (actually, just after the
|
|
|
|
// lock of c above.)
|
|
|
|
runtime_atomicstore(&s->sweepgen, runtime_mheap.sweepgen);
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
if(s->ref != 0) {
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_unlock(c);
|
2014-06-07 00:37:27 +02:00
|
|
|
return false;
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
2014-06-07 00:37:27 +02:00
|
|
|
|
|
|
|
// s is completely freed, return it to the heap.
|
2014-07-19 10:53:52 +02:00
|
|
|
MCentral_ReturnToHeap(c, s); // unlocks c
|
2014-06-07 00:37:27 +02:00
|
|
|
return true;
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
void
|
2011-03-17 00:05:44 +01:00
|
|
|
runtime_MGetSizeClassInfo(int32 sizeclass, uintptr *sizep, int32 *npagesp, int32 *nobj)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
int32 size;
|
|
|
|
int32 npages;
|
|
|
|
|
|
|
|
npages = runtime_class_to_allocnpages[sizeclass];
|
|
|
|
size = runtime_class_to_size[sizeclass];
|
|
|
|
*npagesp = npages;
|
|
|
|
*sizep = size;
|
2011-03-17 00:05:44 +01:00
|
|
|
*nobj = (npages << PageShift) / size;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch a new span from the heap and
|
|
|
|
// carve into objects for the free list.
|
|
|
|
static bool
|
|
|
|
MCentral_Grow(MCentral *c)
|
|
|
|
{
|
2011-03-17 00:05:44 +01:00
|
|
|
int32 i, n, npages;
|
|
|
|
uintptr size;
|
2010-12-03 05:34:57 +01:00
|
|
|
MLink **tailp, *v;
|
|
|
|
byte *p;
|
|
|
|
MSpan *s;
|
|
|
|
|
|
|
|
runtime_unlock(c);
|
|
|
|
runtime_MGetSizeClassInfo(c->sizeclass, &size, &npages, &n);
|
2013-11-06 20:49:01 +01:00
|
|
|
s = runtime_MHeap_Alloc(&runtime_mheap, npages, c->sizeclass, 0, 1);
|
2010-12-03 05:34:57 +01:00
|
|
|
if(s == nil) {
|
|
|
|
// TODO(rsc): Log out of memory
|
|
|
|
runtime_lock(c);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Carve span into sequence of blocks.
|
|
|
|
tailp = &s->freelist;
|
|
|
|
p = (byte*)(s->start << PageShift);
|
2016-08-30 23:07:47 +02:00
|
|
|
s->limit = (uintptr)(p + size*n);
|
2010-12-03 05:34:57 +01:00
|
|
|
for(i=0; i<n; i++) {
|
|
|
|
v = (MLink*)p;
|
|
|
|
*tailp = v;
|
|
|
|
tailp = &v->next;
|
|
|
|
p += size;
|
|
|
|
}
|
|
|
|
*tailp = nil;
|
2011-03-17 00:05:44 +01:00
|
|
|
runtime_markspan((byte*)(s->start<<PageShift), size, n, size*n < (s->npages<<PageShift));
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
runtime_lock(c);
|
|
|
|
c->nfree += n;
|
|
|
|
runtime_MSpanList_Insert(&c->nonempty, s);
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
|
|
|
|
// Return s to the heap. s must be unused (s->ref == 0). Unlocks c.
|
|
|
|
static void
|
|
|
|
MCentral_ReturnToHeap(MCentral *c, MSpan *s)
|
|
|
|
{
|
|
|
|
int32 size;
|
|
|
|
|
|
|
|
size = runtime_class_to_size[c->sizeclass];
|
|
|
|
runtime_MSpanList_Remove(s);
|
|
|
|
s->needzero = 1;
|
|
|
|
s->freelist = nil;
|
|
|
|
if(s->ref != 0)
|
|
|
|
runtime_throw("ref wrong");
|
|
|
|
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);
|
|
|
|
}
|