summaryrefslogtreecommitdiff
path: root/magic_buddy/magic_buddy.c
diff options
context:
space:
mode:
authorMatthew Sotoudeh <matthew@masot.net>2024-05-17 13:49:51 -0700
committerMatthew Sotoudeh <matthew@masot.net>2024-05-17 13:49:51 -0700
commit7601ee7bb9b56ab3220bf7b632325de124bacab1 (patch)
tree4bc91ec53513c5e68f3e8292ceed8c20c4eb27e6 /magic_buddy/magic_buddy.c
parentbc4a25899d400ae9bdd52bbc0018792d3a19cca7 (diff)
simpler size2log
Diffstat (limited to 'magic_buddy/magic_buddy.c')
-rw-r--r--magic_buddy/magic_buddy.c14
1 files changed, 6 insertions, 8 deletions
diff --git a/magic_buddy/magic_buddy.c b/magic_buddy/magic_buddy.c
index e4942ee..9d3e1e9 100644
--- a/magic_buddy/magic_buddy.c
+++ b/magic_buddy/magic_buddy.c
@@ -12,15 +12,13 @@ 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) {
- int bitcnt = 0;
- for (int i = 0; size; i++) {
- bitcnt += size & 1;
- size >>= 1;
- if (size) continue;
- if (ceil) return (bitcnt > 1) ? (i + 1) : i;
- return i;
+ 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 ? 1 : 0;
+ return (ceil && (bitcnt > 1)) ? (floor_log + 1) : floor_log;
}
static struct free_block *buddy_of(struct free_block *block, size_t logsize,
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback