-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreeblock.java
More file actions
306 lines (275 loc) · 7.7 KB
/
Freeblock.java
File metadata and controls
306 lines (275 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// -------------------------------------------------------------------------
/**
* freeblock list which is a double link list keeps track of the free blocks in
* memeory pool.
*
* @author wenfeng ren (rwenfeng)
* @version Sep 7, 2014
*/
public class Freeblock
{
// data files
/**
* head node
*/
private Block head;
/**
* tail node
*/
private Block tail;
// ----------------------------------------------------------
/**
* Create a new Freeblock object and initialize. insert the node with
* position = 0, free block size = blockSize.
*
* @param blockSize
* the initial blockSize
*/
public Freeblock(int blockSize)
{
head = new Block();
tail = new Block();
Block first = new Block(blockSize, 0);
first.setPrev(head);
first.setNext(tail);
head.setNext(first);
tail.setPrev(first);
}
// ----------------------------------------------------------
/**
* use best fit method to find where to insert the record.
*
* @param recordSize
* the size of the record
* @return best fit node
*/
public Block bestFit(int recordSize)
{
Block best = null;
Block temp = head;
int fitBlock = Integer.MAX_VALUE;
while ((temp = temp.next()) != tail)
{
if (recordSize <= temp.getBlockSize()
&& temp.getBlockSize() < fitBlock)
{
best = temp;
fitBlock = best.getBlockSize();
}
}
return best;
}
// ----------------------------------------------------------
/**
* find the position of record.
*
* @param recordSize
* of the record
* @return position of record
*/
public int findPosition(int recordSize)
{
Block best = bestFit(recordSize);
int position;
if (best == null)
{
position = -1;
}
else
{
position = best.getPosition();
}
return position;
}
// ----------------------------------------------------------
/**
* when a best fit block is found, this block need to change the position
* and size info. If best is not found, return position = -1
*
* @param recordSize
* the size of the block
* @return position of the best fit block
*/
public int updateList(int recordSize)
{
Block best = bestFit(recordSize);
int position;
if (best == null)
{
position = -1;
}
else
{
if (recordSize == best.getBlockSize())
{
remove(best);
}
else
{
// update the blockSize and position
best.setBlockSize(best.getBlockSize() - recordSize);
best.setPosition(best.getPosition() + recordSize);
}
position = best.getPosition();
}
return position;
}
// ----------------------------------------------------------
/**
* append the node to the list.
*
* @param newNode
* needs to be appended
*/
public void append(Block newNode)
{
tail.prev().setNext(newNode);
newNode.setPrev(tail.prev());
tail.setPrev(newNode);
newNode.setNext(tail);
}
// ----------------------------------------------------------
/**
* insert a free block, which has position and size reference.
*
* @param newNode
* to be inserted
*/
public void insert(Block newNode)
{
Block temp = head;
// check whether is a special case: append the node.
boolean append = true;
while (temp.next() != tail)
{
if (temp.next().getPosition() > newNode.getPosition())
{
// the node need to be inserted in the middle of the list.
append = false;
temp = temp.next();
break;
}
temp = temp.next();
}
if (append) // append is needed
{
append(newNode);
}
else
{
temp.prev().setNext(newNode);
newNode.setPrev(temp.prev());
temp.setPrev(newNode);
newNode.setNext(temp);
}
if (isMergeNeed(newNode))
{
merge(newNode);
}
}
// ----------------------------------------------------------
/**
* remove the node from list.
*
* @param remove
* the node that need to be removed
*/
public void remove(Block remove)
{
//
remove.prev().setNext(remove.next());
remove.next().setPrev(remove.prev());
}
// ----------------------------------------------------------
/**
* merge the adjacent node when merge is needed.
*
* @param node
* that will need to right || left
*/
public void merge(Block node)
{
if (node.getPosition() == node.prev().getPosition()
+ node.prev().getBlockSize()
&& node.next().getPosition() != node.getPosition()
+ node.getBlockSize())
{
// only merge left
// System.out.println("merge left------");
node.prev().setBlockSize(
node.prev().getBlockSize() + node.getBlockSize());
remove(node);
}
if (node.next().getPosition() == node.getPosition()
+ node.getBlockSize()
&& node.getPosition() != node.prev().getPosition()
+ node.prev().getBlockSize())
{
// only merge right
// System.out.println("merge left------");
node.setBlockSize(node.next().getBlockSize() + node.getBlockSize());
remove(node.next());
}
if (node.next().getPosition() == node.getPosition()
+ node.getBlockSize()
&& node.getPosition() == node.prev().getPosition()
+ node.prev().getBlockSize())
{
// merge both
// System.out.println("merge both------");
node.prev().setBlockSize(
node.prev().getBlockSize() + node.getBlockSize()
+ node.next().getBlockSize());
remove(node.next());
remove(node);
}
}
// ----------------------------------------------------------
/**
* the boolean method to check whether merge is needed.
*
* @param node
* that needs to check whether it is needed to merge
* @return merge or not in boolean type
*/
public boolean isMergeNeed(Block node)
{
boolean merge;
if (node.getPosition() != node.prev().getPosition()
+ node.prev().getBlockSize()
&& node.next().getPosition() != node.getPosition()
+ node.getBlockSize())
{
merge = false;
}
else
{
merge = true;
}
return merge;
}
// ----------------------------------------------------------
/**
* print out the list information.
*
* @return toString list
*/
public String toString()
{
Block temp = head;
String list = "";
if (temp.next().equals(tail))
{
return list;
}
else
{
while (!temp.next().next().equals(tail))
{
list = list + temp.next().toString() + " -> ";
temp = temp.next();
}
return list + temp.next().toString();
}
}
}