MySQL 9.0.0
Source Code Documentation
helper.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2021, 2024, 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 designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 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
36namespace ut {
37namespace 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 */
45constexpr 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 */
57constexpr 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 */
68constexpr 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
Definition: ut0tuple.h:57
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:57
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:45
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:68
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:48
int n
Definition: xcom_base.cc:509