1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
#include "stdafx.h"
#include "Gc.h"
#include "Utils/Memory.h"
#include "Core/Exception.h"
#ifndef STORM_GC
#error "This file must be compiled from the Gc project!"
#endif
namespace storm {
// Function callable by GC implementations to produce a stack trace to standard out. Useful to
// call when a segmentation fault happened, to aid in debugging.
// This function might crash while generating a stack trace, but that is fine, as we assume
// that the system would crash anyway.
extern "C" void gc_panic_stacktrace() {
std::wcerr << L"Crash backtrace:" << std::endl;
std::wcerr << format(stackTrace()) << std::endl;
}
struct ImplWrap : public GcImpl {
ImplWrap(Gc &owner, size_t initialArena, nat finalizationInterval)
: GcImpl(initialArena, finalizationInterval), owner(owner) {}
Gc &owner;
};
Gc::Gc(size_t initialArena, nat finalizationInterval)
: impl(new ImplWrap(*this, initialArena, finalizationInterval)), destroyed(false) {
#ifdef STORM_GC_EH_CALLBACK
ehCallback = null;
#endif
}
Gc::~Gc() {
destroy();
delete (ImplWrap *)impl;
}
void Gc::destroy() {
if (destroyed)
return;
destroyed = true;
{
util::Lock::L z(threadLock);
for (ThreadMap::iterator i = threads.begin(); i != threads.end(); ++i) {
impl->detachThread(i->second.data);
}
threads.clear();
}
{
// If there are any roots registered now, we destroy them before shutdown as
// implementations are a bit picky about having all roots unregistered before
// shutdown.
// We don't free the memory here, as any remaining roots are probably owned
// by some GC allocation, which will be freed when finalizers are executed
// as a part of shutdown.
util::Lock::L z(rootLock);
for (os::InlineSet<Root>::iterator i = roots.begin(), end = roots.end(); i != end; ++i) {
Root *r = *i;
GcImpl::destroyRoot(r);
r->owner = null;
}
// We won't remove the elements, so make sure to clear them properly. Otherwise, the
// implementation will assert whenever they are freed later.
roots.clear();
}
impl->destroy();
}
MemorySummary Gc::summary() {
return impl->summary();
}
void Gc::collect() {
impl->collect();
}
bool Gc::collect(nat time) {
return impl->collect(time);
}
void Gc::attachThread() {
os::Thread thread = os::Thread::current();
util::Lock::L z(threadLock);
ThreadMap::iterator i = threads.find(thread.id());
if (i != threads.end()) {
// Already attached. Increase the refcount.
i->second.refs++;
} else {
// New thread!
ThreadData d = { 1, impl->attachThread() };
threads.insert(make_pair(thread.id(), d));
}
}
void Gc::reattachThread(const os::Thread &thread) {
util::Lock::L z(threadLock);
ThreadMap::iterator i = threads.find(thread.id());
if (i != threads.end()) {
i->second.refs++;
} else {
assert(false, L"Trying to re-attach a new thread!");
}
}
void Gc::detachThread(const os::Thread &thread) {
util::Lock::L z(threadLock);
ThreadMap::iterator i = threads.find(thread.id());
if (i == threads.end())
return;
if (i->second.refs > 1) {
// Not yet...
i->second.refs--;
return;
}
impl->detachThread(i->second.data);
threads.erase(i);
}
GcImpl::ThreadData Gc::threadData(GcImpl *from, const os::Thread &thread, const GcImpl::ThreadData &def) {
Gc &me = ((ImplWrap *)from)->owner;
util::Lock::L z(me.threadLock);
ThreadMap::iterator i = me.threads.find(thread.id());
if (i == me.threads.end())
return def;
else
return i->second.data;
}
#ifdef STORM_GC_EH_CALLBACK
STORM_GC_EH_CALLBACK Gc::getEhCallback(GcImpl *from) {
Gc &me = ((ImplWrap *)from)->owner;
return me.ehCallback;
}
#endif
vector<GcImpl::ThreadData> Gc::allThreads(GcImpl *from, const os::Thread ¤t) {
Gc &me = ((ImplWrap *)from)->owner;
util::Lock::L z(me.threadLock);
vector<GcImpl::ThreadData> result;
ThreadMap::iterator c = me.threads.find(current.id());
if (c != me.threads.end())
result.push_back(c->second.data);
for (ThreadMap::iterator i = me.threads.begin(), end = me.threads.end(); i != end; i++) {
if (i->first != current.id())
result.push_back(i->second.data);
}
return result;
}
const os::InlineSet<GcRoot> &Gc::allRoots(GcImpl *from) {
Gc &me = ((ImplWrap *)from)->owner;
return me.roots;
}
GcType *Gc::allocType(GcType::Kind kind, Type *type, size_t stride, size_t entries) {
// Disallow too large type allocations by limiting the number of entries way below what
// would cause an overflow.
if (entries > 0x01000000)
throw new (runtime::someEngine()) GcError(S("Too many entries to allocType."));
return impl->allocType(kind, type, stride, entries);
}
GcType *Gc::allocType(const GcType *original) {
GcType *t = allocType(GcType::Kind(original->kind), original->type, original->stride, original->count);
t->finalizer = original->finalizer;
for (Nat i = 0; i < original->count; i++) {
t->offset[i] = original->offset[i];
}
return t;
}
void Gc::freeType(GcType *type) {
impl->freeType(type);
}
void Gc::switchType(void *mem, const GcType *to) {
assert(typeOf(mem)->stride == to->stride, L"Can not change size of allocations.");
assert(typeOf(mem)->kind == to->kind, L"Can not change kind of allocations.");
GcImpl::switchType(mem, to);
}
Gc::RampAlloc::RampAlloc(Gc &owner) : owner(owner) {
owner.impl->startRamp();
}
Gc::RampAlloc::~RampAlloc() {
owner.impl->endRamp();
}
void Gc::walk(Walker &walker) {
impl->walk(walker);
}
void Gc::checkMemory() {
impl->checkMemory();
}
void Gc::checkMemory(const void *object, bool recursive) {
impl->checkMemory(object, recursive);
}
void Gc::checkMemoryCollect() {
impl->checkMemoryCollect();
}
void Gc::dbg_dump() {
impl->dbg_dump();
}
const GcLicense *Gc::license() {
return impl->license();
}
const GcType Gc::weakArrayType = {
GcType::tWeakArray,
null,
null,
sizeof(void *),
1,
{}
};
/**
* Simple linked list to test gc.
*/
struct GcLink {
nat value;
GcLink *next;
};
static const GcType linkType = {
GcType::tFixed,
null,
null,
sizeof(GcLink),
1,
{ OFFSET_OF(GcLink, next) }
};
bool Gc::test(nat times) {
bool ok = true;
for (nat j = 0; j < times && ok; j++) {
GcLink *first = null;
GcLink *at = null;
for (nat i = 0; i < 10000; i++) {
GcLink *l = (GcLink *)alloc(&linkType);
l->value = i;
if (at) {
at->next = l;
at = l;
} else {
first = at = l;
}
}
at = first;
for (nat i = 0; i < 10000; i++) {
if (!at) {
PLN("Premature end at " << i);
ok = false;
break;
}
if (at->value != i) {
PLN("Failed: " << at->value << " should be " << i);
ok = false;
}
at = at->next;
}
// collect();
}
return ok;
}
}
|