Coverage Report

Created: 2021-11-03 07:11

/rust/registry/src/github.com-1ecc6299db9ec823/alloc-stdlib-0.2.1/src/heap_alloc.rs
Line
Count
Source
1
use std;
2
3
4
use super::{SliceWrapper, SliceWrapperMut, Allocator};
5
6
use std::ops;
7
use std::ops::Range;
8
use std::boxed::Box;
9
use std::vec::Vec;
10
pub struct WrapBox<T>(std::boxed::Box<[T]>);
11
12
impl<T> From<Vec<T>> for WrapBox<T> {
13
54.7k
    fn from(data: Vec<T>) -> Self {
14
54.7k
        WrapBox(data.into_boxed_slice())
15
54.7k
    }
<alloc_stdlib::heap_alloc::WrapBox<u32> as core::convert::From<alloc::vec::Vec<u32>>>::from
Line
Count
Source
13
12.7k
    fn from(data: Vec<T>) -> Self {
14
12.7k
        WrapBox(data.into_boxed_slice())
15
12.7k
    }
<alloc_stdlib::heap_alloc::WrapBox<u8> as core::convert::From<alloc::vec::Vec<u8>>>::from
Line
Count
Source
13
19.7k
    fn from(data: Vec<T>) -> Self {
14
19.7k
        WrapBox(data.into_boxed_slice())
15
19.7k
    }
<alloc_stdlib::heap_alloc::WrapBox<brotli_decompressor::huffman::HuffmanCode> as core::convert::From<alloc::vec::Vec<brotli_decompressor::huffman::HuffmanCode>>>::from
Line
Count
Source
13
22.1k
    fn from(data: Vec<T>) -> Self {
14
22.1k
        WrapBox(data.into_boxed_slice())
15
22.1k
    }
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<u32> as core::convert::From<alloc::vec::Vec<u32>>>::from
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<brotli_decompressor::huffman::HuffmanCode> as core::convert::From<alloc::vec::Vec<brotli_decompressor::huffman::HuffmanCode>>>::from
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<u8> as core::convert::From<alloc::vec::Vec<u8>>>::from
16
}
17
18
impl<T> Into<Box<[T]>> for WrapBox<T> {
19
    fn into(self) -> Box<[T]> {
20
        self.0
21
    }
22
}
23
24
impl<T> Default for WrapBox<T> {
25
14.2M
    fn default() -> Self {
26
14.2M
       let v : std::vec::Vec<T> = std::vec::Vec::new();
27
14.2M
       let b = v.into_boxed_slice();
28
14.2M
       return WrapBox::<T>(b);
29
14.2M
    }
<alloc_stdlib::heap_alloc::WrapBox<u8> as core::default::Default>::default
Line
Count
Source
25
3.65M
    fn default() -> Self {
26
3.65M
       let v : std::vec::Vec<T> = std::vec::Vec::new();
27
3.65M
       let b = v.into_boxed_slice();
28
3.65M
       return WrapBox::<T>(b);
29
3.65M
    }
<alloc_stdlib::heap_alloc::WrapBox<u32> as core::default::Default>::default
Line
Count
Source
25
5.30M
    fn default() -> Self {
26
5.30M
       let v : std::vec::Vec<T> = std::vec::Vec::new();
27
5.30M
       let b = v.into_boxed_slice();
28
5.30M
       return WrapBox::<T>(b);
29
5.30M
    }
<alloc_stdlib::heap_alloc::WrapBox<brotli_decompressor::huffman::HuffmanCode> as core::default::Default>::default
Line
Count
Source
25
5.33M
    fn default() -> Self {
26
5.33M
       let v : std::vec::Vec<T> = std::vec::Vec::new();
27
5.33M
       let b = v.into_boxed_slice();
28
5.33M
       return WrapBox::<T>(b);
29
5.33M
    }
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<brotli_decompressor::huffman::HuffmanCode> as core::default::Default>::default
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<u8> as core::default::Default>::default
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<u32> as core::default::Default>::default
30
}
31
32
impl<T> super::SliceWrapper<T> for WrapBox<T> {
33
695M
    fn slice(&self) -> & [T] {
34
695M
       return &*self.0
35
695M
    }
<alloc_stdlib::heap_alloc::WrapBox<brotli_decompressor::huffman::HuffmanCode> as alloc_no_stdlib::allocated_memory::SliceWrapper<brotli_decompressor::huffman::HuffmanCode>>::slice
Line
Count
Source
33
1.69M
    fn slice(&self) -> & [T] {
34
1.69M
       return &*self.0
35
1.69M
    }
<alloc_stdlib::heap_alloc::WrapBox<u8> as alloc_no_stdlib::allocated_memory::SliceWrapper<u8>>::slice
Line
Count
Source
33
692M
    fn slice(&self) -> & [T] {
34
692M
       return &*self.0
35
692M
    }
<alloc_stdlib::heap_alloc::WrapBox<u32> as alloc_no_stdlib::allocated_memory::SliceWrapper<u32>>::slice
Line
Count
Source
33
811k
    fn slice(&self) -> & [T] {
34
811k
       return &*self.0
35
811k
    }
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<u32> as alloc_no_stdlib::allocated_memory::SliceWrapper<u32>>::slice
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<brotli_decompressor::huffman::HuffmanCode> as alloc_no_stdlib::allocated_memory::SliceWrapper<brotli_decompressor::huffman::HuffmanCode>>::slice
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<u8> as alloc_no_stdlib::allocated_memory::SliceWrapper<u8>>::slice
36
}
37
38
impl<T> super::SliceWrapperMut<T> for WrapBox<T> {
39
835M
    fn slice_mut(&mut self) -> &mut [T] {
40
835M
       return &mut*self.0
41
835M
    }
<alloc_stdlib::heap_alloc::WrapBox<u32> as alloc_no_stdlib::allocated_memory::SliceWrapperMut<u32>>::slice_mut
Line
Count
Source
39
21.0k
    fn slice_mut(&mut self) -> &mut [T] {
40
21.0k
       return &mut*self.0
41
21.0k
    }
<alloc_stdlib::heap_alloc::WrapBox<u8> as alloc_no_stdlib::allocated_memory::SliceWrapperMut<u8>>::slice_mut
Line
Count
Source
39
835M
    fn slice_mut(&mut self) -> &mut [T] {
40
835M
       return &mut*self.0
41
835M
    }
<alloc_stdlib::heap_alloc::WrapBox<brotli_decompressor::huffman::HuffmanCode> as alloc_no_stdlib::allocated_memory::SliceWrapperMut<brotli_decompressor::huffman::HuffmanCode>>::slice_mut
Line
Count
Source
39
43.9k
    fn slice_mut(&mut self) -> &mut [T] {
40
43.9k
       return &mut*self.0
41
43.9k
    }
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<brotli_decompressor::huffman::HuffmanCode> as alloc_no_stdlib::allocated_memory::SliceWrapperMut<brotli_decompressor::huffman::HuffmanCode>>::slice_mut
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<u8> as alloc_no_stdlib::allocated_memory::SliceWrapperMut<u8>>::slice_mut
Unexecuted instantiation: <alloc_stdlib::heap_alloc::WrapBox<u32> as alloc_no_stdlib::allocated_memory::SliceWrapperMut<u32>>::slice_mut
42
}
43
impl<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
50
impl<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
56
impl<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
63
impl<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
70
pub struct HeapAlloc<T : Clone>{
71
   pub default_value : T,
72
}
73
74
impl<T: Clone+Default> Default for HeapAlloc<T> {
75
    fn default() -> Self {
76
        Self::new(T::default())
77
    }
78
}
79
80
impl<T : Clone> HeapAlloc<T> {
81
   pub fn new(data : T) -> HeapAlloc<T> {
82
      return HeapAlloc::<T>{default_value : data};
83
   }
84
}
85
86
impl<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
#[cfg(feature="unsafe")]
100
pub struct HeapAllocUninitialized<T>{
101
   #[allow(dead_code)]
102
   default_value : Option<T>,
103
}
104
105
#[cfg(feature="unsafe")]
106
impl<T> HeapAllocUninitialized<T>{
107
   pub unsafe fn new() -> HeapAllocUninitialized<T> {
108
       return HeapAllocUninitialized::<T>{default_value:None};
109
   }
110
}
111
112
#[cfg(feature="unsafe")]
113
impl<T> super::Allocator<T> for HeapAllocUninitialized<T> {
114
   type AllocatedMemory = WrapBox<T>;
115
   fn alloc_cell(self : &mut Self, len : usize) -> WrapBox<T> {
116
117
       let mut v : std::vec::Vec<T> = std::vec::Vec::with_capacity(len);
118
       unsafe {v.set_len(len)};
119
       let b = v.into_boxed_slice();
120
       return WrapBox::<T>(b);
121
   }
122
   fn free_cell(self : &mut Self, _data : WrapBox<T>) {
123
124
   }
125
}
126
127
128
pub struct HeapPrealloc<'a, T : 'a> {
129
   freelist : std::boxed::Box<[&'a mut [T]]>,
130
}
131
define_stack_allocator_traits!(HeapPrealloc, heap);
132
133
impl<'a, T : Clone+'a> HeapPrealloc<'a, T> {
134
    fn make_freelist(freelist_size : usize) -> std::boxed::Box<[&'a mut[T]]> {
135
        let mut retval = Vec::<&'a mut[T]>::with_capacity(freelist_size);
136
        for _i in 0..freelist_size {
137
            retval.push(&mut[]);
138
        }
139
        return retval.into_boxed_slice();
140
    }
141
    pub fn new_allocator(freelist_size : usize,
142
                     memory_pool : &'a mut Box<[T]>,
143
                     initializer : fn(&mut[T])) -> super::StackAllocator<'a, T, HeapPrealloc<'a, T> > {
144
        let mut retval = super::StackAllocator::<T, HeapPrealloc<T> > {
145
            nop : &mut [],
146
            system_resources : HeapPrealloc::<T> {
147
                freelist : Self::make_freelist(freelist_size),
148
            },
149
            free_list_start : freelist_size,
150
            free_list_overflow_count : 0,
151
            initialize : initializer,
152
        };
153
        retval.free_cell(super::AllocatedStackMemory::<T>{mem:&mut*memory_pool});
154
        return retval;
155
    }
156
    #[cfg(feature="unsafe")]
157
    pub unsafe fn new_uninitialized_memory_pool(len : usize) -> Box<[T]> {
158
        let mut v : std::vec::Vec<T> = std::vec::Vec::with_capacity(len);
159
        v.set_len(len);
160
        return v.into_boxed_slice();
161
    }
162
}
163