ChibiOS 21.11.5
chalign.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 Giovanni Di Sirio.
3
4 This file is part of ChibiOS.
5
6 ChibiOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file rt/include/chalign.h
21 * @brief Memory alignment macros and structures.
22 *
23 * @addtogroup mem
24 * @details Memory Alignment services.
25 * @{
26 */
27
28#ifndef CHALIGN_H
29#define CHALIGN_H
30
31/*===========================================================================*/
32/* Module constants. */
33/*===========================================================================*/
34
35/*===========================================================================*/
36/* Module pre-compile time settings. */
37/*===========================================================================*/
38
39/*===========================================================================*/
40/* Derived constants and error checks. */
41/*===========================================================================*/
42
43/*===========================================================================*/
44/* Module data structures and types. */
45/*===========================================================================*/
46
47/*===========================================================================*/
48/* Module macros. */
49/*===========================================================================*/
50
51/**
52 * @name Memory alignment support macros
53 * @{
54 */
55/**
56 * @brief Alignment mask constant.
57 *
58 * @param[in] a alignment, must be a power of two
59 */
60#define MEM_ALIGN_MASK(a) ((size_t)(a) - 1U)
61
62/**
63 * @brief Aligns to the previous aligned memory address.
64 *
65 * @param[in] p variable to be aligned
66 * @param[in] a alignment, must be a power of two
67 */
68#define MEM_ALIGN_PREV(p, a) \
69 /*lint -save -e9033 [10.8] The cast is safe.*/ \
70 ((size_t)(p) & ~MEM_ALIGN_MASK(a)) \
71 /*lint -restore*/
72
73/**
74 * @brief Aligns to the next aligned memory address.
75 *
76 * @param[in] p variable to be aligned
77 * @param[in] a alignment, must be a power of two
78 */
79#define MEM_ALIGN_NEXT(p, a) \
80 /*lint -save -e9033 [10.8] The cast is safe.*/ \
81 MEM_ALIGN_PREV((size_t)(p) + MEM_ALIGN_MASK(a), (a)) \
82 /*lint -restore*/
83
84/**
85 * @brief Returns whatever a pointer or memory size is aligned.
86 *
87 * @param[in] p variable to be aligned
88 * @param[in] a alignment, must be a power of two
89 */
90#define MEM_IS_ALIGNED(p, a) (((size_t)(p) & MEM_ALIGN_MASK(a)) == 0U)
91
92/**
93 * @brief Returns whatever a constant is a valid alignment.
94 * @details Valid alignments are powers of two.
95 *
96 * @param[in] a alignment to be checked, must be a constant
97 */
98#define MEM_IS_VALID_ALIGNMENT(a) \
99 (((size_t)(a) != 0U) && (((size_t)(a) & ((size_t)(a) - 1U)) == 0U))
100/** @} */
101
102/*===========================================================================*/
103/* External declarations. */
104/*===========================================================================*/
105
106#ifdef __cplusplus
107extern "C" {
108#endif
109
110#ifdef __cplusplus
111}
112#endif
113
114/*===========================================================================*/
115/* Module inline functions. */
116/*===========================================================================*/
117
118#endif /* CHALIGN_H */
119
120/** @} */