2007-06-20 02:28:31 +00:00
|
|
|
#include "heap.h"
|
|
|
|
#include "system.h"
|
2007-06-20 04:26:36 +00:00
|
|
|
#include "common.h"
|
2007-06-20 02:28:31 +00:00
|
|
|
|
|
|
|
using namespace vm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2007-06-20 16:58:35 +00:00
|
|
|
// an object must survive TenureThreshold + 2 garbage collections
|
2007-08-18 21:24:29 +00:00
|
|
|
// before being copied to gen2 (muat be at least 1):
|
2007-06-21 22:23:35 +00:00
|
|
|
const unsigned TenureThreshold = 3;
|
2007-06-20 16:58: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
|
|
|
|
2007-08-18 22:42:11 +00:00
|
|
|
const unsigned InitialGen2CapacityInBytes = 4 * 1024 * 1024;
|
|
|
|
|
2007-07-17 13:22:29 +00:00
|
|
|
const bool Verbose = true;
|
2007-08-18 22:42:11 +00:00
|
|
|
const bool Verbose2 = false;
|
2007-07-29 23:32:23 +00:00
|
|
|
const bool Debug = false;
|
2007-06-21 22:51:55 +00:00
|
|
|
|
2007-06-20 16:58:35 +00:00
|
|
|
class Context;
|
|
|
|
|
|
|
|
void NO_RETURN abort(Context*);
|
|
|
|
void assert(Context*, bool);
|
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
System* system(Context*);
|
|
|
|
|
2007-07-01 21:34:22 +00:00
|
|
|
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:
|
|
|
|
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;
|
2007-06-22 22:47:57 +00:00
|
|
|
Map* child;
|
2007-06-20 04:26:36 +00:00
|
|
|
unsigned bitsPerRecord;
|
|
|
|
unsigned scale;
|
2007-06-22 22:47:57 +00:00
|
|
|
bool clearNewData;
|
2007-06-22 02:13:17 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
Map(Segment* segment, unsigned bitsPerRecord, unsigned scale,
|
|
|
|
Map* child, bool clearNewData):
|
2007-06-20 16:58:35 +00:00
|
|
|
segment(segment),
|
2007-06-22 22:47:57 +00:00
|
|
|
child(child),
|
2007-06-20 16:58:35 +00:00
|
|
|
bitsPerRecord(bitsPerRecord),
|
|
|
|
scale(scale),
|
2007-06-22 22:47:57 +00:00
|
|
|
clearNewData(clearNewData)
|
2007-07-19 23:45:44 +00:00
|
|
|
{ }
|
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
void init() {
|
2007-07-19 23:45:44 +00:00
|
|
|
assert(segment->context, bitsPerRecord);
|
|
|
|
assert(segment->context, scale);
|
|
|
|
assert(segment->context, powerOfTwo(scale));
|
2007-08-18 21:24:29 +00:00
|
|
|
|
|
|
|
if (clearNewData) {
|
|
|
|
memset(data(), 0, size() * BytesPerWord);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child) {
|
|
|
|
child->init();
|
|
|
|
}
|
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-08-18 21:24:29 +00:00
|
|
|
return segment->data + segment->capacity() + offset();
|
2007-06-20 04:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned size(unsigned capacity) {
|
2007-06-20 16:58:35 +00:00
|
|
|
unsigned result
|
2007-07-26 00:48:28 +00:00
|
|
|
= ceiling(ceiling(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);
|
2007-07-18 01:33:00 +00:00
|
|
|
if (segment->position()) {
|
|
|
|
memcpy(p, data(), size(segment->position()) * BytesPerWord);
|
|
|
|
}
|
2007-06-22 02:13:17 +00:00
|
|
|
|
|
|
|
if (child) {
|
|
|
|
child->update(newData, capacity);
|
|
|
|
}
|
2007-06-20 04:26:36 +00:00
|
|
|
}
|
|
|
|
|
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 17:42:13 +00:00
|
|
|
Context* context;
|
2007-08-18 21:24:29 +00:00
|
|
|
uintptr_t* data;
|
|
|
|
unsigned position_;
|
|
|
|
unsigned capacity_;
|
2007-06-20 04:26:36 +00:00
|
|
|
Map* map;
|
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
Segment(Context* context, Map* map, unsigned desired, unsigned minimum):
|
2007-06-20 16:58:35 +00:00
|
|
|
context(context),
|
2007-08-18 21:24:29 +00:00
|
|
|
data(0),
|
|
|
|
position_(0),
|
|
|
|
capacity_(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) {
|
2007-08-18 21:24:29 +00:00
|
|
|
assert(context, desired >= minimum);
|
|
|
|
|
|
|
|
capacity_ = desired;
|
|
|
|
while (data == 0) {
|
|
|
|
data = static_cast<uintptr_t*>
|
|
|
|
(system(context)->tryAllocate
|
|
|
|
((capacity_ + map->footprint(capacity_)) * BytesPerWord));
|
|
|
|
|
|
|
|
if (data == 0) {
|
|
|
|
if (capacity_ > minimum) {
|
|
|
|
capacity_ = avg(minimum, capacity_);
|
2007-08-18 22:42:11 +00:00
|
|
|
if (capacity_ == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2007-08-18 21:24:29 +00:00
|
|
|
} else {
|
|
|
|
abort(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-20 05:16:43 +00:00
|
|
|
|
|
|
|
if (map) {
|
2007-08-18 21:24:29 +00:00
|
|
|
map->init();
|
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() {
|
2007-08-18 21:24:29 +00:00
|
|
|
return capacity_;
|
2007-06-20 16:58:35 +00:00
|
|
|
}
|
|
|
|
|
2007-06-22 02:13:17 +00:00
|
|
|
unsigned position() {
|
2007-08-18 21:24:29 +00:00
|
|
|
return position_;
|
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-08-18 21:24:29 +00:00
|
|
|
system(context)->free(data);
|
|
|
|
data = s->data;
|
|
|
|
s->data = 0;
|
2007-06-20 04:26:36 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
position_ = s->position_;
|
|
|
|
s->position_ = 0;
|
2007-06-20 04:26:36 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
capacity_ = s->capacity_;
|
|
|
|
s->capacity_ = 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);
|
2007-07-19 23:45:44 +00:00
|
|
|
s->map = 0;
|
2007-06-21 20:44:35 +00:00
|
|
|
} else {
|
2007-07-19 23:45:44 +00:00
|
|
|
abort(context);
|
2007-06-21 20:44:35 +00:00
|
|
|
}
|
2007-06-20 04:26:36 +00:00
|
|
|
} 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) {
|
2007-08-18 21:24:29 +00:00
|
|
|
return position() and p >= data and p < data + position();
|
2007-06-22 02:13:17 +00:00
|
|
|
}
|
2007-06-20 04:26:36 +00:00
|
|
|
|
2007-06-22 02:13:17 +00:00
|
|
|
bool almostContains(void* p) {
|
2007-08-18 21:24:29 +00:00
|
|
|
return contains(p) or p == data + position();
|
2007-06-22 02:13:17 +00:00
|
|
|
}
|
2007-06-20 04:26:36 +00:00
|
|
|
|
2007-06-22 02:13:17 +00:00
|
|
|
void* get(unsigned offset) {
|
2007-08-18 21:24:29 +00:00
|
|
|
assert(context, offset <= position());
|
|
|
|
return data + offset;
|
2007-06-22 02:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned indexOf(void* p) {
|
2007-08-18 21:24:29 +00:00
|
|
|
assert(context, almostContains(p));
|
|
|
|
return static_cast<uintptr_t*>(p) - data;
|
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);
|
2007-08-18 21:24:29 +00:00
|
|
|
assert(context, position() + size <= capacity());
|
2007-06-22 02:13:17 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
void* p = data + position();
|
|
|
|
position_ += size;
|
2007-06-22 02:13:17 +00:00
|
|
|
return p;
|
2007-06-20 04:26:36 +00:00
|
|
|
}
|
|
|
|
|
2007-06-22 02:13:17 +00:00
|
|
|
void dispose() {
|
2007-08-18 21:24:29 +00:00
|
|
|
system(context)->free(data);
|
|
|
|
data = 0;
|
2007-06-20 04:26:36 +00:00
|
|
|
map = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-06-20 02:28:31 +00:00
|
|
|
class Context {
|
|
|
|
public:
|
2007-06-20 17:42:13 +00:00
|
|
|
Context(System* system):
|
|
|
|
system(system),
|
|
|
|
client(0),
|
2007-07-19 23:45:44 +00:00
|
|
|
|
|
|
|
ageMap(&gen1, log(TenureThreshold), 1, 0, false),
|
2007-08-18 21:24:29 +00:00
|
|
|
gen1(this, &ageMap, 0, 0),
|
2007-07-19 23:45:44 +00:00
|
|
|
|
|
|
|
nextAgeMap(&nextGen1, log(TenureThreshold), 1, 0, false),
|
2007-08-18 21:24:29 +00:00
|
|
|
nextGen1(this, &nextAgeMap, 0, 0),
|
2007-07-19 23:45:44 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
pointerMap(&gen2, 1, 1, 0, true),
|
|
|
|
pageMap(&gen2, 1, LikelyPageSizeInBytes / BytesPerWord, &pointerMap, true),
|
|
|
|
heapMap(&gen2, 1, pageMap.scale * 1024, &pageMap, true),
|
|
|
|
gen2(this, &heapMap, 0, 0),
|
2007-07-19 23:45:44 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
nextPointerMap(&nextGen2, 1, 1, 0, true),
|
2007-07-19 23:45:44 +00:00
|
|
|
nextPageMap(&nextGen2, 1, LikelyPageSizeInBytes / BytesPerWord,
|
2007-08-18 21:24:29 +00:00
|
|
|
&nextPointerMap, true),
|
|
|
|
nextHeapMap(&nextGen2, 1, nextPageMap.scale * 1024, &nextPageMap, true),
|
|
|
|
nextGen2(this, &nextHeapMap, 0, 0),
|
|
|
|
|
|
|
|
gen2Base(0),
|
|
|
|
tenureFootprint(0),
|
|
|
|
gen1padding(0),
|
|
|
|
gen2padding(0),
|
|
|
|
mode(Heap::MinorCollection)
|
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
|
|
|
|
2007-07-19 23:45:44 +00:00
|
|
|
Segment::Map ageMap;
|
2007-06-20 16:58:35 +00:00
|
|
|
Segment gen1;
|
2007-06-21 22:23:35 +00:00
|
|
|
|
2007-06-20 17:42:13 +00:00
|
|
|
Segment::Map nextAgeMap;
|
2007-07-19 23:45:44 +00:00
|
|
|
Segment nextGen1;
|
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-07-19 23:45:44 +00:00
|
|
|
Segment gen2;
|
2007-06-20 16:58:35 +00:00
|
|
|
|
2007-06-22 20:55:11 +00:00
|
|
|
Segment::Map nextPointerMap;
|
|
|
|
Segment::Map nextPageMap;
|
|
|
|
Segment::Map nextHeapMap;
|
2007-07-19 23:45:44 +00:00
|
|
|
Segment nextGen2;
|
|
|
|
|
|
|
|
unsigned gen2Base;
|
2007-08-18 21:24:29 +00:00
|
|
|
|
|
|
|
unsigned tenureFootprint;
|
|
|
|
unsigned gen1padding;
|
|
|
|
unsigned gen2padding;
|
2007-06-22 20:55:11 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
Heap::CollectionType mode;
|
2007-06-20 02:28:31 +00:00
|
|
|
};
|
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
inline System*
|
|
|
|
system(Context* c)
|
|
|
|
{
|
|
|
|
return c->system;
|
|
|
|
}
|
|
|
|
|
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 void NO_RETURN
|
|
|
|
abort(Context* c)
|
|
|
|
{
|
2007-06-22 23:17:13 +00:00
|
|
|
abort(c->system);
|
2007-06-20 16:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
assert(Context* c, bool v)
|
|
|
|
{
|
2007-06-22 23:17:13 +00:00
|
|
|
assert(c->system, v);
|
2007-06-20 16:58:35 +00:00
|
|
|
}
|
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
inline void
|
|
|
|
initNextGen1(Context* c, unsigned footprint)
|
2007-06-22 20:55:11 +00:00
|
|
|
{
|
2007-06-22 22:47:57 +00:00
|
|
|
new (&(c->nextAgeMap)) Segment::Map
|
|
|
|
(&(c->nextGen1), log(TenureThreshold), 1, 0, false);
|
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
unsigned minimum
|
|
|
|
= (c->gen1.position() - c->tenureFootprint) + footprint + c->gen1padding;
|
2007-06-22 20:55:11 +00:00
|
|
|
unsigned desired = minimum;
|
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
new (&(c->nextGen1)) Segment(c, &(c->nextAgeMap), desired, minimum);
|
2007-06-22 22:47:57 +00:00
|
|
|
|
2007-08-18 22:42:11 +00:00
|
|
|
if (Verbose2) {
|
2007-08-18 21:24:29 +00:00
|
|
|
fprintf(stderr, "init nextGen1 to %d bytes\n",
|
|
|
|
c->nextGen1.capacity() * BytesPerWord);
|
2007-06-22 22:47:57 +00:00
|
|
|
}
|
2007-06-20 04:26:36 +00:00
|
|
|
}
|
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
inline void
|
2007-06-20 04:26:36 +00:00
|
|
|
initNextGen2(Context* c)
|
|
|
|
{
|
2007-08-18 21:24:29 +00:00
|
|
|
new (&(c->nextPointerMap)) Segment::Map
|
|
|
|
(&(c->nextGen2), 1, 1, 0, true);
|
2007-06-22 20:55:11 +00:00
|
|
|
|
|
|
|
new (&(c->nextPageMap)) Segment::Map
|
|
|
|
(&(c->nextGen2), 1, LikelyPageSizeInBytes / BytesPerWord,
|
2007-08-18 21:24:29 +00:00
|
|
|
&(c->nextPointerMap), true);
|
|
|
|
|
2007-06-22 20:55:11 +00:00
|
|
|
new (&(c->nextHeapMap)) Segment::Map
|
2007-08-18 21:24:29 +00:00
|
|
|
(&(c->nextGen2), 1, c->pageMap.scale * 1024, &(c->nextPageMap), true);
|
|
|
|
|
|
|
|
unsigned minimum = c->gen2.position() + c->tenureFootprint + c->gen2padding;
|
2007-08-18 22:42:11 +00:00
|
|
|
unsigned desired = max
|
|
|
|
(minimum * 2, InitialGen2CapacityInBytes / BytesPerWord);
|
2007-06-22 20:55:11 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
new (&(c->nextGen2)) Segment(c, &(c->nextHeapMap), desired, minimum);
|
2007-06-22 22:47:57 +00:00
|
|
|
|
2007-08-18 22:42:11 +00:00
|
|
|
if (Verbose2) {
|
2007-06-22 22:47:57 +00:00
|
|
|
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
|
|
|
{
|
2007-07-01 21:34:22 +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
|
2007-07-02 14:19:05 +00:00
|
|
|
copyTo(Context* c, Segment* s, object o, unsigned size)
|
2007-06-20 04:26:36 +00:00
|
|
|
{
|
2007-08-18 21:24:29 +00:00
|
|
|
assert(c, s->remaining() >= size);
|
2007-07-02 14:19:05 +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)
|
|
|
|
{
|
2007-07-02 14:19:05 +00:00
|
|
|
unsigned size = c->client->copiedSizeInWords(o);
|
|
|
|
|
2007-06-20 04:26:36 +00:00
|
|
|
if (c->gen2.contains(o)) {
|
2007-08-18 21:24:29 +00:00
|
|
|
assert(c, c->mode == Heap::MajorCollection);
|
2007-06-20 04:26:36 +00:00
|
|
|
|
2007-07-02 14:19:05 +00:00
|
|
|
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-08-18 21:24:29 +00:00
|
|
|
if (c->mode == Heap::MinorCollection) {
|
|
|
|
assert(c, c->gen2.remaining() >= size);
|
2007-06-22 02:13:17 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
if (c->gen2Base == Top) {
|
|
|
|
c->gen2Base = c->gen2.position();
|
2007-06-20 04:26:36 +00:00
|
|
|
}
|
2007-08-18 21:24:29 +00:00
|
|
|
|
|
|
|
return copyTo(c, &(c->gen2), o, size);
|
2007-06-20 04:26:36 +00:00
|
|
|
} else {
|
2007-07-02 14:19:05 +00:00
|
|
|
return copyTo(c, &(c->nextGen2), o, size);
|
2007-06-20 04:26:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2007-07-02 14:19:05 +00:00
|
|
|
o = copyTo(c, &(c->nextGen1), o, size);
|
2007-08-18 21:24:29 +00:00
|
|
|
|
2007-06-20 04:26:36 +00:00
|
|
|
c->nextAgeMap.setOnly(o, age + 1);
|
2007-08-18 21:24:29 +00:00
|
|
|
if (age + 1 == TenureThreshold) {
|
|
|
|
c->tenureFootprint += size;
|
|
|
|
}
|
|
|
|
|
2007-06-20 04:26:36 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(c, not c->nextGen1.contains(o));
|
|
|
|
assert(c, not c->nextGen2.contains(o));
|
|
|
|
|
2007-07-02 14:19:05 +00:00
|
|
|
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);
|
|
|
|
|
2007-06-21 22:51:55 +00:00
|
|
|
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 22:51:55 +00:00
|
|
|
}
|
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
|
2007-07-01 21:34:22 +00:00
|
|
|
update3(Context* c, object o, bool* needsVisit)
|
2007-06-20 04:26:36 +00:00
|
|
|
{
|
2007-07-01 21:34:22 +00:00
|
|
|
if (wasCollected(c, o)) {
|
2007-06-20 04:26:36 +00:00
|
|
|
*needsVisit = false;
|
2007-07-01 21:34:22 +00:00
|
|
|
return follow(c, o);
|
2007-06-20 04:26:36 +00:00
|
|
|
} else {
|
|
|
|
*needsVisit = true;
|
2007-07-01 21:34:22 +00:00
|
|
|
return copy(c, o);
|
2007-06-20 04:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object
|
2007-07-01 21:34:22 +00:00
|
|
|
update2(Context* c, object o, bool* needsVisit)
|
2007-06-20 04:26:36 +00:00
|
|
|
{
|
2007-08-18 21:24:29 +00:00
|
|
|
if (c->mode == Heap::MinorCollection and c->gen2.contains(o)) {
|
|
|
|
*needsVisit = false;
|
|
|
|
return o;
|
2007-06-20 04:26:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-01 21:34:22 +00:00
|
|
|
return update3(c, o, needsVisit);
|
2007-06-20 04:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
object
|
|
|
|
update(Context* c, object* p, bool* needsVisit)
|
|
|
|
{
|
2007-07-01 21:34:22 +00:00
|
|
|
if (mask(*p) == 0) {
|
2007-06-20 04:26:36 +00:00
|
|
|
*needsVisit = false;
|
2007-07-01 21:34:22 +00:00
|
|
|
return 0;
|
2007-06-20 04:26:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-01 21:34:22 +00:00
|
|
|
object r = update2(c, mask(*p), needsVisit);
|
2007-06-20 04:26:36 +00:00
|
|
|
|
|
|
|
// update heap map.
|
|
|
|
if (r) {
|
2007-08-18 21:24:29 +00:00
|
|
|
if (c->mode == Heap::MinorCollection) {
|
2007-06-20 04:26:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 02:28:31 +00:00
|
|
|
void
|
2007-07-01 21:34:22 +00:00
|
|
|
collect(Context* c, object* p)
|
2007-06-20 02:28:31 +00:00
|
|
|
{
|
2007-07-01 21:34:22 +00:00
|
|
|
object original = mask(*p);
|
2007-06-20 02:28:31 +00:00
|
|
|
object parent = 0;
|
2007-06-21 22:51:55 +00:00
|
|
|
|
|
|
|
if (Debug) {
|
2007-06-22 20:55:11 +00:00
|
|
|
fprintf(stderr, "update %p (%s) at %p (%s)\n",
|
2007-07-01 21:34:22 +00:00
|
|
|
mask(*p), segment(c, *p), p, segment(c, p));
|
2007-06-21 22:51:55 +00:00
|
|
|
}
|
2007-06-21 18:35:24 +00:00
|
|
|
|
2007-06-20 02:28:31 +00:00
|
|
|
bool needsVisit;
|
2007-07-01 21:34:22 +00:00
|
|
|
set(p, update(c, mask(p), &needsVisit));
|
2007-06-20 02:28:31 +00:00
|
|
|
|
2007-06-21 22:51:55 +00:00
|
|
|
if (Debug) {
|
2007-06-22 20:55:11 +00:00
|
|
|
fprintf(stderr, " result: %p (%s) (visit? %d)\n",
|
2007-07-01 21:34:22 +00:00
|
|
|
mask(*p), segment(c, *p), needsVisit);
|
2007-06-21 22:51:55 +00:00
|
|
|
}
|
2007-06-21 18:35:24 +00:00
|
|
|
|
2007-06-20 02:28:31 +00:00
|
|
|
if (not needsVisit) return;
|
|
|
|
|
|
|
|
visit: {
|
2007-06-21 18:35:24 +00:00
|
|
|
object copy = follow(c, original);
|
2007-06-20 02:28:31 +00:00
|
|
|
|
|
|
|
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),
|
2007-06-20 02:28:31 +00:00
|
|
|
last(0),
|
|
|
|
visits(0),
|
|
|
|
total(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual bool visit(unsigned offset) {
|
2007-06-21 22:51:55 +00:00
|
|
|
if (Debug) {
|
2007-06-22 20:55:11 +00:00
|
|
|
fprintf(stderr, " update %p (%s) at %p - offset %d from %p (%s)\n",
|
2007-07-01 21:34:22 +00:00
|
|
|
get(copy, offset),
|
|
|
|
segment(c, get(copy, offset)),
|
|
|
|
getp(copy, offset),
|
2007-06-21 22:51:55 +00:00
|
|
|
offset,
|
2007-06-22 20:55:11 +00:00
|
|
|
copy,
|
|
|
|
segment(c, copy));
|
2007-06-21 22:51:55 +00:00
|
|
|
}
|
2007-06-21 18:35:24 +00:00
|
|
|
|
2007-06-20 02:28:31 +00:00
|
|
|
bool needsVisit;
|
2007-07-01 21:34:22 +00:00
|
|
|
object childCopy = update(c, getp(copy, offset), &needsVisit);
|
2007-06-20 02:28:31 +00:00
|
|
|
|
2007-06-21 22:51:55 +00:00
|
|
|
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 22:51:55 +00:00
|
|
|
}
|
2007-06-21 18:35:24 +00:00
|
|
|
|
2007-06-20 02:28:31 +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-20 02:28:31 +00:00
|
|
|
}
|
2007-06-21 18:35:24 +00:00
|
|
|
} else {
|
2007-07-01 21:34:22 +00:00
|
|
|
set(copy, offset, childCopy);
|
2007-06-21 18:35:24 +00:00
|
|
|
}
|
2007-06-20 02:28:31 +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-20 02:28:31 +00:00
|
|
|
|
2007-06-21 18:35:24 +00:00
|
|
|
if (second) {
|
|
|
|
bitsetSet(bitset, second, true);
|
|
|
|
second = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needsVisit) {
|
2007-06-20 02:28:31 +00:00
|
|
|
bitsetSet(bitset, offset, true);
|
2007-06-21 18:35:24 +00:00
|
|
|
}
|
2007-06-20 02:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Context* c;
|
|
|
|
object copy;
|
|
|
|
uintptr_t* bitset;
|
|
|
|
unsigned first;
|
2007-06-21 18:35:24 +00:00
|
|
|
unsigned second;
|
2007-06-20 02:28:31 +00:00
|
|
|
unsigned last;
|
|
|
|
unsigned visits;
|
|
|
|
unsigned total;
|
2007-06-21 18:35:24 +00:00
|
|
|
} walker(c, copy, bitset(c, original));
|
|
|
|
|
2007-06-21 22:51:55 +00:00
|
|
|
if (Debug) {
|
2007-06-22 20:55:11 +00:00
|
|
|
fprintf(stderr, "walk %p (%s)\n", copy, segment(c, copy));
|
2007-06-21 22:51:55 +00:00
|
|
|
}
|
2007-06-20 02:28:31 +00:00
|
|
|
|
|
|
|
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;
|
2007-06-20 02:28:31 +00:00
|
|
|
parent = original;
|
|
|
|
}
|
|
|
|
|
2007-07-01 21:34:22 +00:00
|
|
|
original = get(copy, walker.first);
|
|
|
|
set(copy, walker.first, follow(c, original));
|
2007-06-20 02:28:31 +00:00
|
|
|
goto visit;
|
|
|
|
} else {
|
|
|
|
// ascend
|
|
|
|
original = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (original) {
|
2007-06-21 18:35:24 +00:00
|
|
|
object copy = follow(c, original);
|
2007-06-20 02:28:31 +00:00
|
|
|
|
|
|
|
class Walker : public Heap::Walker {
|
|
|
|
public:
|
2007-06-20 04:26:36 +00:00
|
|
|
Walker(Context* c, uintptr_t* bitset):
|
|
|
|
c(c),
|
2007-06-20 02:28:31 +00:00
|
|
|
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);
|
2007-06-20 02:28:31 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 04:26:36 +00:00
|
|
|
Context* c;
|
2007-06-20 02:28:31 +00:00
|
|
|
uintptr_t* bitset;
|
|
|
|
unsigned next;
|
|
|
|
unsigned total;
|
2007-06-21 18:35:24 +00:00
|
|
|
} walker(c, bitset(c, original));
|
|
|
|
|
2007-06-21 22:51:55 +00:00
|
|
|
if (Debug) {
|
|
|
|
fprintf(stderr, "scan %p\n", copy);
|
|
|
|
}
|
2007-06-21 18:35:24 +00:00
|
|
|
|
|
|
|
c->client->walk(copy, &walker);
|
2007-06-20 02:28:31 +00:00
|
|
|
|
|
|
|
assert(c, walker.total > 1);
|
|
|
|
|
2007-06-21 18:35:24 +00:00
|
|
|
if (walker.total == 3 and bitsetHasMore(bitset(c, original))) {
|
2007-06-20 02:28:31 +00:00
|
|
|
parent = original;
|
|
|
|
} else {
|
2007-06-21 18:35:24 +00:00
|
|
|
parent = ::parent(c, original);
|
2007-06-20 02:28:31 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 22:51:55 +00:00
|
|
|
if (Debug) {
|
2007-06-22 20:55:11 +00:00
|
|
|
fprintf(stderr, " next is %p (%s) at %p - offset %d from %p (%s)\n",
|
2007-07-01 21:34:22 +00:00
|
|
|
get(copy, walker.next),
|
|
|
|
segment(c, get(copy, walker.next)),
|
|
|
|
getp(copy, walker.next),
|
2007-06-21 22:51:55 +00:00
|
|
|
walker.next,
|
2007-06-22 20:55:11 +00:00
|
|
|
copy,
|
|
|
|
segment(c, copy));
|
2007-06-21 22:51:55 +00:00
|
|
|
}
|
2007-06-21 18:35:24 +00:00
|
|
|
|
2007-07-01 21:34:22 +00:00
|
|
|
original = get(copy, walker.next);
|
|
|
|
set(copy, walker.next, follow(c, original));
|
2007-06-20 04:26:36 +00:00
|
|
|
goto visit;
|
2007-06-20 02:28:31 +00:00
|
|
|
} 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-08-18 21:24:29 +00:00
|
|
|
if (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-08-18 21:24:29 +00:00
|
|
|
if (not c->gen2.contains(*p)) {
|
2007-06-20 04:26:36 +00:00
|
|
|
map->setOnly(p);
|
|
|
|
*dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(c, wasDirty or not expectDirty);
|
|
|
|
}
|
|
|
|
|
2007-06-20 02:28:31 +00:00
|
|
|
void
|
|
|
|
collect2(Context* c)
|
|
|
|
{
|
2007-08-18 21:24:29 +00:00
|
|
|
c->gen2Base = Top;
|
|
|
|
c->tenureFootprint = 0;
|
|
|
|
c->gen1padding = 0;
|
|
|
|
c->gen2padding = 0;
|
|
|
|
|
|
|
|
if (c->mode == Heap::MinorCollection and c->gen2.position()) {
|
2007-06-20 02:28:31 +00:00
|
|
|
unsigned start = 0;
|
2007-06-22 02:13:17 +00:00
|
|
|
unsigned end = start + c->gen2.position();
|
2007-06-20 02:28:31 +00:00
|
|
|
bool dirty;
|
2007-06-20 04:26:36 +00:00
|
|
|
collect(c, &(c->heapMap), start, end, &dirty, false);
|
2007-06-20 02:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2007-06-20 02:28:31 +00:00
|
|
|
} v(c);
|
|
|
|
|
2007-06-20 17:42:13 +00:00
|
|
|
c->client->visitRoots(&v);
|
2007-06-20 02:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-18 21:24:29 +00:00
|
|
|
collect(Context* c, unsigned footprint)
|
2007-06-20 02:28:31 +00:00
|
|
|
{
|
2007-08-18 21:24:29 +00:00
|
|
|
if (c->tenureFootprint > c->gen2.remaining()) {
|
|
|
|
c->mode = Heap::MajorCollection;
|
|
|
|
}
|
2007-06-20 02:28:31 +00:00
|
|
|
|
2007-08-18 22:42:11 +00:00
|
|
|
int64_t then;
|
2007-08-18 21:24:29 +00:00
|
|
|
if (Verbose) {
|
|
|
|
if (c->mode == Heap::MajorCollection) {
|
2007-08-18 22:42:11 +00:00
|
|
|
fprintf(stderr, "major collection ");
|
2007-08-18 21:24:29 +00:00
|
|
|
} else {
|
2007-08-18 22:42:11 +00:00
|
|
|
fprintf(stderr, "minor collection ");
|
2007-06-21 22:51:55 +00:00
|
|
|
}
|
2007-08-18 22:42:11 +00:00
|
|
|
|
|
|
|
then = c->system->now();
|
2007-08-18 21:24:29 +00:00
|
|
|
}
|
2007-06-21 22:51:55 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
initNextGen1(c, footprint);
|
|
|
|
if (c->mode == Heap::MajorCollection) {
|
2007-06-20 02:28:31 +00:00
|
|
|
initNextGen2(c);
|
2007-08-18 21:24:29 +00:00
|
|
|
}
|
2007-06-20 02:28:31 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
collect2(c);
|
2007-06-20 17:42:13 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
c->gen1.replaceWith(&(c->nextGen1));
|
|
|
|
if (c->mode == Heap::MajorCollection) {
|
|
|
|
c->gen2.replaceWith(&(c->nextGen2));
|
2007-06-20 02:28:31 +00:00
|
|
|
}
|
2007-08-18 22:42:11 +00:00
|
|
|
|
|
|
|
if (Verbose) {
|
|
|
|
fprintf(stderr, "- " LLD "ms\n", (c->system->now() - then));
|
|
|
|
}
|
2007-06-20 02:28:31 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
class MyHeap: public Heap {
|
|
|
|
public:
|
|
|
|
MyHeap(System* system): c(system) { }
|
2007-06-20 02:28:31 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
virtual void collect(CollectionType type, Client* client, unsigned footprint)
|
|
|
|
{
|
|
|
|
c.mode = type;
|
2007-07-20 14:36:31 +00:00
|
|
|
c.client = client;
|
2007-06-20 16:58:35 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
::collect(&c, footprint);
|
2007-07-20 14:36:31 +00:00
|
|
|
}
|
2007-06-20 16:58:35 +00:00
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
virtual bool needsMark(void** p) {
|
|
|
|
return *p and c.gen2.contains(p) and not c.gen2.contains(*p);
|
|
|
|
}
|
2007-06-20 16:58:35 +00:00
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
virtual void mark(void** p) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
c.heapMap.set(p);
|
|
|
|
}
|
2007-06-20 16:58:35 +00:00
|
|
|
|
2007-08-18 21:24:29 +00:00
|
|
|
virtual void pad(void* p, unsigned extra) {
|
|
|
|
if (c.gen1.contains(p)) {
|
|
|
|
if (c.ageMap.get(p) == TenureThreshold) {
|
|
|
|
c.gen2padding += extra;
|
|
|
|
} else {
|
|
|
|
c.gen1padding += extra;
|
|
|
|
}
|
|
|
|
} else if (c.gen2.contains(p)) {
|
|
|
|
c.gen2padding += extra;
|
|
|
|
} else {
|
|
|
|
c.gen1padding += extra;
|
|
|
|
}
|
2007-07-20 14:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void* follow(void* p) {
|
|
|
|
if (wasCollected(&c, p)) {
|
|
|
|
if (Debug) {
|
|
|
|
fprintf(stderr, "follow %p (%s) to %p (%s)\n",
|
|
|
|
p, segment(&c, p),
|
|
|
|
::follow(&c, p), segment(&c, ::follow(&c, p)));
|
2007-06-22 20:55:11 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
return ::follow(&c, p);
|
|
|
|
} else {
|
|
|
|
return p;
|
2007-06-20 16:58:35 +00:00
|
|
|
}
|
2007-07-20 14:36:31 +00:00
|
|
|
}
|
2007-06-20 02:28:31 +00:00
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
virtual Status status(void* p) {
|
|
|
|
p = mask(p);
|
|
|
|
|
|
|
|
if (p == 0) {
|
|
|
|
return Null;
|
|
|
|
} else if (c.nextGen1.contains(p)) {
|
|
|
|
return Reachable;
|
|
|
|
} else if (c.nextGen2.contains(p)
|
|
|
|
or (c.gen2.contains(p)
|
2007-08-18 21:24:29 +00:00
|
|
|
and (c.mode == Heap::MinorCollection
|
2007-07-20 14:36:31 +00:00
|
|
|
or c.gen2.indexOf(p) >= c.gen2Base)))
|
|
|
|
{
|
|
|
|
return Tenured;
|
|
|
|
} else if (wasCollected(&c, p)) {
|
|
|
|
return Reachable;
|
|
|
|
} else {
|
|
|
|
return Unreachable;
|
2007-06-20 16:58:35 +00:00
|
|
|
}
|
2007-07-20 14:36:31 +00:00
|
|
|
}
|
2007-06-20 16:58:35 +00:00
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
virtual CollectionType collectionType() {
|
2007-08-18 21:24:29 +00:00
|
|
|
return c.mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void dispose() {
|
|
|
|
c.dispose();
|
|
|
|
c.system->free(this);
|
2007-07-20 14:36:31 +00:00
|
|
|
}
|
2007-06-20 17:42:13 +00:00
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
Context c;
|
|
|
|
};
|
2007-07-10 01:43:43 +00:00
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
} // namespace
|
2007-07-10 01:43:43 +00:00
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
namespace vm {
|
2007-07-10 01:43:43 +00:00
|
|
|
|
2007-07-20 14:36:31 +00:00
|
|
|
Heap*
|
|
|
|
makeHeap(System* system)
|
|
|
|
{
|
|
|
|
return new (system->allocate(sizeof(MyHeap))) MyHeap(system);
|
2007-06-20 02:28:31 +00:00
|
|
|
}
|
2007-06-20 19:20:25 +00:00
|
|
|
|
|
|
|
} // namespace vm
|