/rust/registry/src/index.crates.io-6f17d22bba15001f/alloc-no-stdlib-2.0.4/src/lib.rs
Line | Count | Source (jump to first uncovered line) |
1 | | #![no_std] |
2 | | |
3 | | #[macro_use] |
4 | | mod allocated_memory; |
5 | | mod stack_allocator; |
6 | | mod allocated_stack_memory; |
7 | | #[macro_use] |
8 | | pub mod init; |
9 | | pub use allocated_memory::SliceWrapper; |
10 | | pub use allocated_memory::SliceWrapperMut; |
11 | | pub use allocated_memory::AllocatedSlice; |
12 | | |
13 | | pub use allocated_stack_memory::AllocatedStackMemory; |
14 | | pub use stack_allocator::Allocator; |
15 | | pub use stack_allocator::StackAllocator; |
16 | | |
17 | | use core::default::Default; |
18 | 0 | pub fn bzero<T : Default> (data : &mut [T]) { |
19 | 0 | for iter in data.iter_mut() { |
20 | 0 | *iter = T::default(); |
21 | 0 | } |
22 | 0 | } Unexecuted instantiation: alloc_no_stdlib::bzero::<brotli_decompressor::huffman::HuffmanCode> Unexecuted instantiation: alloc_no_stdlib::bzero::<u8> Unexecuted instantiation: alloc_no_stdlib::bzero::<u32> |
23 | | |
24 | | pub fn uninitialized<T> (_data : &mut[T]) {} |
25 | | |
26 | | |
27 | | |
28 | | #[derive(Debug)] |
29 | | pub struct CallocBackingStore<'a, T : 'a> { |
30 | | pub raw_data : *mut u8, |
31 | | pub data : &'a mut[T], |
32 | | free : unsafe extern "C" fn(*mut u8), |
33 | | } |
34 | | |
35 | | pub enum AllocatorC { |
36 | | Calloc(unsafe extern "C" fn(usize, usize) -> *mut u8), |
37 | | Malloc(unsafe extern "C" fn(usize) -> *mut u8), |
38 | | Custom(fn(usize) -> *mut u8), |
39 | | } |
40 | | impl<'a, T : 'a> CallocBackingStore<'a, T> { |
41 | | pub unsafe fn new(num_elements : usize, alloc : AllocatorC, free : unsafe extern "C" fn (*mut u8), should_free : bool) -> Self{ |
42 | | let retval : *mut u8 = if num_elements == 0 {core::ptr::null_mut()} else { |
43 | | match alloc { |
44 | | AllocatorC::Calloc(calloc) => calloc(num_elements, core::mem::size_of::<T>()), |
45 | | AllocatorC::Malloc(malloc) => malloc(num_elements *core::mem::size_of::<T>()), |
46 | | AllocatorC::Custom(malloc) => malloc(num_elements *core::mem::size_of::<T>()), |
47 | | } |
48 | | }; |
49 | | if num_elements == 0 || retval.is_null() { |
50 | | return CallocBackingStore::<'a, T>{ |
51 | | raw_data : core::ptr::null_mut(), |
52 | | data : &mut[], |
53 | | free : free, |
54 | | } |
55 | | } |
56 | | let raw_data : *mut T = core::mem::transmute(retval); |
57 | | if should_free { |
58 | | return CallocBackingStore::<'a, T>{ |
59 | | raw_data : retval, |
60 | | data : core::slice::from_raw_parts_mut(raw_data, |
61 | | num_elements), |
62 | | free : free, |
63 | | }; |
64 | | } else { |
65 | | let null_ptr : *const u8 = core::ptr::null(); |
66 | | return CallocBackingStore::<'a, T>{ |
67 | | raw_data : core::mem::transmute(null_ptr),//retval, |
68 | | data : core::slice::from_raw_parts_mut(raw_data, |
69 | | num_elements), |
70 | | free : free, |
71 | | }; |
72 | | } |
73 | | } |
74 | | } |
75 | | impl<'a, T:'a> Drop for CallocBackingStore<'a, T> { |
76 | | fn drop(self :&mut Self) { |
77 | | // core::mem::forget(core::mem::replace(self.data, &mut[])); |
78 | | core::mem::forget(core::mem::replace(&mut self.data, &mut[])); |
79 | | if !self.raw_data.is_null() { |
80 | | let local_free = self.free; |
81 | | unsafe {(local_free)(self.raw_data)}; |
82 | | |
83 | | } |
84 | | } |
85 | | } |