ChibiOS/RT  5.1.0
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  * @{
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  * @brief Alignment mask constant.
56  *
57  * @param[in] a alignment, must be a power of two
58  */
59 #define MEM_ALIGN_MASK(a) ((size_t)(a) - 1U)
60 
61 /**
62  * @brief Aligns to the previous aligned memory address.
63  *
64  * @param[in] p variable to be aligned
65  * @param[in] a alignment, must be a power of two
66  */
67 #define MEM_ALIGN_PREV(p, a) \
68  /*lint -save -e9033 [10.8] The cast is safe.*/ \
69  ((size_t)(p) & ~MEM_ALIGN_MASK(a)) \
70  /*lint -restore*/
71 
72 /**
73  * @brief Aligns to the next aligned memory address.
74  *
75  * @param[in] p variable to be aligned
76  * @param[in] a alignment, must be a power of two
77  */
78 #define MEM_ALIGN_NEXT(p, a) \
79  /*lint -save -e9033 [10.8] The cast is safe.*/ \
80  MEM_ALIGN_PREV((size_t)(p) + MEM_ALIGN_MASK(a), (a)) \
81  /*lint -restore*/
82 
83 /**
84  * @brief Returns whatever a pointer or memory size is aligned.
85  *
86  * @param[in] p variable to be aligned
87  * @param[in] a alignment, must be a power of two
88  */
89 #define MEM_IS_ALIGNED(p, a) (((size_t)(p) & MEM_ALIGN_MASK(a)) == 0U)
90 
91 /**
92  * @brief Returns whatever a constant is a valid alignment.
93  * @details Valid alignments are powers of two.
94  *
95  * @param[in] a alignment to be checked, must be a constant
96  */
97 #define MEM_IS_VALID_ALIGNMENT(a) \
98  (((size_t)(a) != 0U) && (((size_t)(a) & ((size_t)(a) - 1U)) == 0U))
99 /** @} */
100 
101 /*===========================================================================*/
102 /* External declarations. */
103 /*===========================================================================*/
104 
105 #ifdef __cplusplus
106 extern "C" {
107 #endif
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 /*===========================================================================*/
114 /* Module inline functions. */
115 /*===========================================================================*/
116 
117 #endif /* CHALIGN_H */
118 
119 /** @} */