[][src]Struct buddy_allocator::Buddies

pub struct Buddies<A: AllocRef = Global> { /* fields omitted */ }

Methods

impl Buddies<Global>[src]

pub fn new(max_order: usize, multiplier: usize, max_idx: Option<usize>) -> Self[src]

create a new instance

max_order determines how many different orders there are.

multiplier multiplies all indices by a certain value. eg if multiplier was 4 it would return 0, 4, 8, 12 instead of 0, 1, 2, 3

Panics

panics if:

  • max_order is zero
  • multiplier is not a power of two
  • max_ids is not a valid index
use buddy_allocator::Buddies;

let buddies = Buddies::new(3, 1, None);
buddies.allocate(2, 2).unwrap();

pub fn with_capacity(capacity: usize, multiplier: usize) -> Self[src]

create a new instance with the appropriate max_order to fit capacity

capacity must not be zero and be divisable by multiplier

see Buddies::new

use buddy_allocator::Buddies;

let buddies = Buddies::with_capacity(500, 1);
assert_eq!(buddies.capacity(), 500);
buddies.allocate(2, 2).unwrap();

impl<A: AllocRef> Buddies<A>[src]

pub fn new_in(
    max_order: usize,
    multiplier: usize,
    max_idx: Option<usize>,
    a: A
) -> Self
[src]

pub fn with_capacity_in(capacity: usize, multiplier: usize, a: A) -> Self[src]

pub fn capacity(&self) -> usize[src]

return the capacity

use buddy_allocator::Buddies;

let buddies = Buddies::new(3, 1, None);
assert_eq!(buddies.capacity(), 4);
let buddies = Buddies::new(3, 4, None);
assert_eq!(buddies.capacity(), 16);
let buddies = Buddies::new(3, 4, Some(12));
assert_eq!(buddies.capacity(), 12);

pub fn is_unused(&self) -> bool[src]

check if there are any allocations

Safety

calling this method is equivalent to trying to allocate the entire memory inside at once thus rendering it useless after it returned true

use buddy_allocator::Buddies;

let buddies = Buddies::new(3, 1, None);
let idx = buddies.allocate(1, 1).unwrap();
assert!(!buddies.is_unused());
buddies.deallocate(idx, 1);
assert!(buddies.is_unused());

pub fn real_size_for_allocation(&self, size: usize) -> usize[src]

get the real size of an allocation for a given size

use buddy_allocator::Buddies;

let buddies = Buddies::new(3, 1, None);
assert_eq!(buddies.real_size_for_allocation(0), 1);
assert_eq!(buddies.real_size_for_allocation(1), 1);
assert_eq!(buddies.real_size_for_allocation(2), 2);
assert_eq!(buddies.real_size_for_allocation(3), 4);
assert_eq!(buddies.real_size_for_allocation(4), 4);

let buddies = Buddies::new(3, 4, None);
assert_eq!(buddies.real_size_for_allocation(0), 4);
assert_eq!(buddies.real_size_for_allocation(4), 4);
assert_eq!(buddies.real_size_for_allocation(8), 8);
assert_eq!(buddies.real_size_for_allocation(12), 16);
assert_eq!(buddies.real_size_for_allocation(16), 16);

pub fn allocate(&self, size: usize, align: usize) -> Option<usize>[src]

allocate a buddy with a given size

Panics

panics if:

  • size or align are too big
  • align is not a power of two
use buddy_allocator::Buddies;

let buddies = Buddies::new(5, 1, None);
assert_eq!(buddies.allocate(1, 1).unwrap(), 0);
assert_eq!(buddies.allocate(2, 1).unwrap(), 2);
assert_eq!(buddies.allocate(2, 1).unwrap(), 4);
assert_eq!(buddies.allocate(2, 4).unwrap(), 8);

pub fn deallocate(&self, idx: usize, size: usize)[src]

deallocate a buddy with a given size

Panics

panics if:

  • there is no buddy with that size allocated at that index
use buddy_allocator::Buddies;

let buddies = Buddies::new(5, 1, None);
let idx1 = buddies.allocate(1, 1).unwrap();
let idx2 = buddies.allocate(2, 1).unwrap();
let idx3 = buddies.allocate(2, 1).unwrap();
let idx4 = buddies.allocate(2, 4).unwrap();
buddies.deallocate(idx1, 1);
buddies.deallocate(idx4, 2);
buddies.deallocate(idx2, 2);
buddies.deallocate(idx3, 2);

pub fn shrink(&self, idx: usize, old_size: usize, new_size: usize)[src]

shrink a buddy

Panics

panics if:

  • there is no buddy with that size allocated at that index
  • new_size is greater that old_size
use buddy_allocator::Buddies;

let buddies = Buddies::new(3, 1, None);
let idx = buddies.allocate(3, 1).unwrap();
buddies.shrink(idx, 3, 2);
buddies.shrink(idx, 2, 1);
buddies.shrink(idx, 1, 0);

pub fn grow(
    &self,
    idx: usize,
    old_size: usize,
    new_size: usize,
    placement: ReallocPlacement
) -> Option<usize>
[src]

grow a buddy

Panics

panics if:

  • there is no buddy with that size allocated at that index
  • new_size is smaller that old_size
  • new_size is too big
use alloc_wg::alloc::ReallocPlacement;
use buddy_allocator::Buddies;

let buddies = Buddies::new(3, 1, None);
let idx = buddies.allocate(0, 1).unwrap();
let idx = buddies.grow(idx, 0, 1, ReallocPlacement::InPlace).unwrap();
let idx = buddies.grow(idx, 1, 2, ReallocPlacement::MayMove).unwrap();
buddies.grow(idx, 2, 3, ReallocPlacement::InPlace).unwrap();

Auto Trait Implementations

impl<A> RefUnwindSafe for Buddies<A> where
    A: RefUnwindSafe

impl<A> Send for Buddies<A> where
    A: Send

impl<A> Sync for Buddies<A> where
    A: Sync

impl<A> Unpin for Buddies<A> where
    A: Unpin

impl<A> UnwindSafe for Buddies<A> where
    A: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.