| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 2016, 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 | #ifndef _ut0byte_h_ |
| 28 | #define _ut0byte_h_ |
| 29 | |
| 30 | #include <stdint.h> |
| 31 | |
| 32 | #include "ut0dbg.h" |
| 33 | |
| 34 | /** The following function rounds up a pointer to the nearest aligned address. |
| 35 | @return aligned pointer */ |
| 36 | inline void *ut_align(const void *ptr, /*!< in: pointer */ |
| 37 | ulint align_no) /*!< in: align by this number */ |
| 38 | { |
| 39 | ut_ad(align_no > 0); |
| 40 | ut_ad(((align_no - 1) & align_no) == 0); |
| 41 | ut_ad(ptr != nullptr); |
| 42 | |
| 43 | ut_ad(sizeof(void *) == sizeof(ulint)); |
| 44 | |
| 45 | return ((void *)((((intptr_t)ptr) + align_no - 1) & ~(align_no - 1))); |
| 46 | } |
| 47 | |
| 48 | inline void *ut_align_down(const void *ptr, ulint align_no) { |
| 49 | ut_ad(align_no > 0); |
| 50 | ut_ad(((align_no - 1) & align_no) == 0); |
| 51 | ut_ad(ptr != nullptr); |
| 52 | |
| 53 | ut_ad(sizeof(void *) == sizeof(ulint)); |
| 54 | |
| 55 | return ((void *)((((intptr_t)ptr)) & ~(align_no - 1))); |
| 56 | } |
| 57 | |
| 58 | inline ulint ut_align_offset(const void *ptr, ulint align_no) { |
| 59 | ut_ad(align_no > 0); |
| 60 | ut_ad(((align_no - 1) & align_no) == 0); |
| 61 | ut_ad(ptr != nullptr); |
| 62 | |
| 63 | ut_ad(sizeof(void *) == sizeof(ulint)); |
| 64 | |
| 65 | return (((intptr_t)ptr) & (align_no - 1)); |
| 66 | } |
| 67 | |
| 68 | /*******************************************************/ /** |
| 69 | Creates a 64-bit integer out of two 32-bit integers. |
| 70 | @return created integer */ |
| 71 | inline ib_uint64_t ut_ull_create(ulint high, /*!< in: high-order 32 bits */ |
| 72 | ulint low) /*!< in: low-order 32 bits */ |
| 73 | { |
| 74 | ut_ad(high <= ULINT32_MASK); |
| 75 | ut_ad(low <= ULINT32_MASK); |
| 76 | return (((ib_uint64_t)high) << 32 | low); |
| 77 | } |
| 78 | |
| 79 | #endif // _ut0byte_h_ |