chmemcore.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 chmemcore.h
  17. * @brief Core memory manager macros and structures.
  18. *
  19. * @addtogroup oslib_memcore
  20. * @{
  21. */
  22. #ifndef CHMEMCORE_H
  23. #define CHMEMCORE_H
  24. #if (CH_CFG_USE_MEMCORE == TRUE) || defined(__DOXYGEN__)
  25. /*===========================================================================*/
  26. /* Module constants. */
  27. /*===========================================================================*/
  28. /*===========================================================================*/
  29. /* Module pre-compile time settings. */
  30. /*===========================================================================*/
  31. /**
  32. * @brief Managed RAM size.
  33. * @details Size of the RAM area to be managed by the OS. If set to zero
  34. * then the whole available RAM is used. The core memory is made
  35. * available to the heap allocator and/or can be used directly through
  36. * the simplified core memory allocator.
  37. *
  38. * @note In order to let the OS manage the whole RAM the linker script must
  39. * provide the @p __heap_base__ and @p __heap_end__ symbols.
  40. * @note Requires @p CH_CFG_USE_MEMCORE.
  41. */
  42. #if !defined(CH_CFG_MEMCORE_SIZE) || defined(__DOXYGEN__)
  43. #define CH_CFG_MEMCORE_SIZE 0
  44. #endif
  45. /*===========================================================================*/
  46. /* Derived constants and error checks. */
  47. /*===========================================================================*/
  48. #if CH_CFG_MEMCORE_SIZE < 0
  49. #error "invalid CH_CFG_MEMCORE_SIZE value specified"
  50. #endif
  51. /*===========================================================================*/
  52. /* Module data structures and types. */
  53. /*===========================================================================*/
  54. /**
  55. * @brief Memory get function.
  56. */
  57. typedef void *(*memgetfunc_t)(size_t size, unsigned align);
  58. /**
  59. * @brief Enhanced memory get function.
  60. */
  61. typedef void *(*memgetfunc2_t)(size_t size, unsigned align, size_t offset);
  62. /**
  63. * @brief Type of memory core object.
  64. */
  65. typedef struct {
  66. /**
  67. * @brief Next free address.
  68. */
  69. uint8_t *nextmem;
  70. /**
  71. * @brief Final address.
  72. */
  73. uint8_t *endmem;
  74. } memcore_t;
  75. /*===========================================================================*/
  76. /* Module macros. */
  77. /*===========================================================================*/
  78. /*===========================================================================*/
  79. /* External declarations. */
  80. /*===========================================================================*/
  81. #if !defined(__DOXYGEN__)
  82. extern memcore_t ch_memcore;
  83. #endif
  84. #ifdef __cplusplus
  85. extern "C" {
  86. #endif
  87. void _core_init(void);
  88. void *chCoreAllocAlignedWithOffsetI(size_t size,
  89. unsigned align,
  90. size_t offset);
  91. void *chCoreAllocAlignedWithOffset(size_t size,
  92. unsigned align,
  93. size_t offset);
  94. size_t chCoreGetStatusX(void);
  95. #ifdef __cplusplus
  96. }
  97. #endif
  98. /*===========================================================================*/
  99. /* Module inline functions. */
  100. /*===========================================================================*/
  101. /**
  102. * @brief Allocates a memory block.
  103. * @details The allocated block is guaranteed to be properly aligned to the
  104. * specified alignment.
  105. *
  106. * @param[in] size the size of the block to be allocated.
  107. * @param[in] align desired memory alignment
  108. * @return A pointer to the allocated memory block.
  109. * @retval NULL allocation failed, core memory exhausted.
  110. *
  111. * @iclass
  112. */
  113. static inline void *chCoreAllocAlignedI(size_t size, unsigned align) {
  114. return chCoreAllocAlignedWithOffsetI(size, align, 0U);
  115. }
  116. /**
  117. * @brief Allocates a memory block.
  118. * @details The allocated block is guaranteed to be properly aligned to the
  119. * specified alignment.
  120. *
  121. * @param[in] size the size of the block to be allocated
  122. * @param[in] align desired memory alignment
  123. * @return A pointer to the allocated memory block.
  124. * @retval NULL allocation failed, core memory exhausted.
  125. *
  126. * @api
  127. */
  128. static inline void *chCoreAllocAligned(size_t size, unsigned align) {
  129. void *p;
  130. chSysLock();
  131. p = chCoreAllocAlignedWithOffsetI(size, align, 0U);
  132. chSysUnlock();
  133. return p;
  134. }
  135. /**
  136. * @brief Allocates a memory block.
  137. * @details The allocated block is guaranteed to be properly aligned for a
  138. * pointer data type.
  139. *
  140. * @param[in] size the size of the block to be allocated.
  141. * @return A pointer to the allocated memory block.
  142. * @retval NULL allocation failed, core memory exhausted.
  143. *
  144. * @iclass
  145. */
  146. static inline void *chCoreAllocI(size_t size) {
  147. return chCoreAllocAlignedWithOffsetI(size, PORT_NATURAL_ALIGN, 0U);
  148. }
  149. /**
  150. * @brief Allocates a memory block.
  151. * @details The allocated block is guaranteed to be properly aligned for a
  152. * pointer data type.
  153. *
  154. * @param[in] size the size of the block to be allocated.
  155. * @return A pointer to the allocated memory block.
  156. * @retval NULL allocation failed, core memory exhausted.
  157. *
  158. * @api
  159. */
  160. static inline void *chCoreAlloc(size_t size) {
  161. return chCoreAllocAlignedWithOffset(size, PORT_NATURAL_ALIGN, 0U);
  162. }
  163. #endif /* CH_CFG_USE_MEMCORE == TRUE */
  164. #endif /* CHMEMCORE_H */
  165. /** @} */