# LockedHeapWithRescue in buddy_system_allocator - Rust
A locked version of `Heap` with rescue before oom
Keywords: rust, rustlang, rust-lang, LockedHeapWithRescue

# Struct buddy_system_allocator::LockedHeapWithRescue

source · [−]

    pub struct LockedHeapWithRescue { /* private fields */ }

Expand description

A locked version of `Heap` with rescue before oom

## Usage

Create a locked heap:

    use buddy_system_allocator::*;
    let heap = LockedHeapWithRescue::new(|heap: &mut Heap| {});

Before oom, the allocator will try to call rescue function and try for one more time.

## Implementations

source

### impl LockedHeapWithRescue

source

#### pub fn new(rescue: fn(_: &mut Heap)) -> LockedHeapWithRescue

Creates an empty heap

## Methods from Deref<Target = Mutex<Heap>>

source

#### pub fn is_locked(&self) -> bool

Returns `true` if the lock is currently held.

##### Safety

This function provides no synchronization guarantees and so its result should be considered ‘out of date’ the instant it is called. Do not use it for synchronization purposes. However, it may be useful as a heuristic.

source

#### pub fn lock(&self) -> MutexGuard<'_, T>

Locks the `Mutex` and returns a guard that permits access to the inner data.

The returned value may be dereferenced for data access and the lock will be dropped when the guard falls out of scope.

    let lock = spin::Mutex::new(0);
    {
        let mut data = lock.lock();
        // The lock is now locked and the data can be accessed
        *data += 1;
        // The lock is implicitly dropped at the end of the scope
    }

source

#### pub unsafe fn force_unlock(&self)

Force unlock this `Mutex`.

##### Safety

This is extremely unsafe if the lock is not held by the current thread. However, this can be useful in some instances for exposing the lock to FFI that doesn’t know how to deal with RAII.

source

#### pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>

Try to lock this `Mutex`, returning a lock guard if successful.

##### Example

    let lock = spin::Mutex::new(42);

    let maybe_guard = lock.try_lock();
    assert!(maybe_guard.is_some());

    // `maybe_guard` is still held, so the second call fails
    let maybe_guard2 = lock.try_lock();
    assert!(maybe_guard2.is_none());

## Trait Implementations

source

### impl Deref for LockedHeapWithRescue

#### type Target = Mutex<Heap>

The resulting type after dereferencing.

source

#### fn deref(&self) -> &Mutex<Heap>

Dereferences the value.

source

### impl GlobalAlloc for LockedHeapWithRescue

source

#### unsafe fn alloc(&self, layout: Layout) -> *mut u8

Allocate memory as described by the given `layout`. Read more

source

#### unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout)

Deallocate the block of memory at the given `ptr` pointer with the given `layout`. Read more

1.28.0 · source

#### unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8

Behaves like `alloc`, but also ensures that the contents are set to zero before being returned. Read more

1.28.0 · source

#### unsafe fn realloc(
&self,
ptr: *mut u8,
layout: Layout,
new_size: usize
) -> *mut u8

Shrink or grow a block of memory to the given `new_size`. The block is described by the given `ptr` pointer and `layout`. Read more

## Auto Trait Implementations

### impl !RefUnwindSafe for LockedHeapWithRescue

### impl Send for LockedHeapWithRescue

### impl Sync for LockedHeapWithRescue

### impl Unpin for LockedHeapWithRescue

### impl UnwindSafe for LockedHeapWithRescue

## Blanket Implementations

source

### impl<T> Any for T where
T: 'static + ?Sized,

source

#### fn type_id(&self) -> TypeId

Gets the `TypeId` of `self`. Read more

source

### impl<T> Borrow<T> for T where
T: ?Sized,

const: unstable · source

#### fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more

source

### impl<T> BorrowMut<T> for T where
T: ?Sized,

const: unstable · source

#### fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more

source

### impl<T> From<T> for T

const: unstable · source

#### fn from(t: T) -> T

Returns the argument unchanged.

source

### impl<T, U> Into<U> for T where
U: From<T>,

const: unstable · source

#### fn into(self) -> U

Calls `U::from(self)`.

That is, this conversion is whatever the implementation of `From<T> for U` chooses to do.

source

### impl<T, U> TryFrom<U> for T where
U: Into<T>,

#### type Error = Infallible

The type returned in the event of a conversion error.

const: unstable · source

#### fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.

source

### impl<T, U> TryInto<U> for T where
U: TryFrom<T>,

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

The type returned in the event of a conversion error.

const: unstable · source

#### fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.