Compare commits
1 Commits
9ec0b728c2
...
weird_rust
| Author | SHA1 | Date | |
|---|---|---|---|
| 43aa454a29 |
@@ -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);
|
||||
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
use core::slice;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::{
|
||||
ops::{Deref, DerefMut},
|
||||
usize,
|
||||
};
|
||||
|
||||
use crate::allocator::ALLOCATOR;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SharedPtr<const N: usize>(*mut u8);
|
||||
|
||||
unsafe impl<const N: usize> Send for SharedPtr<N> {}
|
||||
unsafe impl<const N: usize> Sync for SharedPtr<N> {}
|
||||
pub struct SharedPtr<const N: usize>([u8; N]);
|
||||
|
||||
impl<const N: usize> SharedPtr<N> {
|
||||
pub fn new() -> Option<Self> {
|
||||
let mut allocator = ALLOCATOR.lock().unwrap();
|
||||
|
||||
let buf = unsafe { allocator.allocate(N)? };
|
||||
let buf = unsafe {
|
||||
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 {
|
||||
@@ -29,21 +32,13 @@ impl<const N: usize> Deref for SharedPtr<N> {
|
||||
type Target = [u8; N];
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe {
|
||||
slice::from_raw_parts(self.0, N)
|
||||
.try_into()
|
||||
.expect("This should never fail")
|
||||
}
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> DerefMut for SharedPtr<N> {
|
||||
impl<'a, const N: usize> DerefMut for SharedPtr<N> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
unsafe {
|
||||
slice::from_raw_parts_mut(self.0, N)
|
||||
.try_into()
|
||||
.expect("This should never fail")
|
||||
}
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +47,7 @@ impl<const N: usize> Drop for SharedPtr<N> {
|
||||
let mut allocator = ALLOCATOR.lock().unwrap();
|
||||
|
||||
unsafe {
|
||||
allocator.deallocate(self.0);
|
||||
allocator.deallocate(self.0.as_mut_ptr());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user