public MemoryManager(string FileName1, string FileName2) { Used = new DoublyLinkedList(); Free = new SinglyLinkedList(); Corrupt = new Stack(); // initialising Used and Free lists readData(FileName1, FileName2); }
// 25 marks /* pre: Have list of allocated (Used) memory blocks and available (Free) memory blocks, each of which could be empty. * post: Remove all memory blocks that are not in use from the list of allocated (Used) memory blocks, EFFICIENTLY adding these * memory blocks into the list of available (Free) memory blocks, ensuring that the list of allocated (Used) memory blocks * retains its ordering of remaining memory blockc and the list of available (Free) memory blocks is always ordered in * descending order of memory block size, with the largest available memory block being at the head of the list. * Wherever possible, use MUST be made of existing methods. * NOTE: you are required to make use of method decCounter in class DoublyLinkedList appropriately. */ public void garbageCollect() { // ADD CODE FOR METHOD garbageCollect BELOW //USE GC.Collect()? :P DLLNode cur = Used.getFirst(); DoublyLinkedList temp = new DoublyLinkedList(); do { if (cur == null) return; if (((Block)cur.value()).inUse()) { temp.addLast((Block)cur.value()); } else { Block tempBlock = (Block)cur.value(); Node curBlock = Free.getFirst(), prev = null; do { if (curBlock == null) { Free.addFirst(tempBlock); return; } if (((Block)curBlock.value()).CompareTo(tempBlock) >= 0) { Free.addBefore(tempBlock, curBlock); return; } else { prev = curBlock; curBlock = curBlock.next(); } } while (curBlock != null); } cur = (DLLNode)cur.next(); } while (cur != null); Used = temp; temp = null; }