terminal: bitmap allocator handles 64-chunk sized allocs

This commit is contained in:
Mitchell Hashimoto
2024-03-20 20:44:39 -07:00
parent dfa5b2e6fc
commit 565a5a6048

View File

@ -175,7 +175,7 @@ fn findFreeChunks(bitmaps: []u64, n: usize) ?usize {
// but unsure. Contributor friendly: let's benchmark and improve this! // but unsure. Contributor friendly: let's benchmark and improve this!
// TODO: handle large chunks // TODO: handle large chunks
assert(n < @bitSizeOf(u64)); assert(n <= @bitSizeOf(u64));
for (bitmaps, 0..) |*bitmap, idx| { for (bitmaps, 0..) |*bitmap, idx| {
// Shift the bitmap to find `n` sequential free chunks. // Shift the bitmap to find `n` sequential free chunks.
@ -237,6 +237,35 @@ test "findFreeChunks multiple found" {
); );
} }
test "findFreeChunks exactly 64 chunks" {
const testing = std.testing;
var bitmaps = [_]u64{
0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111,
};
const idx = findFreeChunks(&bitmaps, 64).?;
try testing.expectEqual(
0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
bitmaps[0],
);
try testing.expectEqual(@as(usize, 0), idx);
}
// test "findFreeChunks larger than 64 chunks" {
// const testing = std.testing;
//
// var bitmaps = [_]u64{
// 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111,
// 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111,
// };
// const idx = findFreeChunks(&bitmaps, 65).?;
// try testing.expectEqual(
// 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000,
// bitmaps[0],
// );
// try testing.expectEqual(@as(usize, 0), idx);
// }
test "BitmapAllocator layout" { test "BitmapAllocator layout" {
const Alloc = BitmapAllocator(4); const Alloc = BitmapAllocator(4);
const cap = 64 * 4; const cap = 64 * 4;
@ -322,3 +351,18 @@ test "BitmapAllocator alloc non-byte multi-chunk" {
const ptr3 = try bm.alloc(u21, buf, 1); const ptr3 = try bm.alloc(u21, buf, 1);
try testing.expectEqual(@intFromPtr(ptr.ptr), @intFromPtr(ptr3.ptr)); try testing.expectEqual(@intFromPtr(ptr.ptr), @intFromPtr(ptr3.ptr));
} }
//
// test "BitmapAllocator alloc large" {
// const Alloc = BitmapAllocator(2);
// const cap = 256;
//
// const testing = std.testing;
// const alloc = testing.allocator;
// const layout = Alloc.layout(cap);
// const buf = try alloc.alignedAlloc(u8, Alloc.base_align, layout.total_size);
// defer alloc.free(buf);
//
// var bm = Alloc.init(OffsetBuf.init(buf), layout);
// const ptr = try bm.alloc(u8, buf, 128);
// ptr[0] = 'A';
// }