注意:这个回复有点老了。由于它谈到了内部结构和不稳定的特性,所以事情发生了一些变化。虽然基本机制保持不变,所以答案仍然能够解释box 的底层机制。
box x 通常使用什么来分配和释放内存?
答案是标记为exchange_malloc 用于分配和exchange_free 用于释放的函数。您可以在heap.rs#L112 和heap.rs#L125 看到默认标准库中的实现。
最后,box x 语法取决于以下语言项:
-
owned_box 在 Box 结构上封装分配的指针。该结构不需要Drop 实现,它由编译器自动实现。
-
exchange_malloc 分配内存。
-
exchange_free 释放之前分配的内存。
这可以在不稳定锈书的lang items 章节中使用no_std 示例有效地看到:
#![feature(lang_items, box_syntax, start, no_std, libc)]
#![no_std]
extern crate libc;
extern {
fn abort() -> !;
}
#[lang = "owned_box"]
pub struct Box<T>(*mut T);
#[lang = "exchange_malloc"]
unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
let p = libc::malloc(size as libc::size_t) as *mut u8;
// malloc failed
if p as usize == 0 {
abort();
}
p
}
#[lang = "exchange_free"]
unsafe fn deallocate(ptr: *mut u8, _size: usize, _align: usize) {
libc::free(ptr as *mut libc::c_void)
}
#[start]
fn main(argc: isize, argv: *const *const u8) -> isize {
let x = box 1;
0
}
#[lang = "stack_exhausted"] extern fn stack_exhausted() {}
#[lang = "eh_personality"] extern fn eh_personality() {}
#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} }
注意Drop 是如何没有为Box 结构实现的?好吧,让我们看看为main 生成的 LLVM IR:
define internal i64 @_ZN4main20hbd13b522fdb5b7d4ebaE(i64, i8**) unnamed_addr #1 {
entry-block:
%argc = alloca i64
%argv = alloca i8**
%x = alloca i32*
store i64 %0, i64* %argc, align 8
store i8** %1, i8*** %argv, align 8
%2 = call i8* @_ZN8allocate20hf9df30890c435d76naaE(i64 4, i64 4)
%3 = bitcast i8* %2 to i32*
store i32 1, i32* %3, align 4
store i32* %3, i32** %x, align 8
call void @"_ZN14Box$LT$i32$GT$9drop.103617h8817b938807fc41eE"(i32** %x)
ret i64 0
}
allocate (_ZN8allocate20hf9df30890c435d76naaE) 被调用以构建Box,同时...看! Box (_ZN14Box$LT$i32$GT$9drop.103617h8817b938807fc41eE) 的 Drop 方法!让我们看看这个方法的 IR:
define internal void @"_ZN14Box$LT$i32$GT$9drop.103617h8817b938807fc41eE"(i32**) unnamed_addr #0 {
entry-block:
%1 = load i32** %0
%2 = ptrtoint i32* %1 to i64
%3 = icmp ne i64 %2, 2097865012304223517
br i1 %3, label %cond, label %next
next: ; preds = %cond, %entry- block
ret void
cond: ; preds = %entry-block
%4 = bitcast i32* %1 to i8*
call void @_ZN10deallocate20he2bff5e01707ad50VaaE(i8* %4, i64 4, i64 4)
br label %next
}
就是这样,deallocate (ZN10deallocate20he2bff5e01707ad50VaaE) 在编译器生成的 Drop 上被调用!
请注意,即使在 standard library 上,Drop 特征也不是由用户代码实现的。确实Box 有点神奇。