ChibiOS/RT  6.0.3
chalign.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 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; either version 3 of the License, or
9  (at your option) any later version.
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 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  * @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
107 extern "C" {
108 #endif
109 
110 #ifdef __cplusplus
111 }
112 #endif
113 
114 /*===========================================================================*/
115 /* Module inline functions. */
116 /*===========================================================================*/
117 
118 #endif /* CHALIGN_H */
119 
120 /** @} */