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
use std; use super::{SliceWrapper, SliceWrapperMut, Allocator}; use std::ops; use std::ops::Range; use std::boxed::Box; use std::vec::Vec; pub struct WrapBox<T>(std::boxed::Box<[T]>); impl<T> From<Vec<T>> for WrapBox<T> { fn from(data: Vec<T>) -> Self { WrapBox(data.into_boxed_slice()) } } impl<T> Into<Box<[T]>> for WrapBox<T> { fn into(self) -> Box<[T]> { self.0 } } impl<T> Default for WrapBox<T> { fn default() -> Self { let v : std::vec::Vec<T> = std::vec::Vec::new(); let b = v.into_boxed_slice(); return WrapBox::<T>(b); } } impl<T> super::SliceWrapper<T> for WrapBox<T> { fn slice(&self) -> & [T] { return &*self.0 } } impl<T> super::SliceWrapperMut<T> for WrapBox<T> { fn slice_mut(&mut self) -> &mut [T] { return &mut*self.0 } } impl<T> ops::Index<usize> for WrapBox<T> { type Output = T; fn index(&self, index: usize) -> &T { &(*self.0)[index] } } impl<T> ops::IndexMut<usize> for WrapBox<T> { fn index_mut(&mut self, index: usize) -> &mut T { &mut (*self.0)[index] } } impl<T> ops::Index<Range<usize>> for WrapBox<T> { type Output = [T]; fn index(&self, index: Range<usize>) -> &[T] { &(*self.0)[index] } } impl<T> ops::IndexMut<Range<usize>> for WrapBox<T> { fn index_mut(&mut self, index: Range<usize>) -> &mut [T] { &mut (*self.0)[index] } } pub struct HeapAlloc<T : Clone>{ pub default_value : T, } impl<T: Clone+Default> Default for HeapAlloc<T> { fn default() -> Self { Self::new(T::default()) } } impl<T : Clone> HeapAlloc<T> { pub fn new(data : T) -> HeapAlloc<T> { return HeapAlloc::<T>{default_value : data}; } } impl<T : Clone> super::Allocator<T> for HeapAlloc<T> { type AllocatedMemory = WrapBox<T>; fn alloc_cell(self : &mut HeapAlloc<T>, len : usize) -> WrapBox<T> { let v : std::vec::Vec<T> = vec![self.default_value.clone();len]; let b = v.into_boxed_slice(); return WrapBox::<T>(b); } fn free_cell(self : &mut HeapAlloc<T>, _data : WrapBox<T>) { } } #[cfg(feature="unsafe")] pub struct HeapAllocUninitialized<T>{ #[allow(dead_code)] default_value : Option<T>, } #[cfg(feature="unsafe")] impl<T> HeapAllocUninitialized<T>{ pub unsafe fn new() -> HeapAllocUninitialized<T> { return HeapAllocUninitialized::<T>{default_value:None}; } } #[cfg(feature="unsafe")] impl<T> super::Allocator<T> for HeapAllocUninitialized<T> { type AllocatedMemory = WrapBox<T>; fn alloc_cell(self : &mut Self, len : usize) -> WrapBox<T> { let mut v : std::vec::Vec<T> = std::vec::Vec::with_capacity(len); unsafe {v.set_len(len)}; let b = v.into_boxed_slice(); return WrapBox::<T>(b); } fn free_cell(self : &mut Self, _data : WrapBox<T>) { } } pub struct HeapPrealloc<'a, T : 'a> { freelist : std::boxed::Box<[&'a mut [T]]>, } define_stack_allocator_traits!(HeapPrealloc, heap); impl<'a, T : Clone+'a> HeapPrealloc<'a, T> { fn make_freelist(freelist_size : usize) -> std::boxed::Box<[&'a mut[T]]> { let mut retval = Vec::<&'a mut[T]>::with_capacity(freelist_size); for _i in 0..freelist_size { retval.push(&mut[]); } return retval.into_boxed_slice(); } pub fn new_allocator(freelist_size : usize, memory_pool : &'a mut Box<[T]>, initializer : fn(&mut[T])) -> super::StackAllocator<'a, T, HeapPrealloc<'a, T> > { let mut retval = super::StackAllocator::<T, HeapPrealloc<T> > { nop : &mut [], system_resources : HeapPrealloc::<T> { freelist : Self::make_freelist(freelist_size), }, free_list_start : freelist_size, free_list_overflow_count : 0, initialize : initializer, }; retval.free_cell(super::AllocatedStackMemory::<T>{mem:&mut*memory_pool}); return retval; } #[cfg(feature="unsafe")] pub unsafe fn new_uninitialized_memory_pool(len : usize) -> Box<[T]> { let mut v : std::vec::Vec<T> = std::vec::Vec::with_capacity(len); v.set_len(len); return v.into_boxed_slice(); } }