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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use crate::error::{DbError, DbResult};
/// In-memory bitmap for tracking occupied/free slots.
///
/// Each bit represents one slot: 1 = occupied, 0 = free.
/// Backed by `Vec<u64>` — 64 slots per word.
pub struct Bitmap {
/// Bit storage: 1 = occupied, 0 = free.
words: Vec<u64>,
/// Total number of logical slots.
count: u32,
/// Number of currently occupied slots.
occupied: u32,
/// Index of the first word that *might* contain a free bit.
/// Invariant: all words before this index are `u64::MAX`.
first_free_hint: usize,
}
#[inline]
const fn words_for(bits: u32) -> usize {
bits.div_ceil(64) as usize
}
#[allow(dead_code)]
impl Bitmap {
/// Create a bitmap with `count` slots, all free.
pub fn new(count: u32) -> Self {
let n = words_for(count);
Self {
words: vec![0u64; n],
count,
occupied: 0,
first_free_hint: 0,
}
}
/// Find the first free slot, mark it occupied, and return its id.
///
/// Returns `Err(SlotsFull)` when every slot is occupied.
pub fn alloc(&mut self) -> DbResult<u32> {
let len = self.words.len();
for i in self.first_free_hint..len {
let word = self.words[i];
if word != u64::MAX {
let bit = (!word).trailing_zeros(); // first 0-bit
let slot_id = i as u32 * 64 + bit;
if slot_id >= self.count {
// Trailing bits beyond `count` — no real slot here.
break;
}
self.words[i] |= 1u64 << bit;
self.occupied += 1;
// If this word is now full, future scans can skip it.
if self.words[i] == u64::MAX {
self.first_free_hint = i + 1;
}
return Ok(slot_id);
}
}
Err(DbError::SlotsFull)
}
/// Mark `slot_id` as occupied.
///
/// # Panics
/// Panics if `slot_id >= count`.
pub fn set(&mut self, slot_id: u32) {
assert!(slot_id < self.count, "slot_id out of range");
let (wi, bit) = (slot_id as usize / 64, slot_id % 64);
let mask = 1u64 << bit;
if self.words[wi] & mask == 0 {
self.words[wi] |= mask;
self.occupied += 1;
}
}
/// Mark `slot_id` as free.
///
/// # Panics
/// Panics if `slot_id >= count`.
pub fn clear(&mut self, slot_id: u32) {
assert!(slot_id < self.count, "slot_id out of range");
let (wi, bit) = (slot_id as usize / 64, slot_id % 64);
let mask = 1u64 << bit;
if self.words[wi] & mask != 0 {
self.words[wi] &= !mask;
self.occupied -= 1;
if wi < self.first_free_hint {
self.first_free_hint = wi;
}
}
}
/// Returns `true` if `slot_id` is occupied.
///
/// # Panics
/// Panics if `slot_id >= count`.
pub fn is_set(&self, slot_id: u32) -> bool {
assert!(slot_id < self.count, "slot_id out of range");
let (wi, bit) = (slot_id as usize / 64, slot_id % 64);
self.words[wi] & (1u64 << bit) != 0
}
/// Number of currently occupied slots.
pub fn occupied(&self) -> u32 {
self.occupied
}
/// Total number of logical slots.
pub fn count(&self) -> u32 {
self.count
}
/// Returns `true` when every slot is occupied.
pub fn is_full(&self) -> bool {
self.occupied == self.count
}
/// Grow the bitmap to hold at least `new_count` slots.
///
/// # Panics
/// Panics if `new_count < count` (shrinking is not supported).
pub fn grow(&mut self, new_count: u32) {
assert!(
new_count >= self.count,
"cannot shrink bitmap: {} -> {}",
self.count,
new_count
);
let new_words = words_for(new_count);
self.words.resize(new_words, 0u64);
self.count = new_count;
}
/// Serialize the bitmap words as a byte slice (little-endian).
pub fn as_bytes(&self) -> &[u8] {
// SAFETY: `u64` has no padding; its natural alignment (8) is >= alignment of u8.
unsafe {
std::slice::from_raw_parts(
self.words.as_ptr() as *const u8,
self.words.len() * std::mem::size_of::<u64>(),
)
}
}
/// Deserialize a bitmap from raw bytes and a logical slot count.
///
/// Recomputes `occupied` and `first_free_hint` from the data.
///
/// # Panics
/// Panics if `data.len()` is not a multiple of 8 or does not have
/// enough words for `count`.
pub fn from_bytes(data: &[u8], count: u32) -> Self {
assert!(
data.len().is_multiple_of(8),
"data length must be a multiple of 8"
);
let n_words = data.len() / 8;
assert!(
n_words >= words_for(count),
"not enough words for {count} slots"
);
let mut words = vec![0u64; n_words];
// SAFETY: copying aligned-enough bytes into Vec<u64>.
unsafe {
std::ptr::copy_nonoverlapping(data.as_ptr(), words.as_mut_ptr() as *mut u8, data.len());
}
// Mask out any trailing bits beyond `count` so they don't
// pollute the occupied counter.
let tail = count % 64;
if tail != 0 {
let last = words_for(count) - 1;
words[last] &= (1u64 << tail) - 1;
}
let mut occupied: u32 = 0;
let mut first_free_hint: usize = n_words; // assume full
for (i, &w) in words.iter().enumerate() {
occupied += w.count_ones();
if first_free_hint == n_words && w != u64::MAX {
first_free_hint = i;
}
}
// If all words are full but occupied < count, the last word has
// free tail bits — handle that edge via the general hint logic:
// `first_free_hint` is already set correctly because the masked
// last word won't be MAX when tail bits exist.
Self {
words,
count,
occupied,
first_free_hint,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_alloc_sequential() {
let mut bm = Bitmap::new(128);
for i in 0..128 {
assert_eq!(bm.alloc().unwrap(), i);
}
assert!(bm.is_full());
assert_eq!(bm.occupied(), 128);
assert!(bm.alloc().is_err());
}
#[test]
fn test_free_and_realloc() {
let mut bm = Bitmap::new(64);
let a = bm.alloc().unwrap();
let b = bm.alloc().unwrap();
let c = bm.alloc().unwrap();
assert_eq!((a, b, c), (0, 1, 2));
// Free the middle slot.
bm.clear(b);
assert_eq!(bm.occupied(), 2);
assert!(!bm.is_set(b));
// Next alloc should reuse slot 1.
let reused = bm.alloc().unwrap();
assert_eq!(reused, b);
assert_eq!(bm.occupied(), 3);
}
#[test]
fn test_grow() {
let mut bm = Bitmap::new(64);
for _ in 0..64 {
bm.alloc().unwrap();
}
assert!(bm.is_full());
bm.grow(128);
assert!(!bm.is_full());
assert_eq!(bm.count(), 128);
let slot = bm.alloc().unwrap();
assert_eq!(slot, 64);
}
#[test]
fn test_serde_roundtrip() {
let mut bm = Bitmap::new(200);
bm.set(0);
bm.set(63);
bm.set(64);
bm.set(127);
bm.set(199);
let bytes = bm.as_bytes().to_vec();
let restored = Bitmap::from_bytes(&bytes, 200);
assert_eq!(restored.count(), 200);
assert_eq!(restored.occupied(), 5);
assert!(restored.is_set(0));
assert!(restored.is_set(63));
assert!(restored.is_set(64));
assert!(restored.is_set(127));
assert!(restored.is_set(199));
assert!(!restored.is_set(1));
assert!(!restored.is_set(100));
}
#[test]
fn test_set_idempotent() {
let mut bm = Bitmap::new(64);
bm.set(5);
bm.set(5);
assert_eq!(bm.occupied(), 1);
}
#[test]
fn test_clear_idempotent() {
let mut bm = Bitmap::new(64);
bm.set(5);
bm.clear(5);
bm.clear(5);
assert_eq!(bm.occupied(), 0);
}
#[test]
#[should_panic(expected = "slot_id out of range")]
fn test_set_out_of_range() {
let mut bm = Bitmap::new(64);
bm.set(64);
}
#[test]
fn test_non_aligned_count() {
// Count not a multiple of 64.
let mut bm = Bitmap::new(100);
for i in 0..100 {
assert_eq!(bm.alloc().unwrap(), i);
}
assert!(bm.is_full());
assert!(bm.alloc().is_err());
}
}