box 关键字有什么作用?
What does the box keyword do?
在 Rust 中,我们可以使用 Box<T>
类型在堆上分配东西。此类型用于安全地抽象指向堆内存的指针。 Box<T>
由 Rust 标准库提供。
我很好奇Box<T>
分配是如何实现的,所以我找到了its source code。这是 Box<T>::new
的代码(从 Rust 1.0 开始):
impl<T> Box<T> {
/// Allocates memory on the heap and then moves `x` into it.
/// [...]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline(always)]
pub fn new(x: T) -> Box<T> {
box x
}
}
唯一行中执行returns值box x
。这个 box
关键字在官方文档中没有任何解释;事实上,它只是在 std::boxed
文档页面上被简要提及。
在 box
被标记为不稳定之前,它被用作调用 Box::new
的 shorthand。但是,它一直旨在能够分配 任意类型 ,例如 Rc
,或使用任意分配器。这些都没有最终确定,所以它没有标记为 1.0 版本稳定。这样做是为了防止支持所有 Rust 1.x.
的错误决定
如需进一步参考,您可以阅读 RFC that changed the "placement new" syntax 并对其进行功能门控。
box
与 Box::new()
完全相同 - 它创建了一个自有框。
我相信您找不到 box
关键字的实现,因为目前它被硬编码为使用拥有的盒子,并且 Box
类型是一个 lang 项目:
#[lang = "owned_box"]
#[stable(feature = "rust1", since = "1.0.0")]
#[fundamental]
pub struct Box<T>(Unique<T>);
因为它是一个 lang 项目,编译器有特殊的逻辑来处理它的实例化,它可以 link 使用 box
关键字。
我相信编译器将框分配委托给 alloc::heap
模块中的函数。
至于 box
关键字的一般作用和应该做什么,Shepmaster 的回答描述得非常完美。
注意:这个回复有点老了。由于它谈到了内部结构和不稳定的特性,所以事情发生了一些变化。尽管基本机制保持不变,所以答案仍然能够解释 box
.
的基本机制
box x
通常使用什么来分配和释放内存?
答案是标有 lang 项的函数 exchange_malloc
用于分配,exchange_free
用于释放。您可以在 heap.rs#L112 and heap.rs#L125.
查看默认标准库中的实现
最终 box x
语法取决于以下 lang 项:
owned_box
在 Box
结构上封装分配的指针。此结构不需要 Drop
实现,它由编译器自动实现。
exchange_malloc
分配内存。
exchange_free
释放之前分配的内存。
这可以在 unstable rust book 的 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$GTdrop.103617h8817b938807fc41eE"(i32** %x)
ret i64 0
}
allocate
(_ZN8allocate20hf9df30890c435d76naaE
) 按预期被调用以构建 Box
,同时...看! Box
(_ZN14Box$LT$i32$GTdrop.103617h8817b938807fc41eE
) 的 Drop
方法!让我们看看这个方法的 IR:
define internal void @"_ZN14Box$LT$i32$GTdrop.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
是一个有点神奇的结构。
在 Rust 中,我们可以使用 Box<T>
类型在堆上分配东西。此类型用于安全地抽象指向堆内存的指针。 Box<T>
由 Rust 标准库提供。
我很好奇Box<T>
分配是如何实现的,所以我找到了its source code。这是 Box<T>::new
的代码(从 Rust 1.0 开始):
impl<T> Box<T> {
/// Allocates memory on the heap and then moves `x` into it.
/// [...]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline(always)]
pub fn new(x: T) -> Box<T> {
box x
}
}
唯一行中执行returns值box x
。这个 box
关键字在官方文档中没有任何解释;事实上,它只是在 std::boxed
文档页面上被简要提及。
在 box
被标记为不稳定之前,它被用作调用 Box::new
的 shorthand。但是,它一直旨在能够分配 任意类型 ,例如 Rc
,或使用任意分配器。这些都没有最终确定,所以它没有标记为 1.0 版本稳定。这样做是为了防止支持所有 Rust 1.x.
如需进一步参考,您可以阅读 RFC that changed the "placement new" syntax 并对其进行功能门控。
box
与 Box::new()
完全相同 - 它创建了一个自有框。
我相信您找不到 box
关键字的实现,因为目前它被硬编码为使用拥有的盒子,并且 Box
类型是一个 lang 项目:
#[lang = "owned_box"]
#[stable(feature = "rust1", since = "1.0.0")]
#[fundamental]
pub struct Box<T>(Unique<T>);
因为它是一个 lang 项目,编译器有特殊的逻辑来处理它的实例化,它可以 link 使用 box
关键字。
我相信编译器将框分配委托给 alloc::heap
模块中的函数。
至于 box
关键字的一般作用和应该做什么,Shepmaster 的回答描述得非常完美。
注意:这个回复有点老了。由于它谈到了内部结构和不稳定的特性,所以事情发生了一些变化。尽管基本机制保持不变,所以答案仍然能够解释 box
.
box x
通常使用什么来分配和释放内存?
答案是标有 lang 项的函数 exchange_malloc
用于分配,exchange_free
用于释放。您可以在 heap.rs#L112 and heap.rs#L125.
最终 box x
语法取决于以下 lang 项:
owned_box
在Box
结构上封装分配的指针。此结构不需要Drop
实现,它由编译器自动实现。exchange_malloc
分配内存。exchange_free
释放之前分配的内存。
这可以在 unstable rust book 的 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
:
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$GTdrop.103617h8817b938807fc41eE"(i32** %x)
ret i64 0
}
allocate
(_ZN8allocate20hf9df30890c435d76naaE
) 按预期被调用以构建 Box
,同时...看! Box
(_ZN14Box$LT$i32$GTdrop.103617h8817b938807fc41eE
) 的 Drop
方法!让我们看看这个方法的 IR:
define internal void @"_ZN14Box$LT$i32$GTdrop.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
是一个有点神奇的结构。