diff --git a/src/allocator.rs b/src/allocator.rs index c497818..b90a0b7 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -43,7 +43,7 @@ pub struct GeneralPurposeAllocator { #[derive(Clone, Copy, FromBytes, FromZeroes, AsBytes, Unaligned)] #[repr(C)] -struct FreeListBlock { +pub struct FreeListBlock { next: FilePointer, size: u8, } diff --git a/src/lib.rs b/src/lib.rs index 61e8e18..6f24c3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,13 +26,24 @@ type U64 = zerocopy::byteorder::U64; type U32 = zerocopy::byteorder::U32; type U16 = zerocopy::byteorder::U16; -#[derive(Clone, Copy, FromBytes, FromZeroes, AsBytes, Unaligned, Hash)] +#[derive(FromBytes, FromZeroes, AsBytes, Unaligned, Hash)] #[repr(transparent)] pub struct FilePointer { inner: RawFilePointer, _phantom: PhantomData<*const T>, } +impl Clone for FilePointer { + fn clone(&self) -> Self { + Self { + inner: self.inner, + _phantom: PhantomData, + } + } +} + +impl Copy for FilePointer {} + impl Debug for FilePointer { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.inner.fmt(f) @@ -196,21 +207,21 @@ impl FileRange { #[derive(Clone, Copy, FromBytes, FromZeroes, AsBytes, Unaligned)] #[repr(C)] -struct Header { +struct Header { magic: [u8; 16], - root: FilePointer, + root: RawFilePointer, // if we make this FilePointer, as it should be, AsBytes can not be derived allocator_state: AllocatorState, } -impl Default for Header { +impl Default for Header { fn default() -> Self { Self { magic: *b"cool db format 1", - root: FilePointer::null(), + root: RawFilePointer::null(), allocator_state: AllocatorState { general: RawFilePointer::null(), slabs: SlabListPointer(FilePointer::new( - RawFilePointer::null() + size_of::>() as u64, + RawFilePointer::null() + size_of::
() as u64, )), }, } @@ -257,11 +268,15 @@ struct SnapshotAndFreeList { } impl Db { - fn header(&self) -> &Header { + fn root(&self) -> FilePointer { + FilePointer::new(self.header().root) + } + + fn header(&self) -> &Header { unsafe { self.reference_range_unchecked(Self::header_ptr().range()) } } - fn header_mut(&mut self) -> &mut Header { + fn header_mut(&mut self) -> &mut Header { unsafe { self.modify_range_unchecked(Self::header_ptr().range()) } } @@ -315,16 +330,16 @@ impl Db { self.snapshots = snapshots; } - fn header_ptr() -> FilePointer> { + fn header_ptr() -> FilePointer
{ FilePointer::new(RawFilePointer(0.into())) } fn root_ptr() -> FilePointer> { - FilePointer::new(RawFilePointer((size_of::>() as u64).into())) + FilePointer::new(RawFilePointer((size_of::
() as u64).into())) } fn allocator_state_ptr() -> RawFilePointer { - RawFilePointer((size_of::>() as u64 + size_of::() as u64).into()) + RawFilePointer((size_of::
() as u64 + size_of::() as u64).into()) } fn general_purpose_allocator() -> GeneralPurposeAllocator { @@ -486,7 +501,7 @@ impl Db { } let _ = db.state.swap(Arc::new(Snapshot { - root: db.header().root, + root: db.root(), map: unsafe { Mmap::map(&db.file).unwrap() }, })); @@ -509,7 +524,7 @@ impl Db { }; let _ = db.state.swap(Arc::new(Snapshot { - root: db.header().root, + root: db.root(), map: unsafe { Mmap::map(&db.file).unwrap() }, })); @@ -520,9 +535,7 @@ impl Db { let allocator_state = self.header().allocator_state; allocator_state.slabs.init( self, - (PAGE_SIZE - size_of::>() as u64) - .try_into() - .unwrap(), + (PAGE_SIZE - size_of::
() as u64).try_into().unwrap(), ); for &size in slabs { diff --git a/src/transaction.rs b/src/transaction.rs index c623798..a0e9046 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -170,6 +170,6 @@ impl<'t, R> TransactionHandle<'t, R> { } pub fn root(&self) -> FilePointer { - self.db.header().root + self.db.root() } }