Compare commits
4 Commits
weird_rust
...
9ec0b728c2
| Author | SHA1 | Date | |
|---|---|---|---|
| 9ec0b728c2 | |||
| 0200b3c913 | |||
| 18f1a1b9a6 | |||
| 7c336c8769 |
@@ -3,7 +3,7 @@ use std::{
|
||||
usize,
|
||||
};
|
||||
|
||||
use libc::{c_char, c_void, ftruncate, memfd_create, mmap, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE};
|
||||
use libc::{c_char, c_void, ftruncate, mmap, perror, syscall, SYS_memfd_secret, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE};
|
||||
|
||||
pub(crate) const MEMFD_INITIAL_SIZE: usize = 1024 * 1024;
|
||||
const MMAP_SIZE: usize = 1024 * 1024;
|
||||
@@ -26,7 +26,10 @@ impl BumpAllocator {
|
||||
unsafe fn new() -> Self {
|
||||
assert!(MMAP_SIZE >= MEMFD_INITIAL_SIZE);
|
||||
|
||||
let data_fd = memfd_create("data\x00".as_ptr() as *const c_char, 0);
|
||||
let data_fd = syscall(SYS_memfd_secret, 0) as i32;
|
||||
if data_fd <= 0 {
|
||||
perror("memfd secret\x00".as_ptr() as *const c_char);
|
||||
}
|
||||
|
||||
assert!(data_fd > 0);
|
||||
|
||||
|
||||
@@ -1,24 +1,21 @@
|
||||
use core::slice;
|
||||
use std::{
|
||||
ops::{Deref, DerefMut},
|
||||
usize,
|
||||
};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use crate::allocator::ALLOCATOR;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SharedPtr<'a, const N: usize>(&'a mut [u8; N]);
|
||||
pub struct SharedPtr<const N: usize>(*mut u8);
|
||||
|
||||
impl<const N: usize> SharedPtr<'_, N> {
|
||||
unsafe impl<const N: usize> Send for SharedPtr<N> {}
|
||||
unsafe impl<const N: usize> Sync for SharedPtr<N> {}
|
||||
|
||||
impl<const N: usize> SharedPtr<N> {
|
||||
pub fn new() -> Option<Self> {
|
||||
let mut allocator = ALLOCATOR.lock().unwrap();
|
||||
|
||||
let buf = unsafe {
|
||||
let buf = allocator.allocate(N)?;
|
||||
slice::from_raw_parts_mut(buf, N)
|
||||
};
|
||||
let buf = unsafe { allocator.allocate(N)? };
|
||||
|
||||
Some(SharedPtr(buf.try_into().expect("Should never fail")))
|
||||
Some(SharedPtr(buf))
|
||||
}
|
||||
|
||||
pub fn get_offset(&self) -> usize {
|
||||
@@ -28,26 +25,34 @@ impl<const N: usize> SharedPtr<'_, N> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, const N: usize> Deref for SharedPtr<'a, N> {
|
||||
impl<const N: usize> Deref for SharedPtr<N> {
|
||||
type Target = [u8; N];
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
unsafe {
|
||||
slice::from_raw_parts(self.0, N)
|
||||
.try_into()
|
||||
.expect("This should never fail")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, const N: usize> DerefMut for SharedPtr<'a, N> {
|
||||
impl<const N: usize> DerefMut for SharedPtr<N> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
unsafe {
|
||||
slice::from_raw_parts_mut(self.0, N)
|
||||
.try_into()
|
||||
.expect("This should never fail")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> Drop for SharedPtr<'_, N> {
|
||||
impl<const N: usize> Drop for SharedPtr<N> {
|
||||
fn drop(&mut self) {
|
||||
let mut allocator = ALLOCATOR.lock().unwrap();
|
||||
|
||||
unsafe {
|
||||
allocator.deallocate(self.0.as_mut_ptr());
|
||||
allocator.deallocate(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user