Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(const_fn_union)]
- #![feature(untagged_unions)]
- use lazy_static::lazy_static; // 1.2.0
- use core::sync::atomic::AtomicU32;
- use core::sync::atomic::AtomicBool;
- use arrayvec::ArrayVec; // 0.4.10
- const PAGE_SIZE: usize = 4096;
- /// The size of the frames_bitmap (~128ko)
- #[cfg(not(test))]
- const FRAMES_BITMAP_SIZE: usize = u32::max_value() as usize / PAGE_SIZE / 8 + 1;
- /// The physical memory manager.
- ///
- /// Serves physical memory in atomic blocks of size [PAGE_SIZE](crate::paging::PAGE_SIZE), called frames.
- ///
- /// An allocation request returns a [PhysicalMemRegion], which represents a list of
- /// physically adjacent frames. When this returned `PhysicalMemRegion` is eventually dropped
- /// the frames are automatically freed and can be re-served by the FrameAllocator.
- ///
- /// Up to 32 physically continuous frames may be allocated at a time.
- pub struct InternalFrameAllocator {
- /// A big bitmap denoting for every frame if it is free or not
- ///
- /// 1 is free, 0 is already allocated/reserved
- /// This may seem backward, but this way when we start the array is filled with 0(reserved)
- /// and it can be put in the bss by the compiler
- memory_bitmap: [AtomicU32; FRAMES_BITMAP_SIZE / core::mem::size_of::<AtomicU32>()],
- /// All operations have to check that the Allocator has been initialized
- initialized: AtomicBool
- }
- /// A physical memory manger to allocate and free memory frames
- // When running tests, each thread has its own view of the `FRAME_ALLOCATOR`.
- static FRAME_ALLOCATOR : InternalFrameAllocator = InternalFrameAllocator::new();
- union ZeroedBuilder {
- atomic: [AtomicU32; FRAMES_BITMAP_SIZE / core::mem::size_of::<AtomicU32>()],
- nonatomic: [u32; FRAMES_BITMAP_SIZE / core::mem::size_of::<AtomicU32>()],
- }
- const unsafe fn zeroed() -> [AtomicU32; FRAMES_BITMAP_SIZE / core::mem::size_of::<AtomicU32>()] {
- ZeroedBuilder {
- nonatomic: [0; FRAMES_BITMAP_SIZE / core::mem::size_of::<AtomicU32>()]
- }.atomic
- }
- impl InternalFrameAllocator {
- /// Called to initialize the [FRAME_ALLOCATOR] global.
- pub const fn new() -> Self {
- InternalFrameAllocator {
- // 0 is allocated/reserved. This is terrible and I feel bad.
- memory_bitmap: unsafe { zeroed() },
- initialized: AtomicBool::new(false),
- }
- }
- }
- fn main() {
- println!("{:?}", FRAME_ALLOCATOR.initialized);
- }
Add Comment
Please, Sign In to add comment