| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2021, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/detail/ut/helper.h |
| 29 |
Small helper functions. Exists mostly because ut0ut.h is not usable from |
| 30 |
every translation unit. |
| 31 |
*/ |
| 32 |
|
| 33 |
#ifndef detail_ut_helper_h |
| 34 |
#define detail_ut_helper_h |
| 35 |
|
| 36 |
namespace ut { |
| 37 |
namespace detail { |
| 38 |
|
| 39 |
/** Calculates the smallest multiple of m that is not smaller than n |
| 40 |
when m is a power of two. In other words, rounds n up to m * k. |
| 41 |
@param n in: number to round up |
| 42 |
@param m in: alignment, must be a power of two |
| 43 |
@return n rounded up to the smallest possible integer multiple of m |
| 44 |
*/ |
| 45 |
constexpr size_t calc_align(size_t n, size_t m) { |
| 46 |
// This is a copy pasta from ut0ut.h but consuming that header from |
| 47 |
// this file bursts the build into flames. Let's at least stick to the |
| 48 |
// similar name. |
| 49 |
return (n + (m - 1)) & ~(m - 1); |
| 50 |
} |
| 51 |
|
| 52 |
/** Calculates the biggest multiple of m that is not bigger than n |
| 53 |
when m is a power of two. In other words, rounds n down to m * k. |
| 54 |
@param n in: number to round down |
| 55 |
@param m in: alignment, must be a power of two |
| 56 |
@return n rounded down to the biggest possible integer multiple of m */ |
| 57 |
constexpr size_t pow2_round(size_t n, size_t m) { |
| 58 |
// This is a copy pasta from ut0ut.h but consuming that header from |
| 59 |
// this file bursts the build into flames. Let's at least stick to the |
| 60 |
// similar name. |
| 61 |
return (n & ~(m - 1)); |
| 62 |
} |
| 63 |
|
| 64 |
/** Calculates the next multiple of m that is bigger or equal to n. |
| 65 |
@param n in: number to find the next multiple of in terms of m |
| 66 |
@param m in: alignment, must be a power of two |
| 67 |
@return next next multiple of m bigger or equal than n */ |
| 68 |
constexpr size_t round_to_next_multiple(size_t n, size_t m) { |
| 69 |
return pow2_round(n + (m - 1), m); |
| 70 |
} |
| 71 |
|
| 72 |
} // namespace detail |
| 73 |
} // namespace ut |
| 74 |
|
| 75 |
#endif |