我正在制作一个简单的mark-and-compact garbage collector。没有太多细节,它公开的API是这样的:
/// Describes the internal structure of a managed object.
pub struct Tag { ... }
/// An unmanaged pointer to a managed object.
pub type Pointer = *mut usize;
/// Mapping from old to new locations.
pub type Adjust = BTreeMap<usize, Pointer>;
/// Mark this object and anything it points to as non-garbage.
pub unsafe fn survive(ptr: Pointer);
pub struct Heap { ... }
impl Heap {
pub fn new() -> Heap;
// Allocate an object with the specified structure.
pub fn allocate(&mut self, tag: Tag) -> Pointer;
/// Move all live objects from `heap` into `self`.
puf unsafe fn reallocate(&mut self, heap: Heap) -> Adjust;
}
这个API显然从根本上说是不安全的。我想重做API(不改变内部,这很好!)来解释以下事实:
当Pointer
堆进入另一个堆时,Heap
的所有merge
到(对象分配)都会变为无效。
merge
会返回Adjust
,其值有效Pointer
到({1}}中分配的对象。
我有以下暂定解决方案:
self
这个设计是否正确?如果没有,需要改变什么?