Compare commits

1 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
4 changed files with 46 additions and 70 deletions

View File

@@ -11,7 +11,7 @@ const INITIAL_HEAP_SIZE: usize = MEMFD_INITIAL_SIZE;
const METADATA_SIZE: usize = size_of::<Metadata>();
pub(crate) static ALLOCATOR: LazyLock<Mutex<Allocator>> =
LazyLock::new(|| Mutex::new(Allocator::new()));
LazyLock::new(|| unsafe { Mutex::new(Allocator::new()) });
struct Metadata {
chunk: NonNull<Chunk>,
@@ -34,14 +34,14 @@ unsafe impl Send for Chunk {}
unsafe impl Send for Allocator {}
impl Allocator {
fn new() -> Self {
unsafe fn new() -> Self {
let mut allocator = BUMP_ALLOCATOR.lock().unwrap();
let mem = unsafe { allocator.alloc(INITIAL_HEAP_SIZE).unwrap() };
let mem = allocator.alloc(INITIAL_HEAP_SIZE).unwrap();
let head = Box::new(Chunk {
buffer: unsafe { mem.byte_add(METADATA_SIZE) },
size: INITIAL_HEAP_SIZE - METADATA_SIZE,
buffer: mem.byte_add(METADATA_SIZE),
size: INITIAL_HEAP_SIZE,
in_use: false,
next_chunk: None,
prev_chunk: None,
@@ -49,32 +49,27 @@ impl Allocator {
let head = NonNull::new(Box::leak(head)).unwrap();
let mem = mem as *mut Metadata;
unsafe {
*mem = Metadata { chunk: head };
}
*mem = Metadata { chunk: head };
Allocator { head, tail: head }
}
pub(crate) fn allocate(&mut self, size: usize) -> Option<*mut u8> {
pub(crate) unsafe fn allocate(&mut self, size: usize) -> Option<*mut u8> {
let size = (size + ALIGNMENT - 1) / ALIGNMENT * ALIGNMENT;
let mut head = Some(self.head);
while head.is_some() {
// The heap uses a global Mutex. Only one thread can operate on it at a time.
let current_head = unsafe { head.unwrap().as_mut() };
let current_head = head.unwrap().as_mut();
if !current_head.in_use && current_head.size >= size {
if current_head.size < (size + METADATA_SIZE + ALIGNMENT) {
if current_head.size == size {
current_head.in_use = true;
return Some(current_head.buffer);
}
let unused_space = Box::new(Chunk {
// We know that size of buffer is larger than size + METADATA_SIZE + ALIGNMENT.
// Therefore size + METADATA_SIZE is still inside of the buffer
buffer: unsafe { current_head.buffer.byte_add(size + METADATA_SIZE) },
buffer: current_head.buffer.byte_add(size + METADATA_SIZE),
size: current_head.size - size - METADATA_SIZE,
in_use: false,
next_chunk: current_head.next_chunk,
@@ -82,20 +77,11 @@ impl Allocator {
});
let ptr = NonNull::new(Box::leak(unused_space)).unwrap();
// buffer points to current_head + size + METADATA_SIZE.
// Therefore buffer - METADATA_SIZE points right after the buffer of current_head
// and right before the buffer of unused_space.
// This is where the pointer to the metadata chunk is expected
unsafe {
*(ptr.as_ref().buffer.byte_sub(METADATA_SIZE) as *mut Metadata) =
Metadata { chunk: ptr };
}
*(ptr.as_ref().buffer.byte_sub(METADATA_SIZE) as *mut Metadata) =
Metadata { chunk: ptr };
// We know that accessing ptr is safe since we just allocated it.
unsafe {
if ptr.as_ref().next_chunk.is_none() {
self.tail = ptr;
}
if ptr.as_ref().next_chunk.is_none() {
self.tail = ptr;
}
current_head.in_use = true;
@@ -113,9 +99,9 @@ impl Allocator {
None
}
pub(crate) fn deallocate(&mut self, ptr: *mut u8) {
let metadata = unsafe { ptr.byte_sub(METADATA_SIZE) as *mut Metadata };
let metadata = unsafe { (*metadata).chunk.as_mut() };
pub(crate) unsafe fn deallocate(&mut self, ptr: *mut u8) {
let metadata = ptr.byte_sub(METADATA_SIZE) as *mut Metadata;
let metadata = (*metadata).chunk.as_mut();
debug_assert_eq!(metadata.in_use, true);
debug_assert_eq!(metadata.buffer, ptr);

View File

@@ -3,7 +3,7 @@ use std::{
usize,
};
use libc::{c_char, c_void, ftruncate, mmap, perror, syscall, SYS_memfd_secret, MAP_FAILED, 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;
const MMAP_SIZE: usize = 1024 * 1024;
@@ -26,10 +26,7 @@ impl BumpAllocator {
unsafe fn new() -> Self {
assert!(MMAP_SIZE >= MEMFD_INITIAL_SIZE);
let data_fd = syscall(SYS_memfd_secret, 0) as i32;
if data_fd <= 0 {
perror("memfd secret\x00".as_ptr() as *const c_char);
}
let data_fd = memfd_create("data\x00".as_ptr() as *const c_char, 0);
assert!(data_fd > 0);

View File

@@ -1,3 +1,6 @@
#![feature(test)]
#![feature(lazy_cell)]
use bump_allocator::BUMP_ALLOCATOR;
mod allocator;

View File

@@ -1,64 +1,54 @@
use core::slice;
use std::ops::{Deref, DerefMut};
use std::{
ops::{Deref, DerefMut},
usize,
};
use crate::allocator::ALLOCATOR;
#[derive(Debug)]
pub struct SharedPtr {
ptr: *mut u8,
size: usize
}
pub struct SharedPtr<const N: usize>([u8; N]);
unsafe impl Send for SharedPtr {}
unsafe impl Sync for SharedPtr {}
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 = allocator.allocate(size)?;
let buf = unsafe {
let buf = allocator.allocate(N)?;
slice::from_raw_parts_mut(buf, N)
};
Some(SharedPtr{ptr: buf, size})
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.ptr) }
}
pub fn get_size(&self) -> usize {
self.size
unsafe { allocator.get_offset(self.as_ptr()) }
}
}
impl Deref for SharedPtr {
type Target = [u8];
impl<const N: usize> Deref for SharedPtr<N> {
type Target = [u8; N];
fn deref(&self) -> &Self::Target {
unsafe {
slice::from_raw_parts(self.ptr, self.size)
.try_into()
.expect("This should never fail")
}
&self.0
}
}
impl DerefMut for SharedPtr {
impl<'a, const N: usize> DerefMut for SharedPtr<N> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe {
slice::from_raw_parts_mut(self.ptr, self.size)
.try_into()
.expect("This should never fail")
}
&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();
allocator.deallocate(self.ptr);
unsafe {
allocator.deallocate(self.0.as_mut_ptr());
}
}
}
@@ -68,7 +58,7 @@ 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);
@@ -76,7 +66,7 @@ mod tests {
#[test]
fn slice() {
let mut x = SharedPtr::new(10).unwrap();
let mut x = SharedPtr::<10>::new().unwrap();
x[0] = 1;
x[1] = 2;
assert_eq!(x[0..=1], [1, 2]);