From 55c64a33929763224b2a81eeae485a5f746506e3 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sun, 23 Apr 2017 10:10:10 -0700 Subject: [PATCH] Added missing *_with_alignment allocation variants. --- mem_arena/src/lib.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/mem_arena/src/lib.rs b/mem_arena/src/lib.rs index bb1a44a..d22f711 100644 --- a/mem_arena/src/lib.rs +++ b/mem_arena/src/lib.rs @@ -110,6 +110,16 @@ impl MemArena { memory } + /// Allocates memory for and initializes a type T, returning a mutable reference to it. + /// + /// Additionally, the allocation will be made with the given byte alignment or + /// the type's inherent alignment, whichever is greater. + pub fn alloc_with_alignment<'a, T: Copy>(&'a self, value: T, align: usize) -> &'a mut T { + let mut memory = unsafe { self.alloc_uninitialized_with_alignment(align) }; + *memory = value; + memory + } + /// Allocates memory for a type `T`, returning a mutable reference to it. /// /// CAUTION: the memory returned is uninitialized. Make sure to initalize before using! @@ -149,6 +159,25 @@ impl MemArena { memory } + /// Allocates memory for `len` values of type `T`, returning a mutable slice to it. + /// All elements are initialized to the given `value`. + /// + /// Additionally, the allocation will be made with the given byte alignment or + /// the type's inherent alignment, whichever is greater. + pub fn alloc_array_with_alignment<'a, T: Copy>(&'a self, + len: usize, + value: T, + align: usize) + -> &'a mut [T] { + let memory = unsafe { self.alloc_array_uninitialized_with_alignment(len, align) }; + + for v in memory.iter_mut() { + *v = value; + } + + memory + } + /// Allocates and initializes memory to duplicate the given slice, returning a mutable slice /// to the new copy. pub fn copy_slice<'a, T: Copy>(&'a self, other: &[T]) -> &'a mut [T] { @@ -161,6 +190,24 @@ impl MemArena { memory } + /// Allocates and initializes memory to duplicate the given slice, returning a mutable slice + /// to the new copy. + /// + /// Additionally, the allocation will be made with the given byte alignment or + /// the type's inherent alignment, whichever is greater. + pub fn copy_slice_with_alignment<'a, T: Copy>(&'a self, + other: &[T], + align: usize) + -> &'a mut [T] { + let memory = unsafe { self.alloc_array_uninitialized_with_alignment(other.len(), align) }; + + for (v, other) in memory.iter_mut().zip(other.iter()) { + *v = *other; + } + + memory + } + /// Allocates memory for `len` values of type `T`, returning a mutable slice to it. /// /// CAUTION: the memory returned is uninitialized. Make sure to initalize before using!