Skip to content

Commit

Permalink
[pdsl_core] Add tests for DynAlloc and fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbepop committed Feb 25, 2019
1 parent 4d927e2 commit 3da097a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
37 changes: 27 additions & 10 deletions core/src/storage/alloc/dyn_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,42 @@ impl Flush for DynAlloc {
}
}

#[cfg(test)]
impl DynAlloc {
pub(crate) fn cells_origin(&self) -> Key {
self.cells_origin
}

pub(crate) fn chunks_origin(&self) -> Key {
self.chunks_origin
}
}

impl DynAlloc {
/// Allocates another cell and returns its key.
fn alloc_cell(&mut self) -> Key {
let offset = match self.free_cells.first_set_position() {
Some(free_idx) => free_idx,
None => self.free_cells.len(),
let offset = if let Some(free) = self.free_cells.first_set_position() {
self.free_cells.set(free, false);
free
} else {
let len = self.free_cells.len();
self.free_cells.push(false);
len
};
self.free_cells.push(false);
self.cells_origin + offset
}

/// Allocates another chunk and returns its key.
fn alloc_chunk(&mut self) -> Key {
let offset = match self.free_chunks.first_set_position() {
Some(free_idx) => free_idx,
None => self.free_chunks.len(),
let offset = if let Some(free) = self.free_chunks.first_set_position() {
self.free_chunks.set(free, false);
free
} else {
let len = self.free_chunks.len();
self.free_chunks.push(false);
len
};
self.free_chunks.push(false);
self.chunks_origin + offset
self.chunks_origin + ((1 << 32) * u64::from(offset))
}

/// Deallocates the cell key.
Expand All @@ -110,7 +127,7 @@ impl DynAlloc {
/// This just frees the associated slot for future allocations.
fn dealloc_chunk(&mut self, key: Key) {
debug_assert!(key >= self.chunks_origin);
debug_assert!(key < self.chunks_origin + self.free_chunks.len());
debug_assert!(key < self.chunks_origin + ((1 << 32) * self.free_chunks.len() as u64));
let position = self.key_to_chunk_position(key);
self.free_chunks.set(position, true);
}
Expand Down
64 changes: 63 additions & 1 deletion core/src/storage/alloc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
};

#[test]
fn simple() {
fn cc_simple() {
run_test(|| {
use crate::storage;

Expand Down Expand Up @@ -77,3 +77,65 @@ fn simple() {
assert_eq!(alloc.alloc(u32::max_value()), chunk_allocs[3]);
})
}

#[test]
fn dyn_simple() {
run_test(|| {
use crate::storage;

let mut alloc = unsafe {
let mut fw_alloc = storage::alloc::BumpAlloc::from_raw_parts(
Key([0x0; 32])
);
let mut dyn_alloc = storage::alloc::DynAlloc::allocate_using(&mut fw_alloc);
dyn_alloc.initialize(());
dyn_alloc
};

let cells_entries = dbg!(alloc.cells_origin());
let chunks_entries = dbg!(alloc.chunks_origin());

println!("cells_entries = {:?}", cells_entries);
println!("chunks_entries = {:?}", chunks_entries);

let mut cell_allocs = Vec::new();
let mut chunk_allocs = Vec::new();

// Cell allocations
for i in 0..10 {
let allocated_key = alloc.alloc(1);
assert_eq!(allocated_key, cells_entries + (i as u32));
cell_allocs.push(allocated_key);
}

// Chunk allocations
let alloc_sizes = &[10, u32::max_value(), 1337, 2, 9999_9999];
for (i, &size) in alloc_sizes.into_iter().enumerate() {
let allocated_key = alloc.alloc(size);
assert_eq!(allocated_key, chunks_entries + ((1 << 32) * (i as u64)));
chunk_allocs.push(allocated_key);
}

// Deallocate first cell again
alloc.dealloc(cell_allocs[0]);
// Now the next cell allocation will take the first allocation cell again
assert_eq!(alloc.alloc(1), cell_allocs[0]);

// Deallocate 2nd and 4th allocations in reverse order
alloc.dealloc(cell_allocs[3]);
alloc.dealloc(cell_allocs[1]);
assert_eq!(alloc.alloc(1), cell_allocs[1]);
assert_eq!(alloc.alloc(1), cell_allocs[3]);

// Deallocate first chunk again
alloc.dealloc(chunk_allocs[0]);
// Now the next chunk allocation will take the first allocation cell again
assert_eq!(alloc.alloc(u32::max_value()), chunk_allocs[0]);

// Deallocate 2nd and 4th allocations in reverse order
alloc.dealloc(chunk_allocs[3]);
alloc.dealloc(chunk_allocs[1]);
assert_eq!(alloc.alloc(u32::max_value()), chunk_allocs[1]);
assert_eq!(alloc.alloc(u32::max_value()), chunk_allocs[3]);
})
}

0 comments on commit 3da097a

Please sign in to comment.