1.1 --- a/libmem/lib/src/memory_utils.cc Tue Apr 26 16:36:17 2022 +0200
1.2 +++ b/libmem/lib/src/memory_utils.cc Tue Apr 26 22:48:33 2022 +0200
1.3 @@ -1,7 +1,7 @@
1.4 /*
1.5 * Memory quantity calculation utilities.
1.6 *
1.7 - * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + * Copyright (C) 2021, 2022 Paul Boddie <paul@boddie.org.uk>
1.9 *
1.10 * This program is free software; you can redistribute it and/or
1.11 * modify it under the terms of the GNU General Public License as
1.12 @@ -19,6 +19,8 @@
1.13 * Boston, MA 02110-1301, USA
1.14 */
1.15
1.16 +#include <l4/util/bitops.h> /* l4util_log2 */
1.17 +
1.18 #include "memory_utils.h"
1.19
1.20
1.21 @@ -30,15 +32,17 @@
1.22 return PAGE_SIZE * n;
1.23 }
1.24
1.25 -/* Return the order of 'size', where 2 ** order yields the size. */
1.26 +/* Return the order of 'size', where 2 ** order yields the size. For sizes not
1.27 + corresponding to 2 ** order precisely, a larger order is returned. */
1.28
1.29 unsigned int page_order(offset_t size)
1.30 {
1.31 - /* Count zeros from the left, stopping at the first set bit, using the width
1.32 - of the size value (in bits, starting with the width in bytes) to
1.33 - calculate the position of this bit and thus the order of the value. */
1.34 + unsigned int exp = l4util_log2(size);
1.35
1.36 - return sizeof(unsigned long) * 8 - 1 - __builtin_clzl(size);
1.37 + if ((1UL << exp) < size)
1.38 + return exp + 1;
1.39 + else
1.40 + return exp;
1.41 }
1.42
1.43 /* Return 'value' rounded up to the nearest 'increment'. */