|
| 1 | +//! GPU and CPU slab allocation. |
| 2 | +//! |
| 3 | +//! Re-exports [`Array`], [`Id`], [`Slab`] and [`SlabItem`] from |
| 4 | +//! [`crabslab`](https://docs.rs/crabslab/latest/crabslab/). |
| 5 | +//! |
| 6 | +//! User types can automatically derive `SlabItem` in most cases. It is |
| 7 | +//! required that your type's fields all implement `SlabItem` and `crabslab` |
| 8 | +//! must be in scope. |
| 9 | +//! |
| 10 | +//! ``` |
| 11 | +//! use renderling::slab::SlabItem; |
| 12 | +//! |
| 13 | +//! #[derive(Clone, Copy, SlabItem)] |
| 14 | +//! struct UserData { |
| 15 | +//! pos: (f32, f32, f32), |
| 16 | +//! acc: (f32, f32, f32), |
| 17 | +//! } |
| 18 | +//! ``` |
| 19 | +
|
| 20 | +pub mod range; |
| 21 | +pub mod runtime; |
| 22 | +pub mod slab; |
| 23 | +pub mod value; |
| 24 | + |
| 25 | +pub mod prelude { |
| 26 | + //! Easy-include prelude module. |
| 27 | +
|
| 28 | + pub use super::runtime::*; |
| 29 | + pub use super::slab::*; |
| 30 | + pub use super::value::*; |
| 31 | +} |
| 32 | + |
| 33 | +// #[cfg(feature = "wgpu")] |
| 34 | +// mod wgpu_slab; |
| 35 | +// #[cfg(feature = "wgpu")] |
| 36 | +// pub use wgpu_slab::*; |
| 37 | + |
| 38 | +// #[cfg(test)] |
| 39 | +// mod test { |
| 40 | +// pub use crabslab::{Array, Id, Slab, SlabItem}; |
| 41 | + |
| 42 | +// use crate::slab::SlabAllocator; |
| 43 | + |
| 44 | +// #[test] |
| 45 | +// fn mngr_updates_count_sanity() { |
| 46 | +// let mngr = SlabAllocator::<Mutex<Vec<u32>>>::default(); |
| 47 | +// { |
| 48 | +// let value = mngr.new_value(666u32); |
| 49 | +// assert_eq!( |
| 50 | +// 1, |
| 51 | +// value.ref_count(), |
| 52 | +// "slab should not retain a count on value" |
| 53 | +// ); |
| 54 | +// } |
| 55 | +// let _ = mngr.upkeep(()); |
| 56 | +// assert_eq!( |
| 57 | +// 0, |
| 58 | +// mngr.update_sources.read().unwrap().len(), |
| 59 | +// "value should have dropped with no refs" |
| 60 | +// ); |
| 61 | +// { |
| 62 | +// let values = mngr.new_array([666u32, 420u32]); |
| 63 | +// assert_eq!( |
| 64 | +// 1, |
| 65 | +// values.ref_count(), |
| 66 | +// "slab should not retain a count on array" |
| 67 | +// ); |
| 68 | +// } |
| 69 | +// let _ = mngr.upkeep(()); |
| 70 | +// assert_eq!( |
| 71 | +// 0, |
| 72 | +// mngr.update_sources.read().unwrap().len(), |
| 73 | +// "array should have dropped with no refs" |
| 74 | +// ); |
| 75 | +// } |
| 76 | + |
| 77 | +// #[test] |
| 78 | +// fn range_sanity() { |
| 79 | +// let a = Range { |
| 80 | +// first_index: 1, |
| 81 | +// last_index: 2, |
| 82 | +// }; |
| 83 | +// let b = Range { |
| 84 | +// first_index: 0, |
| 85 | +// last_index: 0, |
| 86 | +// }; |
| 87 | +// assert!(!a.intersects(&b)); |
| 88 | +// assert!(!b.intersects(&a)); |
| 89 | +// } |
| 90 | + |
| 91 | +// #[test] |
| 92 | +// fn slab_manager_sanity() { |
| 93 | +// let m = SlabAllocator::<Mutex<Vec<u32>>>::default(); |
| 94 | +// log::info!("allocating 4 unused u32 slots"); |
| 95 | +// let _ = m.allocate::<u32>(); |
| 96 | +// let _ = m.allocate::<u32>(); |
| 97 | +// let _ = m.allocate::<u32>(); |
| 98 | +// let _ = m.allocate::<u32>(); |
| 99 | + |
| 100 | +// log::info!("creating 4 update sources"); |
| 101 | +// let h4 = m.new_value(0u32); |
| 102 | +// let h5 = m.new_value(0u32); |
| 103 | +// let h6 = m.new_value(0u32); |
| 104 | +// let h7 = m.new_value(0u32); |
| 105 | +// log::info!("running upkeep"); |
| 106 | +// let _ = m.upkeep(()); |
| 107 | +// assert!(m.recycles.read().unwrap().ranges.is_empty()); |
| 108 | +// assert_eq!(4, m.update_sources.read().unwrap().len()); |
| 109 | +// let k = m.update_k.load(Ordering::Relaxed); |
| 110 | +// assert_eq!(4, k); |
| 111 | + |
| 112 | +// log::info!("dropping 4 update sources"); |
| 113 | +// drop(h4); |
| 114 | +// drop(h5); |
| 115 | +// drop(h6); |
| 116 | +// drop(h7); |
| 117 | +// let _ = m.upkeep(()); |
| 118 | +// assert_eq!(1, m.recycles.read().unwrap().ranges.len()); |
| 119 | +// assert!(m.update_sources.read().unwrap().is_empty()); |
| 120 | + |
| 121 | +// log::info!("creating 4 update sources, round two"); |
| 122 | +// let h4 = m.new_value(0u32); |
| 123 | +// let h5 = m.new_value(0u32); |
| 124 | +// let h6 = m.new_value(0u32); |
| 125 | +// let h7 = m.new_value(0u32); |
| 126 | +// assert!(m.recycles.read().unwrap().ranges.is_empty()); |
| 127 | +// assert_eq!(4, m.update_sources.read().unwrap().len()); |
| 128 | +// let k = m.update_k.load(Ordering::Relaxed); |
| 129 | +// // MAYBE_TODO: recycle "update_k"s instead of incrementing for each new source |
| 130 | +// assert_eq!(8, k); |
| 131 | + |
| 132 | +// log::info!("creating one more update source, immediately dropping it and two others"); |
| 133 | +// let h8 = m.new_value(0u32); |
| 134 | +// drop(h8); |
| 135 | +// drop(h4); |
| 136 | +// drop(h6); |
| 137 | +// let _ = m.upkeep(()); |
| 138 | +// assert_eq!(3, m.recycles.read().unwrap().ranges.len()); |
| 139 | +// assert_eq!(2, m.update_sources.read().unwrap().len()); |
| 140 | +// assert_eq!(9, m.update_k.load(Ordering::Relaxed)); |
| 141 | + |
| 142 | +// drop(h7); |
| 143 | +// drop(h5); |
| 144 | +// let _ = m.upkeep(()); |
| 145 | +// m.defrag(); |
| 146 | +// assert_eq!( |
| 147 | +// 1, |
| 148 | +// m.recycles.read().unwrap().ranges.len(), |
| 149 | +// "ranges: {:#?}", |
| 150 | +// m.recycles.read().unwrap().ranges |
| 151 | +// ); |
| 152 | +// } |
| 153 | +// } |
0 commit comments