chalign.h 4.3 KB

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