diff options
author | Matthew Sotoudeh <matthew@masot.net> | 2024-05-17 13:52:35 -0700 |
---|---|---|
committer | Matthew Sotoudeh <matthew@masot.net> | 2024-05-17 13:52:35 -0700 |
commit | 789daf4fac13a91d5c526cf93b65571ecb4f460c (patch) | |
tree | b4e119e751470e17da23db353e5758f4cffbc5f8 | |
parent | 7601ee7bb9b56ab3220bf7b632325de124bacab1 (diff) |
simpler size2log
-rw-r--r-- | magic_buddy/magic_buddy.c | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/magic_buddy/magic_buddy.c b/magic_buddy/magic_buddy.c index 9d3e1e9..1eb2d8f 100644 --- a/magic_buddy/magic_buddy.c +++ b/magic_buddy/magic_buddy.c @@ -12,13 +12,10 @@ struct free_block { // log2 of size rounded up (ceil) or down (!ceil) to the nearest power of 2 static size_t size2log(size_t size, int ceil) { - size_t bitcnt = 0, floor_log = 0; - for (int i = 0; i < (8 * sizeof(size)); i++, size >>= 1) { - if (!(size & 1)) continue; - bitcnt++; - floor_log = i; - } - return (ceil && (bitcnt > 1)) ? (floor_log + 1) : floor_log; + size_t floor_log = 0; + for (size_t i = 0; i < (8 * sizeof(size)); i++) + if (size & (1 << i)) floor_log = i; + return (ceil && (size > (1 << floor_log))) ? (floor_log + 1) : floor_log; } static struct free_block *buddy_of(struct free_block *block, size_t logsize, |