在Rust中,我们可以使用Box<T>类型在堆上分配内容.此类型用于安全地抽象指向堆内存的指针. Box<T>由Rust标准库提供.
我很好奇如何Box<T>实现分配,所以我找到了它的源代码.这是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
}
}
Run Code Online (Sandbox Code Playgroud)
实现中唯一的行返回值box x.box官方文档中的任何地方都没有解释此关键字; 事实上,它仅在std::boxed文档页面上简要提及.
Den*_*rim 23
box x通常用什么来分配和释放内存?
答案是标有lang项目的功能,exchange_malloc用于分配和exchange_free释放.您可以在heap.rs#L112和heap.rs#L125中看到默认标准库中的实现.
最后,box x语法取决于以下lang项:
owned_box在一个Box结构上封装分配的指针.此结构不需要Drop实现,它由编译器自动实现.exchange_malloc 分配内存.exchange_free 释放先前分配的内存.使用此示例可以在Rust书的lang项目章节中有效地看到这一点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 {} }
Run Code Online (Sandbox Code Playgroud)
注意结构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
}
Run Code Online (Sandbox Code Playgroud)
将allocate(_ZN8allocate20hf9df30890c435d76naaE)被调用如预期建Box,同时...看看!一种Drop方法Box(_ZN14Box$LT$i32$GT$9drop.103617h8817b938807fc41eE)!让我们看看这个方法的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
}
Run Code Online (Sandbox Code Playgroud)
它是,deallocate(ZN10deallocate20he2bff5e01707ad50VaaE)在编译器上调用生成的Drop!
请注意,即使在标准库上,Drop特征也不是由用户代码实现的.确实Box是一个神奇的结构.
She*_*ter 14
之前box被标记为不稳定,它被用作呼叫的简写Box::new.但是,它始终旨在能够分配任意类型,例如Rc,或使用任意分配器.这些都没有最终确定,因此1.0版本没有标记为稳定.这样做是为了防止支持Rust 1.x的所有错误决定.
为了进一步参考,您可以阅读更改"placement new"语法的RFC,并对其进行门控操作.
box确实做了什么Box::new()- 它创造了一个拥有的盒子.
我相信你找不到box关键字的实现,因为目前它是硬编码的,可以使用自己的盒子,而Boxtype是一个lang项目:
#[lang = "owned_box"]
#[stable(feature = "rust1", since = "1.0.0")]
#[fundamental]
pub struct Box<T>(Unique<T>);
Run Code Online (Sandbox Code Playgroud)
因为它是一个lang项,所以编译器具有特殊的逻辑来处理它可以与box关键字链接的实例化.
我相信编译器将盒子分配委托给alloc::heap模块中的函数.
至于box关键字的作用和应该做的一般,Shepmaster的答案完美地描述.