Skip to content

Commit e7cb045

Browse files
committed
Free space tracking for bitfield allocator
1 parent 08490ac commit e7cb045

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/main/java/myworld/hummingbird/util/BitFieldAllocator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class BitFieldAllocator {
1111

1212
private final BitField state;
1313
private int lastFree = 0;
14+
private int allocated = 0;
1415

1516
public BitFieldAllocator(int initialSize){
1617
state = new BitField(initialSize);
@@ -21,6 +22,7 @@ public synchronized int allocate(){
2122
state.set(lastFree);
2223
var ptr = lastFree;
2324
lastFree = -1;
25+
allocated++;
2426
return ptr;
2527
}
2628

@@ -30,6 +32,7 @@ public synchronized int allocate(){
3032
if(freeBit != 0){
3133
var ptr = i * 64 + Long.numberOfLeadingZeros(freeBit);
3234
state.set(ptr);
35+
allocated++;
3336
return ptr;
3437
}
3538
}
@@ -41,8 +44,13 @@ public synchronized boolean isAllocated(int ptr){
4144
}
4245

4346
public synchronized void free(int ptr){
47+
allocated--;
4448
state.clear(ptr);
4549
lastFree = ptr;
4650
}
4751

52+
public synchronized int freeSpace(){
53+
return state.bitCount() - allocated;
54+
}
55+
4856
}

0 commit comments

Comments
 (0)