Implementation of BitVec; Boilerplate for LinkedList

This commit is contained in:
xqtc161 2024-06-23 02:02:23 +02:00
parent 2737864f67
commit 4a2445870e
2 changed files with 81 additions and 0 deletions

View file

@ -1,5 +1,8 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(const_mut_refs)]
#![feature(const_maybe_uninit_write)]
#![feature(const_maybe_uninit_as_mut_ptr)]
use core::alloc::GlobalAlloc; use core::alloc::GlobalAlloc;
use core::panic::PanicInfo; use core::panic::PanicInfo;
@ -11,9 +14,11 @@ use talc::*;
mod mm; mod mm;
mod print; mod print;
mod util;
static mut START_ARENA: [u8; 10000] = [0; 10000]; static mut START_ARENA: [u8; 10000] = [0; 10000];
// temp till mm is ready
#[global_allocator] #[global_allocator]
static ALLOCATOR: Talck<spin::Mutex<()>, ClaimOnOom> = static ALLOCATOR: Talck<spin::Mutex<()>, ClaimOnOom> =
Talc::new(unsafe { ClaimOnOom::new(Span::from_const_array(core::ptr::addr_of!(START_ARENA))) }) Talc::new(unsafe { ClaimOnOom::new(Span::from_const_array(core::ptr::addr_of!(START_ARENA))) })

76
src/util/mod.rs Normal file
View file

@ -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<LinkedList>,
next: NonNull<LinkedList>,
}
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<Self>) {
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<LinkedList>) {}
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 {
() => {};
}