gpt4 book ai didi

rust - 简单资源分配器结构的生命周期错误

转载 作者:行者123 更新时间:2023-11-29 08:16:52 27 4
gpt4 key购买 nike

我正在尝试制作一个简单的分配器,用于从固定的缓冲区池中分配和释放缓冲区。

struct AllocatedMemory<'a> {
mem: &'a mut [u8],
next: Option<&'a mut AllocatedMemory<'a>>,
}

struct Alloc<'a> {
glob: Option<&'a mut AllocatedMemory<'a>>,
}

impl<'a> Alloc<'a> {
fn alloc_cell(mut self: &mut Alloc<'a>) -> &mut AllocatedMemory<'a> {
let rest: Option<&'a mut AllocatedMemory<'a>>;
match self.glob {
Some(ref mut root_cell) => {
rest = std::mem::replace(&mut root_cell.next, None);
}
None => rest = None,
}
match std::mem::replace(&mut self.glob, rest) {
Some(mut root_cell) => {
return root_cell;
}
None => panic!("OOM"),
}
}

fn free_cell(mut self: &mut Alloc<'a>, mut val: &'a mut AllocatedMemory<'a>) {
match std::mem::replace(&mut self.glob, None) {
Some(mut x) => {
let discard = std::mem::replace(&mut val.next, Some(x));
let rest: Option<&'a mut AllocatedMemory<'a>>;
}
None => {}
}
self.glob = Some(val);
}
}

fn main() {
let mut buffer0: [u8; 1024] = [0; 1024];
let mut buffer1: [u8; 1024] = [0; 1024];
{
let mut cell1: AllocatedMemory = AllocatedMemory {
mem: &mut buffer1[0..1024],
next: None,
};
let mut cell0: AllocatedMemory = AllocatedMemory {
mem: &mut buffer0[0..1024],
next: None,
};
let mut allocator = Alloc { glob: None };
allocator.free_cell(&mut cell1); //populate allocator with a cell
allocator.free_cell(&mut cell0); //populate allocator with another cell (why does this fail?)

let mut x = allocator.alloc_cell();
allocator.free_cell(x);
let mut y = allocator.alloc_cell();
let mut z = allocator.alloc_cell();
allocator.free_cell(y);
allocator.free_cell(z);
}
}

错误是

error: `cell0` does not live long enough
allocator.free_cell(&mut cell0); //populate allocator with another cell (why does this fail?)

当我简单地删除 cell0 并且只有 cell1 可用于我的单元池时,会发生以下错误:

error: allocator does not live long enough
let mut x = allocator.alloc_cell();
^~~~~~~~~
note: reference must be valid for the block suffix following statement 0 at 46:69...
next: None};
let mut cell0 : AllocatedMemory = AllocatedMemory{mem: &mut buffer0[0..1024],
next: None};
let mut allocator = Alloc {glob : None};
allocator.free_cell(&mut cell1); //populate allocator with a cell
//allocator.free_cell(&mut cell0); //populate allocator with another cell (why does this fail?)

note: ...but borrowed value is only valid for the block suffix following statement 2 at 49:48
let mut allocator = Alloc {glob : None};
allocator.free_cell(&mut cell1); //populate allocator with a cell
//allocator.free_cell(&mut cell0); //populate allocator with another cell (why does this fail?)

let mut x = allocator.alloc_cell();
allocator.free_cell(x);
...
error: aborting due to previous error

有没有人建议如何修复此代码,以便它编译并在空闲列表中可能有 2 个以上的项目?

我想填充对数组的引用列表,然后能够弹出它们 - 使用它们一段时间,然后将使用过的/完成的值放回空闲列表。

这里的动机是构建一个使用 #![nostd] 指令的库,因此它需要一个分配器接口(interface)才能正常运行。

最佳答案

问题是你总是使用相同的生命周期'a。这会强制 cell0cell1 具有相同的生命周期,这是不可能的,因为必须先定义一个。如果您仔细阅读错误消息,您会发现它提示第二个单元格的生命周期不包括定义第一个单元格的语句。

我不知道这是严格执行生命周期的错误或功能不当,还是生命周期类型系统固有的(我还没有看到正式定义)。

我也不知道怎么解决。我通过引入额外的生命周期变量解决了一些示例代码中的类似问题,但我无法使其适用于您的代码。

关于rust - 简单资源分配器结构的生命周期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36233735/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com