惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
雷峰网
雷峰网
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
G
Google Developers Blog
腾讯CDC
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
博客园_首页
T
Tailwind CSS Blog
C
Check Point Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
Y
Y Combinator Blog
L
LangChain Blog
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI

The Rust Programming Language Forum - Latest topics

Beginner building a Rust backend framework (AI-assisted) — feedback appreciated C++ to Rust -- Exceptions Re-exporting a trait with a custom derive_macro Gold linker is deprecated Why is using PhantomData valid in this case? Looking for a pool based allocator Why is this legal? What does it mean for Rustc to run "out of TLS keys"? Using async/await internally with no internal runtime, but exposing a nonblocking poll API — is this a reasonable design? Testing functions that use randomness `cargo-path`: improve coding agents&#39; ability to find Rust documentation Rust task runners Lifetime weird case Windows - USB device not detected A random rustc-ice-[...].txt file appeared Develop rust where the environment is setup in a docker Undefined Behavior: in-bounds pointer arithmetic failed: attempting to offset pointer by 20 bytes, but got alloc238 which is only 1 byte from the end of the allocation Unbug 0.5 - Runtime debug assertions Is there a tiny error in section 6.2 Reference types? Lifetime woes implementing ratatui::Widget for a reference Rusqlite + Chrono: How do I simplify code to obtain chrono datetime value From OOP to Rust – struggling with code organization and data structure design Way to avoid a self-referential struct Rust RF and audio resources/communities Whyhttp - HTTP mocks that fail where the bug actually is Using tokio channel permits in a tower service Arc::increment_strong_count design question (cross-post) `&T`, `&mut T`, `Pin<&mut T>` and `&Cell<T>`: Ways of Borrowing a `T` Ratatui detect arrow key press and release Feedback about post in medium
Code review for Static Pool Allocator
@fuji-184 Fu · 2026-04-24 · via The Rust Programming Language Forum - Latest topics
Hello, I just finished my Static Pool Allocator. I checked with Miri then fixed all errors reported by Miri, now no error is reported by Miri. I need human review help Here is the code : #![feature(allocator_api)] pub struct Block { next: Option<std::ptr::NonNull<Block>> } pub struct StaticPoolAllocator { start: std::ptr::NonNull<u8>, head: std::cell::UnsafeCell<Option<std::ptr::NonNull<Block>>>, block_size: usize, layout: std::alloc::Layout } impl StaticPoolAllocator { pub fn new(num_blocks: usize, block_size: usize) -> Self { let block_size = block_size.max(std::mem::size_of::<Block>()); let layout = std::alloc::Layout::from_size_align( num_blocks * block_size, std::mem::align_of::<Block>() ).expect("Error in creating layout in fn new -> PoolAllocator"); // SAFETY: the layout is valid, because if not it will trigger panic let start = unsafe { std::alloc::alloc(layout) }; let start_ptr = std::ptr::NonNull::new(start).expect("Error OOM in fn new -> PoolAllocator"); unsafe { for i in 0..num_blocks { let current_ptr = start.add(i * block_size).cast::<Block>(); let next_ptr = if i < num_blocks - 1 { Some(std::ptr::NonNull::new_unchecked(start.add((i + 1) * block_size).cast::<Block>())) } else { None }; (*current_ptr).next = next_ptr; } } Self { start: start_ptr, head: std::cell::UnsafeCell::new(Some(std::ptr::NonNull::new(start_ptr.as_ptr().cast::<Block>()).unwrap())), block_size, layout, } } } impl Drop for StaticPoolAllocator { fn drop(&mut self) { // SAFETY: deallocate using the pointer. The layout is saved, so it is the correct layout unsafe { std::alloc::dealloc(self.start.as_ptr(), self.layout); } } } unsafe impl std::alloc::Allocator for StaticPoolAllocator { #[inline(always)] fn allocate(&self, layout: std::alloc::Layout) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> { println!("allocate"); let size = layout.size(); if size > self.block_size { return Err(std::alloc::AllocError); } let head_ptr = unsafe { &mut *self.head.get() }; if let Some(block) = *head_ptr { let taken_block = block; unsafe { *head_ptr = block.as_ref().next }; let ptr = taken_block.cast::<u8>(); return Ok(std::ptr::NonNull::slice_from_raw_parts( ptr, self.block_size )); } Err(std::alloc::AllocError) } #[inline(always)] unsafe fn deallocate(&self, ptr: std::ptr::NonNull<u8>, _layout: std::alloc::Layout) { println!("deallocate"); let new_block = ptr.cast::<Block>(); // dereference raw pointer is unsafe let head_ptr = unsafe { &mut *self.head.get() }; unsafe { (*new_block.as_ptr()).next = *head_ptr }; *head_ptr = Some(new_block); } } fn main() { let pool_allocator = StaticPoolAllocator::new(10, 1024); let mut vec = Vec::with_capacity_in(1, &pool_allocator); let a: i32 = 10; vec.push(a); vec.push(a); println!("{}", vec[0]); }