# HG changeset patch # User Paul Boddie # Date 1651006113 -7200 # Node ID ca77703917089203d4bcd5cf0c8e5f1299745d4b # Parent 2f085a61c06bec8adabcf62c255ccf25e29b934d Made page_order able to handle arbitrary sizes, also employing l4util_log2. diff -r 2f085a61c06b -r ca7770391708 libmem/lib/src/memory_utils.cc --- a/libmem/lib/src/memory_utils.cc Tue Apr 26 16:36:17 2022 +0200 +++ b/libmem/lib/src/memory_utils.cc Tue Apr 26 22:48:33 2022 +0200 @@ -1,7 +1,7 @@ /* * Memory quantity calculation utilities. * - * Copyright (C) 2021 Paul Boddie + * Copyright (C) 2021, 2022 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -19,6 +19,8 @@ * Boston, MA 02110-1301, USA */ +#include /* l4util_log2 */ + #include "memory_utils.h" @@ -30,15 +32,17 @@ return PAGE_SIZE * n; } -/* Return the order of 'size', where 2 ** order yields the size. */ +/* Return the order of 'size', where 2 ** order yields the size. For sizes not + corresponding to 2 ** order precisely, a larger order is returned. */ unsigned int page_order(offset_t size) { - /* Count zeros from the left, stopping at the first set bit, using the width - of the size value (in bits, starting with the width in bytes) to - calculate the position of this bit and thus the order of the value. */ + unsigned int exp = l4util_log2(size); - return sizeof(unsigned long) * 8 - 1 - __builtin_clzl(size); + if ((1UL << exp) < size) + return exp + 1; + else + return exp; } /* Return 'value' rounded up to the nearest 'increment'. */