Struct maskerad_memory_allocators::DoubleEndedStackAllocator [] [src]

pub struct DoubleEndedStackAllocator { /* fields omitted */ }

A double-ended allocator.

It manages two StackAllocators.

Purpose

Suppose you want to load data A, and this data need the temporary data B. You need to load B before A in order to create it.

After A is loaded, B is no longer needed. However, since this allocator is a stack, you need to free A before freeing B.

That's why this allocator has two memory chunks, one for temporary data, one for the resident data who need temporary data to be created.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;

//50 bytes for the MemoryChunks storing data implementing the Drop trait, 50 for the others.
let double_ended_allocator = DoubleEndedStackAllocator::with_capacity(100, 100);

//Markers to the bottom of the stacks.
let top_resident = double_ended_allocator.marker_resident();
let top_temp = double_ended_allocator.marker_temp();

let my_vec: &Vec<u8> = double_ended_allocator.alloc_temp(|| {
    Vec::with_capacity(10)
}).unwrap();

let my_vec_2: &Vec<u8> = double_ended_allocator.alloc_resident(|| {
    Vec::with_capacity(10)
}).unwrap();

double_ended_allocator.reset_temp();

assert_eq!(top_temp, double_ended_allocator.marker_temp());
assert_ne!(top_resident, double_ended_allocator.marker_resident());

Methods

impl DoubleEndedStackAllocator
[src]

[src]

Creates a DoubleEndedStackAllocator with the given capacity, in bytes.

Example

#![feature(alloc)]
use maskerad_memory_allocators::DoubleEndedStackAllocator;

let allocator = DoubleEndedStackAllocator::with_capacity(100, 50);
assert_eq!(allocator.stack_temp().storage().capacity(), 50);
assert_eq!(allocator.stack_temp().storage_copy().capacity(), 25);
assert_eq!(allocator.stack_resident().storage().capacity(), 50);
assert_eq!(allocator.stack_resident().storage_copy().capacity(), 25);

[src]

Returns an immutable reference to the StackAllocator used for resident allocation.

[src]

Returns an immutable reference to the StackAllocator used for temporary allocation.

[src]

Allocates data in the StackAllocator used for resident memory, returning a mutable reference to the allocated data.

If the allocated data implements Drop, it will be placed in the MemoryChunk storing data implementing the Drop trait. Otherwise, it will be placed in the other MemoryChunk.

Panics

This function will panic if the allocation exceeds the maximum storage capacity of the allocator.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;


let allocator = DoubleEndedStackAllocator::with_capacity(100, 100);

let my_vec: &mut Vec<u8> = allocator.alloc_mut_resident(|| {
    Vec::with_capacity(10)
}).unwrap();

my_vec.push(1);

assert!(!my_vec.is_empty());

[src]

Allocates data in the StackAllocator used for resident memory, returning an immutable reference to the allocated data.

If the allocated data implements Drop, it will be placed in the MemoryChunk storing data implementing the Drop trait. Otherwise, it will be placed in the other MemoryChunk.

Panics

This function will panic if the allocation exceeds the maximum storage capacity of the allocator.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;

let allocator = DoubleEndedStackAllocator::with_capacity(100, 100);

let my_vec: &Vec<u8> = allocator.alloc_resident(|| {
    Vec::with_capacity(10)
}).unwrap();

assert!(my_vec.is_empty());

[src]

Allocates data in the StackAllocator used for temporary memory, returning a mutable reference to the allocated data.

If the allocated data implements Drop, it will be placed in the MemoryChunk storing data implementing the Drop trait. Otherwise, it will be placed in the other MemoryChunk.

Panics

This function will panic if the allocation exceeds the maximum storage capacity of the allocator.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;


let allocator = DoubleEndedStackAllocator::with_capacity(100, 100);

let my_vec: &mut Vec<u8> = allocator.alloc_mut_temp(|| {
    Vec::with_capacity(10)
}).unwrap();

my_vec.push(1);

assert!(!my_vec.is_empty());

[src]

Allocates data in the StackAllocator used for temporary memory, returning an immutable reference to the allocated data.

If the allocated data implements Drop, it will be placed in the MemoryChunk storing data implementing the Drop trait. Otherwise, it will be placed in the other MemoryChunk.

Panics

This function will panic if the allocation exceeds the maximum storage capacity of the allocator.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;

let allocator = DoubleEndedStackAllocator::with_capacity(100, 100);

let my_vec: &Vec<u8> = allocator.alloc_temp(|| {
    Vec::with_capacity(10)
}).unwrap();

assert!(my_vec.is_empty());

[src]

Returns the index of the first unused memory address in the MemoryChunk storing data implementing the Drop trait of the StackAllocator used for resident memory.

[src]

Returns the index of the first unused memory address in the MemoryChunk storing data implementing the Copy trait of the StackAllocator used for resident memory.

[src]

Returns the index of the first unused memory address in the MemoryChunk storing data implementing the Drop trait of the StackAllocator used for temporary memory.

[src]

Returns the index of the first unused memory address in the MemoryChunk storing data implementing the Copy trait of the StackAllocator used for temporary memory.

[src]

Reset the MemoryChunk storing data implementing the Drop trait of the StackAllocator used for resident memory, dropping all the content residing inside it.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;


let allocator = DoubleEndedStackAllocator::with_capacity(100, 100);

//When nothing has been allocated, the first unused memory address is at index 0.
assert_eq!(allocator.marker_resident(), 0);
assert_eq!(allocator.marker_temp(), 0);

let my_vec: &Vec<u8> = allocator.alloc_resident(|| {
    Vec::with_capacity(10)
}).unwrap();

assert_ne!(allocator.marker_resident(), 0);

allocator.reset_resident();

//The memory chunk storing data implementing the Drop trait of the StackAllocator used for resident memory data has been totally reset, and all its content has been dropped.
assert_eq!(allocator.marker_resident(), 0);

[src]

Reset the MemoryChunk storing data implementing the Copy trait of the StackAllocator used for resident memory.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;


let allocator = DoubleEndedStackAllocator::with_capacity(100, 100);

//When nothing has been allocated, the first unused memory address is at index 0.
assert_eq!(allocator.marker_resident_copy(), 0);
assert_eq!(allocator.marker_temp_copy(), 0);

let my_i32 = allocator.alloc_resident(|| {
    8 as i32
}).unwrap();

assert_ne!(allocator.marker_resident_copy(), 0);

allocator.reset_resident_copy();

//The memory chunk storing data implementing the Copy trait of the StackAllocator used for resident memory data has been totally reset.
assert_eq!(allocator.marker_resident_copy(), 0);

[src]

Reset the MemoryChunk storing data implementing the Drop trait of the StackAllocator used for temporary memory, dropping all the content residing inside it.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;


let allocator = DoubleEndedStackAllocator::with_capacity(200, 200);

//When nothing has been allocated, the first unused memory address is at index 0.
assert_eq!(allocator.marker_resident(), 0);
assert_eq!(allocator.marker_temp(), 0);

let my_vec: &Vec<u8> = allocator.alloc_temp(|| {
    Vec::with_capacity(10)
}).unwrap();

assert_ne!(allocator.marker_temp(), 0);

allocator.reset_temp();

//The memory chunk storing data implementing the Drop trait of the StackAllocator used for temporary memory data has been totally reset, and all its content has been dropped.
assert_eq!(allocator.marker_temp(), 0);

[src]

Reset the MemoryChunk storing data implementing the Copy trait of the StackAllocator used for temporary memory.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;


let allocator = DoubleEndedStackAllocator::with_capacity(200, 200);

//When nothing has been allocated, the first unused memory address is at index 0.
assert_eq!(allocator.marker_resident_copy(), 0);
assert_eq!(allocator.marker_temp_copy(), 0);

let my_i32 = allocator.alloc_temp(|| {
    8 as i32
}).unwrap();

assert_ne!(allocator.marker_temp_copy(), 0);

allocator.reset_temp_copy();

//The memory chunk storing data implementing the Copy trait of the StackAllocator used for temporary memory data has been totally reset.
assert_eq!(allocator.marker_temp_copy(), 0);

[src]

Reset partially the MemoryChunk storing data implementing the Drop trait of the StackAllocator used for resident memory, dropping all the content residing between the current top of the stack and this marker.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;


