| 1 |
#![no_std] |
| 2 |
|
| 3 |
extern crate libc; |
| 4 |
#[cfg(test)] |
| 5 |
extern crate std; |
| 6 |
|
| 7 |
use core::fmt; |
| 8 |
use core::ops::Deref; |
| 9 |
use core::ptr; |
| 10 |
use core::slice; |
| 11 |
use core::str::{Utf8Error, self}; |
| 12 |
use libc::{c_char, c_void}; |
| 13 |
|
| •14 |
const DUMMY_PTR: *mut c_void = 0x10 as *mut c_void; |
| 15 |
|
| 16 |
/// A type that represents a `malloc`'d chunk of memory. |
| 17 |
/// When dropped, a `Malloc` will `free` that memory. |
| 18 |
pub struct Malloc<T: ?Sized> { |
| 19 |
ptr: *mut T, |
| 20 |
} |
| 21 |
|
| 22 |
impl<T> Malloc<T> { |
| 23 |
/** |
| 24 |
Constructs a new `Malloc` for a `malloc`'d value. |
| 25 |
|
| 26 |
Unsafe because there must be a valid instance of `T` at `ptr`. |
| 27 |
*/ |
| 28 |
pub unsafe fn from_ptr(ptr: *mut T) -> Malloc<T> { |
| 29 |
Malloc { ptr: ptr } |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl<T> Malloc<[T]> { |
| 34 |
/** |
| 35 |
Constructs a new `Malloc` for a `malloc`'d buffer with the given length |
| 36 |
at the given pointer. |
| 37 |
|
| 38 |
Unsafe because there must be `len` contiguous, valid instances of `T` |
| 39 |
at `ptr`. |
| 40 |
|
| 41 |
The given pointer must not be null unless the length is 0; this function |
| 42 |
will specially handle null, 0-length buffers safely. |
| 43 |
*/ |
| 44 |
pub unsafe fn from_array(ptr: *mut T, len: usize) -> Malloc<[T]> { |
| 45 |
// Even a 0-size slice cannot be null, so just use another pointer |
| 46 |
let ptr = if ptr.is_null() && len == 0 { DUMMY_PTR as *mut T } |
| 47 |
else { ptr }; |
| 48 |
let slice = slice::from_raw_parts(ptr, len); |
| 49 |
Malloc { ptr: slice as *const [T] as *mut [T] } |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
impl Malloc<str> { |
| 54 |
/** |
| 55 |
Constructs a new `Malloc` for a `malloc`'d nul-terminated C string |
| 56 |
at the given pointer. Returns an error if the bytes are not valid UTF8. |
| 57 |
|
| 58 |
Unsafe because `ptr` must point to valid, nul-terminated bytes. |
| 59 |
|
| 60 |
Once created, when dereferenced the `Malloc<str>` will return the string |
| 61 |
without its nul-terminator. |
| 62 |
*/ |
| 63 |
pub unsafe fn from_c_str(ptr: *mut c_char) |
| 64 |
-> Result<Malloc<str>, Utf8Error> { |
| 65 |
let len = libc::strlen(ptr); |
| 66 |
let slice = slice::from_raw_parts(ptr as *mut u8, len); |
| 67 |
str::from_utf8(slice).map(|s| { |
| 68 |
Malloc { ptr: s as *const str as *mut str } |
| 69 |
}) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl<T: ?Sized> Deref for Malloc<T> { |
| 74 |
type Target = T; |
| 75 |
|
| 76 |
fn deref(&self) -> &T { |
| 77 |
unsafe { &*self.ptr } |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
impl<T: ?Sized> Drop for Malloc<T> { |
| 82 |
fn drop(&mut self) { |
| 83 |
if (self.ptr as *mut c_void) != DUMMY_PTR { |
| 84 |
unsafe { |
| 85 |
ptr::drop_in_place(self.ptr); |
| 86 |
libc::free(self.ptr as *mut c_void); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
impl<T: fmt::Debug + ?Sized> fmt::Debug for Malloc<T> { |
| 93 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 94 |
fmt::Debug::fmt(&**self, f) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
impl<T: fmt::Display + ?Sized> fmt::Display for Malloc<T> { |
| 99 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 100 |
fmt::Display::fmt(&**self, f) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl<T: ?Sized> AsRef<T> for Malloc<T> { |
| 105 |
fn as_ref(&self) -> &T { |
| 106 |
&**self |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[cfg(test)] |
| 111 |
mod tests { |
| 112 |
use std::mem; |
| 113 |
use std::ptr; |
| 114 |
use libc::{c_char, self}; |
| 115 |
|
| 116 |
use super::Malloc; |
| 117 |
|
| 118 |
fn alloc<T>(value: T) -> *mut T { |
| 119 |
unsafe { |
| 120 |
let ptr = libc::malloc(mem::size_of::<T>()) as *mut T; |
| 121 |
ptr::write(ptr, value); |
| 122 |
ptr |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_null_buf() { |
| 128 |
let buf = unsafe { |
| 129 |
Malloc::<[u32]>::from_array(ptr::null_mut(), 0) |
| 130 |
}; |
| 131 |
assert!(&*buf == []); |
| 132 |
assert!(Some(&*buf) == Some(&[])); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn test_buf() { |
| 137 |
let ptr = alloc([1, 2, 3]); |
| 138 |
let buf = unsafe { Malloc::from_array(ptr as *mut i32, 3) }; |
| 139 |
assert!(&*buf == [1, 2, 3]); |
| 140 |
} |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn test_string() { |
| 144 |
let ptr = alloc(['h' as c_char, 'e' as c_char, 'y' as c_char, '\0' as c_char]); |
| 145 |
let s = unsafe { Malloc::from_c_str(ptr as *mut c_char).unwrap() }; |
| 146 |
assert!(&*s == "hey"); |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn test_single() { |
| 151 |
use std::string::ToString; |
| 152 |
|
| 153 |
let m = unsafe { Malloc::from_ptr(alloc(4)) }; |
| 154 |
assert!(&*m == &4); |
| 155 |
|
| 156 |
let m = unsafe { Malloc::from_ptr(alloc("hello".to_string())) }; |
| 157 |
assert!(&**m == "hello"); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn test_drop() { |
| 162 |
use std::rc::Rc; |
| 163 |
|
| 164 |
let num: Rc<i32> = Rc::new(4); |
| 165 |
assert_eq!(Rc::strong_count(&num), 1); |
| 166 |
|
| 167 |
let ptr = alloc([num.clone(), num.clone()]); |
| 168 |
let buf = unsafe { Malloc::from_array(ptr as *mut Rc<i32>, 2) }; |
| 169 |
assert_eq!(Rc::strong_count(&num), 3); |
| 170 |
|
| 171 |
drop(buf); |
| 172 |
assert_eq!(Rc::strong_count(&num), 1); |
| 173 |
} |
| 174 |
} |