memory - Rust 可以保证我使用正确的对象池释放对象吗?

标签 memory rust borrow-checker double-free

假设我定义了自己的对象池结构。在内部,它保留了所有对象的 Vec 和一些数据结构,使其知道向量中的哪些项目当前已分发,哪些是免费的。它有一个 allocate 方法返回向量中未使用项的索引,还有一个 free 方法告诉池中向量中的索引可以再次使用。

我是否可以通过类型系统和借用检查器保证我将对象释放回正确的池的方式来定义我的对象池的 API?这是假设我可能有多个相同类型的池实例的情况。在我看来,对于常规的全局分配器,rust 不必担心这个问题,因为只有一个全局分配器。

使用示例:

fn foo() {
    let new_obj1 = global_pool1.allocate();
    let new_obj2 = global_pool2.allocate();

    // do stuff with objects

    global_pool1.free(new_obj2); // oops!, giving back to the wrong pool
    global_pool2.free(new_obj1); // oops!, giving back to the wrong pool
}

最佳答案

您可以使用零大小类型(简称 ZST)来获取所需的 API,而无需其他指针的开销。

这是 2 个池的实现,它可以扩展为支持任意数量的池,使用宏生成“标记”结构(P1P2, ETC)。 一个主要的缺点是忘记使用池来free 会“泄漏”内存。

Ferrous Systems blog post有许多您可能感兴趣的可能的改进,特别是如果您静态分配池,并且它们还有许多技巧来玩弄 P1 的可见性,这样用户就不会滥用API。


use std::marker::PhantomData;
use std::{cell::RefCell, mem::size_of};

struct Index<D>(usize, PhantomData<D>);
struct Pool<D> {
    data: Vec<[u8; 4]>,
    free_list: RefCell<Vec<bool>>,
    marker: PhantomData<D>,
}

impl<D> Pool<D> {
    fn new() -> Pool<D> {
        Pool {
            data: vec![[0,0,0,0]],
            free_list: vec![true].into(),
            marker: PhantomData::default(),
        }
    }
    
    fn allocate(&self) -> Index<D> {
        self.free_list.borrow_mut()[0] = false;
        
        Index(0, self.marker)
    }
    
    fn free<'a>(&self, item: Index<D>) {
        self.free_list.borrow_mut()[item.0] = true;
    }
}

struct P1;
fn create_pool1() -> Pool<P1> {
    assert_eq!(size_of::<Index<P1>>(), size_of::<usize>());
    Pool::new()
}

struct P2;
fn create_pool2() -> Pool<P2> {
    Pool::new()
}


fn main() {
    
    let global_pool1 = create_pool1();
    let global_pool2 = create_pool2();
    
    let new_obj1 = global_pool1.allocate();
    let new_obj2 = global_pool2.allocate();

    // do stuff with objects

    global_pool1.free(new_obj1);
    global_pool2.free(new_obj2);

    global_pool1.free(new_obj2); // oops!, giving back to the wrong pool
    global_pool2.free(new_obj1); // oops!, giving back to the wrong pool
}

尝试使用错误的池进行释放会导致:

error[E0308]: mismatched types
  --> zpool\src\main.rs:57:23
   |
57 |     global_pool1.free(new_obj2); // oops!, giving back to the wrong pool
   |                       ^^^^^^^^ expected struct `P1`, found struct `P2`
   |
   = note: expected struct `Index<P1>`
              found struct `Index<P2>`

Link to playground

这可以稍微改进,以便借用检查器将强制 Index 不会超过 Pool,使用:

fn allocate(&self) -> Index<&'_ D> {
    self.free_list.borrow_mut()[0] = false;
        
    Index(0, Default::default())
}

因此,如果在 Index 处于事件状态时删除池,则会出现此错误:

error[E0505]: cannot move out of `global_pool1` because it is borrowed
  --> zpool\src\main.rs:54:10
   |
49 |     let new_obj1 = global_pool1.allocate();
   |                    ------------ borrow of `global_pool1` occurs here
...
54 |     drop(global_pool1);
   |          ^^^^^^^^^^^^ move out of `global_pool1` occurs here
...
58 |     println!("{}", new_obj1.0);
   |                    ---------- borrow later used here

Link to playground

此外,a link to playground with Item API (返回一个 Item,与仅返回一个 Index)

关于memory - Rust 可以保证我使用正确的对象池释放对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63369825/

相关文章:

rust - 有什么方法或方法可以检查近帐户 ID 上的 accesskey 权限吗?

rust - serde/bincode 如何序列化字节数组?

Rust:错误:无法借用 `&` 的不可变取消引用 - 指针 `*strm` 作为可变的

rust - 如何使用(不安全的)别名?

javascript - 将很长的变量从 PHP 传递到 Javascript

recursion - 不能在内部函数中使用外部类型参数的理由是什么?

c++ - 如何将虚拟内存地址转换为物理地址?

rust - 为什么在涉及 += 运算符的同一表达式中使用可变引用和不可变引用有时似乎被允许,有时则不允许?

c# - 如何在.NET中以编程方式测量当前进程的总内存消耗?

c++ - 用于内存缓冲区或文件指针的 OpenCV