9个版本
0.5.3 | 2023年1月9日 |
---|---|
0.5.2 | 2023年1月5日 |
0.5.1 | 2022年12月17日 |
0.5.0 | 2022年11月15日 |
0.3.1 | 2022年8月27日 |
#2 in #buddy
在safe_drive中使用
52KB
1K SLoC
memac: 使用slab和buddy分配器的内存分配器
用法
首先,为slab和buddy分配器分配64KiB对齐的内存区域。
#![feature(start)]
use memac;
use libc::{posix_memalign, c_void};
#[global_allocator]
static mut ALLOC: memac::Allocator<memac::budyy::Buddy32M> = memac::Allocator::new();
fn main() {
println!("Hello, world!");
}
const HEAP_SIZE: usize = 32 * 1024 * 1024; // 32MiB
#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
// initialize memory
unsafe {
// allocate memory
let mut ptr: *mut c_void = std::ptr::null_mut();
if posix_memalign(&mut ptr, memac::ALIGNMENT, HEAP_SIZE) != 0 {
panic!("posix_memalign");
}
ALLOC.init(ptr as usize, HEAP_SIZE);
}
main();
0
}
[dependencies]
libc = "0.2"
memac = "0.5"
memac::Allocator<memac::budyy::Buddy32M>
表示slab分配器使用buddy分配器来分配slab。如果请求的大小大于(65512 - 8)字节,则使用buddy分配器来分配内存。
memac::Allocator<memac::pager::PageManager>
表示分配器使用页面管理器来分配slab。如果请求的大小大于(65512 - 8)字节,则使用页面管理器来分配内存。如果请求的大小大于64K字节,则分配将失败。
依赖关系
~0–24MB
~334K SLoC