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

推荐订阅源

Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
J
Java Code Geeks
L
LangChain Blog
V
V2EX
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
博客园 - Franky
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
博客园 - 司徒正美
B
Blog
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog

The Rust Programming Language Forum - Latest posts

What's everyone working on this week (21/2026)? Rust Function Call with one Param Iced + Canvas + Text: How do I create a style for canvas text "Value dropped while borrowed" as a lifetime mismatch Weird `use of moved value` behaviour Slightly surprising behavior of a while loop Clap: how to disable options after a certain positional argument How do i tell rust-analyzer what kind of bracket to use for a macro? Iterator + Borrow Checker: Work around borrowing already borrowed mutable variable Requesting data via mavlink SKILLS.md for Rust development 🧵 Stringlet UTF-8 Hack Option Niche? Iterator + Map - How do divide every element, including the last element, by the last element Why can't we store returned value of a function in a variable if one of the parameters goes out of scope My first crate :D, floop: A more convenient and less error prone replacement for loop `{ select! { .. }}` Sorting is slow because architectures are wrong — zan-sort redesigns the architecture, not the algorithm Iced Font - Create a custom monospace font to display dollar amounts Is the UnsafePinned RFC wrong about being able to return `&mut T` from `get_mut_unchecked`? Fixing Polars 0.37 compilation errors: Hashbrown 0.17 dependency conflict and decimal parsing Any plans for improving error diagnostic in Rust 2.0? How do I build completely offline? Is `&mut T -> &mut ManuallyDrop<T>` well-defined and sound? Would you use this? — fixtura, declarative fake data injection for tests Foreign trait restrictions on native types make generics hard to use Cargo Exclude Directive Compiler reasoning around a modulo counter Multiple mutable references to elements within one vector Handling non-`Send` data in a `Send` closure Review: Static Multi Pool Allocator Rmquickjs - High-level MicroQuickJS bindings for Rust
Code review for Static Pool Allocator
@fuji-184 Fu · 2026-04-24 · via The Rust Programming Language Forum - Latest posts
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]); }