From 11c8bdc00e90ed7e2708143479b44650c769e7b6 Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Mon, 8 Jul 2024 22:17:56 -0400 Subject: [PATCH] BitmapAllocator: slightly improve findFreeChunks mask calculation --- src/terminal/bitmap_allocator.zig | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/terminal/bitmap_allocator.zig b/src/terminal/bitmap_allocator.zig index a13236217..f96d39831 100644 --- a/src/terminal/bitmap_allocator.zig +++ b/src/terminal/bitmap_allocator.zig @@ -240,20 +240,28 @@ fn findFreeChunks(bitmaps: []u64, n: usize) ?usize { assert(n <= @bitSizeOf(u64)); for (bitmaps, 0..) |*bitmap, idx| { // Shift the bitmap to find `n` sequential free chunks. + // EXAMPLE: + // n = 4 + // shifted = 001111001011110010 + // & 000111100101111001 + // & 000011110010111100 + // & 000001111001011110 + // = 000001000000010000 + // ^ ^ + // In this example there are 2 places with at least 4 sequential 1s. var shifted: u64 = bitmap.*; for (1..n) |i| shifted &= bitmap.* >> @intCast(i); // If we have zero then we have no matches if (shifted == 0) continue; - // Trailing zeroes gets us the bit 1-indexed + // Trailing zeroes gets us the index of the first bit index with at + // least `n` sequential 1s. In the example above, that would be `4`. const bit = @ctz(shifted); // Calculate the mask so we can mark it as used - for (0..n) |i| { - const mask = @as(u64, 1) << @intCast(bit + i); - bitmap.* ^= mask; - } + const mask = (@as(u64, std.math.maxInt(u64)) >> @intCast(64 - n)) << @intCast(bit); + bitmap.* ^= mask; return (idx * 64) + bit; }