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