1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libmem/lib/src/memory_utils.cc Thu Apr 15 23:15:17 2021 +0200
1.3 @@ -0,0 +1,132 @@
1.4 +/*
1.5 + * Memory quantity calculation utilities.
1.6 + *
1.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include "memory_utils.h"
1.26 +
1.27 +
1.28 +
1.29 +/* Return page 'n' for the configured page size. */
1.30 +
1.31 +offset_t page(unsigned int n)
1.32 +{
1.33 + return PAGE_SIZE * n;
1.34 +}
1.35 +
1.36 +/* Return the order of 'size', where 2 ** order yields the size. */
1.37 +
1.38 +unsigned int page_order(offset_t size)
1.39 +{
1.40 + /* Count zeros from the left, stopping at the first set bit, using the width
1.41 + of the size value (in bits, starting with the width in bytes) to
1.42 + calculate the position of this bit and thus the order of the value. */
1.43 +
1.44 + return sizeof(unsigned long) * 8 - 1 - __builtin_clzl(size);
1.45 +}
1.46 +
1.47 +/* Return 'value' rounded up to the nearest 'increment'. */
1.48 +
1.49 +offset_t round(offset_t value, offset_t increment)
1.50 +{
1.51 + return trunc(value + increment - 1, increment);
1.52 +}
1.53 +
1.54 +/* Return 'value' rounded up to the nearest multiple of 'increment'. */
1.55 +
1.56 +offset_t round_multiple(offset_t value, offset_t increment)
1.57 +{
1.58 + offset_t last = increment;
1.59 +
1.60 + while (1)
1.61 + {
1.62 + if (value < increment)
1.63 + return round(value, last);
1.64 +
1.65 + last = increment;
1.66 + increment *= 2;
1.67 + }
1.68 +}
1.69 +
1.70 +/* Return 'value' rounded down (or truncated) to the nearest 'increment'. */
1.71 +
1.72 +offset_t trunc(offset_t value, offset_t increment)
1.73 +{
1.74 + return (value / increment) * increment;
1.75 +}
1.76 +
1.77 +/* Return 'value' rounded down (or truncated) to the nearest multiple of
1.78 + 'increment'. */
1.79 +
1.80 +offset_t trunc_multiple(offset_t value, offset_t increment)
1.81 +{
1.82 + offset_t last = increment;
1.83 +
1.84 + while (1)
1.85 + {
1.86 + if (value < increment)
1.87 + return trunc(value, last);
1.88 +
1.89 + last = increment;
1.90 + increment *= 2;
1.91 + }
1.92 +}
1.93 +
1.94 +/* Find the maximum size aligned region within the region from 'start' to (but
1.95 + not including) 'end', with the given initial 'increment'. */
1.96 +
1.97 +offset_t max_multiple(offset_t start, offset_t end, offset_t increment)
1.98 +{
1.99 + /* The largest possible aligned region is derived from the region size. */
1.100 +
1.101 + offset_t size = trunc_multiple(end - start, increment);
1.102 +
1.103 + /* Apply the alignment to the start. */
1.104 +
1.105 + offset_t aligned = round(start, size);
1.106 +
1.107 + /* If the region is aligned, return the size. */
1.108 +
1.109 + if (aligned == start)
1.110 + return size;
1.111 +
1.112 + /* If the region is not aligned to the current size, recalculate the aligned
1.113 + size. */
1.114 +
1.115 + offset_t aligned_size;
1.116 +
1.117 + do
1.118 + {
1.119 + aligned_size = trunc_multiple(end - aligned, increment);
1.120 + size /= 2;
1.121 +
1.122 + /* Determine whether a smaller alignment could yield a larger aligned
1.123 + size. */
1.124 +
1.125 + if (aligned_size >= size)
1.126 + return aligned_size;
1.127 +
1.128 + aligned = round(start, size);
1.129 + }
1.130 + while (aligned > start);
1.131 +
1.132 + return aligned_size;
1.133 +}
1.134 +
1.135 +// vim: tabstop=4 expandtab shiftwidth=4