use std::marker::PhantomData; use std::ops::Deref; use std::slice; use libc::c_void;
struct MallocPtr(*mut c_void);
impl Drop for MallocPtr { fn drop(&mutself) { unsafe {
libc::free(self.0);
}
}
}
/// A type that represents a `malloc`'d chunk of memory. pubstruct MallocBuffer<T> {
ptr: MallocPtr,
len: usize,
items: PhantomData<[T]>,
}
impl<T: Copy> MallocBuffer<T> { /// Constructs a new `MallocBuffer` for a `malloc`'d buffer /// with the given length at the given pointer. /// Returns `None` if the given pointer is null and the length is not 0. /// /// When this `MallocBuffer` drops, the buffer will be `free`'d. /// /// Unsafe because there must be `len` contiguous, valid instances of `T` /// at `ptr`. pubunsafefn new(ptr: *mut T, len: usize) -> Option<MallocBuffer<T>> { if len > 0 && ptr.is_null() {
None
} else {
Some(MallocBuffer {
ptr: MallocPtr(ptr as *mut c_void),
len: len,
items: PhantomData,
})
}
}
}
impl<T> Deref for MallocBuffer<T> { type Target = [T];
fn deref(&self) -> &[T] { let ptr = ifself.len == 0 && self.ptr.0.is_null() { // Even a 0-size slice cannot be null, so just use another pointer 0x1 as *const T
} else { self.ptr.0as *const T
}; unsafe {
slice::from_raw_parts(ptr, self.len)
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.