pub struct System;
Expand description
操作系统提供的默认内存分配器。
它基于 Unix 平台上的 malloc
和 Windows 上的 HeapAlloc
,以及相关的函数。
但是,将支持系统分配器与 System
混合使用是无效的,因为此实现可能包括额外的工作,例如提供比支持系统分配器直接提供的对齐更大的对齐请求。
此类型默认情况下实现 GlobalAlloc
trait 和 Rust 程序,就像它们具有以下定义一样:
use std::alloc::System;
#[global_allocator]
static A: System = System;
fn main() {
let a = Box::new(4); // 从系统分配器分配。
println!("{a}");
}
Run如果愿意,还可以围绕 System
定义自己的包装器,例如跟踪分配的所有字节数:
use std::alloc::{System, GlobalAlloc, Layout};
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
struct Counter;
static ALLOCATED: AtomicUsize = AtomicUsize::new(0);
unsafe impl GlobalAlloc for Counter {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ret = System.alloc(layout);
if !ret.is_null() {
ALLOCATED.fetch_add(layout.size(), Relaxed);
}
ret
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
System.dealloc(ptr, layout);
ALLOCATED.fetch_sub(layout.size(), Relaxed);
}
}
#[global_allocator]
static A: Counter = Counter;
fn main() {
println!("allocated bytes before main: {}", ALLOCATED.load(Relaxed));
}
Run它也可以直接用于独立于 Rust 程序选择的分配器来分配内存。
例如,如果 Rust 程序选择使用 jemalloc 作为分配器,则 System
仍将使用 malloc
和 HeapAlloc
分配内存。
Trait Implementations§
source§impl Allocator for System
impl Allocator for System
source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
🔬This is a nightly-only experimental API. (
allocator_api
#32838)尝试分配一块内存。 Read more
source§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
🔬This is a nightly-only experimental API. (
allocator_api
#32838)行为类似于
allocate
,但也确保返回的内存被零初始化。 Read moresource§unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
🔬This is a nightly-only experimental API. (
allocator_api
#32838)释放
ptr
引用的内存。 Read moresource§unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout ) -> Result<NonNull<[u8]>, AllocError>
🔬This is a nightly-only experimental API. (
allocator_api
#32838)尝试扩展内存块。 Read more
source§unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout ) -> Result<NonNull<[u8]>, AllocError>
🔬This is a nightly-only experimental API. (
allocator_api
#32838)行为类似于
grow
,但也确保在返回新内容之前将其设置为零。 Read moresource§impl GlobalAlloc for System
impl GlobalAlloc for System
impl Copy for System
Auto Trait Implementations§
impl RefUnwindSafe for System
impl Send for System
impl Sync for System
impl Unpin for System
impl UnwindSafe for System
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
从拥有的值中借用。 Read more