rt: Call maybe_gc on mallocs

This commit is contained in:
Patrick Walton 2011-08-19 13:03:46 -07:00
parent 390dd38619
commit cede5e53b3
3 changed files with 38 additions and 11 deletions

View file

@ -3,6 +3,7 @@
#include <utility> #include <utility>
#include <stdint.h> #include <stdint.h>
#include "rust_gc.h"
#include "rust_internal.h" #include "rust_internal.h"
#ifdef __WIN32__ #ifdef __WIN32__
@ -31,38 +32,51 @@ class safe_point_map {
public: public:
safe_point_map() { safe_point_map() {
const uintptr_t *data; const uintptr_t *data = get_safe_point_data();
#ifdef __WIN32__
data = (const uintptr_t *)GetProcAddress(GetModuleHandle(NULL),
"rust_gc_safe_points");
#else
data = (const uintptr_t *)dlsym(RTLD_DEFAULT, "rust_gc_safe_points");
#endif
n_safe_points = *data++; n_safe_points = *data++;
index = (const std::pair<void *,const safe_point *> *)data; index = (const std::pair<void *,const safe_point *> *)data;
data += n_safe_points * 2; data += n_safe_points * 2;
safe_points = (const safe_point *)data; safe_points = (const safe_point *)data;
} }
static const uintptr_t *get_safe_point_data() {
static bool init = false;
static const uintptr_t *data;
if (!init) {
#ifdef __WIN32__
data = (const uintptr_t *)GetProcAddress(GetModuleHandle(NULL),
"rust_gc_safe_points");
#else
data = (const uintptr_t *)dlsym(RTLD_DEFAULT,
"rust_gc_safe_points");
#endif
init = true;
}
return data;
}
}; };
void void
gc() { gc(rust_task *task) {
safe_point_map map; safe_point_map map;
// TODO // TODO
} }
void void
maybe_gc() { maybe_gc(rust_task *task) {
if (safe_point_map::get_safe_point_data() == NULL)
return;
// FIXME: We ought to lock this. // FIXME: We ought to lock this.
static int zeal = -1; static int zeal = -1;
if (zeal == -1) { if (zeal == -1) {
char *ev = getenv("RUST_GC_ZEAL"); char *ev = getenv("RUST_GC_ZEAL");
zeal = ev[0] != '\0' && ev[0] != '0'; zeal = ev && ev[0] != '\0' && ev[0] != '0';
} }
if (zeal) if (zeal)
gc(); gc(task);
} }
} }

10
src/rt/rust_gc.h Normal file
View file

@ -0,0 +1,10 @@
// Rust garbage collection.
struct rust_task;
namespace gc {
void maybe_gc(rust_task *task);
}

View file

@ -1,3 +1,4 @@
#include "rust_gc.h"
#include "rust_internal.h" #include "rust_internal.h"
#include "rust_upcall.h" #include "rust_upcall.h"
@ -130,6 +131,8 @@ upcall_malloc(rust_task *task, size_t nbytes, type_desc *td) {
" with gc-chain head = 0x%" PRIxPTR, " with gc-chain head = 0x%" PRIxPTR,
nbytes, td, task->gc_alloc_chain); nbytes, td, task->gc_alloc_chain);
gc::maybe_gc(task);
// TODO: Maybe use dladdr here to find a more useful name for the // TODO: Maybe use dladdr here to find a more useful name for the
// type_desc. // type_desc.