chmempools.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 chmempools.h
  17. * @brief Memory Pools macros and structures.
  18. *
  19. * @addtogroup oslib_mempools
  20. * @{
  21. */
  22. #ifndef CHMEMPOOLS_H
  23. #define CHMEMPOOLS_H
  24. #if (CH_CFG_USE_MEMPOOLS == TRUE) || defined(__DOXYGEN__)
  25. /*===========================================================================*/
  26. /* Module constants. */
  27. /*===========================================================================*/
  28. /*===========================================================================*/
  29. /* Module pre-compile time settings. */
  30. /*===========================================================================*/
  31. /*===========================================================================*/
  32. /* Derived constants and error checks. */
  33. /*===========================================================================*/
  34. #if CH_CFG_USE_MEMCORE == FALSE
  35. #error "CH_CFG_USE_MEMPOOLS requires CH_CFG_USE_MEMCORE"
  36. #endif
  37. /*===========================================================================*/
  38. /* Module data structures and types. */
  39. /*===========================================================================*/
  40. /**
  41. * @brief Memory pool free object header.
  42. */
  43. struct pool_header {
  44. struct pool_header *next; /**< @brief Pointer to the next pool
  45. header in the list. */
  46. };
  47. /**
  48. * @brief Memory pool descriptor.
  49. */
  50. typedef struct {
  51. struct pool_header *next; /**< @brief Pointer to the header. */
  52. size_t object_size; /**< @brief Memory pool objects
  53. size. */
  54. unsigned align; /**< @brief Required alignment. */
  55. memgetfunc_t provider; /**< @brief Memory blocks provider
  56. for this pool. */
  57. } memory_pool_t;
  58. #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
  59. /**
  60. * @brief Guarded memory pool descriptor.
  61. */
  62. typedef struct {
  63. semaphore_t sem; /**< @brief Counter semaphore guarding
  64. the memory pool. */
  65. memory_pool_t pool; /**< @brief The memory pool itself. */
  66. } guarded_memory_pool_t;
  67. #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
  68. /*===========================================================================*/
  69. /* Module macros. */
  70. /*===========================================================================*/
  71. /**
  72. * @brief Data part of a static memory pool initializer.
  73. * @details This macro should be used when statically initializing a
  74. * memory pool that is part of a bigger structure.
  75. *
  76. * @param[in] name the name of the memory pool variable
  77. * @param[in] size size of the memory pool contained objects
  78. * @param[in] align required memory alignment
  79. * @param[in] provider memory provider function for the memory pool
  80. */
  81. #define _MEMORYPOOL_DATA(name, size, align, provider) \
  82. {NULL, size, align, provider}
  83. /**
  84. * @brief Static memory pool initializer.
  85. * @details Statically initialized memory pools require no explicit
  86. * initialization using @p chPoolInit().
  87. *
  88. * @param[in] name the name of the memory pool variable
  89. * @param[in] size size of the memory pool contained objects
  90. * @param[in] align required memory alignment
  91. * @param[in] provider memory provider function for the memory pool or @p NULL
  92. * if the pool is not allowed to grow automatically
  93. */
  94. #define MEMORYPOOL_DECL(name, size, align, provider) \
  95. memory_pool_t name = _MEMORYPOOL_DATA(name, size, align, provider)
  96. #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
  97. /**
  98. * @brief Data part of a static guarded memory pool initializer.
  99. * @details This macro should be used when statically initializing a
  100. * memory pool that is part of a bigger structure.
  101. *
  102. * @param[in] name the name of the memory pool variable
  103. * @param[in] size size of the memory pool contained objects
  104. * @param[in] align required memory alignment
  105. */
  106. #define _GUARDEDMEMORYPOOL_DATA(name, size, align) { \
  107. _SEMAPHORE_DATA(name.sem, (cnt_t)0), \
  108. _MEMORYPOOL_DATA(NULL, size, align, NULL) \
  109. }
  110. /**
  111. * @brief Static guarded memory pool initializer.
  112. * @details Statically initialized guarded memory pools require no explicit
  113. * initialization using @p chGuardedPoolInit().
  114. *
  115. * @param[in] name the name of the guarded memory pool variable
  116. * @param[in] size size of the memory pool contained objects
  117. * @param[in] align required memory alignment
  118. */
  119. #define GUARDEDMEMORYPOOL_DECL(name, size, align) \
  120. guarded_memory_pool_t name = _GUARDEDMEMORYPOOL_DATA(name, size, align)
  121. #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
  122. /*===========================================================================*/
  123. /* External declarations. */
  124. /*===========================================================================*/
  125. #ifdef __cplusplus
  126. extern "C" {
  127. #endif
  128. void chPoolObjectInitAligned(memory_pool_t *mp, size_t size,
  129. unsigned align, memgetfunc_t provider);
  130. void chPoolLoadArray(memory_pool_t *mp, void *p, size_t n);
  131. void *chPoolAllocI(memory_pool_t *mp);
  132. void *chPoolAlloc(memory_pool_t *mp);
  133. void chPoolFreeI(memory_pool_t *mp, void *objp);
  134. void chPoolFree(memory_pool_t *mp, void *objp);
  135. #if CH_CFG_USE_SEMAPHORES == TRUE
  136. void chGuardedPoolObjectInitAligned(guarded_memory_pool_t *gmp,
  137. size_t size,
  138. unsigned align);
  139. void chGuardedPoolLoadArray(guarded_memory_pool_t *gmp, void *p, size_t n);
  140. void *chGuardedPoolAllocTimeoutS(guarded_memory_pool_t *gmp,
  141. sysinterval_t timeout);
  142. void *chGuardedPoolAllocTimeout(guarded_memory_pool_t *gmp,
  143. sysinterval_t timeout);
  144. void chGuardedPoolFree(guarded_memory_pool_t *gmp, void *objp);
  145. #endif
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. /*===========================================================================*/
  150. /* Module inline functions. */
  151. /*===========================================================================*/
  152. /**
  153. * @brief Initializes an empty memory pool.
  154. *
  155. * @param[out] mp pointer to a @p memory_pool_t structure
  156. * @param[in] size the size of the objects contained in this memory pool,
  157. * the minimum accepted size is the size of a pointer to
  158. * void.
  159. * @param[in] provider memory provider function for the memory pool or
  160. * @p NULL if the pool is not allowed to grow
  161. * automatically
  162. *
  163. * @init
  164. */
  165. static inline void chPoolObjectInit(memory_pool_t *mp,
  166. size_t size,
  167. memgetfunc_t provider) {
  168. chPoolObjectInitAligned(mp, size, PORT_NATURAL_ALIGN, provider);
  169. }
  170. /**
  171. * @brief Adds an object to a memory pool.
  172. * @pre The memory pool must be already been initialized.
  173. * @pre The added object must be of the right size for the specified
  174. * memory pool.
  175. * @pre The added object must be properly aligned.
  176. * @note This function is just an alias for @p chPoolFree() and has been
  177. * added for clarity.
  178. *
  179. * @param[in] mp pointer to a @p memory_pool_t structure
  180. * @param[in] objp the pointer to the object to be added
  181. *
  182. * @api
  183. */
  184. static inline void chPoolAdd(memory_pool_t *mp, void *objp) {
  185. chPoolFree(mp, objp);
  186. }
  187. /**
  188. * @brief Adds an object to a memory pool.
  189. * @pre The memory pool must be already been initialized.
  190. * @pre The added object must be of the right size for the specified
  191. * memory pool.
  192. * @pre The added object must be properly aligned.
  193. * @note This function is just an alias for @p chPoolFreeI() and has been
  194. * added for clarity.
  195. *
  196. * @param[in] mp pointer to a @p memory_pool_t structure
  197. * @param[in] objp the pointer to the object to be added
  198. *
  199. * @iclass
  200. */
  201. static inline void chPoolAddI(memory_pool_t *mp, void *objp) {
  202. chPoolFreeI(mp, objp);
  203. }
  204. #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
  205. /**
  206. * @brief Initializes an empty guarded memory pool.
  207. *
  208. * @param[out] gmp pointer to a @p guarded_memory_pool_t structure
  209. * @param[in] size the size of the objects contained in this guarded
  210. * memory pool, the minimum accepted size is the size
  211. * of a pointer to void.
  212. *
  213. * @init
  214. */
  215. static inline void chGuardedPoolObjectInit(guarded_memory_pool_t *gmp,
  216. size_t size) {
  217. chGuardedPoolObjectInitAligned(gmp, size, PORT_NATURAL_ALIGN);
  218. }
  219. /**
  220. * @brief Allocates an object from a guarded memory pool.
  221. * @pre The guarded memory pool must be already been initialized.
  222. *
  223. * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
  224. * @return The pointer to the allocated object.
  225. * @retval NULL if the pool is empty.
  226. *
  227. * @iclass
  228. */
  229. static inline void *chGuardedPoolAllocI(guarded_memory_pool_t *gmp) {
  230. void *p;
  231. p = chPoolAllocI(&gmp->pool);
  232. if (p != NULL) {
  233. chSemFastWaitI(&gmp->sem);
  234. chDbgAssert(chSemGetCounterI(&gmp->sem) >= (cnt_t)0,
  235. "semaphore out of sync");
  236. }
  237. return p;
  238. }
  239. /**
  240. * @brief Releases an object into a guarded memory pool.
  241. * @pre The guarded memory pool must already be initialized.
  242. * @pre The freed object must be of the right size for the specified
  243. * guarded memory pool.
  244. * @pre The added object must be properly aligned.
  245. *
  246. * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
  247. * @param[in] objp the pointer to the object to be released
  248. *
  249. * @iclass
  250. */
  251. static inline void chGuardedPoolFreeI(guarded_memory_pool_t *gmp, void *objp) {
  252. chPoolFreeI(&gmp->pool, objp);
  253. chSemSignalI(&gmp->sem);
  254. }
  255. /**
  256. * @brief Releases an object into a guarded memory pool.
  257. * @pre The guarded memory pool must already be initialized.
  258. * @pre The freed object must be of the right size for the specified
  259. * guarded memory pool.
  260. * @pre The added object must be properly aligned.
  261. *
  262. * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
  263. * @param[in] objp the pointer to the object to be released
  264. *
  265. * @sclass
  266. */
  267. static inline void chGuardedPoolFreeS(guarded_memory_pool_t *gmp, void *objp) {
  268. chGuardedPoolFreeI(gmp, objp);
  269. chSchRescheduleS();
  270. }
  271. /**
  272. * @brief Adds an object to a guarded memory pool.
  273. * @pre The guarded memory pool must be already been initialized.
  274. * @pre The added object must be of the right size for the specified
  275. * guarded memory pool.
  276. * @pre The added object must be properly aligned.
  277. * @note This function is just an alias for @p chGuardedPoolFree() and
  278. * has been added for clarity.
  279. *
  280. * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
  281. * @param[in] objp the pointer to the object to be added
  282. *
  283. * @api
  284. */
  285. static inline void chGuardedPoolAdd(guarded_memory_pool_t *gmp, void *objp) {
  286. chGuardedPoolFree(gmp, objp);
  287. }
  288. /**
  289. * @brief Adds an object to a guarded memory pool.
  290. * @pre The guarded memory pool must be already been initialized.
  291. * @pre The added object must be of the right size for the specified
  292. * guarded memory pool.
  293. * @pre The added object must be properly aligned.
  294. * @note This function is just an alias for @p chGuardedPoolFreeI() and
  295. * has been added for clarity.
  296. *
  297. * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
  298. * @param[in] objp the pointer to the object to be added
  299. *
  300. * @iclass
  301. */
  302. static inline void chGuardedPoolAddI(guarded_memory_pool_t *gmp, void *objp) {
  303. chGuardedPoolFreeI(gmp, objp);
  304. }
  305. /**
  306. * @brief Adds an object to a guarded memory pool.
  307. * @pre The guarded memory pool must be already been initialized.
  308. * @pre The added object must be of the right size for the specified
  309. * guarded memory pool.
  310. * @pre The added object must be properly aligned.
  311. * @note This function is just an alias for @p chGuardedPoolFreeI() and
  312. * has been added for clarity.
  313. *
  314. * @param[in] gmp pointer to a @p guarded_memory_pool_t structure
  315. * @param[in] objp the pointer to the object to be added
  316. *
  317. * @sclass
  318. */
  319. static inline void chGuardedPoolAddS(guarded_memory_pool_t *gmp, void *objp) {
  320. chGuardedPoolFreeS(gmp, objp);
  321. }
  322. #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
  323. #endif /* CH_CFG_USE_MEMPOOLS == TRUE */
  324. #endif /* CHMEMPOOLS_H */
  325. /** @} */