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