Compare commits

...

7 Commits

Author SHA1 Message Date
43aa454a29 chore: The slice test encounters a weird error that looks like rust is doing something weird
---- sharedptr::tests::slice stdout ----
thread 'sharedptr::tests::slice' panicked at src/allocator.rs:106:9:
assertion `left == right` failed
  left: true
 right: true
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
2024-05-06 14:13:07 +02:00
72b2e58244 fix(sharedptr): fix type of Deref 2024-04-29 15:50:13 +02:00
f8d497f3e1 feat(sharedptr): add size to slice type information 2024-04-29 15:29:28 +02:00
5cd37dfc93 feat: put lenght of SharedPtr buffer in type information 2024-04-26 11:58:38 +02:00
e649e2b595 fix: reduce size of memory map 2024-04-22 15:02:11 +02:00
6edbb046fa feat: add function to get fd of memfd 2024-04-22 15:01:25 +02:00
d409a38eca feat: get_offset for SharedPtr 2024-04-05 13:12:19 +02:00
4 changed files with 58 additions and 17 deletions

View File

@@ -110,6 +110,11 @@ impl Allocator {
// TODO: Consolidate chunks
}
pub(crate) unsafe fn get_offset(&self, ptr: *const u8) -> usize {
let allocator = BUMP_ALLOCATOR.lock().unwrap();
allocator.get_offset(ptr)
}
}
#[cfg(test)]

View File

@@ -3,10 +3,10 @@ use std::{
usize,
};
use libc::{c_char, c_void, ftruncate, memfd_create, mmap, MAP_SHARED, PROT_READ, PROT_WRITE};
use libc::{c_char, c_void, ftruncate, memfd_create, mmap, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE};
pub(crate) const MEMFD_INITIAL_SIZE: usize = 1024 * 1024 * 4;
const MMAP_SIZE: usize = 1024 * 1024 * 1024;
pub(crate) const MEMFD_INITIAL_SIZE: usize = 1024 * 1024;
const MMAP_SIZE: usize = 1024 * 1024;
pub(crate) static BUMP_ALLOCATOR: LazyLock<Mutex<BumpAllocator>> =
LazyLock::new(|| unsafe { Mutex::new(BumpAllocator::new()) });
@@ -41,7 +41,7 @@ impl BumpAllocator {
0,
) as *mut u8;
assert_ne!(start_of_mem, 0 as *mut u8);
assert_ne!(start_of_mem, MAP_FAILED as *mut u8);
let end_of_mem = start_of_mem.byte_add(MEMFD_INITIAL_SIZE);
@@ -55,7 +55,7 @@ impl BumpAllocator {
}
}
pub unsafe fn alloc(&mut self, size: usize) -> Option<*mut u8> {
pub(crate) unsafe fn alloc(&mut self, size: usize) -> Option<*mut u8> {
let new_head = self.head.byte_add(size);
if new_head > self.end_of_mem {
@@ -75,13 +75,26 @@ impl BumpAllocator {
ret
}
pub fn dealloc(&mut self) {
pub(crate) fn dealloc(&mut self) {
self.number_of_allocated_chunks -= 1;
if self.number_of_allocated_chunks == 0 {
self.head = self.start_of_mem;
}
}
pub(crate) unsafe fn get_offset(&self, ptr: *const u8) -> usize {
let offset = ptr.byte_offset_from(self.start_of_mem);
debug_assert!(offset >= 0);
debug_assert!(offset < self.end_of_mem.byte_offset_from(self.start_of_mem));
offset as usize
}
pub(crate) fn get_fd(&self) -> i32 {
self.backing_fd
}
}
#[cfg(test)]

View File

@@ -1,6 +1,14 @@
#![feature(test)]
#![feature(lazy_cell)]
use bump_allocator::BUMP_ALLOCATOR;
mod allocator;
mod bump_allocator;
pub mod sharedptr;
pub fn get_shared_mem_fd() -> i32 {
let allocator = BUMP_ALLOCATOR.lock().unwrap();
allocator.get_fd()
}

View File

@@ -6,36 +6,43 @@ use std::{
use crate::allocator::ALLOCATOR;
pub struct SharedPtr<'a>(&'a mut [u8]);
#[derive(Debug)]
pub struct SharedPtr<const N: usize>([u8; N]);
impl SharedPtr<'_> {
pub fn new(size: usize) -> Option<Self> {
impl<const N: usize> SharedPtr<N> {
pub fn new() -> Option<Self> {
let mut allocator = ALLOCATOR.lock().unwrap();
let buf = unsafe {
let buf = allocator.allocate(size)?;
slice::from_raw_parts_mut(buf, size)
let buf = allocator.allocate(N)?;
slice::from_raw_parts_mut(buf, N)
};
Some(SharedPtr(buf))
Some(SharedPtr(buf.try_into().expect("Should never fail")))
}
pub fn get_offset(&self) -> usize {
let allocator = ALLOCATOR.lock().unwrap();
unsafe { allocator.get_offset(self.as_ptr()) }
}
}
impl<'a> Deref for SharedPtr<'a> {
type Target = &'a mut [u8];
impl<const N: usize> Deref for SharedPtr<N> {
type Target = [u8; N];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> DerefMut for SharedPtr<'a> {
impl<'a, const N: usize> DerefMut for SharedPtr<N> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Drop for SharedPtr<'_> {
impl<const N: usize> Drop for SharedPtr<N> {
fn drop(&mut self) {
let mut allocator = ALLOCATOR.lock().unwrap();
@@ -51,9 +58,17 @@ mod tests {
#[test]
fn test() {
let mut x = SharedPtr::new(10).unwrap();
let mut x = SharedPtr::<10>::new().unwrap();
x[0] = 1;
assert_eq!(x[0], 1);
drop(x);
}
#[test]
fn slice() {
let mut x = SharedPtr::<10>::new().unwrap();
x[0] = 1;
x[1] = 2;
assert_eq!(x[0..=1], [1, 2]);
}
}