Guest User

Untitled

a guest
Feb 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #![feature(const_fn_union)]
  2. #![feature(untagged_unions)]
  3. use lazy_static::lazy_static; // 1.2.0
  4. use core::sync::atomic::AtomicU32;
  5. use core::sync::atomic::AtomicBool;
  6. use arrayvec::ArrayVec; // 0.4.10
  7.  
  8. const PAGE_SIZE: usize = 4096;
  9.  
  10. /// The size of the frames_bitmap (~128ko)
  11. #[cfg(not(test))]
  12. const FRAMES_BITMAP_SIZE: usize = u32::max_value() as usize / PAGE_SIZE / 8 + 1;
  13.  
  14.  
  15. /// The physical memory manager.
  16. ///
  17. /// Serves physical memory in atomic blocks of size [PAGE_SIZE](crate::paging::PAGE_SIZE), called frames.
  18. ///
  19. /// An allocation request returns a [PhysicalMemRegion], which represents a list of
  20. /// physically adjacent frames. When this returned `PhysicalMemRegion` is eventually dropped
  21. /// the frames are automatically freed and can be re-served by the FrameAllocator.
  22. ///
  23. /// Up to 32 physically continuous frames may be allocated at a time.
  24. pub struct InternalFrameAllocator {
  25. /// A big bitmap denoting for every frame if it is free or not
  26. ///
  27. /// 1 is free, 0 is already allocated/reserved
  28. /// This may seem backward, but this way when we start the array is filled with 0(reserved)
  29. /// and it can be put in the bss by the compiler
  30. memory_bitmap: [AtomicU32; FRAMES_BITMAP_SIZE / core::mem::size_of::<AtomicU32>()],
  31.  
  32. /// All operations have to check that the Allocator has been initialized
  33. initialized: AtomicBool
  34. }
  35.  
  36.  
  37. /// A physical memory manger to allocate and free memory frames
  38. // When running tests, each thread has its own view of the `FRAME_ALLOCATOR`.
  39. static FRAME_ALLOCATOR : InternalFrameAllocator = InternalFrameAllocator::new();
  40.  
  41. union ZeroedBuilder {
  42. atomic: [AtomicU32; FRAMES_BITMAP_SIZE / core::mem::size_of::<AtomicU32>()],
  43. nonatomic: [u32; FRAMES_BITMAP_SIZE / core::mem::size_of::<AtomicU32>()],
  44. }
  45.  
  46. const unsafe fn zeroed() -> [AtomicU32; FRAMES_BITMAP_SIZE / core::mem::size_of::<AtomicU32>()] {
  47. ZeroedBuilder {
  48. nonatomic: [0; FRAMES_BITMAP_SIZE / core::mem::size_of::<AtomicU32>()]
  49. }.atomic
  50. }
  51.  
  52. impl InternalFrameAllocator {
  53. /// Called to initialize the [FRAME_ALLOCATOR] global.
  54. pub const fn new() -> Self {
  55.  
  56. InternalFrameAllocator {
  57. // 0 is allocated/reserved. This is terrible and I feel bad.
  58. memory_bitmap: unsafe { zeroed() },
  59. initialized: AtomicBool::new(false),
  60. }
  61. }
  62. }
  63.  
  64. fn main() {
  65.  
  66. println!("{:?}", FRAME_ALLOCATOR.initialized);
  67. }
Add Comment
Please, Sign In to add comment