1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! A lock-free, cache-line-aware concurrent table.
//!
//! `ptab` provides [`PTab`], a fixed-capacity table for storing entries and
//! accessing them by opaque indices. It is optimized for read-heavy workloads
//! where entries are looked up frequently from multiple threads simultaneously.
//!
//! # Overview
//!
//! The table assigns each inserted value a [`Detached`] index that uniquely
//! identifies that entry. This index can be used to look up, access, or remove
//! the entry. Indices include a generational component to mitigate the
//! [ABA problem] in concurrent algorithms.
//!
//! # Usage
//!
//! ```
//! use ptab::PTab;
//!
//! // Create a table with default capacity
//! let table: PTab<String> = PTab::new();
//!
//! // Insert an entry and get its index
//! let index = table.insert("hello".to_string()).unwrap();
//!
//! // Access the entry by index
//! let greeting = table.with(index, |s| s.to_uppercase());
//! assert_eq!(greeting, Some("HELLO".to_string()));
//!
//! // Remove the entry
//! assert!(table.remove(index));
//!
//! // The entry is gone
//! assert!(!table.exists(index));
//! ```
//!
//! # Configuration
//!
//! Table capacity is configured at compile time through the [`Params`] trait.
//! The default configuration ([`DefaultParams`]) provides [`Capacity::DEF`]
//! slots:
//!
//! ```
//! use ptab::{PTab, DefaultParams};
//!
//! // These are equivalent:
//! let table1: PTab<u64> = PTab::new();
//! let table2: PTab<u64, DefaultParams> = PTab::new();
//! ```
//!
//! For custom capacities, use [`ConstParams`]:
//!
//! ```
//! use ptab::{PTab, ConstParams};
//!
//! let table: PTab<u64, ConstParams<512>> = PTab::new();
//! assert_eq!(table.capacity(), 512);
//! ```
//!
//! Capacity is always rounded up to the nearest power of two and clamped
//! to the range <code>[Capacity::MIN]..=[Capacity::MAX]</code>.
//!
//! # Concurrency
//!
//! All operations on [`PTab`] are thread-safe and lock-free. Multiple threads
//! can concurrently insert, remove, and access entries without blocking.
//!
//! ```no_run
//! use ptab::{PTab, ConstParams};
//! use std::sync::Arc;
//! use std::thread;
//!
//! let table: Arc<PTab<u64, ConstParams<1024>>> = Arc::new(PTab::new());
//!
//! let handles: Vec<_> = (0..4)
//! .map(|thread_id| {
//! let table = Arc::clone(&table);
//! thread::spawn(move || {
//! for i in 0..100 {
//! if let Some(idx) = table.insert(thread_id * 1000 + i) {
//! table.remove(idx);
//! }
//! }
//! })
//! })
//! .collect();
//!
//! for handle in handles {
//! handle.join().unwrap();
//! }
//! ```
//!
//! ## Memory Reclamation
//!
//! Removed entries are reclaimed using a pluggable memory management strategy.
//! By default, epoch-based reclamation via [`sdd`] ensures safe concurrent
//! access. In no-std environments, a leak-based fallback is available.
//!
//! See [`memory`] for reclamation trait details and custom implementations.
//!
//! # Memory Layout
//!
//! The table uses a cache-line-aware memory layout to minimize false sharing
//! between threads. Consecutive allocations are distributed across different
//! cache lines, reducing contention when multiple threads operate on
//! recently-allocated entries. See [`CACHE_LINE_SLOTS`] for the distribution
//! stride.
//!
//! # Capacity Limits
//!
//! Capacity is bounded by [`Capacity::MIN`] and [`Capacity::MAX`]. The default
//! is [`Capacity::DEF`]. When full, [`PTab::insert()`] returns [`None`].
//!
//! [Capacity::MAX]: crate::params::Capacity::MAX
//! [Capacity::MIN]: crate::params::Capacity::MIN
//! [`CACHE_LINE_SLOTS`]: crate::params::CACHE_LINE_SLOTS
//! [`Capacity::DEF`]: crate::params::Capacity::DEF
//! [`Capacity::MAX`]: crate::params::Capacity::MAX
//! [`Capacity::MIN`]: crate::params::Capacity::MIN
//! [`ConstParams`]: crate::params::ConstParams
//! [`DefaultParams`]: crate::params::DefaultParams
//! [`Params`]: crate::params::Params
//! [`PTab::insert()`]: crate::public::PTab::insert
//! [`memory`]: crate::memory
//!
//! [ABA problem]: https://en.wikipedia.org/wiki/ABA_problem
//! [`sdd`]: https://docs.rs/sdd
//!
extern crate alloc as rust_alloc;
compile_error!;
pub use cratealloc;
pub use cratesync;
pub use Capacity;
pub use ConstParams;
pub use DefaultParams;
pub use Params;
pub use Detached;
pub use PTab;
pub use WeakKeys;