From 4a2445870e892ffd3ed06d1bc818176c832c6fcb Mon Sep 17 00:00:00 2001 From: xqtc161 Date: Sun, 23 Jun 2024 02:02:23 +0200 Subject: [PATCH] Implementation of BitVec; Boilerplate for LinkedList --- src/main.rs | 5 ++++ src/util/mod.rs | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/util/mod.rs diff --git a/src/main.rs b/src/main.rs index ed2eff1..1526af4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ #![no_std] #![no_main] +#![feature(const_mut_refs)] +#![feature(const_maybe_uninit_write)] +#![feature(const_maybe_uninit_as_mut_ptr)] use core::alloc::GlobalAlloc; use core::panic::PanicInfo; @@ -11,9 +14,11 @@ use talc::*; mod mm; mod print; +mod util; static mut START_ARENA: [u8; 10000] = [0; 10000]; +// temp till mm is ready #[global_allocator] static ALLOCATOR: Talck, ClaimOnOom> = Talc::new(unsafe { ClaimOnOom::new(Span::from_const_array(core::ptr::addr_of!(START_ARENA))) }) diff --git a/src/util/mod.rs b/src/util/mod.rs new file mode 100644 index 0000000..23bd8c8 --- /dev/null +++ b/src/util/mod.rs @@ -0,0 +1,76 @@ +use core::mem::{transmute, MaybeUninit}; +use core::ptr::{null_mut, NonNull}; + +pub struct BitVec { + data: NonNull<[u8]>, +} + +impl BitVec { + pub fn new(data: NonNull<[u8]>) -> Self { + Self { data } + } + + pub fn get(&self, index: usize) -> bool { + let data = unsafe { self.data.as_ref() }; + let byte = data[index / u8::BITS as usize]; + let bit_index = index % u8::BITS as usize; + byte & (1 << bit_index) != 0 + } + + pub fn flip(&mut self, index: usize) -> bool { + let data = unsafe { self.data.as_mut() }; + let byte = &mut data[index / u8::BITS as usize]; + let bit_index = index % u8::BITS as usize; + *byte ^= 1 << bit_index; + *byte & (1 << bit_index) != 0 + } +} + +pub struct LinkedList { + prev: NonNull, + next: NonNull, +} + +impl LinkedList { + /// # Safety + /// + /// THIS DOES NOT ACTUALLY INITIALIZE [`LinkedList`]! + /// Call [`LinkedList::init`] to initalize. + pub const unsafe fn new() -> Self { + let totally_non_null = unsafe { NonNull::new_unchecked(null_mut()) }; + Self { + prev: totally_non_null, + next: totally_non_null, + } + } + + pub const unsafe fn init(this: &mut MaybeUninit) { + let this_ptr = unsafe { NonNull::new_unchecked(this.as_mut_ptr()) }; + this.write(Self { + prev: this_ptr, + next: this_ptr, + }); + } + + pub fn insert_after(&mut self, node: NonNull) {} + + fn prev(&self) -> &Self { + unsafe { self.prev.as_ref() } + } + + fn next(&self) -> &Self { + unsafe { self.next.as_ref() } + } + + fn prev_mut(&mut self) -> &mut Self { + unsafe { self.prev.as_mut() } + } + + fn next_mut(&mut self) -> &mut Self { + unsafe { self.next.as_mut() } + } +} + +macro_rules! add { + () => {}; +}