let allocator = DoubleEndedStackAllocator::with_capacity(200, 200);

//When nothing has been allocated, the first unused memory address is at index 0.
assert_eq!(allocator.marker_resident(), 0);
assert_eq!(allocator.marker_temp(), 0);

let my_vec: &Vec<u8> = allocator.alloc_resident(|| {
    Vec::with_capacity(10)
}).unwrap();

assert_ne!(allocator.marker_resident(), 0);
//Get a marker
let marker = allocator.marker_resident();

let my_vec_2: &Vec<u8> = allocator.alloc_resident(|| {
    Vec::with_capacity(10)
}).unwrap();

allocator.reset_to_marker_resident(marker);

//The memory chunk storing data implementing the Drop trait of the StackAllocator used for resident memory data has been partially reset,
//and all the content between the current top of the stack and the marker has been dropped.
assert_ne!(allocator.marker_resident(), 0);
assert_eq!(allocator.marker_resident(), marker);

[src]

Reset partially the MemoryChunk storing data implementing the Copy trait of the StackAllocator used for resident memory.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;


let allocator = DoubleEndedStackAllocator::with_capacity(200, 200);

//When nothing has been allocated, the first unused memory address is at index 0.
assert_eq!(allocator.marker_resident_copy(), 0);
assert_eq!(allocator.marker_temp_copy(), 0);

let my_i32 = allocator.alloc_resident(|| {
    8 as i32
}).unwrap();

assert_ne!(allocator.marker_resident_copy(), 0);
//Get a marker
let marker = allocator.marker_resident_copy();

let my_i32_2 = allocator.alloc_resident(|| {
    9 as i32
}).unwrap();

allocator.reset_to_marker_resident_copy(marker);

//The memory chunk storing data implementing the Copy trait of the StackAllocator used for resident memory data has been partially reset.
assert_ne!(allocator.marker_resident_copy(), 0);
assert_eq!(allocator.marker_resident_copy(), marker);

[src]

Reset partially the MemoryChunk storing data implementing the Drop trait of the StackAllocator used for temporary memory, dropping all the content residing between the current top of the stack and this marker.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;


let allocator = DoubleEndedStackAllocator::with_capacity(200, 200);

//When nothing has been allocated, the first unused memory address is at index 0.
assert_eq!(allocator.marker_resident(), 0);
assert_eq!(allocator.marker_temp(), 0);

let my_vec: &Vec<u8> = allocator.alloc_temp(|| {
    Vec::with_capacity(10)
}).unwrap();

assert_ne!(allocator.marker_temp(), 0);
//Get a marker
let marker = allocator.marker_temp();

let my_vec_2: &Vec<u8> = allocator.alloc_temp(|| {
    Vec::with_capacity(10)
}).unwrap();

allocator.reset_to_marker_temp(marker);

//The memory chunk storing data implementing the Drop trait of the StackAllocator used for temporary memory data has been partially reset,
//and all the content between the current top of the stack and the marker has been dropped.
assert_ne!(allocator.marker_temp(), 0);
assert_eq!(allocator.marker_temp(), marker);

[src]

Reset partially the MemoryChunk storing data implementing the Copy trait of the StackAllocator used for temporary memory.

Example

use maskerad_memory_allocators::DoubleEndedStackAllocator;


let allocator = DoubleEndedStackAllocator::with_capacity(200, 200);

//When nothing has been allocated, the first unused memory address is at index 0.
assert_eq!(allocator.marker_resident_copy(), 0);
assert_eq!(allocator.marker_temp_copy(), 0);

let my_i32 = allocator.alloc_temp(|| {
    8 as i32
}).unwrap();

assert_ne!(allocator.marker_temp_copy(), 0);
//Get a marker
let marker = allocator.marker_temp_copy();

let my_i32_2 = allocator.alloc_temp(|| {
    9 as i32
}).unwrap();

allocator.reset_to_marker_temp_copy(marker);

//The memory chunk storing data implementing the Copy trait of the StackAllocator used for temporary memory data has been partially reset.
assert_ne!(allocator.marker_temp_copy(), 0);
assert_eq!(allocator.marker_temp_copy(), marker);