rust/src/rt/rust_gc.cpp

84 lines
1.7 KiB
C++
Raw Normal View History

2011-08-18 18:48:32 -07:00
// Rust garbage collection.
#include <utility>
#include <stdint.h>
2011-08-19 13:03:46 -07:00
#include "rust_gc.h"
2011-08-18 18:48:32 -07:00
#include "rust_internal.h"
#ifdef __WIN32__
#include <windows.h>
#else
#include <dlfcn.h>
#endif
namespace gc {
struct root {
intptr_t frame_offset;
uintptr_t dynamic; // 0 = static, 1 = dynamic
type_desc *tydesc;
};
struct safe_point {
uintptr_t n_roots;
root roots[0];
};
class safe_point_map {
uintptr_t n_safe_points;
const std::pair<void *,const safe_point *> *index;
const safe_point *safe_points;
public:
safe_point_map() {
2011-08-19 13:03:46 -07:00
const uintptr_t *data = get_safe_point_data();
2011-08-18 18:48:32 -07:00
n_safe_points = *data++;
index = (const std::pair<void *,const safe_point *> *)data;
data += n_safe_points * 2;
2011-08-18 18:48:32 -07:00
safe_points = (const safe_point *)data;
}
2011-08-19 13:03:46 -07:00
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;
}
2011-08-18 18:48:32 -07:00
};
void
2011-08-19 13:03:46 -07:00
gc(rust_task *task) {
2011-08-18 18:48:32 -07:00
safe_point_map map;
// TODO
}
void
2011-08-19 13:03:46 -07:00
maybe_gc(rust_task *task) {
if (safe_point_map::get_safe_point_data() == NULL)
return;
2011-08-18 18:48:32 -07:00
// FIXME: We ought to lock this.
static int zeal = -1;
if (zeal == -1) {
char *ev = getenv("RUST_GC_ZEAL");
2011-08-19 13:03:46 -07:00
zeal = ev && ev[0] != '\0' && ev[0] != '0';
2011-08-18 18:48:32 -07:00
}
if (zeal)
gc::gc(task);
2011-08-18 18:48:32 -07:00
}
}