/// Allocates unaligned memory of the given length, moving the cursor
/// forward as necessary.
Allocates unaligned memory of the given length, moving the cursor
forward as necessary.
Allocates unaligned memory of the given length moving the cursor forward as necessary .
fn alloc_raw(&self, len: usize) -> Result<&mut [u8], OutOfMemory> {
        if len == 0 {
            return Ok(&mut []);
        }
        let cursor = self.cursor.get();
        let proposed_cursor = len.checked_add(cursor).ok_or(OutOfMemory)?;
        if proposed_cursor > self.buf_len {
            return Err(OutOfMemory);
        }
        self.cursor.set(proposed_cursor);
        // At this point, it is not possible for following calls to access
        // the range buf_ptr[cursor..proposed_cursor]. Moreover, the invariants
        // of BumpArena ensure that buf_ptr + cursor must land inside (or one
        // past) the allocation. Hence, the pointer arithmetic that follows is
        // safe, as is the cast to isize.
        let slice = unsafe {
            // SAFE: buf_len >= cursor, and buf_len < isize::MAX; therefore,
            // this cast always produces a positive offset.
            let offset_ptr = self.buf_ptr.as_ptr().add(cursor);
            // SAFE: buf_ptr[cursor..proposed_cursor] is initialized.
            slice::from_raw_parts_mut(offset_ptr, len)
        };
        Ok(slice)
    }
fn alloc_raw ( & self , len : usize ) -> Result < & mut [ u8 ] , OutOfMemory > { if len == 0 { return Ok ( & mut [ ] ) ; } let cursor = self . cursor . get ( ) ; let proposed_cursor = len . checked_add ( cursor ) . ok_or ( OutOfMemory ) ? ; if proposed_cursor > self . buf_len { return Err ( OutOfMemory ) ; } self . cursor . set ( proposed_cursor ) ; // At this point, it is not possible for following calls to access // the range buf_ptr[cursor..proposed_cursor]. Moreover, the invariants // of BumpArena ensure that buf_ptr + cursor must land inside (or one // past) the allocation. Hence, the pointer arithmetic that follows is // safe, as is the cast to isize. let slice = unsafe { // SAFE: buf_len >= cursor, and buf_len < isize::MAX; therefore, // this cast always produces a positive offset. let offset_ptr = self . buf_ptr . as_ptr ( ) . add ( cursor ) ; // SAFE: buf_ptr[cursor..proposed_cursor] is initialized. slice :: from_raw_parts_mut ( offset_ptr , len ) } ; Ok ( slice ) }
Allocates unaligned memory of the given length, moving the cursor
forward as necessary.
Allocates unaligned memory of the given length moving the cursor forward as necessary .
&self
len
usize