ferntree 0.5.0

Concurrent in-memory B+ Tree featuring optimistic lock coupling
Documentation
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Epoch-based memory reclamation with loom support.
//!
//! Under normal compilation, re-exports crossbeam_epoch.
//! Under `cfg(loom)`, provides a simplified mock implementation that
//! uses immediate reclamation, which is safe for loom's deterministic
//! execution model.
//!
//! # Loom Mock
//!
//! The loom mock uses `Arc<T>` internally to track allocations and
//! provides the same API surface as crossbeam_epoch, but with immediate
//! memory reclamation when no references remain.

// Allow unused items - some are only used under loom cfg
#![allow(unused)]

#[cfg(not(loom))]
pub use crossbeam_epoch::{pin, Atomic, Guard, Owned, Shared};

#[cfg(loom)]
mod mock_epoch {
	//! Mock epoch-based memory reclamation for loom testing.
	//!
	//! This provides a simplified version of crossbeam_epoch's API that
	//! uses reference counting for memory management. Under loom's
	//! deterministic execution model, this is sufficient for testing
	//! concurrent algorithms.

	use std::marker::PhantomData;
	use std::ptr::NonNull;
	use std::sync::atomic::Ordering;
	use std::sync::Arc;

	use loom::sync::atomic::AtomicPtr;

	/// A mock epoch guard. In the real implementation, this would pin
	/// the current thread to an epoch to prevent premature reclamation.
	/// In our mock, it's just a marker type.
	pub struct Guard {
		// Prevent Send/Sync to match crossbeam_epoch behavior
		_marker: PhantomData<*mut ()>,
	}

	impl Guard {
		fn new() -> Self {
			Guard {
				_marker: PhantomData,
			}
		}

		/// Defers destruction of the given shared pointer until it's safe.
		/// In our mock, we don't actually defer - the Arc handles cleanup.
		///
		/// # Safety
		///
		/// Marked `unsafe` to match the signature of `crossbeam_epoch::Guard::defer_destroy`
		/// so call sites compile identically under both real and loom builds.
		#[allow(clippy::missing_safety_doc)]
		pub unsafe fn defer_destroy<T>(&self, _ptr: Shared<'_, T>) {
			// Arc's Drop handles cleanup when all references are gone.
			// The Shared pointer doesn't own the data, so nothing to do here.
		}

		/// Flush deferred operations. No-op in our mock.
		pub fn flush(&self) {
			// No-op: we don't defer anything
		}

		/// Defers execution of the given closure. In our mock, executes
		/// immediately — loom serializes execution, so the difference is
		/// invisible to the test.
		pub fn defer<F, R>(&self, f: F)
		where
			F: FnOnce() -> R + Send + 'static,
		{
			let _ = f();
		}
	}

	/// Pin the current thread to the epoch. Returns a guard that must
	/// be held while accessing epoch-protected data.
	pub fn pin() -> Guard {
		Guard::new()
	}

	/// A shared reference to epoch-protected data.
	///
	/// This is similar to `&T` but can be null and is tied to an epoch guard.
	pub struct Shared<'g, T> {
		ptr: *const T,
		_marker: PhantomData<(&'g (), *const T)>,
	}

	impl<'g, T> Clone for Shared<'g, T> {
		fn clone(&self) -> Self {
			*self
		}
	}

	impl<'g, T> Copy for Shared<'g, T> {}

	impl<'g, T> Shared<'g, T> {
		/// Creates a null shared pointer.
		pub fn null() -> Self {
			Shared {
				ptr: std::ptr::null(),
				_marker: PhantomData,
			}
		}

		/// Returns true if this pointer is null.
		pub fn is_null(&self) -> bool {
			self.ptr.is_null()
		}

		/// Converts to a raw pointer.
		pub fn as_raw(&self) -> *const T {
			self.ptr
		}

		/// Dereferences the pointer.
		///
		/// # Safety
		///
		/// The pointer must be non-null and the data must be valid.
		pub unsafe fn deref(&self) -> &'g T {
			// SAFETY: Caller upholds non-null + valid data.
			unsafe { &*self.ptr }
		}

