[][src]Function tao_of_rust::ch13::global_alloc::global_alloc

pub fn global_alloc()

全局分配器

Basic usage: 自定义全局分配器

use std::alloc::{GlobalAlloc, System, Layout};
 
struct MyAllocator;
 
unsafe impl GlobalAlloc for MyAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        System.alloc(layout)
    }
    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        System.dealloc(ptr, layout)
    }
}
 
#[global_allocator]
static GLOBAL: MyAllocator = MyAllocator;
 
fn main() {
   // 此处Vec的内存会由GLOBAL全局分配器来分配
   let mut v = Vec::new();
   v.push(1);
}Run

Basic usage: 指定全局分配器为jemalloc

需要引入第三方包 jemallocator

extern crate jemallocator;
use jemallacator::Jemalloc;
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;Run