// 10 marks /* pre: Have list of allocated (Used) memory blocks, which could be empty. * post: EFFICIENTLY insert the new memory allocation in order of memory block identifier so that the smallest memory identifier is * at the head of the list. * Wherever possible, use MUST be made of existing methods. */ // MODIFY THE APPROPRIATE METHODS IN THE RELEVANT PLACES BELOW public void allocateMemory(Block MemBlock) { // ADD CODE FOR METHOD allocateMemory BELOW if (Used.Count() == 0) { if (MemBlock.inUse()) Used.addFirst(MemBlock); else Free.addLast(MemBlock); } else { if (MemBlock.inUse()) { DLLNode cur = Used.getFirst(); do { if (((Block)cur.value()).CompareTo(MemBlock) >= 0) { Used.addBefore(MemBlock, cur); return; } else { cur = (DLLNode)cur.next(); } } while (cur != null); } else Free.addLast(MemBlock); } }
// 10 marks /* pre: Have list of allocated (Used) memory blocks, which could be empty. * post: EFFICIENTLY insert the new memory allocation in order of memory block identifier so that the smallest memory identifier is * at the head of the list. * Wherever possible, use MUST be made of existing methods. */ // MODIFY THE APPROPRIATE METHODS IN THE RELEVANT PLACES BELOW public void allocateMemory(Block MemBlock) { // ADD CODE FOR METHOD allocateMemory BELOW //if the used list is empty, just add it if (Used.Count () == 0) Used.addFirst (MemBlock); //if the used list contains already allocated blocks, add MemBlock in order of identifier else { //get the first block DLLNode cur = Used.getFirst (); //traverse the list and check where to add the block do { //when the id of the current block is bigger than the one to be inserted, //add it before the current block. if (((Block)cur.value ()).CompareTo(MemBlock) >= 0) { Used.addBefore (MemBlock, cur); return; // break out the loop and stop, otherwise we are wasting time } cur = (DLLNode)cur.next (); } while (cur != null); Used.addLast (MemBlock); //if the whole list has been traversed, add the block at the end. } }
/* pre: Lists of memory block are empty. * post: Populates lists with test data from text files. */ public void readData(string FileName1, string FileName2) { Console.WriteLine("List of allocated memory being populated ......"); StreamReader Input = new StreamReader(FileName1); while (!Input.EndOfStream) { Block newOne = new Block(int.Parse(Input.ReadLine()), int.Parse(Input.ReadLine()), bool.Parse(Input.ReadLine())); allocateMemory(newOne); } Input = new StreamReader(FileName2); while (!Input.EndOfStream) { Block newOne = new Block(int.Parse(Input.ReadLine()), int.Parse(Input.ReadLine()), false); Corrupt.Push(newOne); } Console.WriteLine("List of allocated memory populated"); Console.WriteLine(); }
public int CompareTo(Block Mem) { return this.MemID - Mem.MemID; }