From 1da8b8465ff033beb4ed66f8b14a571f9dbaea91 Mon Sep 17 00:00:00 2001 From: AmauryBrodu <60550980+AmauryBrodu@users.noreply.github.com> Date: Wed, 29 Mar 2023 15:02:04 +0200 Subject: [PATCH 1/2] commented the bitmap + init_bitmap + init_file_hdr --- src/filesys/filehdr.rs | 18 ++++++++++++++++ src/utility/bitmap.rs | 49 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/filesys/filehdr.rs b/src/filesys/filehdr.rs index e6dcad9..920ae1a 100644 --- a/src/filesys/filehdr.rs +++ b/src/filesys/filehdr.rs @@ -20,6 +20,19 @@ pub struct FileHdr { impl FileHdr { + + pub fn init_file_hdr() -> FileHdr { + FileHdr { + is_dir: 0, + num_bytes: 0, + num_sectors: 0, + data_sectors: Vec::new(), + num_header_sectors: 0, + header_sectors: [0;MAX_HEADER_SECTORS as usize], + } + } + + pub fn allocate(&mut self, mut free_map: BitMap, file_size: i32) -> bool { self.num_bytes = file_size; if file_size > MAX_FILE_LENGTH { @@ -87,6 +100,11 @@ impl FileHdr { //TODO: fetchFrom WriteBack + + + + + pub fn byte_to_sector(&self,offset: i32) -> i32 { return self.data_sectors[ (offset / SECTOR_SIZE) as usize]; } diff --git a/src/utility/bitmap.rs b/src/utility/bitmap.rs index a34e5b4..d66dd2e 100644 --- a/src/utility/bitmap.rs +++ b/src/utility/bitmap.rs @@ -4,6 +4,8 @@ pub const BITS_IN_WORD: usize = 32; use std::fs::File; use std::io::{Cursor, Write, Read}; +use crate::simulator::disk::SECTOR_SIZE; + pub struct BitMap { num_bits: usize, num_words: usize, @@ -12,16 +14,44 @@ pub struct BitMap { impl BitMap { + /// Initialize a bitmap with "nitems" bits, so that every bit is clear. + /// it can be added somewhere on a list. + /// + /// ### Parameters + /// - **nitems** is the number of bits in the bitmap. + ///---------------------------------------------------------------------- + pub fn init_bitmap(&self, n_items: usize) -> BitMap { + let mut tmp: Vec; + BitMap{ + num_bits: n_items, + num_words: (n_items + SECTOR_SIZE as usize -1) / SECTOR_SIZE as usize, + map: tmp, + }; + + *self + } + + /// Set the "nth" bit in a bitmap. + /// ### Parameters + /// - **which** is the number of the bit to be set. pub fn mark(&mut self, which: usize) { assert!(which >= 0 && which < self.num_bits); self.map[which / BITS_IN_WORD] |= 1 << (which % BITS_IN_WORD); } + /// return true if the "nth" bit is set. + /// + /// ### Paramenters + /// - **which** is the number of the bit to be tested. pub fn test(&self, which: usize) -> bool { assert!(which < self.num_bits); (self.map[which / BITS_IN_WORD] & (1 << (which % BITS_IN_WORD))) != 0 } + /// Return the number of the first bit which is clear. + /// As a side effect, set the bit (mark it as in use). + /// (In other words, find and allocate a bit.) + /// If no bits are clear, return ERROR pub fn find(&mut self) -> i32 { for i in 0..self.num_bits { if !self.test(i) { @@ -32,6 +62,9 @@ impl BitMap { -1 } + /// Clear the "nth" bit in a bitmap. + /// ### Parameters + /// - **which** is the number of the bit to be cleared. pub fn clear(&mut self, which: usize) { assert!(which < self.num_bits, "index out of range"); @@ -40,6 +73,8 @@ impl BitMap { self.map[word_idx] &= !(1 << bit_idx); } + /// Return the number of clear bits in the bitmap. + /// (In other words, how many bits are unallocated?) pub fn num_clear(&self) -> i32 { let mut count = 0; for i in 0..self.num_bits { @@ -50,6 +85,9 @@ impl BitMap { count } + /// Print the contents of the bitmap, for debugging. + /// Could be done in a number of ways, but we just print the #'s of + /// all the bits that are set in the bitmap. pub fn print(&self) { println!("Bitmap set:"); for i in 0..self.num_bits { @@ -60,8 +98,10 @@ impl BitMap { println!(); } - - + ///Initialize the contents of a bitmap from a Nachos file. + /// + /// ### Parameters + /// - **file** is the place to read the bitmap from pub fn fetch_from(&mut self, file_path: &str) -> std::io::Result<&Vec> { // Ouvre le fichier en mode lecture seule let mut file = File::open(file_path)?; @@ -83,6 +123,10 @@ impl BitMap { Ok(&self.map) } + /// Store the contents of a bitmap to a Nachos file. + /// + /// ### Paramenters + /// - **file** is the place to write the bitmap to pub fn write_back(&mut self, file_path: &str) -> std::io::Result<()> { // Encapsule le vecteur dans un std::io::Cursor pour l'utiliser comme un std::io::Write let mut cursor = Cursor::new(Vec::::new()); @@ -98,5 +142,4 @@ impl BitMap { Ok(()) } - } \ No newline at end of file From 60410efd1abd0815593871bc2fcea85414879055 Mon Sep 17 00:00:00 2001 From: AmauryBrodu <60550980+AmauryBrodu@users.noreply.github.com> Date: Wed, 29 Mar 2023 15:05:07 +0200 Subject: [PATCH 2/2] bug fix of bitmap constructor --- src/utility/bitmap.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/utility/bitmap.rs b/src/utility/bitmap.rs index d66dd2e..4af76a1 100644 --- a/src/utility/bitmap.rs +++ b/src/utility/bitmap.rs @@ -20,15 +20,13 @@ impl BitMap { /// ### Parameters /// - **nitems** is the number of bits in the bitmap. ///---------------------------------------------------------------------- - pub fn init_bitmap(&self, n_items: usize) -> BitMap { + pub fn init_bitmap(n_items: usize) -> BitMap { let mut tmp: Vec; BitMap{ num_bits: n_items, num_words: (n_items + SECTOR_SIZE as usize -1) / SECTOR_SIZE as usize, map: tmp, - }; - - *self + } } /// Set the "nth" bit in a bitmap.