mtb_entity_slab/
lib.rs

1//! # MTB Entity Allocator and Utilities
2//!
3//! Address-stable, internally mutable entity Slab allocator. Mainly solves the problem
4//! where `Slab` cannot allocate new elements while reading existing ones.
5//!
6//! 地址稳定、内部可变的实体 Slab 分块分配器. 主要解决 `Slab` 无法在读取元素的同时
7//! 分配新元素的问题.
8//!
9//! ## Example 使用示例
10//!
11//! ```
12//! use mtb_entity_slab::*;
13//!
14//! #[derive(Debug, Clone, PartialEq, Eq)]
15//! struct Inst {
16//!     pub opcode: u32,
17//!     pub operands: [u64; 4],
18//!     pub heap_data: String,
19//! }
20//!
21//! impl Inst {
22//!     fn new(opcode: u32) -> Self {
23//!         Self {
24//!             opcode,
25//!             operands: [0; 4],
26//!             heap_data: format!("InstData{}", opcode),
27//!         }
28//!     }
29//! }
30//!
31//! fn main() {
32//!     let mut alloc: EntityAlloc<Inst> = EntityAlloc::with_capacity(1024);
33//!     let ptrs = {
34//!         let mut v = Vec::new();
35//!         for i in 0..1000 {
36//!             let ptr = alloc.allocate(Inst::new(i));
37//!             v.push(ptr);
38//!         }
39//!         v
40//!     };
41//!
42//!     let inst = ptrs[500].deref(&alloc);
43//!
44//!     // Allocates a new element while reading existing ones
45//!     // 在读取时分配新元素
46//!     let new_id = alloc.allocate(Inst::new(2000));
47//!     assert_eq!(inst.opcode, 500);
48//!     assert_eq!(new_id.deref(&alloc).opcode, 2000);
49//!
50//!     for &ptr in ptrs.iter() {
51//!         let inst = ptr.deref(&alloc);
52//!         let indexed_id = ptr.get_index(&alloc).unwrap();
53//!         assert_eq!(inst.opcode as usize, indexed_id);
54//!         assert!(ptr.free(&mut alloc).is_some());
55//!     }
56//! }
57//! ```
58//!
59//! ## Safety 安全性
60//!
61//! This crate provides access to allocated elements via `PtrID`. `PtrID` contains a raw pointer,
62//! and most operations on `PtrID` are safe, but there is no ABA usage detection.
63//!
64//! This crate is designed for Remusys-IR and is for learning purposes only. It does not guarantee
65//! safety in all scenarios. Please carefully assess the risks when using it in production environments.
66//!
67//! 本库通过 `PtrID` 提供对分配元素的访问. `PtrID` 内部包含裸指针, 对 PtrID 的大多数操作都是安全的,
68//! 但没有 ABA 使用检测.
69//!
70//! 本库为 Remusys-IR 设计, 仅为学习用途, 不保证在所有场景下的安全性. 在生产环境中使用时请谨慎评估风险.
71
72mod alloc;
73mod bitalloc;
74mod chunk;
75mod container;
76mod id;
77mod iter;
78mod policy;
79
80pub use crate::{
81    alloc::EntityAlloc,
82    container::ptrlist::*,
83    id::{IEntityAllocID, IPolicyPtrID, IndexedID, PtrID},
84    iter::{EntityAllocConsumeIter, EntityAllocEditIter, EntityAllocReadIter},
85    policy::{
86        AllocPolicy128, AllocPolicy256, AllocPolicy512, AllocPolicy1024, AllocPolicy2048,
87        AllocPolicy4096, IAllocPolicy, SlicePtrError,
88    },
89};
90pub use mtb_entity_slab_macros::entity_ptr_id;
91
92pub const MTBLIB_PACKAGE_NAME: &str = "io.medihbt.mtb.entity";
93pub const MTBLIB_PACKAGE_VERSION: &str = env!("CARGO_PKG_VERSION");
94pub const NULL_INDEXED_ID: usize = usize::MAX;
95
96mod tests;