Remove unnecessary special case in BuddyAllocator.free
This commit is contained in:
parent
2c385c7656
commit
a39fdad1a0
@ -87,27 +87,22 @@ pub fn alloc(self: *BuddyAllocator, size: usize) !Alloc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn free(self: *BuddyAllocator, allocation: Alloc) void {
|
pub fn free(self: *BuddyAllocator, allocation: Alloc) void {
|
||||||
if (allocation.depth == 0) {
|
const size = self.getDepthSize(@intCast(allocation.depth));
|
||||||
std.debug.assert(self.states[0] == BlockState.Allocated);
|
const depth_offset = (@as(usize, @intCast(1)) << allocation.depth) - 1;
|
||||||
self.states[0] = BlockState.Free;
|
|
||||||
} else {
|
|
||||||
const size = self.getDepthSize(@intCast(allocation.depth));
|
|
||||||
const depth_offset = (@as(usize, @intCast(1)) << allocation.depth) - 1;
|
|
||||||
|
|
||||||
const idx = @as(usize, @intCast(allocation.offset)) / size;
|
const idx = @as(usize, @intCast(allocation.offset)) / size;
|
||||||
const node = idx + depth_offset;
|
const node = idx + depth_offset;
|
||||||
|
|
||||||
std.debug.assert(self.states[node] == BlockState.Allocated);
|
std.debug.assert(self.states[node] == BlockState.Allocated);
|
||||||
self.states[node] = BlockState.Free;
|
self.states[node] = BlockState.Free;
|
||||||
|
|
||||||
// Merge
|
// Merge
|
||||||
{
|
{
|
||||||
var parent = @divFloor(@as(isize, @intCast(node)) - 1, 2);
|
var parent = @divFloor(@as(isize, @intCast(node)) - 1, 2);
|
||||||
while (parent >= 0) : (parent = @divFloor(parent - 1, 2)) {
|
while (parent >= 0) : (parent = @divFloor(parent - 1, 2)) {
|
||||||
const parent_usize: usize = @intCast(parent);
|
const parent_usize: usize = @intCast(parent);
|
||||||
if (self.states[parent_usize * 2 + 1] == BlockState.Free and self.states[parent_usize * 2 + 2] == BlockState.Free) {
|
if (self.states[parent_usize * 2 + 1] == BlockState.Free and self.states[parent_usize * 2 + 2] == BlockState.Free) {
|
||||||
self.states[parent_usize] = BlockState.Free;
|
self.states[parent_usize] = BlockState.Free;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user