commented the bitmap + init_bitmap + init_file_hdr

This commit is contained in:
AmauryBrodu 2023-03-29 15:02:04 +02:00
parent c1f436bcfb
commit 1da8b8465f
2 changed files with 64 additions and 3 deletions

View File

@ -20,6 +20,19 @@ pub struct FileHdr {
impl 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 { pub fn allocate(&mut self, mut free_map: BitMap, file_size: i32) -> bool {
self.num_bytes = file_size; self.num_bytes = file_size;
if file_size > MAX_FILE_LENGTH { if file_size > MAX_FILE_LENGTH {
@ -87,6 +100,11 @@ impl FileHdr {
//TODO: fetchFrom WriteBack //TODO: fetchFrom WriteBack
pub fn byte_to_sector(&self,offset: i32) -> i32 { pub fn byte_to_sector(&self,offset: i32) -> i32 {
return self.data_sectors[ (offset / SECTOR_SIZE) as usize]; return self.data_sectors[ (offset / SECTOR_SIZE) as usize];
} }

View File

@ -4,6 +4,8 @@ pub const BITS_IN_WORD: usize = 32;
use std::fs::File; use std::fs::File;
use std::io::{Cursor, Write, Read}; use std::io::{Cursor, Write, Read};
use crate::simulator::disk::SECTOR_SIZE;
pub struct BitMap { pub struct BitMap {
num_bits: usize, num_bits: usize,
num_words: usize, num_words: usize,
@ -12,16 +14,44 @@ pub struct BitMap {
impl 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<u32>;
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) { pub fn mark(&mut self, which: usize) {
assert!(which >= 0 && which < self.num_bits); assert!(which >= 0 && which < self.num_bits);
self.map[which / BITS_IN_WORD] |= 1 << (which % BITS_IN_WORD); 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 { pub fn test(&self, which: usize) -> bool {
assert!(which < self.num_bits); assert!(which < self.num_bits);
(self.map[which / BITS_IN_WORD] & (1 << (which % BITS_IN_WORD))) != 0 (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 { pub fn find(&mut self) -> i32 {
for i in 0..self.num_bits { for i in 0..self.num_bits {
if !self.test(i) { if !self.test(i) {
@ -32,6 +62,9 @@ impl BitMap {
-1 -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) { pub fn clear(&mut self, which: usize) {
assert!(which < self.num_bits, "index out of range"); assert!(which < self.num_bits, "index out of range");
@ -40,6 +73,8 @@ impl BitMap {
self.map[word_idx] &= !(1 << bit_idx); 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 { pub fn num_clear(&self) -> i32 {
let mut count = 0; let mut count = 0;
for i in 0..self.num_bits { for i in 0..self.num_bits {
@ -50,6 +85,9 @@ impl BitMap {
count 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) { pub fn print(&self) {
println!("Bitmap set:"); println!("Bitmap set:");
for i in 0..self.num_bits { for i in 0..self.num_bits {
@ -60,8 +98,10 @@ impl BitMap {
println!(); 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<u32>> { pub fn fetch_from(&mut self, file_path: &str) -> std::io::Result<&Vec<u32>> {
// Ouvre le fichier en mode lecture seule // Ouvre le fichier en mode lecture seule
let mut file = File::open(file_path)?; let mut file = File::open(file_path)?;
@ -83,6 +123,10 @@ impl BitMap {
Ok(&self.map) 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<()> { 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 // Encapsule le vecteur dans un std::io::Cursor pour l'utiliser comme un std::io::Write
let mut cursor = Cursor::new(Vec::<u8>::new()); let mut cursor = Cursor::new(Vec::<u8>::new());
@ -98,5 +142,4 @@ impl BitMap {
Ok(()) Ok(())
} }
} }