made Header non generic, but everything is now broken :(

This commit is contained in:
soruh 2023-08-03 00:40:55 +02:00
parent b6003fab95
commit ac8bca3868
3 changed files with 31 additions and 18 deletions

View File

@ -43,7 +43,7 @@ pub struct GeneralPurposeAllocator {
#[derive(Clone, Copy, FromBytes, FromZeroes, AsBytes, Unaligned)]
#[repr(C)]
struct FreeListBlock {
pub struct FreeListBlock {
next: FilePointer<FreeListBlock>,
size: u8,
}

View File

@ -26,13 +26,24 @@ type U64 = zerocopy::byteorder::U64<LE>;
type U32 = zerocopy::byteorder::U32<LE>;
type U16 = zerocopy::byteorder::U16<LE>;
#[derive(Clone, Copy, FromBytes, FromZeroes, AsBytes, Unaligned, Hash)]
#[derive(FromBytes, FromZeroes, AsBytes, Unaligned, Hash)]
#[repr(transparent)]
pub struct FilePointer<T> {
inner: RawFilePointer,
_phantom: PhantomData<*const T>,
}
impl<T> Clone for FilePointer<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner,
_phantom: PhantomData,
}
}
}
impl<T> Copy for FilePointer<T> {}
impl<T> Debug for FilePointer<T> {
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<R> {
struct Header {
magic: [u8; 16],
root: FilePointer<R>,
root: RawFilePointer, // if we make this FilePointer<R>, as it should be, AsBytes can not be derived
allocator_state: AllocatorState,
}
impl<R> Default for Header<R> {
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::<Header<R>>() as u64,
RawFilePointer::null() + size_of::<Header>() as u64,
)),
},
}
@ -257,11 +268,15 @@ struct SnapshotAndFreeList<R> {
}
impl<R> Db<R> {
fn header(&self) -> &Header<R> {
fn root(&self) -> FilePointer<R> {
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<R> {
fn header_mut(&mut self) -> &mut Header {
unsafe { self.modify_range_unchecked(Self::header_ptr().range()) }
}
@ -315,16 +330,16 @@ impl<R> Db<R> {
self.snapshots = snapshots;
}
fn header_ptr() -> FilePointer<Header<R>> {
fn header_ptr() -> FilePointer<Header> {
FilePointer::new(RawFilePointer(0.into()))
}
fn root_ptr() -> FilePointer<FilePointer<R>> {
FilePointer::new(RawFilePointer((size_of::<Header<R>>() as u64).into()))
FilePointer::new(RawFilePointer((size_of::<Header>() as u64).into()))
}
fn allocator_state_ptr() -> RawFilePointer {
RawFilePointer((size_of::<Header<R>>() as u64 + size_of::<RawFilePointer>() as u64).into())
RawFilePointer((size_of::<Header>() as u64 + size_of::<RawFilePointer>() as u64).into())
}
fn general_purpose_allocator() -> GeneralPurposeAllocator {
@ -486,7 +501,7 @@ impl<R> Db<R> {
}
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<R> Db<R> {
};
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<R> Db<R> {
let allocator_state = self.header().allocator_state;
allocator_state.slabs.init(
self,
(PAGE_SIZE - size_of::<Header<R>>() as u64)
.try_into()
.unwrap(),
(PAGE_SIZE - size_of::<Header>() as u64).try_into().unwrap(),
);
for &size in slabs {

View File

@ -170,6 +170,6 @@ impl<'t, R> TransactionHandle<'t, R> {
}
pub fn root(&self) -> FilePointer<R> {
self.db.header().root
self.db.root()
}
}