Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
// Class for managing the linked list of free blockspublic class DoubleLinkedList {private LinkedListBlock head; // Start of linked listpublic void insert(LinkedListBlock entry) {boolean adjacencyFound = false; // Adjacency flag// Create the first entry if there are none existingif (head == null) {head = entry;head.setNextBlock(null);head.setPrevBlock(null);return;}LinkedListBlock current = head;// Check to see if adjacent entries existwhile (current != null) {if (current.getStartAddress() == entry.getEndAddress() + 1) {// Entry is adjacent to start of currentLinkedListBlock newEntry = new LinkedListBlock(entry.getStartAddress(),current.getEndAddress(), entry.getSize() + current.getSize());remove(current.getStartAddress());insert(newEntry);adjacencyFound = true;break;}if (current.getEndAddress() == entry.getStartAddress() - 1) {// Entry is adjacent to end of currentLinkedListBlock newEntry = new LinkedListBlock(current.getStartAddress(),entry.getEndAddress(), current.getSize() + entry.getSize());remove(current.getStartAddress());insert(newEntry);adjacencyFound = true;break;}current = current.getNextBlock();}// Otherwise insert entry into sorted list (by size)current = head;if (!adjacencyFound && (entry.getSize() > current.getSize())) {// Insert into beginning of list if size is largestentry.setNextBlock(current);current.setPrevBlock(entry);head = entry;return;} else if (!adjacencyFound) {// Otherwise find location to insert intoLinkedListBlock prev = head;while (prev.getNextBlock() != null) {current = prev.getNextBlock();if (entry.getSize() == current.getSize()) {// Sort by address location in accending orderif (entry.getStartAddress() < current.getStartAddress()) {// Insert before currententry.setNextBlock(current);entry.setPrevBlock(prev);prev.setNextBlock(entry);current.setPrevBlock(entry);return;} else {// Insert after currentprev = current;if (prev.getNextBlock() != null) {current = prev.getNextBlock();entry.setNextBlock(current);current.setPrevBlock(entry);}entry.setPrevBlock(prev);prev.setNextBlock(entry);return;}} else if (entry.getSize() > current.getSize()) {// Insert block between prev and currententry.setNextBlock(current);entry.setPrevBlock(prev);prev.setNextBlock(entry);current.setPrevBlock(entry);return;} else {// Otherwise continue searchingprev = current;}}// Insert into end of listentry.setPrevBlock(prev);prev.setNextBlock(entry);}}// Deletes an entry from the list, returns true if address is foundpublic boolean remove(long address) {// First check if list is emptyif (head == null) {return false;} else {// Otherwise loop through and fine entryLinkedListBlock entry = head;do {if (entry.getStartAddress() == address) {if (entry.getPrevBlock() == null && entry.getNextBlock() == null) {// Deletes entry (only entry in list)head = null;} else if (entry.getPrevBlock() == null && entry.getNextBlock() != null) {// Deletes entry (first in list)head = entry.getNextBlock();head.setPrevBlock(null);} else if (entry.getPrevBlock() != null && entry.getNextBlock() == null) {// Deletes entry (last in list)entry.getPrevBlock().setNextBlock(null);} else {// Deletes entry (in between two entries)LinkedListBlock prev = entry.getPrevBlock();prev.setNextBlock(entry.getNextBlock());entry.getNextBlock().setPrevBlock(prev);}return true;}entry = entry.getNextBlock();} while (entry != null);}return false;}// Returns an entry from the list with the passed addresspublic LinkedListBlock getFirstEntry() {// First check if list is emptyif (head == null) {return null;} else {// Otherwise return head entryreturn head;}}}