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::{EntityAlloc, PtrID, IEntityAllocID, IEntityAllocatable, EntityAllocPolicy256};
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//! impl IEntityAllocatable for Inst {
21//! /// Allocation policy type. You can switch types from 128 to 4096 bytes.
22//! /// 分配策略类型. 可在 128 到 4096 字节间切换.
23//! type AllocatePolicyT = EntityAllocPolicy256<Self>;
24//!
25//! /// Pointer ID type. You can define your own PtrID type if needed.
26//! /// 指针 ID 类型. 如有需要, 可自定义 PtrID 类型.
27//! type PtrID = PtrID<Self>;
28//! }
29//!
30//! impl Inst {
31//! fn new(opcode: u32) -> Self {
32//! Self {
33//! opcode,
34//! operands: [0; 4],
35//! heap_data: format!("InstData{}", opcode),
36//! }
37//! }
38//! }
39//!
40//! fn main() {
41//! let mut alloc = EntityAlloc::with_capacity(1024);
42//! let ptrs = {
43//! let mut v = Vec::new();
44//! for i in 0..1000 {
45//! let ptr = alloc.allocate(Inst::new(i));
46//! v.push(ptr);
47//! }
48//! v
49//! };
50//!
51//! let inst = ptrs[500].deref(&alloc);
52//!
53//! // Allocates a new element while reading existing ones
54//! // 在读取时分配新元素
55//! let new_id = alloc.allocate(Inst::new(2000));
56//! assert_eq!(inst.opcode, 500);
57//! assert_eq!(new_id.deref(&alloc).opcode, 2000);
58//!
59//! for &ptr in ptrs.iter() {
60//! let inst = ptr.deref(&alloc);
61//! let indexed_id = ptr.as_indexed(&alloc).unwrap();
62//! assert_eq!(inst.opcode as usize, indexed_id.0);
63//! assert!(ptr.free(&mut alloc).is_some());
64//! }
65//! }
66//! ```
67//!
68//! ## Safety 安全性
69//!
70//! This crate provides access to allocated elements via `PtrID`. `PtrID` contains a raw pointer,
71//! and most operations on `PtrID` are safe, but there is no ABA usage detection.
72//!
73//! This crate is designed for Remusys-IR and is for learning purposes only. It does not guarantee
74//! safety in all scenarios. Please carefully assess the risks when using it in production environments.
75//!
76//! 本库通过 `PtrID` 提供对分配元素的访问. `PtrID` 内部包含裸指针, 对 PtrID 的大多数操作都是安全的,
77//! 但没有 ABA 使用检测.
78//!
79//! 本库为 Remusys-IR 设计, 仅为学习用途, 不保证在所有场景下的安全性. 在生产环境中使用时请谨慎评估风险.
80
81mod alloc;
82mod bitalloc;
83mod chunk;
84mod container;
85mod id;
86mod iter;
87mod policy;
88
89pub use crate::{
90 alloc::{EntityAlloc, IEntityAllocatable},
91 chunk::NULL_INDEXED_ID,
92 container::ptrlist::*,
93 id::{IDProxy, IEntityAllocID, IndexedID, PtrID},
94 iter::{EntityAllocConsumeIter, EntityAllocEditIter, EntityAllocReadIter},
95 policy::*,
96};
97
98pub const MTBLIB_PACKAGE_NAME: &str = "io.medihbt.mtb.entity";
99pub const MTBLIB_PACKAGE_VERSION: &str = env!("CARGO_PKG_VERSION");
100
101mod tests;