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
//! Allocation cache for owned objects. // FIXME: add a limit to the cache? /// Cache for owned objects. /// /// Useful if fast allocation/deallocation of small owned objects is needed. pub struct OwnedAllocationCache<T> { priv cache: ~[~T] } impl<T> OwnedAllocationCache<T> { /// Initializes the cache. #[inline] pub fn new() -> OwnedAllocationCache<T> { OwnedAllocationCache { cache: ~[] } } /// Box a value into a potentially already allocated box. #[inline] pub fn alloc(&mut self, value: T) -> ~T { if !self.cache.is_empty() { let mut res = self.cache.pop(); *res = value; res } else { ~value } } /// Retains a box which can be re-used by the `box` method. #[inline] pub fn retain(&mut self, elem: ~T) { self.cache.push(elem) } /// Clears the cache, destroying any stored pointer. #[inline] pub fn clear(&mut self) { self.cache.clear() } }