feat: get_offset for SharedPtr

This commit is contained in:
2024-04-05 13:12:19 +02:00
parent 536fb4cac3
commit d409a38eca
3 changed files with 24 additions and 2 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

@@ -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,22 @@ 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
}
}
#[cfg(test)]

View File

@@ -19,6 +19,14 @@ impl SharedPtr<'_> {
Some(SharedPtr(buf))
}
pub fn get_offset(&self) -> usize {
let allocator = ALLOCATOR.lock().unwrap();
unsafe {
allocator.get_offset(self.as_ptr())
}
}
}
impl<'a> Deref for SharedPtr<'a> {