chmemheaps.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 chmemheaps.h
  17. * @brief Memory heaps macros and structures.
  18. *
  19. * @addtogroup oslib_memheaps
  20. * @{
  21. */
  22. #ifndef CHMEMHEAPS_H
  23. #define CHMEMHEAPS_H
  24. #if (CH_CFG_USE_HEAP == TRUE) || defined(__DOXYGEN__)
  25. /*===========================================================================*/
  26. /* Module constants. */
  27. /*===========================================================================*/
  28. /**
  29. * @brief Minimum alignment used for heap.
  30. * @note Cannot use the sizeof operator in this macro.
  31. */
  32. #if (SIZEOF_PTR == 4) || defined(__DOXYGEN__)
  33. #define CH_HEAP_ALIGNMENT 8U
  34. #elif (SIZEOF_PTR == 2)
  35. #define CH_HEAP_ALIGNMENT 4U
  36. #else
  37. #error "unsupported pointer size"
  38. #endif
  39. /*===========================================================================*/
  40. /* Module pre-compile time settings. */
  41. /*===========================================================================*/
  42. /*===========================================================================*/
  43. /* Derived constants and error checks. */
  44. /*===========================================================================*/
  45. #if CH_CFG_USE_MEMCORE == FALSE
  46. #error "CH_CFG_USE_HEAP requires CH_CFG_USE_MEMCORE"
  47. #endif
  48. #if (CH_CFG_USE_MUTEXES == FALSE) && (CH_CFG_USE_SEMAPHORES == FALSE)
  49. #error "CH_CFG_USE_HEAP requires CH_CFG_USE_MUTEXES and/or CH_CFG_USE_SEMAPHORES"
  50. #endif
  51. /*===========================================================================*/
  52. /* Module data structures and types. */
  53. /*===========================================================================*/
  54. /**
  55. * @brief Type of a memory heap.
  56. */
  57. typedef struct memory_heap memory_heap_t;
  58. /**
  59. * @brief Type of a memory heap header.
  60. */
  61. typedef union heap_header heap_header_t;
  62. /**
  63. * @brief Memory heap block header.
  64. */
  65. union heap_header {
  66. struct {
  67. heap_header_t *next; /**< @brief Next block in free list. */
  68. size_t pages; /**< @brief Size of the area in pages. */
  69. } free;
  70. struct {
  71. memory_heap_t *heap; /**< @brief Block owner heap. */
  72. size_t size; /**< @brief Size of the area in bytes. */
  73. } used;
  74. };
  75. /**
  76. * @brief Structure describing a memory heap.
  77. */
  78. struct memory_heap {
  79. memgetfunc2_t provider; /**< @brief Memory blocks provider for
  80. this heap. */
  81. heap_header_t header; /**< @brief Free blocks list header. */
  82. #if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
  83. mutex_t mtx; /**< @brief Heap access mutex. */
  84. #else
  85. semaphore_t sem; /**< @brief Heap access semaphore. */
  86. #endif
  87. };
  88. /*===========================================================================*/
  89. /* Module macros. */
  90. /*===========================================================================*/
  91. /**
  92. * @brief Allocation of an aligned static heap buffer.
  93. */
  94. #define CH_HEAP_AREA(name, size) \
  95. ALIGNED_VAR(CH_HEAP_ALIGNMENT) \
  96. uint8_t name[MEM_ALIGN_NEXT((size), CH_HEAP_ALIGNMENT)]
  97. /*===========================================================================*/
  98. /* External declarations. */
  99. /*===========================================================================*/
  100. #ifdef __cplusplus
  101. extern "C" {
  102. #endif
  103. void _heap_init(void);
  104. void chHeapObjectInit(memory_heap_t *heapp, void *buf, size_t size);
  105. void *chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align);
  106. void chHeapFree(void *p);
  107. size_t chHeapStatus(memory_heap_t *heapp, size_t *totalp, size_t *largestp);
  108. #ifdef __cplusplus
  109. }
  110. #endif
  111. /*===========================================================================*/
  112. /* Module inline functions. */
  113. /*===========================================================================*/
  114. /**
  115. * @brief Allocates a block of memory from the heap by using the first-fit
  116. * algorithm.
  117. * @details The allocated block is guaranteed to be properly aligned for a
  118. * pointer data type.
  119. *
  120. * @param[in] heapp pointer to a heap descriptor or @p NULL in order to
  121. * access the default heap.
  122. * @param[in] size the size of the block to be allocated. Note that the
  123. * allocated block may be a bit bigger than the requested
  124. * size for alignment and fragmentation reasons.
  125. * @return A pointer to the allocated block.
  126. * @retval NULL if the block cannot be allocated.
  127. *
  128. * @api
  129. */
  130. static inline void *chHeapAlloc(memory_heap_t *heapp, size_t size) {
  131. return chHeapAllocAligned(heapp, size, CH_HEAP_ALIGNMENT);
  132. }
  133. /**
  134. * @brief Returns the size of an allocated block.
  135. * @note The returned value is the requested size, the real size is the
  136. * same value aligned to the next @p CH_HEAP_ALIGNMENT multiple.
  137. *
  138. * @param[in] p pointer to the memory block
  139. * @return Size of the block.
  140. *
  141. * @api
  142. */
  143. static inline size_t chHeapGetSize(const void *p) {
  144. return ((heap_header_t *)p - 1U)->used.size;
  145. }
  146. #endif /* CH_CFG_USE_HEAP == TRUE */
  147. #endif /* CHMEMHEAPS_H */
  148. /** @} */