-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmap.java
More file actions
50 lines (42 loc) · 1.13 KB
/
Bitmap.java
File metadata and controls
50 lines (42 loc) · 1.13 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
package file_structure;
// Bitmap.java - Track free/used blocks and inodes
import java.util.BitSet;
public class Bitmap {
private BitSet bits;
private int size;
public Bitmap(int size) {
this.size = size;
this.bits = new BitSet(size);
// All bits start as 0 (free)
}
// Allocate the first free bit
public int allocate() {
for (int i = 0; i < size; i++) {
if (!bits.get(i)) {
bits.set(i);
return i;
}
}
return -1; // No free bits
}
// Free a specific bit
public void free(int index) {
if (index >= 0 && index < size) {
bits.clear(index);
}
}
// Check if a bit is allocated
public boolean isAllocated(int index) {
return index >= 0 && index < size && bits.get(index);
}
// Get number of free bits
public int getFreeCount() {
return size - bits.cardinality();
}
// Reserve a specific bit (for system use)
public void reserve(int index) {
if (index >= 0 && index < size) {
bits.set(index);
}
}
}