corda/src/heap.cpp

1447 lines
31 KiB
C++
Raw Normal View History

#include "heap.h"
#include "system.h"
2007-06-20 04:26:36 +00:00
#include "common.h"
2007-06-22 02:13:17 +00:00
#define CHAIN_HEADER_SIZE divide(sizeof(Segment::Chain), BytesPerWord)
using namespace vm;
namespace {
2007-06-20 16:58:35 +00:00
// an object must survive TenureThreshold + 2 garbage collections
// before being copied to gen2:
2007-06-21 22:23:35 +00:00
const unsigned TenureThreshold = 3;
2007-06-20 16:58:35 +00:00
2007-06-22 20:55:11 +00:00
const unsigned MinimumGen1SizeInBytes = 64 * 1024;
const unsigned MinimumGen2SizeInBytes = 128 * 1024;
2007-06-21 22:23:35 +00:00
2007-06-22 02:13:17 +00:00
const unsigned Top = ~static_cast<unsigned>(0);
2007-06-20 16:58:35 +00:00
const bool Verbose = false;
const bool Debug = false;
2007-06-20 16:58:35 +00:00
class Context;
System* system(Context*);
void NO_RETURN abort(Context*);
void assert(Context*, bool);
inline object
get(object o, unsigned offsetInWords)
{
return mask(cast<object>(o, offsetInWords * BytesPerWord));
}
inline object*
getp(object o, unsigned offsetInWords)
{
return &cast<object>(o, offsetInWords * BytesPerWord);
}
inline void
set(object* o, object value)
{
*o = reinterpret_cast<object>
(reinterpret_cast<uintptr_t>(value)
| reinterpret_cast<uintptr_t>(*o) & (~PointerMask));
}
inline void
set(object o, unsigned offsetInWords, object value)
{
set(getp(o, offsetInWords), value);
}
2007-06-20 04:26:36 +00:00
class Segment {
public:
class Map {
public:
2007-06-22 02:13:17 +00:00
class Chain;
2007-06-20 04:26:36 +00:00
class Iterator {
public:
Map* map;
unsigned index;
unsigned limit;
2007-06-20 17:42:13 +00:00
Iterator(Map* map, unsigned start, unsigned end):
2007-06-20 04:26:36 +00:00
map(map)
{
2007-06-20 16:58:35 +00:00
assert(map->segment->context, map->bitsPerRecord == 1);
assert(map->segment->context, map->segment);
2007-06-22 02:13:17 +00:00
assert(map->segment->context, start <= map->segment->position());
2007-06-20 04:26:36 +00:00
2007-06-22 02:13:17 +00:00
if (end > map->segment->position()) end = map->segment->position();
2007-06-20 04:26:36 +00:00
2007-06-22 02:13:17 +00:00
index = map->indexOf(start);
2007-06-21 22:23:35 +00:00
limit = map->indexOf(end);
2007-06-20 04:26:36 +00:00
2007-06-22 02:13:17 +00:00
if ((end - start) % map->scale) ++ limit;
2007-06-20 04:26:36 +00:00
}
bool hasMore() {
unsigned word = wordOf(index);
unsigned bit = bitOf(index);
unsigned wordLimit = wordOf(limit);
unsigned bitLimit = bitOf(limit);
for (; word <= wordLimit and (word < wordLimit or bit < bitLimit);
++word)
{
2007-06-20 05:16:43 +00:00
uintptr_t* p = map->data() + word;
2007-06-20 04:26:36 +00:00
if (*p) {
for (; bit < BitsPerWord and (word < wordLimit or bit < bitLimit);
++bit)
{
2007-06-20 05:16:43 +00:00
if (map->data()[word] & (static_cast<uintptr_t>(1) << bit)) {
2007-06-20 04:26:36 +00:00
index = ::indexOf(word, bit);
// printf("hit at index %d\n", index);
return true;
} else {
// printf("miss at index %d\n", indexOf(word, bit));
}
}
}
bit = 0;
}
index = limit;
return false;
}
2007-06-20 17:42:13 +00:00
unsigned next() {
2007-06-20 05:16:43 +00:00
assert(map->segment->context, hasMore());
assert(map->segment->context, map->segment);
2007-06-20 04:26:36 +00:00
2007-06-20 17:42:13 +00:00
return (index++) * map->scale;
2007-06-20 04:26:36 +00:00
}
};
Segment* segment;
Map* child;
2007-06-20 04:26:36 +00:00
unsigned bitsPerRecord;
unsigned scale;
bool clearNewData;
2007-06-22 02:13:17 +00:00
2007-06-20 16:58:35 +00:00
Map(Segment* segment = 0, unsigned bitsPerRecord = 1,
unsigned scale = 1, Map* child = 0, bool clearNewData = true):
2007-06-20 16:58:35 +00:00
segment(segment),
child(child),
2007-06-20 16:58:35 +00:00
bitsPerRecord(bitsPerRecord),
scale(scale),
clearNewData(clearNewData)
2007-06-20 04:26:36 +00:00
{
2007-06-20 21:27:22 +00:00
if (segment) {
assert(segment->context, bitsPerRecord);
assert(segment->context, scale);
assert(segment->context, powerOfTwo(scale));
}
2007-06-20 16:58:35 +00:00
}
2007-06-20 04:26:36 +00:00
2007-06-21 20:44:35 +00:00
void replaceWith(Map* m) {
assert(segment->context, bitsPerRecord == m->bitsPerRecord);
assert(segment->context, scale == m->scale);
m->segment = 0;
if (child) child->replaceWith(m->child);
}
2007-06-22 02:13:17 +00:00
unsigned offset(unsigned capacity) {
unsigned n = 0;
if (child) n += child->footprint(capacity);
2007-06-20 16:58:35 +00:00
return n;
2007-06-20 04:26:36 +00:00
}
2007-06-22 02:13:17 +00:00
unsigned offset() {
return offset(segment->capacity());
}
2007-06-20 05:16:43 +00:00
uintptr_t* data() {
2007-06-22 02:13:17 +00:00
return segment->rear->data() + segment->rear->capacity + offset();
2007-06-20 04:26:36 +00:00
}
unsigned size(unsigned capacity) {
2007-06-20 16:58:35 +00:00
unsigned result
= divide(divide(capacity, scale) * bitsPerRecord, BitsPerWord);
2007-06-20 05:16:43 +00:00
assert(segment->context, result);
2007-06-20 04:26:36 +00:00
return result;
}
unsigned size() {
2007-06-22 02:13:17 +00:00
return size(max(segment->capacity(), 1));
2007-06-20 04:26:36 +00:00
}
2007-06-21 22:23:35 +00:00
unsigned indexOf(unsigned segmentIndex) {
return (segmentIndex / scale) * bitsPerRecord;
}
2007-06-20 04:26:36 +00:00
unsigned indexOf(void* p) {
2007-06-22 02:13:17 +00:00
assert(segment->context, segment->almostContains(p));
assert(segment->context, segment->capacity());
return indexOf(segment->indexOf(p));
2007-06-20 04:26:36 +00:00
}
2007-06-22 02:13:17 +00:00
void update(uintptr_t* newData, unsigned capacity) {
assert(segment->context, capacity >= segment->capacity());
2007-06-20 04:26:36 +00:00
2007-06-22 02:13:17 +00:00
uintptr_t* p = newData + offset(capacity);
memcpy(p, data(), size(segment->position()) * BytesPerWord);
if (child) {
child->update(newData, capacity);
}
2007-06-20 04:26:36 +00:00
}
void clear() {
2007-06-20 16:58:35 +00:00
memset(data(), 0, size() * BytesPerWord);
2007-06-20 04:26:36 +00:00
if (child) child->clear();
}
2007-06-22 02:13:17 +00:00
void clearBit(unsigned i) {
2007-06-22 03:16:42 +00:00
assert(segment->context, wordOf(i) < size());
2007-06-22 02:13:17 +00:00
2007-06-20 16:58:35 +00:00
data()[wordOf(i)] &= ~(static_cast<uintptr_t>(1) << bitOf(i));
2007-06-20 04:26:36 +00:00
}
2007-06-22 02:13:17 +00:00
void setBit(unsigned i) {
2007-06-22 03:16:42 +00:00
assert(segment->context, wordOf(i) < size());
2007-06-22 02:13:17 +00:00
2007-06-20 16:58:35 +00:00
data()[wordOf(i)] |= static_cast<uintptr_t>(1) << bitOf(i);
2007-06-20 04:26:36 +00:00
}
2007-06-21 22:23:35 +00:00
void clearOnlyIndex(unsigned index) {
2007-06-20 04:26:36 +00:00
for (unsigned i = index, limit = index + bitsPerRecord; i < limit; ++i) {
2007-06-22 02:13:17 +00:00
clearBit(i);
2007-06-20 04:26:36 +00:00
}
}
2007-06-21 22:23:35 +00:00
void clearOnly(unsigned segmentIndex) {
clearOnlyIndex(indexOf(segmentIndex));
}
2007-06-20 17:42:13 +00:00
void clearOnly(void* p) {
2007-06-21 22:23:35 +00:00
clearOnlyIndex(indexOf(p));
2007-06-20 17:42:13 +00:00
}
2007-06-20 04:26:36 +00:00
void clear(void* p) {
clearOnly(p);
if (child) child->clear(p);
}
2007-06-21 22:23:35 +00:00
void setOnlyIndex(unsigned index, unsigned v = 1) {
2007-06-20 04:26:36 +00:00
unsigned i = index + bitsPerRecord - 1;
while (true) {
2007-06-22 02:13:17 +00:00
if (v & 1) setBit(i); else clearBit(i);
2007-06-20 04:26:36 +00:00
v >>= 1;
if (i == index) break;
--i;
}
}
2007-06-21 22:23:35 +00:00
void setOnly(unsigned segmentIndex, unsigned v = 1) {
setOnlyIndex(indexOf(segmentIndex), v);
}
2007-06-20 17:42:13 +00:00
void setOnly(void* p, unsigned v = 1) {
2007-06-21 22:23:35 +00:00
setOnlyIndex(indexOf(p), v);
2007-06-20 17:42:13 +00:00
}
2007-06-20 04:26:36 +00:00
void set(void* p, unsigned v = 1) {
setOnly(p, v);
2007-06-20 16:58:35 +00:00
assert(segment->context, get(p) == v);
2007-06-20 04:26:36 +00:00
if (child) child->set(p, v);
}
unsigned get(void* p) {
unsigned index = indexOf(p);
unsigned v = 0;
for (unsigned i = index, limit = index + bitsPerRecord; i < limit; ++i) {
unsigned wi = bitOf(i);
v <<= 1;
2007-06-20 16:58:35 +00:00
v |= ((data()[wordOf(i)]) & (static_cast<uintptr_t>(1) << wi)) >> wi;
2007-06-20 04:26:36 +00:00
}
return v;
}
unsigned footprint(unsigned capacity) {
unsigned n = size(capacity);
if (child) n += child->footprint(capacity);
return n;
}
2007-06-20 16:58:35 +00:00
void setSegment(Segment* s) {
2007-06-20 04:26:36 +00:00
segment = s;
2007-06-20 16:58:35 +00:00
2007-06-20 04:26:36 +00:00
if (child) child->setSegment(s);
}
};
2007-06-22 02:13:17 +00:00
class Chain {
public:
Segment* segment;
unsigned offset;
unsigned position;
unsigned capacity;
Chain* next;
Chain* previous;
2007-06-22 20:55:11 +00:00
Chain(Segment* segment, unsigned capacity, Chain* previous):
2007-06-22 02:13:17 +00:00
segment(segment),
2007-06-22 20:55:11 +00:00
offset(previous ? previous->offset + previous->position : 0),
2007-06-22 02:13:17 +00:00
position(0),
capacity(capacity),
next(0),
previous(previous)
{
assert(segment->context, sizeof(Chain) % BytesPerWord == 0);
}
2007-06-22 20:55:11 +00:00
static Chain* make(Segment* s, unsigned minimum, unsigned desired) {
assert(s->context, minimum > 0);
assert(s->context, desired >= minimum);
void* p = 0;
unsigned capacity = desired;
while (p == 0) {
p = system(s->context)->tryAllocate
(footprint(capacity, s->rear, s->map) * BytesPerWord);
if (p == 0) {
if (capacity > minimum) {
capacity = avg(minimum, capacity);
2007-06-22 20:55:11 +00:00
} else {
abort(s->context);
}
}
}
return new (p) Chain(s, capacity, s->rear);
}
2007-06-22 02:13:17 +00:00
static void dispose(Chain* c) {
if (c) {
if (c->next) dispose(c->next);
system(c->segment->context)->free(c);
}
}
uintptr_t* data() {
return reinterpret_cast<uintptr_t*>(this) + CHAIN_HEADER_SIZE;
}
2007-06-22 03:16:42 +00:00
static unsigned footprint(unsigned capacity, Chain* previous,
Map* map)
{
2007-06-22 02:13:17 +00:00
unsigned n = CHAIN_HEADER_SIZE + capacity;
if (map) {
2007-06-22 03:16:42 +00:00
unsigned segmentCapacity = capacity;
if (previous) {
segmentCapacity += previous->offset + previous->position;
}
n += map->footprint(segmentCapacity);
2007-06-22 02:13:17 +00:00
}
return n;
}
unsigned footprint() {
2007-06-22 03:16:42 +00:00
return footprint(capacity, previous, segment->map);
2007-06-22 02:13:17 +00:00
}
};
2007-06-20 17:42:13 +00:00
Context* context;
2007-06-22 02:13:17 +00:00
Chain* front;
Chain* rear;
2007-06-20 04:26:36 +00:00
Map* map;
Segment(Context* context, unsigned minimum, unsigned desired, Map* map = 0):
2007-06-20 16:58:35 +00:00
context(context),
2007-06-22 02:13:17 +00:00
front(0),
rear(0),
2007-06-20 16:58:35 +00:00
map(map)
2007-06-20 05:16:43 +00:00
{
2007-06-22 20:55:11 +00:00
if (desired) {
front = rear = Chain::make(this, minimum, desired);
2007-06-20 05:16:43 +00:00
if (map) {
if (map->clearNewData) {
2007-06-22 20:55:11 +00:00
memset(front->data() + front->capacity, 0,
map->footprint(front->capacity) * BytesPerWord);
2007-06-22 02:13:17 +00:00
}
2007-06-22 20:55:11 +00:00
map->setSegment(this);
2007-06-20 05:16:43 +00:00
}
2007-06-20 04:26:36 +00:00
}
}
2007-06-22 02:13:17 +00:00
unsigned capacity() {
return (rear? rear->offset + rear->capacity : 0);
2007-06-20 16:58:35 +00:00
}
2007-06-22 02:13:17 +00:00
unsigned position() {
return (rear? rear->offset + rear->position : 0);
2007-06-20 16:58:35 +00:00
}
2007-06-22 02:13:17 +00:00
void truncate(unsigned offset) {
assert(context, offset <= position());
for (Chain* c = front; c; c = c->next) {
if (offset >= c->offset
and offset <= c->offset + c->position)
{
c->position = offset - c->offset;
Chain::dispose(c->next);
return;
}
}
abort(context);
2007-06-20 05:16:43 +00:00
}
2007-06-22 02:13:17 +00:00
unsigned footprint() {
unsigned n = 0;
for (Chain* c = front; c; c = c->next) n += c->footprint();
return n;
2007-06-20 17:42:13 +00:00
}
2007-06-20 04:26:36 +00:00
unsigned remaining() {
2007-06-22 02:13:17 +00:00
return capacity() - position();
2007-06-20 04:26:36 +00:00
}
void replaceWith(Segment* s) {
2007-06-22 02:13:17 +00:00
Chain::dispose(front);
2007-06-20 04:26:36 +00:00
2007-06-22 02:13:17 +00:00
front = s->front;
rear = s->rear;
2007-06-20 04:26:36 +00:00
2007-06-22 02:13:17 +00:00
s->front = s->rear = 0;
2007-06-20 04:26:36 +00:00
if (s->map) {
2007-06-21 20:44:35 +00:00
if (map) {
map->replaceWith(s->map);
} else {
map = s->map;
map->setSegment(this);
}
2007-06-20 04:26:36 +00:00
s->map = 0;
} else {
2007-06-20 16:58:35 +00:00
map = 0;
2007-06-20 04:26:36 +00:00
}
}
2007-06-22 02:13:17 +00:00
bool contains(void* p) {
for (Chain* c = front; c; c = c->next) {
if (c->position and p >= c->data() and p < c->data() + c->position) {
return true;
}
}
return false;
}
2007-06-20 04:26:36 +00:00
2007-06-22 02:13:17 +00:00
bool almostContains(void* p) {
return contains(p) or p == rear->data() + rear->position;
}
2007-06-20 04:26:36 +00:00
2007-06-22 02:13:17 +00:00
void* get(unsigned offset) {
for (Chain* c = front; c; c = c->next) {
if (c->position
and offset >= c->offset
and offset < c->offset + c->position)
{
return c->data() + (offset - c->offset);
}
}
2007-06-20 05:16:43 +00:00
2007-06-22 02:13:17 +00:00
if (offset == rear->offset + rear->position) {
return rear->data() + (offset - rear->offset);
}
2007-06-20 05:16:43 +00:00
2007-06-22 02:13:17 +00:00
abort(context);
}
unsigned indexOf(void* p) {
for (Chain* c = front; c; c = c->next) {
if (c->position and p >= c->data() and p < c->data() + c->position) {
return (static_cast<uintptr_t*>(p) - c->data()) + c->offset;
2007-06-20 04:26:36 +00:00
}
}
2007-06-22 02:13:17 +00:00
if (p == rear->data() + rear->position) {
return (static_cast<uintptr_t*>(p) - rear->data()) + rear->offset;
}
abort(context);
2007-06-20 04:26:36 +00:00
}
2007-06-22 03:16:42 +00:00
void* allocate(unsigned size) {
2007-06-22 02:13:17 +00:00
assert(context, size);
assert(context, rear->position + size <= rear->capacity);
void* p = reinterpret_cast<void**>(rear->data()) + rear->position;
rear->position += size;
return p;
2007-06-20 04:26:36 +00:00
}
2007-06-22 02:13:17 +00:00
void ensure(unsigned minimum) {
if (remaining() < minimum) {
2007-06-22 03:16:42 +00:00
assert(context, rear->position);
assert(context, rear->next == 0);
unsigned desired = capacity() + minimum;
2007-06-22 02:13:17 +00:00
2007-06-22 20:55:11 +00:00
Chain* c = Chain::make(this, minimum, desired);
2007-06-20 04:26:36 +00:00
2007-06-22 02:13:17 +00:00
if (map) {
if (map->clearNewData) {
memset(c->data() + c->capacity, 0,
map->footprint(c->offset + c->capacity) * BytesPerWord);
}
2007-06-22 20:55:11 +00:00
2007-06-22 03:16:42 +00:00
map->update(c->data() + c->capacity, c->offset + c->capacity);
2007-06-22 02:13:17 +00:00
}
2007-06-22 03:16:42 +00:00
rear->next = c;
2007-06-22 02:13:17 +00:00
rear = c;
}
}
void dispose() {
Chain::dispose(front);
front = rear = 0;
2007-06-20 04:26:36 +00:00
map = 0;
}
};
2007-06-20 16:58:35 +00:00
enum CollectionMode {
MinorCollection,
MajorCollection,
OverflowCollection,
Gen2Collection
};
class Context {
public:
2007-06-20 17:42:13 +00:00
Context(System* system):
system(system),
client(0),
2007-06-22 20:55:11 +00:00
gen1(this, 0, 0),
nextGen1(this, 0, 0),
gen2(this, 0, 0),
nextGen2(this, 0, 0)
2007-06-20 17:42:13 +00:00
{ }
2007-06-20 16:58:35 +00:00
void dispose() {
gen1.dispose();
nextGen1.dispose();
gen2.dispose();
nextGen2.dispose();
}
System* system;
2007-06-20 17:42:13 +00:00
Heap::Client* client;
2007-06-20 16:58:35 +00:00
Segment gen1;
Segment nextGen1;
Segment gen2;
Segment nextGen2;
2007-06-22 02:13:17 +00:00
unsigned gen2Base;
2007-06-21 22:23:35 +00:00
2007-06-20 16:58:35 +00:00
Segment::Map ageMap;
2007-06-20 17:42:13 +00:00
Segment::Map nextAgeMap;
2007-06-22 20:55:11 +00:00
2007-06-20 16:58:35 +00:00
Segment::Map pointerMap;
Segment::Map pageMap;
Segment::Map heapMap;
2007-06-22 20:55:11 +00:00
Segment::Map nextPointerMap;
Segment::Map nextPageMap;
Segment::Map nextHeapMap;
2007-06-20 16:58:35 +00:00
CollectionMode mode;
};
2007-06-22 20:55:11 +00:00
const char*
segment(Context* c, void* p)
{
if (c->gen1.contains(p)) {
return "gen1";
} else if (c->nextGen1.contains(p)) {
return "nextGen1";
} else if (c->gen2.contains(p)) {
return "gen2";
} else if (c->nextGen2.contains(p)) {
return "nextGen2";
} else {
return "none";
}
}
2007-06-20 16:58:35 +00:00
inline System*
system(Context* c)
{
return c->system;
}
inline void NO_RETURN
abort(Context* c)
{
abort(c->system);
2007-06-20 16:58:35 +00:00
}
inline void
assert(Context* c, bool v)
{
assert(c->system, v);
2007-06-20 16:58:35 +00:00
}
2007-06-20 04:26:36 +00:00
void
initGen1(Context* c)
{
2007-06-22 20:55:11 +00:00
unsigned minimum = MinimumGen1SizeInBytes / BytesPerWord;
unsigned desired = minimum;
new (&(c->ageMap)) Segment::Map
(&(c->gen1), log(TenureThreshold), 1, 0, false);
new (&(c->gen1)) Segment(c, minimum, desired, &(c->ageMap));
2007-06-22 20:55:11 +00:00
if (Verbose) {
fprintf(stderr, "init gen1 to %d bytes\n",
c->gen1.capacity() * BytesPerWord);
}
2007-06-22 20:55:11 +00:00
}
void
initNextGen1(Context* c)
{
unsigned minimum = MinimumGen1SizeInBytes / BytesPerWord;
unsigned desired = max(minimum, avg(c->gen1.position(), c->gen1.capacity()));
2007-06-22 20:55:11 +00:00
new (&(c->nextAgeMap)) Segment::Map
(&(c->nextGen1), log(TenureThreshold), 1, 0, false);
new (&(c->nextGen1)) Segment(c, minimum, desired, &(c->nextAgeMap));
2007-06-22 20:55:11 +00:00
if (Verbose) {
fprintf(stderr, "init nextGen1 to %d bytes\n",
c->nextGen1.capacity() * BytesPerWord);
}
2007-06-20 04:26:36 +00:00
}
void
initGen2(Context* c)
{
2007-06-22 20:55:11 +00:00
unsigned minimum = MinimumGen2SizeInBytes / BytesPerWord;
unsigned desired = minimum;
2007-06-20 16:58:35 +00:00
new (&(c->pointerMap)) Segment::Map(&(c->gen2));
new (&(c->pageMap)) Segment::Map
2007-06-22 20:55:11 +00:00
(&(c->gen2), 1, LikelyPageSizeInBytes / BytesPerWord, &(c->pointerMap));
2007-06-20 16:58:35 +00:00
new (&(c->heapMap)) Segment::Map
(&(c->gen2), 1, c->pageMap.scale * 1024, &(c->pageMap));
2007-06-20 04:26:36 +00:00
2007-06-22 20:55:11 +00:00
new (&(c->gen2)) Segment(c, minimum, desired, &(c->heapMap));
if (Verbose) {
fprintf(stderr, "init gen2 to %d bytes\n",
c->gen2.capacity() * BytesPerWord);
}
2007-06-20 04:26:36 +00:00
}
void
initNextGen2(Context* c)
{
2007-06-22 20:55:11 +00:00
unsigned minimum = MinimumGen2SizeInBytes / BytesPerWord;
unsigned desired = max(minimum, avg(c->gen2.position(), c->gen2.capacity()));
2007-06-22 20:55:11 +00:00
new (&(c->nextPointerMap)) Segment::Map(&(c->nextGen2));
new (&(c->nextPageMap)) Segment::Map
(&(c->nextGen2), 1, LikelyPageSizeInBytes / BytesPerWord,
&(c->nextPointerMap));
new (&(c->nextHeapMap)) Segment::Map
(&(c->nextGen2), 1, c->pageMap.scale * 1024, &(c->nextPageMap));
new (&(c->nextGen2)) Segment(c, minimum, desired, &(c->nextHeapMap));
if (Verbose) {
fprintf(stderr, "init nextGen2 to %d bytes\n",
c->nextGen2.capacity() * BytesPerWord);
}
2007-06-20 04:26:36 +00:00
}
2007-06-21 22:23:35 +00:00
inline bool
fresh(Context* c, object o)
{
return c->nextGen1.contains(o)
or c->nextGen2.contains(o)
2007-06-22 03:16:42 +00:00
or (c->gen2.contains(o) and c->gen2.indexOf(o) >= c->gen2Base);
2007-06-21 22:23:35 +00:00
}
2007-06-21 18:35:24 +00:00
inline bool
wasCollected(Context* c, object o)
2007-06-20 04:26:36 +00:00
{
return o and (not fresh(c, o)) and fresh(c, get(o, 0));
2007-06-21 18:35:24 +00:00
}
inline object
follow(Context* c, object o)
{
assert(c, wasCollected(c, o));
2007-06-20 04:26:36 +00:00
return cast<object>(o, 0);
}
inline object&
2007-06-21 18:35:24 +00:00
parent(Context* c, object o)
2007-06-20 04:26:36 +00:00
{
2007-06-21 18:35:24 +00:00
assert(c, wasCollected(c, o));
2007-06-20 04:26:36 +00:00
return cast<object>(o, BytesPerWord);
}
inline uintptr_t*
2007-06-21 18:35:24 +00:00
bitset(Context* c, object o)
2007-06-20 04:26:36 +00:00
{
2007-06-21 18:35:24 +00:00
assert(c, wasCollected(c, o));
2007-06-20 04:26:36 +00:00
return &cast<uintptr_t>(o, BytesPerWord * 2);
}
2007-06-20 17:42:13 +00:00
inline object
copyTo(Context* c, Segment* s, object o, unsigned size)
2007-06-20 04:26:36 +00:00
{
if (s->remaining() < size) {
s->ensure(size);
if (Verbose) {
if (s == &(c->gen2)) {
fprintf(stderr, "grow gen2 to %d bytes\n",
c->gen2.capacity() * BytesPerWord);
} else if (s == &(c->nextGen1)) {
fprintf(stderr, "grow nextGen1 to %d bytes\n",
c->nextGen1.capacity() * BytesPerWord);
} else if (s == &(c->nextGen2)) {
fprintf(stderr, "grow nextGen2 to %d bytes\n",
c->nextGen2.capacity() * BytesPerWord);
} else {
abort(c);
2007-06-22 20:55:11 +00:00
}
}
}
object dst = s->allocate(size);
c->client->copy(o, dst);
return dst;
2007-06-20 04:26:36 +00:00
}
object
copy2(Context* c, object o)
{
unsigned size = c->client->copiedSizeInWords(o);
2007-06-20 04:26:36 +00:00
if (c->gen2.contains(o)) {
assert(c, c->mode == MajorCollection
or c->mode == Gen2Collection);
return copyTo(c, &(c->nextGen2), o, size);
2007-06-20 04:26:36 +00:00
} else if (c->gen1.contains(o)) {
unsigned age = c->ageMap.get(o);
2007-06-20 17:42:13 +00:00
if (age == TenureThreshold) {
2007-06-20 04:26:36 +00:00
if (c->mode == MinorCollection) {
2007-06-22 02:13:17 +00:00
if (c->gen2.front == 0) initGen2(c);
2007-06-20 04:26:36 +00:00
2007-06-20 05:16:43 +00:00
if (c->gen2.remaining() >= size) {
2007-06-21 22:23:35 +00:00
if (c->gen2Base == Top) {
2007-06-22 02:13:17 +00:00
c->gen2Base = c->gen2.position();
2007-06-21 22:23:35 +00:00
}
return copyTo(c, &(c->gen2), o, size);
2007-06-20 04:26:36 +00:00
} else {
2007-06-22 02:13:17 +00:00
if (Verbose) {
fprintf(stderr, "overflow collection\n");
}
2007-06-20 04:26:36 +00:00
c->mode = OverflowCollection;
2007-06-20 17:42:13 +00:00
initNextGen2(c);
return copyTo(c, &(c->nextGen2), o, size);
2007-06-20 04:26:36 +00:00
}
} else {
return copyTo(c, &(c->nextGen2), o, size);
2007-06-20 04:26:36 +00:00
}
} else {
o = copyTo(c, &(c->nextGen1), o, size);
2007-06-20 04:26:36 +00:00
c->nextAgeMap.setOnly(o, age + 1);
return o;
}
} else {
assert(c, not c->nextGen1.contains(o));
assert(c, not c->nextGen2.contains(o));
o = copyTo(c, &(c->nextGen1), o, size);
2007-06-20 04:26:36 +00:00
c->nextAgeMap.clear(o);
return o;
}
}
object
copy(Context* c, object o)
{
object r = copy2(c, o);
if (Debug) {
2007-06-22 20:55:11 +00:00
fprintf(stderr, "copy %p (%s) to %p (%s)\n",
o, segment(c, o), r, segment(c, r));
}
2007-06-21 18:35:24 +00:00
2007-06-20 04:26:36 +00:00
// leave a pointer to the copy in the original
2007-06-21 18:35:24 +00:00
cast<object>(o, 0) = r;
2007-06-20 04:26:36 +00:00
return r;
}
object
update3(Context* c, object o, bool* needsVisit)
2007-06-20 04:26:36 +00:00
{
if (wasCollected(c, o)) {
2007-06-20 04:26:36 +00:00
*needsVisit = false;
return follow(c, o);
2007-06-20 04:26:36 +00:00
} else {
*needsVisit = true;
return copy(c, o);
2007-06-20 04:26:36 +00:00
}
}
object
update2(Context* c, object o, bool* needsVisit)
2007-06-20 04:26:36 +00:00
{
switch (c->mode) {
case MinorCollection:
case OverflowCollection:
if (c->gen2.contains(o)) {
2007-06-20 04:26:36 +00:00
*needsVisit = false;
return o;
2007-06-20 04:26:36 +00:00
}
break;
case Gen2Collection:
if (c->gen2.contains(o)) {
return update3(c, o, needsVisit);
2007-06-20 04:26:36 +00:00
} else {
assert(c, c->nextGen1.contains(o) or c->nextGen2.contains(o));
2007-06-22 20:55:11 +00:00
2007-06-20 04:26:36 +00:00
*needsVisit = false;
return o;
2007-06-20 04:26:36 +00:00
}
break;
default: break;
}
return update3(c, o, needsVisit);
2007-06-20 04:26:36 +00:00
}
object
update(Context* c, object* p, bool* needsVisit)
{
if (mask(*p) == 0) {
2007-06-20 04:26:36 +00:00
*needsVisit = false;
return 0;
2007-06-20 04:26:36 +00:00
}
object r = update2(c, mask(*p), needsVisit);
2007-06-20 04:26:36 +00:00
// update heap map.
if (r) {
if (c->mode == MinorCollection) {
if (c->gen2.contains(p) and not c->gen2.contains(r)) {
2007-06-22 20:55:11 +00:00
if (Debug) {
fprintf(stderr, "mark %p (%s) at %p (%s)\n",
r, segment(c, r), p, segment(c, p));
}
2007-06-20 04:26:36 +00:00
c->heapMap.set(p);
}
} else {
if (c->nextGen2.contains(p) and not c->nextGen2.contains(r)) {
2007-06-22 20:55:11 +00:00
if (Debug) {
fprintf(stderr, "mark %p (%s) at %p (%s)\n",
r, segment(c, r), p, segment(c, p));
}
c->nextHeapMap.set(p);
2007-06-20 04:26:36 +00:00
}
}
}
return r;
}
const uintptr_t BitsetExtensionBit
= (static_cast<uintptr_t>(1) << (BitsPerWord - 1));
void
bitsetInit(uintptr_t* p)
{
memset(p, 0, BytesPerWord);
}
void
bitsetClear(uintptr_t* p, unsigned start, unsigned end)
{
if (end < BitsPerWord - 1) {
// do nothing
} else if (start < BitsPerWord - 1) {
memset(p + 1, 0, (wordOf(end + (BitsPerWord * 2) + 1)) * BytesPerWord);
} else {
unsigned startWord = wordOf(start + (BitsPerWord * 2) + 1);
unsigned endWord = wordOf(end + (BitsPerWord * 2) + 1);
if (endWord > startWord) {
memset(p + startWord + 1, 0, (endWord - startWord) * BytesPerWord);
}
}
}
void
bitsetSet(uintptr_t* p, unsigned i, bool v)
{
if (i >= BitsPerWord - 1) {
i += (BitsPerWord * 2) + 1;
if (v) {
p[0] |= BitsetExtensionBit;
if (p[2] <= wordOf(i) - 3) p[2] = wordOf(i) - 2;
}
}
if (v) {
p[wordOf(i)] |= static_cast<uintptr_t>(1) << bitOf(i);
} else {
p[wordOf(i)] &= ~(static_cast<uintptr_t>(1) << bitOf(i));
}
}
unsigned
bitsetHasMore(uintptr_t* p)
{
switch (*p) {
case 0: return false;
case BitsetExtensionBit: {
uintptr_t length = p[2];
uintptr_t word = wordOf(p[1]);
for (; word < length; ++word) {
if (p[word + 3]) {
p[1] = indexOf(word, 0);
return true;
}
}
p[1] = indexOf(word, 0);
return false;
}
default: return true;
}
}
unsigned
bitsetNext(Context* c, uintptr_t* p)
{
assert(c, bitsetHasMore(p));
switch (*p) {
case 0: abort(c);
case BitsetExtensionBit: {
uintptr_t i = p[1];
uintptr_t word = wordOf(i);
assert(c, word < p[2]);
for (uintptr_t bit = bitOf(i); bit < BitsPerWord; ++bit) {
if (p[word + 3] & (static_cast<uintptr_t>(1) << bit)) {
p[1] = indexOf(word, bit) + 1;
bitsetSet(p, p[1] + BitsPerWord - 2, false);
return p[1] + BitsPerWord - 2;
}
}
abort(c);
}
default: {
for (unsigned i = 0; i < BitsPerWord - 1; ++i) {
if (*p & (static_cast<uintptr_t>(1) << i)) {
bitsetSet(p, i, false);
return i;
}
}
abort(c);
}
}
}
void
collect(Context* c, object* p)
{
object original = mask(*p);
object parent = 0;
if (Debug) {
2007-06-22 20:55:11 +00:00
fprintf(stderr, "update %p (%s) at %p (%s)\n",
mask(*p), segment(c, *p), p, segment(c, p));
}
2007-06-21 18:35:24 +00:00
bool needsVisit;
set(p, update(c, mask(p), &needsVisit));
if (Debug) {
2007-06-22 20:55:11 +00:00
fprintf(stderr, " result: %p (%s) (visit? %d)\n",
mask(*p), segment(c, *p), needsVisit);
}
2007-06-21 18:35:24 +00:00
if (not needsVisit) return;
visit: {
2007-06-21 18:35:24 +00:00
object copy = follow(c, original);
class Walker : public Heap::Walker {
public:
Walker(Context* c, object copy, uintptr_t* bitset):
c(c),
copy(copy),
bitset(bitset),
first(0),
2007-06-21 18:35:24 +00:00
second(0),
last(0),
visits(0),
total(0)
{ }
virtual bool visit(unsigned offset) {
if (Debug) {
2007-06-22 20:55:11 +00:00
fprintf(stderr, " update %p (%s) at %p - offset %d from %p (%s)\n",
get(copy, offset),
segment(c, get(copy, offset)),
getp(copy, offset),
offset,
2007-06-22 20:55:11 +00:00
copy,
segment(c, copy));
}
2007-06-21 18:35:24 +00:00
bool needsVisit;
object childCopy = update(c, getp(copy, offset), &needsVisit);
if (Debug) {
2007-06-22 20:55:11 +00:00
fprintf(stderr, " result: %p (%s) (visit? %d)\n",
childCopy, segment(c, childCopy), needsVisit);
}
2007-06-21 18:35:24 +00:00
++ total;
if (total == 3) {
bitsetInit(bitset);
}
if (needsVisit) {
++ visits;
if (visits == 1) {
first = offset;
2007-06-21 18:35:24 +00:00
} else if (visits == 2) {
second = offset;
}
2007-06-21 18:35:24 +00:00
} else {
set(copy, offset, childCopy);
2007-06-21 18:35:24 +00:00
}
2007-06-21 19:43:33 +00:00
if (visits > 1 and total > 2 and (second or needsVisit)) {
2007-06-21 18:35:24 +00:00
bitsetClear(bitset, last, offset);
last = offset;
2007-06-21 18:35:24 +00:00
if (second) {
bitsetSet(bitset, second, true);
second = 0;
}
if (needsVisit) {
bitsetSet(bitset, offset, true);
2007-06-21 18:35:24 +00:00
}
}
return true;
}
Context* c;
object copy;
uintptr_t* bitset;
unsigned first;
2007-06-21 18:35:24 +00:00
unsigned second;
unsigned last;
unsigned visits;
unsigned total;
2007-06-21 18:35:24 +00:00
} walker(c, copy, bitset(c, original));
if (Debug) {
2007-06-22 20:55:11 +00:00
fprintf(stderr, "walk %p (%s)\n", copy, segment(c, copy));
}
c->client->walk(copy, &walker);
if (walker.visits) {
// descend
if (walker.visits > 1) {
2007-06-21 18:35:24 +00:00
::parent(c, original) = parent;
parent = original;
}
original = get(copy, walker.first);
set(copy, walker.first, follow(c, original));
goto visit;
} else {
// ascend
original = parent;
}
}
if (original) {
2007-06-21 18:35:24 +00:00
object copy = follow(c, original);
class Walker : public Heap::Walker {
public:
2007-06-20 04:26:36 +00:00
Walker(Context* c, uintptr_t* bitset):
c(c),
bitset(bitset),
next(0),
total(0)
{ }
virtual bool visit(unsigned offset) {
switch (++ total) {
case 1:
return true;
case 2:
next = offset;
return true;
case 3:
2007-06-20 04:26:36 +00:00
next = bitsetNext(c, bitset);
return false;
default:
abort(c);
}
}
2007-06-20 04:26:36 +00:00
Context* c;
uintptr_t* bitset;
unsigned next;
unsigned total;
2007-06-21 18:35:24 +00:00
} walker(c, bitset(c, original));
if (Debug) {
fprintf(stderr, "scan %p\n", copy);
}
2007-06-21 18:35:24 +00:00
c->client->walk(copy, &walker);
assert(c, walker.total > 1);
2007-06-21 18:35:24 +00:00
if (walker.total == 3 and bitsetHasMore(bitset(c, original))) {
parent = original;
} else {
2007-06-21 18:35:24 +00:00
parent = ::parent(c, original);
}
if (Debug) {
2007-06-22 20:55:11 +00:00
fprintf(stderr, " next is %p (%s) at %p - offset %d from %p (%s)\n",
get(copy, walker.next),
segment(c, get(copy, walker.next)),
getp(copy, walker.next),
walker.next,
2007-06-22 20:55:11 +00:00
copy,
segment(c, copy));
}
2007-06-21 18:35:24 +00:00
original = get(copy, walker.next);
set(copy, walker.next, follow(c, original));
2007-06-20 04:26:36 +00:00
goto visit;
} else {
return;
}
}
2007-06-20 04:26:36 +00:00
void
collect(Context* c, Segment::Map* map, unsigned start, unsigned end,
2007-06-20 17:42:13 +00:00
bool* dirty, bool expectDirty)
2007-06-20 04:26:36 +00:00
{
bool wasDirty = false;
for (Segment::Map::Iterator it(map, start, end); it.hasMore();) {
wasDirty = true;
if (map->child) {
assert(c, map->scale > 1);
unsigned s = it.next();
unsigned e = s + map->scale;
map->clearOnly(s);
bool childDirty = false;
collect(c, map->child, s, e, &childDirty, true);
2007-06-22 20:55:11 +00:00
if (c->mode != OverflowCollection and childDirty) {
2007-06-20 04:26:36 +00:00
map->setOnly(s);
*dirty = true;
}
} else {
assert(c, map->scale == 1);
2007-06-20 17:42:13 +00:00
object* p = reinterpret_cast<object*>(map->segment->get(it.next()));
2007-06-20 04:26:36 +00:00
map->clearOnly(p);
if (c->nextGen1.contains(*p)) {
map->setOnly(p);
*dirty = true;
} else {
collect(c, p);
2007-06-22 20:55:11 +00:00
if (c->mode != OverflowCollection and not c->gen2.contains(*p)) {
2007-06-20 04:26:36 +00:00
map->setOnly(p);
*dirty = true;
}
}
}
}
assert(c, wasDirty or not expectDirty);
}
class ObjectSegmentIterator {
public:
2007-06-22 02:13:17 +00:00
Context* context;
Segment::Chain* chain;
unsigned index;
unsigned end;
bool dirty;
ObjectSegmentIterator(Segment* segment, unsigned end):
context(segment->context),
chain(segment->front),
index(0),
end(end),
dirty(false)
2007-06-20 04:26:36 +00:00
{ }
bool hasNext() {
2007-06-22 02:13:17 +00:00
if (dirty) {
dirty = false;
uintptr_t* p = chain->data() + index;
index += context->client->sizeInWords(p);
}
if (chain and index == chain->position) {
chain = chain->next;
index = 0;
}
return chain and index + chain->offset < end;
2007-06-20 04:26:36 +00:00
}
object next() {
2007-06-22 02:13:17 +00:00
dirty = true;
return chain->data() + index;
2007-06-20 04:26:36 +00:00
}
};
void
collect(Context* c, Segment* s, unsigned limit)
{
2007-06-22 02:13:17 +00:00
for (ObjectSegmentIterator it(s, limit); it.hasNext();) {
2007-06-20 04:26:36 +00:00
object p = it.next();
class Walker : public Heap::Walker {
public:
Walker(Context* c, object p): c(c), p(p) { }
virtual bool visit(unsigned offset) {
collect(c, getp(p, offset));
2007-06-20 17:42:13 +00:00
return true;
2007-06-20 04:26:36 +00:00
}
Context* c;
object p;
} walker(c, p);
c->client->walk(p, &walker);
}
}
void
collect2(Context* c)
{
2007-06-22 02:13:17 +00:00
if (c->mode == MinorCollection and c->gen2.position()) {
unsigned start = 0;
2007-06-22 02:13:17 +00:00
unsigned end = start + c->gen2.position();
bool dirty;
2007-06-20 04:26:36 +00:00
collect(c, &(c->heapMap), start, end, &dirty, false);
} else if (c->mode == Gen2Collection) {
2007-06-22 02:13:17 +00:00
unsigned ng2Position = c->nextGen2.position();
collect(c, &(c->nextGen1), c->nextGen1.position());
2007-06-20 04:26:36 +00:00
collect(c, &(c->nextGen2), ng2Position);
}
class Visitor : public Heap::Visitor {
public:
Visitor(Context* c): c(c) { }
virtual void visit(void** p) {
collect(c, p);
}
2007-06-20 17:42:13 +00:00
Context* c;
} v(c);
2007-06-20 17:42:13 +00:00
c->client->visitRoots(&v);
}
void
2007-06-20 04:26:36 +00:00
collect(Context* c)
{
2007-06-22 02:13:17 +00:00
if (c->gen1.front == 0) initGen1(c);
2007-06-21 20:44:35 +00:00
2007-06-21 22:23:35 +00:00
c->gen2Base = Top;
switch (c->mode) {
case MinorCollection: {
initNextGen1(c);
if (Verbose) {
fprintf(stderr, "minor collection\n");
}
collect2(c);
if (c->mode == OverflowCollection) {
c->mode = Gen2Collection;
2007-06-21 22:23:35 +00:00
if (Verbose) {
fprintf(stderr, "gen2 collection\n");
}
2007-06-21 22:23:35 +00:00
c->gen2Base = Top;
collect2(c);
c->gen2.replaceWith(&(c->nextGen2));
}
c->gen1.replaceWith(&(c->nextGen1));
} break;
case MajorCollection: {
initNextGen1(c);
initNextGen2(c);
2007-06-20 04:26:36 +00:00
c->heapMap.clear();
if (Verbose) {
fprintf(stderr, "major collection\n");
}
collect2(c);
c->gen1.replaceWith(&(c->nextGen1));
c->gen2.replaceWith(&(c->nextGen2));
} break;
2007-06-20 17:42:13 +00:00
default: abort(c);
}
}
} // namespace
2007-06-20 19:20:25 +00:00
namespace vm {
Heap*
makeHeap(System* system)
{
2007-06-20 16:58:35 +00:00
class Heap: public vm::Heap {
public:
Heap(System* system): c(system) { }
virtual void collect(CollectionType type, Client* client) {
switch (type) {
case MinorCollection:
c.mode = ::MinorCollection;
break;
case MajorCollection:
c.mode = ::MajorCollection;
break;
default: abort(&c);
}
c.client = client;
::collect(&c);
}
virtual bool needsMark(void** p) {
return *p and c.gen2.contains(p) and not c.gen2.contains(*p);
}
virtual void mark(void** p) {
2007-06-22 20:55:11 +00:00
if (Debug) {
fprintf(stderr, "mark %p (%s) at %p (%s)\n",
*p, segment(&c, *p), p, segment(&c, p));
}
2007-06-20 16:58:35 +00:00
c.heapMap.set(p);
}
2007-06-20 16:58:35 +00:00
virtual void dispose() {
c.dispose();
c.system->free(this);
}
2007-06-20 17:42:13 +00:00
virtual void* follow(void* p) {
if (wasCollected(&c, p)) {
if (Debug) {
2007-06-22 20:55:11 +00:00
fprintf(stderr, "follow %p (%s) to %p (%s)\n",
p, segment(&c, p),
::follow(&c, p), segment(&c, ::follow(&c, p)));
}
2007-06-21 18:35:24 +00:00
return ::follow(&c, p);
2007-06-20 17:42:13 +00:00
} else {
return p;
}
}
virtual Status status(void* p) {
p = mask(p);
if (c.nextGen1.contains(p)) {
return Reachable;
} else if (c.nextGen2.contains(p)
or (c.gen2.contains(p)
and c.gen2.indexOf(p) >= c.gen2Base))
{
return Tenured;
} else if (wasCollected(&c, p)) {
return Reachable;
} else {
return Unreachable;
}
}
virtual CollectionType collectionType() {
if (c.mode == ::MinorCollection) {
return MinorCollection;
} else {
return MajorCollection;
}
}
2007-06-20 16:58:35 +00:00
Context c;
};
2007-06-20 19:20:25 +00:00
return new (system->allocate(sizeof(Heap))) Heap(system);
}
2007-06-20 19:20:25 +00:00
} // namespace vm