|
| 1 | +use bevy::ecs::system::Resource; |
| 2 | +use bevy::reflect::{PartialReflect, Reflect}; |
| 3 | +use std::any::TypeId; |
| 4 | +use std::cell::UnsafeCell; |
| 5 | +use std::collections::HashMap; |
| 6 | +use std::fmt::{Display, Formatter}; |
| 7 | +use std::sync::Arc; |
| 8 | + |
| 9 | +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] |
| 10 | +pub struct ReflectAllocationId(pub(crate) usize); |
| 11 | +impl ReflectAllocationId { |
| 12 | + pub fn id(&self) -> usize { |
| 13 | + self.0 |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Clone, Debug)] |
| 18 | +pub struct ReflectAllocation(pub(self) Arc<UnsafeCell<dyn PartialReflect>>); |
| 19 | + |
| 20 | +unsafe impl Send for ReflectAllocation {} |
| 21 | +unsafe impl Sync for ReflectAllocation {} |
| 22 | + |
| 23 | +impl ReflectAllocation { |
| 24 | + pub fn get_ptr(&self) -> *mut dyn PartialReflect { |
| 25 | + self.0.get() |
| 26 | + } |
| 27 | + pub fn new(value: Arc<UnsafeCell<dyn PartialReflect>>) -> Self { |
| 28 | + Self(value) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl Display for ReflectAllocationId { |
| 33 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 34 | + write!(f, "{}", self.0) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// Allocator used to allocate and deallocate `dyn PartialReflect` values |
| 39 | +/// Used to be able to ensure we have a "common root" for values allocated outside the world. |
| 40 | +#[derive(Resource, Default)] |
| 41 | +pub struct ReflectAllocator { |
| 42 | + // TODO: experiment with object pools, sparse set etc. |
| 43 | + allocations: HashMap<ReflectAllocationId, ReflectAllocation>, |
| 44 | + types: HashMap<ReflectAllocationId, TypeId>, |
| 45 | +} |
| 46 | + |
| 47 | +impl ReflectAllocator { |
| 48 | + /// Allocates a new [`Reflect`] value and returns an [`AllocationId`] which can be used to access it later |
| 49 | + pub fn allocate<T: PartialReflect>( |
| 50 | + &mut self, |
| 51 | + value: T, |
| 52 | + ) -> (ReflectAllocationId, ReflectAllocation) { |
| 53 | + let id = ReflectAllocationId(self.allocations.len()); |
| 54 | + let value = ReflectAllocation::new(Arc::new(UnsafeCell::new(value))); |
| 55 | + self.allocations.insert(id, value.clone()); |
| 56 | + self.types.insert(id, TypeId::of::<T>()); |
| 57 | + (id, value) |
| 58 | + } |
| 59 | + |
| 60 | + pub fn get(&self, id: ReflectAllocationId) -> Option<ReflectAllocation> { |
| 61 | + self.allocations.get(&id).cloned() |
| 62 | + } |
| 63 | + |
| 64 | + pub fn get_type_id(&self, id: ReflectAllocationId) -> Option<TypeId> { |
| 65 | + self.types.get(&id).cloned() |
| 66 | + } |
| 67 | + |
| 68 | + pub fn get_mut(&self, id: ReflectAllocationId) -> Option<ReflectAllocation> { |
| 69 | + self.allocations.get(&id).cloned() |
| 70 | + } |
| 71 | + |
| 72 | + /// Deallocates the [`PartialReflect`] value with the given [`AllocationId`] |
| 73 | + pub fn deallocate(&mut self, id: ReflectAllocationId) { |
| 74 | + self.allocations.remove(&id); |
| 75 | + } |
| 76 | + |
| 77 | + /// Runs a garbage collection pass on the allocations, removing any allocations which have no more strong references |
| 78 | + /// Needs to be run periodically to prevent memory leaks |
| 79 | + pub fn clean_garbage_allocations(&mut self) { |
| 80 | + self.allocations.retain(|_, v| Arc::strong_count(&v.0) > 1); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod test { |
| 86 | + use super::*; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_reflect_allocator() { |
| 90 | + let mut allocator = ReflectAllocator::default(); |
| 91 | + let (id, val) = allocator.allocate(0); |
| 92 | + assert_eq!(allocator.allocations.len(), 1); |
| 93 | + drop(val); |
| 94 | + allocator.clean_garbage_allocations(); |
| 95 | + assert_eq!(allocator.allocations.len(), 0); |
| 96 | + } |
| 97 | +} |
0 commit comments