管理垃圾收集对象的生命周期

时间:2015-05-20 05:18:14

标签: garbage-collection rust allocation lifetime

我正在制作一个简单的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(不改变内部,这很好!)来解释以下事实:

  1. Pointer堆进入另一个堆时,Heap的所有merge到(对象分配)都会变为无效。

  2. merge会返回Adjust,其值有效Pointer到({1}}中分配的对象。

  3. 我有以下暂定解决方案:

    self

    这个设计是否正确?如果没有,需要改变什么?

0 个答案:

没有答案