feat: get_offset for SharedPtr
This commit is contained in:
@@ -110,6 +110,11 @@ impl Allocator {
|
|||||||
|
|
||||||
// TODO: Consolidate chunks
|
// 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)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -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);
|
let new_head = self.head.byte_add(size);
|
||||||
|
|
||||||
if new_head > self.end_of_mem {
|
if new_head > self.end_of_mem {
|
||||||
@@ -75,13 +75,22 @@ impl BumpAllocator {
|
|||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dealloc(&mut self) {
|
pub(crate) fn dealloc(&mut self) {
|
||||||
self.number_of_allocated_chunks -= 1;
|
self.number_of_allocated_chunks -= 1;
|
||||||
|
|
||||||
if self.number_of_allocated_chunks == 0 {
|
if self.number_of_allocated_chunks == 0 {
|
||||||
self.head = self.start_of_mem;
|
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)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -19,6 +19,14 @@ impl SharedPtr<'_> {
|
|||||||
|
|
||||||
Some(SharedPtr(buf))
|
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> {
|
impl<'a> Deref for SharedPtr<'a> {
|
||||||
|
|||||||
Reference in New Issue
Block a user