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
#![no_std]

#[macro_use]
mod allocated_memory;
mod stack_allocator;
mod allocated_stack_memory;
#[macro_use]
pub mod init;
pub use allocated_memory::SliceWrapper;
pub use allocated_memory::SliceWrapperMut;
pub use allocated_memory::AllocatedSlice;

pub use allocated_stack_memory::AllocatedStackMemory;
pub use stack_allocator::Allocator;
pub use stack_allocator::StackAllocator;

use core::default::Default;
pub fn bzero<T : Default> (data : &mut [T]) {
    for iter in data.iter_mut() {
        *iter = T::default();
    }
}

pub fn uninitialized<T> (_data : &mut[T]) {}



#[derive(Debug)]
pub struct CallocBackingStore<'a, T : 'a> {
    pub raw_data : *mut u8,
    pub data : &'a mut[T],
    free : unsafe extern "C" fn(*mut u8),
}

pub enum AllocatorC {
   Calloc(unsafe extern "C" fn(usize, usize) -> *mut u8),
   Malloc(unsafe extern "C" fn(usize) -> *mut u8),
   Custom(fn(usize) -> *mut u8),
}
impl<'a, T : 'a> CallocBackingStore<'a, T> {
  pub unsafe fn new(num_elements : usize, alloc : AllocatorC, free : unsafe extern "C" fn (*mut u8), should_free : bool) -> Self{
     let retval : *mut u8 = if num_elements == 0 {core::ptr::null_mut()} else {
        match alloc {
           AllocatorC::Calloc(calloc) => calloc(num_elements, core::mem::size_of::<T>()),
           AllocatorC::Malloc(malloc) => malloc(num_elements *core::mem::size_of::<T>()),
           AllocatorC::Custom(malloc) => malloc(num_elements *core::mem::size_of::<T>()),
        }
     };
     if num_elements == 0 || retval.is_null() {
        return CallocBackingStore::<'a, T>{
         raw_data : core::ptr::null_mut(),
         data : &mut[],
         free : free,
       }
     }
     let raw_data : *mut T = core::mem::transmute(retval);
     if should_free {
       return CallocBackingStore::<'a, T>{
         raw_data : retval,
         data : core::slice::from_raw_parts_mut(raw_data,
                                                num_elements),
         free : free,
       };
     } else {
       let null_ptr : *const u8 = core::ptr::null();
       return CallocBackingStore::<'a, T>{
         raw_data : core::mem::transmute(null_ptr),//retval,
         data : core::slice::from_raw_parts_mut(raw_data,
                                                num_elements),
         free : free,
       };
    }
  }
}
impl<'a, T:'a> Drop for CallocBackingStore<'a, T> {
  fn drop(self :&mut Self) {
//      core::mem::forget(core::mem::replace(self.data, &mut[]));
    core::mem::forget(core::mem::replace(&mut self.data, &mut[]));
    if !self.raw_data.is_null() {
      let local_free = self.free;
      unsafe {(local_free)(self.raw_data)};

    }
  }
}