Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
96 Kevin 1
// Class for managing the linked list of free blocks
2
public class DoubleLinkedList {
3
 
4
	private LinkedListBlock head;	// Start of linked list
5
 
6
	public void insert(LinkedListBlock entry) {
7
 
8
		boolean adjacencyFound = false;	// Adjacency flag
9
 
10
		// Create the first entry if there are none existing
11
		if (head == null) {
12
			head = entry;
13
			head.setNextBlock(null);
14
			head.setPrevBlock(null);
15
			return;
16
		}
17
 
18
		LinkedListBlock current = head;
19
		// Check to see if adjacent entries exist
20
		while (current != null) {
21
			if (current.getStartAddress() == entry.getEndAddress() + 1) {
22
				// Entry is adjacent to start of current
23
				LinkedListBlock newEntry = new LinkedListBlock(entry.getStartAddress(),
24
						current.getEndAddress(), entry.getSize() + current.getSize());
25
				remove(current.getStartAddress());
26
				insert(newEntry);
27
				adjacencyFound = true;
28
				break;
29
			}
30
			if (current.getEndAddress() == entry.getStartAddress() - 1) {
31
				// Entry is adjacent to end of current
32
				LinkedListBlock newEntry = new LinkedListBlock(current.getStartAddress(),
33
						entry.getEndAddress(), current.getSize() + entry.getSize());
34
				remove(current.getStartAddress());
35
				insert(newEntry);
36
				adjacencyFound = true;
37
				break;
38
			}
39
			current = current.getNextBlock();
40
		}
41
 
42
		// Otherwise insert entry into sorted list (by size)
43
		current = head;
44
		if (!adjacencyFound && (entry.getSize() > current.getSize())) {
45
			// Insert into beginning of list if size is largest
46
			entry.setNextBlock(current);
47
			current.setPrevBlock(entry);
48
			head = entry;
49
			return;
50
		} else if (!adjacencyFound) {
51
			// Otherwise find location to insert into
52
			LinkedListBlock prev = head;
53
			while (prev.getNextBlock() != null) {
54
				current = prev.getNextBlock();
55
				if (entry.getSize() == current.getSize()) {
56
					// Sort by address location in accending order
57
					if (entry.getStartAddress() < current.getStartAddress()) {
58
						// Insert before current
59
						entry.setNextBlock(current);
60
						entry.setPrevBlock(prev);
61
						prev.setNextBlock(entry);
62
						current.setPrevBlock(entry);
63
						return;
64
					} else {
65
						// Insert after current
66
						prev = current;
67
						if (prev.getNextBlock() != null) {
68
							current = prev.getNextBlock();
69
							entry.setNextBlock(current);
70
							current.setPrevBlock(entry);
71
						}
72
						entry.setPrevBlock(prev);
73
						prev.setNextBlock(entry);
74
						return;
75
					}
76
				} else if (entry.getSize() > current.getSize()) {
77
					// Insert block between prev and current
78
					entry.setNextBlock(current);
79
					entry.setPrevBlock(prev);
80
					prev.setNextBlock(entry);
81
					current.setPrevBlock(entry);
82
					return;
83
				} else {
84
					// Otherwise continue searching
85
					prev = current;
86
				}
87
			}
88
			// Insert into end of list
89
			entry.setPrevBlock(prev);
90
			prev.setNextBlock(entry);
91
		}
92
	}
93
 
94
	// Deletes an entry from the list, returns true if address is found
95
	public boolean remove(long address) {
96
		// First check if list is empty
97
		if (head == null) {
98
			return false;
99
		} else {
100
			// Otherwise loop through and fine entry
101
			LinkedListBlock entry = head;
102
			do {
103
				if (entry.getStartAddress() == address) {
104
					if (entry.getPrevBlock() == null && entry.getNextBlock() == null) {
105
						// Deletes entry (only entry in list)
106
						head = null;
107
					} else if (entry.getPrevBlock() == null && entry.getNextBlock() != null) {
108
						// Deletes entry (first in list)
109
						head = entry.getNextBlock();
110
						head.setPrevBlock(null);
111
					} else if (entry.getPrevBlock() != null && entry.getNextBlock() == null) {
112
						// Deletes entry (last in list)
113
						entry.getPrevBlock().setNextBlock(null);
114
					} else {
115
						// Deletes entry (in between two entries)
116
						LinkedListBlock prev = entry.getPrevBlock();
117
						prev.setNextBlock(entry.getNextBlock());
118
						entry.getNextBlock().setPrevBlock(prev);
119
					}
120
					return true;
121
				}
122
				entry = entry.getNextBlock();
123
			} while (entry != null);
124
		}
125
		return false;
126
	}
127
 
128
	// Returns an entry from the list with the passed address
129
	public LinkedListBlock getFirstEntry() {
130
		// First check if list is empty
131
		if (head == null) {
132
			return null;
133
		} else {
134
			// Otherwise return head entry
135
			return head;
136
		}
137
	}
138
}