alt_std/
mem.rs

1pub unsafe fn allocRaw(size: usize) -> *mut u8 {
2    //let addr = libc::memalign(core::mem::size_of::<usize>(), size) as *mut u8;
3    let addr = libc::calloc(core::mem::size_of::<usize>(), size) as *mut u8;
4    //libc::memset(addr as *mut libc::c_void, 0, size);
5    addr
6}
7
8pub unsafe fn freeRaw(arr: *mut u8) {
9    libc::free(arr as *mut libc::c_void);
10}
11
12pub unsafe fn alloc<T>() -> *mut T {
13    allocRaw(core::mem::size_of::<T>()) as *mut T
14}
15
16pub unsafe fn free<T>(t: *mut T) {
17    freeRaw(t as *mut u8)
18}
19
20// TODO: change this to const generics when they become stable and return a slice
21pub unsafe fn allocArray<T>(count: usize) -> *mut T {
22    allocRaw(core::mem::size_of::<T>() * count) as *mut T
23}
24
25// TODO: change this to slice once const generics stable
26pub unsafe fn freeArray<T>(ptr: *mut T, count: usize) {
27    let arr      = core::slice::from_raw_parts_mut(ptr, count); // this will keep a pointer (will not free it)
28    for i in 0..count {
29        ::core::ptr::drop_in_place(&arr[i] as *const T as *mut T);
30    }
31    free(ptr);
32}
33
34#[repr(C)]
35pub struct Unique<T: ?Sized> {
36    ptr         : *mut T,
37    _marker     : ::core::marker::PhantomData<T>,
38}
39
40impl<T> Unique<T> {
41    pub fn new(ptr: *mut T) -> Self { Self { ptr : ptr, _marker: ::core::marker::PhantomData } }
42    pub fn getMutPtr(&mut self) -> *mut T { self.ptr }
43    pub fn getPtr(&self) -> *const T { self.ptr }
44}
45
46#[repr(C)]
47pub struct Box<T>{
48    uptr: Unique<T>
49}
50
51impl<T> Box<T> {
52    /// Allocates memory on the heap and then places `x` into it.
53    ///
54    /// # Examples
55    ///
56    /// ```
57    /// let five = Box::new(5);
58    /// ```
59    #[inline(always)]
60    pub fn new(x: T) -> Box<T> {
61        unsafe {
62            let addr = alloc::<T>();
63            *addr = x;
64            Self { uptr: Unique::new(addr) }
65        }
66    }
67
68    pub fn asRef(&self) -> &T { unsafe { &(*self.uptr.getPtr()) } }
69    pub fn asMut(&mut self) -> &T { unsafe { &mut (*self.uptr.getMutPtr()) } }
70    pub fn intoRaw(self) -> *mut T {
71        let m = ::core::mem::ManuallyDrop::new(self);
72        m.uptr.ptr
73    }
74
75    pub fn fromRaw(raw: *mut T) -> Self {
76        Self { uptr: Unique::new(raw) }
77    }
78
79    pub fn unbox(self) -> T {
80        unsafe {
81            let ptr = self.uptr.ptr;
82            let v = self.intoRaw().read();
83            free(ptr);
84            v
85        }
86    }
87}
88
89impl<T> Drop for Box<T> {
90    fn drop(&mut self) {
91        unsafe {
92            let addr = self.uptr.getMutPtr();
93            ::core::ptr::drop_in_place(addr);
94            free(addr);
95        }
96    }
97}
98
99
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn testDrop() {
107        let _b0 = Box::new(1234);
108        let _b1 = Box::new(1234345);
109        let mut v = crate::vec::Vec::new();
110        for i in 0..100 {
111            v.pushBack(i);
112        }
113        let _bv = Box::new(v);
114    }
115
116    #[test]
117    fn testDropVecVec() {
118        let _b0 = Box::new(1234);
119        let _b1 = Box::new(1234345);
120        let mut v = crate::vec::Vec::new();
121        for _ in 0..100 {
122            let mut vj = crate::vec::Vec::new();
123            for j in 0..100 {
124                vj.pushBack(j);
125            }
126            v.pushBack(vj);
127        }
128        let _bv = Box::new(v);
129    }
130
131    #[test]
132    fn testBoxUnbox() {
133        let b = Box::new(1234);
134        let _v = b.unbox();
135    }
136
137    #[test]
138    fn testBoxUnboxVecVec() {
139        let _b0 = Box::new(1234);
140        let _b1 = Box::new(1234345);
141        let mut v = crate::vec::Vec::new();
142        for _ in 0..100 {
143            let mut vj = crate::vec::Vec::new();
144            for j in 0..100 {
145                vj.pushBack(j);
146            }
147            v.pushBack(vj);
148        }
149        let v2 = Box::new(v);
150        let _v3 = v2.unbox();
151    }
152
153    #[test]
154    fn testBoxFromToRaw() {
155        let b = Box::new(1234);
156        let r = b.intoRaw();
157        let _b = Box::fromRaw(r);
158    }
159}