Skip to content

Commit

Permalink
tmp commit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Wünsche committed Jul 21, 2023
1 parent 959f136 commit d82f950
Show file tree
Hide file tree
Showing 4 changed files with 555 additions and 102 deletions.
8 changes: 8 additions & 0 deletions betree/pmem-hashmap/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,17 @@ impl Pal {
unsafe { pmemobj_close(self.pool.as_ptr()) };
}

pub fn allocate_variable<T>(&self, v: T) -> Result<PalPtr<T>, PalError> {
let mut ptr = self.allocate(std::mem::size_of_val(&v))?;
assert!(ptr.size < 8192);
ptr.init(&v, std::mem::size_of_val(&v));
Ok(ptr)
}

/// Allocate an area of size in the persistent memory. Allocations are
/// always guaranteed to be cache line aligned for Optane PMem (64 bytes).
pub fn allocate<T>(&self, size: usize) -> Result<PalPtr<T>, PalError> {
assert!(size < 8192);
let mut oid = std::mem::MaybeUninit::<PMEMoid>::uninit();
if unsafe {
haura_alloc(
Expand Down
1 change: 1 addition & 0 deletions betree/src/replication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use zstd_safe::WriteBuf;

mod lru;
mod lru_worker;
mod shift_array;
mod tree;
use lru::Plru;
use serde::{Deserialize, Serialize};
Expand Down
105 changes: 105 additions & 0 deletions betree/src/replication/shift_array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#[derive(Debug)]
pub struct ShiftArray<T, const A: usize> {
count: usize,
arr: [Option<T>; A],
}

impl<T: PartialOrd, const A: usize> ShiftArray<T, A> {
/// In an ordered array find the index of the next largest element.
pub fn find(&self, v: &T) -> Option<usize> {
for idx in 0..self.count {
if self.arr[idx].as_ref().unwrap() >= v {
return Some(idx);
}
}
Some(self.count)
}
}

impl<T, const A: usize> ShiftArray<T, A> {
pub fn new() -> Self {
Self {
arr: [0u8; A].map(|_| None),
count: 0,
}
}

pub fn iter(&self) -> impl Iterator<Item = &T> {
self.arr.iter().filter_map(|e| e.as_ref())
}

pub fn split_after(&mut self, idx: usize) -> ShiftArray<T, A> {
assert!(idx < self.count);
let mut other = Self::new();
for cur in (idx + 1)..self.count {
other.push_back(self.arr[cur].take().unwrap());
}
self.count = idx + 1;
other
}

pub fn push_back(&mut self, val: T) {
// Full
assert!(self.count < A);
self.arr[self.count] = Some(val);
self.count += 1;
}

pub fn push_front(&mut self, val: T) {
self.insert(0, val)
}

pub fn get(&self, idx: usize) -> Option<&T> {
self.arr[idx].as_ref()
}

pub fn get_mut(&mut self, idx: usize) -> Option<&mut T> {
self.arr[idx].as_mut()
}

pub fn pop_back(&mut self) -> Option<T> {
self.remove(self.count - 1)
}

pub fn pop_front(&mut self) -> Option<T> {
self.remove(0)
}

pub fn last(&self) -> Option<&T> {
self.arr.get(self.count.saturating_sub(1)).unwrap().as_ref()
}

pub fn first(&self) -> Option<&T> {
self.arr[0].as_ref()
}

pub fn last_mut(&mut self) -> Option<&mut T> {
self.arr
.get_mut(self.count.saturating_sub(1))
.unwrap()
.as_mut()
}

pub fn insert(&mut self, idx: usize, val: T) {
assert!(self.count < A);
let mut tmp = Some(val);
for cur in idx..A {
std::mem::swap(&mut tmp, &mut self.arr[cur])
}
self.count += 1;
}

pub fn remove(&mut self, idx: usize) -> Option<T> {
let val = self.arr[idx].take();
// Skip last entry
for cur in idx..A - 1 {
self.arr[cur] = self.arr[cur + 1].take()
}
self.count -= 1;
val
}

pub fn size(&self) -> usize {
self.count
}
}
Loading

0 comments on commit d82f950

Please sign in to comment.