pub fn allocate(&self, offset: usize, bytes: usize) -> Result<AllocatedMemory, Error> {
        self.addr
            .checked_add(offset)
            .and_then(|sum| sum.checked_add(bytes))
            .ok_or_else(|| ErrorKind::TooLarge)?; // overflow

        if offset + bytes > self.len {
            return Err(ErrorKind::TooLarge.into()); // doesn't fit in `self`
        }

        if bytes == 0 {
            return Err(ErrorKind::ZeroSize.into()); // zero-sized allocation would be 0 pages, don't allow it
        }

        if offset & (self.page_size() - 1) != 0 {
            return Err(ErrorKind::NotAligned(offset).into()); // not a multiple of the page size
        }

        // round the amount of memory up to full pages
        let bytes = bytes + self.page_size() - 1; // move up
        let bytes = bytes & !(self.page_size() - 1); // clear all lower bits

        let mut allocs = self.allocations.lock().unwrap();
        if allocs.find_allocation_overlapping(offset).is_ok() {
            return Err(ErrorKind::Overlap.into()); // overlaps existing allocation
        }

        let addr = self.addr + offset;
        imp::alloc(addr, bytes).map_err(ErrorKind::Os)?;

        allocs.register_allocation(offset, bytes);

        Ok(AllocatedMemory {
            addr,
            len: bytes,
            _p: PhantomData,
        })
    }
/// Allocates pages inside this reserved memory section and makes them
/// readable and writeable.
///
/// The content of the pages is undefined. Don't assume allocated memory
/// always starts out zeroed.
///
/// After writing your data to the allocation, you can mark it as read-only
/// or executable using `AllocatedMemory::set_protection`.
///
/// # Parameters
///
/// * `offset`: Offset into the reserved address space. The offset must be
///   aligned to whole pages, whose size can be obtained by calling
///   [`page_size`].
/// * `bytes`: Number of bytes to allocate.
///
/// [`page_size`]: #method.page_size
Allocates pages inside this reserved memory section and makes them
readable and writeable.

The content of the pages is undefined. Don't assume allocated memory
always starts out zeroed.

After writing your data to the allocation, you can mark it as read-only
or executable using `AllocatedMemory::set_protection`.
Allocates pages inside this reserved memory section and makes them readable and writeable . The content of the pages is undefined . Don ' t assume allocated memory always starts out zeroed . After writing your data to the allocation you can mark it as read - only or executable using ` AllocatedMemory :: set_protection ` .
pub fn allocate(&self, offset: usize, bytes: usize) -> Result<AllocatedMemory, Error> {
        self.addr
            .checked_add(offset)
            .and_then(|sum| sum.checked_add(bytes))
            .ok_or_else(|| ErrorKind::TooLarge)?; // overflow
        if offset + bytes > self.len {
            return Err(ErrorKind::TooLarge.into()); // doesn't fit in `self`
        }
        if bytes == 0 {
            return Err(ErrorKind::ZeroSize.into()); // zero-sized allocation would be 0 pages, don't allow it
        }
        if offset & (self.page_size() - 1) != 0 {
            return Err(ErrorKind::NotAligned(offset).into()); // not a multiple of the page size
        }
        // round the amount of memory up to full pages
        let bytes = bytes + self.page_size() - 1; // move up
        let bytes = bytes & !(self.page_size() - 1); // clear all lower bits
        let mut allocs = self.allocations.lock().unwrap();
        if allocs.find_allocation_overlapping(offset).is_ok() {
            return Err(ErrorKind::Overlap.into()); // overlaps existing allocation
        }
        let addr = self.addr + offset;
        imp::alloc(addr, bytes).map_err(ErrorKind::Os)?;
        allocs.register_allocation(offset, bytes);
        Ok(AllocatedMemory {
            addr,
            len: bytes,
            _p: PhantomData,
        })
    }
pub fn allocate ( & self , offset : usize , bytes : usize ) -> Result < AllocatedMemory , Error > { self . addr . checked_add ( offset ) . and_then ( | sum | sum . checked_add ( bytes ) ) . ok_or_else ( | | ErrorKind :: TooLarge ) ? ; // overflow if offset + bytes > self . len { return Err ( ErrorKind :: TooLarge . into ( ) ) ; // doesn't fit in `self` } if bytes == 0 { return Err ( ErrorKind :: ZeroSize . into ( ) ) ; // zero-sized allocation would be 0 pages, don't allow it } if offset & ( self . page_size ( ) - 1 ) != 0 { return Err ( ErrorKind :: NotAligned ( offset ) . into ( ) ) ; // not a multiple of the page size } // round the amount of memory up to full pages let bytes = bytes + self . page_size ( ) - 1 ; // move up let bytes = bytes & ! ( self . page_size ( ) - 1 ) ; // clear all lower bits let mut allocs = self . allocations . lock ( ) . unwrap ( ) ; if allocs . find_allocation_overlapping ( offset ) . is_ok ( ) { return Err ( ErrorKind :: Overlap . into ( ) ) ; // overlaps existing allocation } let addr = self . addr + offset ; imp :: alloc ( addr , bytes ) . map_err ( ErrorKind :: Os ) ? ; allocs . register_allocation ( offset , bytes ) ; Ok ( AllocatedMemory { addr , len : bytes , _p : PhantomData , } ) }
Allocates pages inside this reserved memory section and makes them
readable and writeable.
Allocates pages inside this reserved memory section and makes them readable and writeable .
&self
offset
usize
bytes
usize
&self
offset
usize
bytes
usize