diff --git a/Cargo.toml b/Cargo.toml index 530a449..fa7ea9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "malloc-array" description = "libc heap array allocator" -version = "1.2.3" +version = "1.3.3" authors = ["Avril "] edition = "2018" license = "GPL-3.0-or-later" diff --git a/src/lib.rs b/src/lib.rs index dac516c..86a025d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,26 @@ extern crate jemalloc_sys; #[cfg(test)] mod tests { use super::*; + + #[test] + fn copy() { + let heap = heap![unsafe 1u16; 10]; + let mut heap2 = heap![unsafe 10u16; 20]; + + unsafe { + assert_eq!(heap2.memory_from_raw(heap.as_ptr(), heap.len()), 10); + } + + assert_eq!(heap2[0], 1); + assert_eq!(heap2[10], 10); + + unsafe { + let heap3 = HeapArray::from_raw_copied(heap2.as_ptr(), 15); + assert_eq!(heap3.len(), 15); + assert_eq!(heap3[0], 1); + assert_eq!(heap3[10], 10); + } + } #[test] fn as_slice() { @@ -583,6 +603,22 @@ impl HeapArray Box::leak(bx) } } + + /// Copy memory in from a raw pointer. + pub unsafe fn memory_from_raw(&mut self, from: *const T, size: usize) -> usize + { + let size = std::cmp::min(size, self.len()); + ptr::memcpy(self.ptr as VoidPointer, from as VoidPointer, size * std::mem::size_of::()); + size + } + + /// Create a new instance with memory copied from a raw pointer. + pub unsafe fn from_raw_copied(from: *const T, size: usize) -> Self + { + let mut inp = Self::new_uninit(size); + inp.memory_from_raw(from, size); + inp + } } impl Index for HeapArray