		/// Dereferences the pointer, returning None if null.
		///
		/// # Safety
		///
		/// If non-null, the data must be valid.
		pub unsafe fn as_ref(&self) -> Option<&'g T> {
			if self.is_null() {
				None
			} else {
				// SAFETY: Non-null branch; caller upholds data validity.
				Some(unsafe { &*self.ptr })
			}
		}

		/// Creates a Shared from a raw pointer.
		///
		/// # Safety
		///
		/// The pointer must be valid for the lifetime 'g or null.
		pub unsafe fn from_raw(ptr: *const T) -> Self {
			Shared {
				ptr,
				_marker: PhantomData,
			}
		}
	}

	/// An owned pointer to epoch-protected data.
	///
	/// This owns the data and can be converted to a Shared reference.
	pub struct Owned<T> {
		// We use Box internally for ownership
		data: NonNull<T>,
	}

	// SAFETY: Owned<T> can be sent across threads if T: Send
	unsafe impl<T: Send> Send for Owned<T> {}

	impl<T> Owned<T> {
		/// Creates a new owned pointer containing the given value.
		pub fn new(value: T) -> Self {
			let boxed = Box::new(value);
			Owned {
				data: NonNull::new(Box::into_raw(boxed)).unwrap(),
			}
		}

		/// Converts this owned pointer into a shared reference.
		pub fn into_shared<'g>(self, _guard: &'g Guard) -> Shared<'g, T> {
			let ptr = self.data.as_ptr();
			std::mem::forget(self); // Don't drop, ownership transfers to the atomic
			Shared {
				ptr,
				_marker: PhantomData,
			}
		}

		/// Returns a reference to the value.
		pub fn as_ref(&self) -> &T {
			// SAFETY: data is always valid
			unsafe { self.data.as_ref() }
		}

		/// Returns a mutable reference to the value.
		pub fn as_mut(&mut self) -> &mut T {
			// SAFETY: data is always valid and we have exclusive access
			unsafe { self.data.as_mut() }
		}
	}

	impl<T> Drop for Owned<T> {
		fn drop(&mut self) {
			// SAFETY: We own the data
			unsafe {
				drop(Box::from_raw(self.data.as_ptr()));
			}
		}
	}

	impl<T> std::ops::Deref for Owned<T> {
		type Target = T;
		fn deref(&self) -> &T {
			self.as_ref()
		}
	}

	impl<T> std::ops::DerefMut for Owned<T> {
		fn deref_mut(&mut self) -> &mut T {
			// SAFETY: data is always valid and we have exclusive access
			unsafe { self.data.as_mut() }
		}
	}

	/// An atomic pointer that can be safely shared between threads.
	pub struct Atomic<T> {
		ptr: AtomicPtr<T>,
		// Track whether we own the data (for cleanup in drop)
		owns_data: std::sync::atomic::AtomicBool,
	}

	// SAFETY: Atomic<T> can be sent/shared if T: Send + Sync
	unsafe impl<T: Send + Sync> Send for Atomic<T> {}
	unsafe impl<T: Send + Sync> Sync for Atomic<T> {}

	impl<T> std::fmt::Debug for Atomic<T> {
		fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
			let ptr = self.ptr.load(Ordering::SeqCst);
			f.debug_struct("Atomic").field("ptr", &ptr).finish()
		}
	}

	impl<T> Atomic<T> {
		/// Creates a new atomic pointer initialized to null.
		pub fn null() -> Self {
			Atomic {
				ptr: AtomicPtr::new(std::ptr::null_mut()),
				owns_data: std::sync::atomic::AtomicBool::new(false),
			}
		}

		/// Creates a new atomic pointer containing the given value.
		pub fn new(value: T) -> Self {
			let boxed = Box::new(value);
			Atomic {
				ptr: AtomicPtr::new(Box::into_raw(boxed)),
				owns_data: std::sync::atomic::AtomicBool::new(true),
			}
		}

		/// Loads the current value.
		pub fn load<'g>(&self, order: Ordering, _guard: &'g Guard) -> Shared<'g, T> {
			let ptr = self.ptr.load(order);
			Shared {
				ptr,
				_marker: PhantomData,
			}
		}

		/// Stores a new value.
		pub fn store(&self, new: Shared<'_, T>, order: Ordering) {
			self.ptr.store(new.ptr as *mut T, order);
		}

		/// Swaps the current value with a new one, returning the old value.
		pub fn swap<'g>(
			&self,
			new: Shared<'_, T>,
			order: Ordering,
			_guard: &'g Guard,
		) -> Shared<'g, T> {
			let old = self.ptr.swap(new.ptr as *mut T, order);
			Shared {
				ptr: old,
				_marker: PhantomData,
			}
		}

		/// Compares and exchanges the value.
		pub fn compare_exchange<'g>(
			&self,
			current: Shared<'_, T>,
			new: Shared<'_, T>,
			success: Ordering,
			failure: Ordering,
			_guard: &'g Guard,
		) -> Result<Shared<'g, T>, CompareExchangeError<'g, T, Shared<'g, T>>> {
			match self.ptr.compare_exchange(
				current.ptr as *mut T,
				new.ptr as *mut T,
				success,
				failure,
			) {
				Ok(ptr) => Ok(Shared {
					ptr,
					_marker: PhantomData,
				}),
				Err(ptr) => Err(CompareExchangeError {
					current: Shared {
						ptr,
						_marker: PhantomData,
					},
					new: Shared {
						ptr: new.ptr,
						_marker: PhantomData,
					},
				}),
			}
		}

		/// Compares and exchanges the value, storing an owned value on success.
		pub fn compare_exchange_owned<'g>(
			&self,
			current: Shared<'_, T>,
			new: Owned<T>,
			success: Ordering,
			failure: Ordering,
			_guard: &'g Guard,
		) -> Result<Shared<'g, T>, CompareExchangeError<'g, T, Owned<T>>> {
			let new_ptr = new.data.as_ptr();
			match self.ptr.compare_exchange(current.ptr as *mut T, new_ptr, success, failure) {
				Ok(ptr) => {
					std::mem::forget(new); // Ownership transferred to atomic
					Ok(Shared {
						ptr,
						_marker: PhantomData,
					})
				}
				Err(ptr) => Err(CompareExchangeError {
					current: Shared {
						ptr,
						_marker: PhantomData,
					},
					new,
				}),
			}
		}

		/// Gets a mutable reference to the underlying pointer.
		/// This is only safe when there are no concurrent accesses.
		/// Note: Under loom, this uses relaxed load which is safe for single-threaded cleanup.
		pub fn get_mut(&mut self) -> Option<&mut T> {
			let ptr = self.ptr.load(Ordering::Relaxed);
			if ptr.is_null() {
				None
			} else {
				// SAFETY: We have exclusive access through &mut self
				unsafe { Some(&mut *ptr) }
			}
		}

		/// Takes the value out, leaving null in its place.
		/// This is only safe when there are no concurrent accesses.
		pub fn take(&mut self) -> Option<Box<T>> {
			let ptr = self.ptr.swap(std::ptr::null_mut(), Ordering::Relaxed);
			if ptr.is_null() {
				None
			} else {
				self.owns_data.store(false, std::sync::atomic::Ordering::Relaxed);
				// SAFETY: We have exclusive access and the pointer is valid
				unsafe { Some(Box::from_raw(ptr)) }
			}
		}
	}

	impl<T> Default for Atomic<T> {
		fn default() -> Self {
			Self::null()
		}
	}

	impl<T> Drop for Atomic<T> {
		fn drop(&mut self) {
			if self.owns_data.load(std::sync::atomic::Ordering::Relaxed) {
				let ptr = self.ptr.load(Ordering::Relaxed);
				if !ptr.is_null() {
					// SAFETY: We own the data
					unsafe {
						drop(Box::from_raw(ptr));
					}
				}
			}
		}
	}

	// Implement From<Owned> for Atomic to match crossbeam_epoch API
	impl<T> From<Owned<T>> for Atomic<T> {
		fn from(owned: Owned<T>) -> Self {
			let ptr = owned.data.as_ptr();
			std::mem::forget(owned);
			Atomic {
				ptr: AtomicPtr::new(ptr),
				owns_data: std::sync::atomic::AtomicBool::new(true),
			}
		}
	}

	// Implement From<Shared> for Atomic to match crossbeam_epoch API
	impl<T> From<Shared<'_, T>> for Atomic<T> {
		fn from(shared: Shared<'_, T>) -> Self {
			Atomic {
				ptr: AtomicPtr::new(shared.ptr as *mut T),
				owns_data: std::sync::atomic::AtomicBool::new(false),
			}
		}
	}

	/// Error returned by compare_exchange operations.
	pub struct CompareExchangeError<'g, T, N> {
		/// The current value that was found instead of the expected value.
		pub current: Shared<'g, T>,
		/// The new value that was not stored.
		pub new: N,
	}
}

#[cfg(loom)]
pub use mock_epoch::*;