alloc_stdlib/
heap_alloc.rs
1use std;
2
3
4use super::{SliceWrapper, SliceWrapperMut, Allocator};
5
6use std::ops;
7use std::ops::Range;
8use std::boxed::Box;
9use std::vec::Vec;
10pub struct WrapBox<T>(std::boxed::Box<[T]>);
11
12impl<T> From<Vec<T>> for WrapBox<T> {
13 fn from(data: Vec<T>) -> Self {
14 WrapBox(data.into_boxed_slice())
15 }
16}
17
18impl<T> Into<Box<[T]>> for WrapBox<T> {
19 fn into(self) -> Box<[T]> {
20 self.0
21 }
22}
23
24impl<T> Default for WrapBox<T> {
25 fn default() -> Self {
26 let v : std::vec::Vec<T> = std::vec::Vec::new();
27 let b = v.into_boxed_slice();
28 return WrapBox::<T>(b);
29 }
30}
31
32impl<T> super::SliceWrapper<T> for WrapBox<T> {
33 fn slice(&self) -> & [T] {
34 return &*self.0
35 }
36}
37
38impl<T> super::SliceWrapperMut<T> for WrapBox<T> {
39 fn slice_mut(&mut self) -> &mut [T] {
40 return &mut*self.0
41 }
42}
43impl<T> ops::Index<usize> for WrapBox<T> {
44 type Output = T;
45 fn index(&self, index: usize) -> &T {
46 &(*self.0)[index]
47 }
48}
49
50impl<T> ops::IndexMut<usize> for WrapBox<T> {
51 fn index_mut(&mut self, index: usize) -> &mut T {
52 &mut (*self.0)[index]
53 }
54}
55
56impl<T> ops::Index<Range<usize>> for WrapBox<T> {
57 type Output = [T];
58 fn index(&self, index: Range<usize>) -> &[T] {
59 &(*self.0)[index]
60 }
61}
62
63impl<T> ops::IndexMut<Range<usize>> for WrapBox<T> {
64 fn index_mut(&mut self, index: Range<usize>) -> &mut [T] {
65 &mut (*self.0)[index]
66 }
67}
68
69
70pub struct HeapAlloc<T : Clone>{
71 pub default_value : T,
72}
73
74impl<T: Clone+Default> Default for HeapAlloc<T> {
75 fn default() -> Self {
76 Self::new(T::default())
77 }
78}
79
80impl<T : Clone> HeapAlloc<T> {
81 pub fn new(data : T) -> HeapAlloc<T> {
82 return HeapAlloc::<T>{default_value : data};
83 }
84}
85
86impl<T : Clone> super::Allocator<T> for HeapAlloc<T> {
87 type AllocatedMemory = WrapBox<T>;
88 fn alloc_cell(self : &mut HeapAlloc<T>, len : usize) -> WrapBox<T> {
89
90 let v : std::vec::Vec<T> = vec![self.default_value.clone();len];
91 let b = v.into_boxed_slice();
92 return WrapBox::<T>(b);
93 }
94 fn free_cell(self : &mut HeapAlloc<T>, _data : WrapBox<T>) {
95
96 }
97}
98
99#[deprecated]
100pub type HeapAllocUninitialized<T> = HeapAlloc<T>;
101
102
103pub struct HeapPrealloc<'a, T : 'a> {
104 freelist : std::boxed::Box<[&'a mut [T]]>,
105}
106define_stack_allocator_traits!(HeapPrealloc, heap);
107
108impl<'a, T : Clone+'a> HeapPrealloc<'a, T> {
109 fn make_freelist(freelist_size : usize) -> std::boxed::Box<[&'a mut[T]]> {
110 let mut retval = Vec::<&'a mut[T]>::with_capacity(freelist_size);
111 for _i in 0..freelist_size {
112 retval.push(&mut[]);
113 }
114 return retval.into_boxed_slice();
115 }
116 pub fn new_allocator(freelist_size : usize,
117 memory_pool : &'a mut Box<[T]>,
118 initializer : fn(&mut[T])) -> super::StackAllocator<'a, T, HeapPrealloc<'a, T> > {
119 let mut retval = super::StackAllocator::<T, HeapPrealloc<T> > {
120 nop : &mut [],
121 system_resources : HeapPrealloc::<T> {
122 freelist : Self::make_freelist(freelist_size),
123 },
124 free_list_start : freelist_size,
125 free_list_overflow_count : 0,
126 initialize : initializer,
127 };
128 retval.free_cell(super::AllocatedStackMemory::<T>{mem:&mut*memory_pool});
129 return retval;
130 }
131 #[cfg(feature="unsafe")]
132 pub unsafe fn new_uninitialized_memory_pool(len : usize) -> Box<[T]> {
133 let mut v : std::vec::Vec<T> = std::vec::Vec::with_capacity(len);
134 v.set_len(len);
135 return v.into_boxed_slice();
136 }
137}
138