mempool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-05-27 Bernard implement memory pool
  9. * 2006-06-03 Bernard fix the thread timer init bug
  10. * 2006-06-30 Bernard fix the allocate/free block bug
  11. * 2006-08-04 Bernard add hook support
  12. * 2006-08-10 Bernard fix interrupt bug in rt_mp_alloc
  13. * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca
  14. * 2010-10-26 yi.qiu add module support in rt_mp_delete
  15. * 2011-01-24 Bernard add object allocation check.
  16. * 2012-03-22 Bernard fix align issue in rt_mp_init and rt_mp_create.
  17. * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to mempool.c
  18. */
  19. #include <rthw.h>
  20. #include <rtthread.h>
  21. #ifdef RT_USING_MEMPOOL
  22. #ifndef __on_rt_mp_alloc_hook
  23. #define __on_rt_mp_alloc_hook(mp, block) __ON_HOOK_ARGS(rt_mp_alloc_hook, (mp, block))
  24. #endif
  25. #ifndef __on_rt_mp_free_hook
  26. #define __on_rt_mp_free_hook(mp, block) __ON_HOOK_ARGS(rt_mp_free_hook, (mp, block))
  27. #endif
  28. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  29. static void (*rt_mp_alloc_hook)(struct rt_mempool *mp, void *block);
  30. static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block);
  31. /**
  32. * @addtogroup Hook
  33. */
  34. /**@{*/
  35. /**
  36. * @brief This function will set a hook function, which will be invoked when a memory
  37. * block is allocated from the memory pool.
  38. *
  39. * @param hook the hook function
  40. */
  41. void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block))
  42. {
  43. rt_mp_alloc_hook = hook;
  44. }
  45. /**
  46. * @brief This function will set a hook function, which will be invoked when a memory
  47. * block is released to the memory pool.
  48. *
  49. * @param hook the hook function
  50. */
  51. void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block))
  52. {
  53. rt_mp_free_hook = hook;
  54. }
  55. /**@}*/
  56. #endif /* RT_USING_HOOK */
  57. /**
  58. * @addtogroup MM
  59. */
  60. /**@{*/
  61. /**
  62. * @brief This function will initialize a memory pool object, normally which is used
  63. * for static object.
  64. *
  65. * @param mp is the memory pool object.
  66. *
  67. * @param name is the name of the memory pool.
  68. *
  69. * @param start is the start address of the memory pool.
  70. *
  71. * @param size is the total size of the memory pool.
  72. *
  73. * @param block_size is the size for each block..
  74. *
  75. * @return RT_EOK
  76. */
  77. rt_err_t rt_mp_init(struct rt_mempool *mp,
  78. const char *name,
  79. void *start,
  80. rt_size_t size,
  81. rt_size_t block_size)
  82. {
  83. rt_uint8_t *block_ptr;
  84. rt_size_t offset;
  85. /* parameter check */
  86. RT_ASSERT(mp != RT_NULL);
  87. RT_ASSERT(name != RT_NULL);
  88. RT_ASSERT(start != RT_NULL);
  89. RT_ASSERT(size > 0 && block_size > 0);
  90. /* initialize object */
  91. rt_object_init(&(mp->parent), RT_Object_Class_MemPool, name);
  92. /* initialize memory pool */
  93. mp->start_address = start;
  94. mp->size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  95. /* align the block size */
  96. block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
  97. mp->block_size = block_size;
  98. /* align to align size byte */
  99. mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t *));
  100. mp->block_free_count = mp->block_total_count;
  101. /* initialize suspended thread list */
  102. rt_list_init(&(mp->suspend_thread));
  103. /* initialize free block list */
  104. block_ptr = (rt_uint8_t *)mp->start_address;
  105. for (offset = 0; offset < mp->block_total_count; offset ++)
  106. {
  107. *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *))) =
  108. (rt_uint8_t *)(block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *)));
  109. }
  110. *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) =
  111. RT_NULL;
  112. mp->block_list = block_ptr;
  113. return RT_EOK;
  114. }
  115. RTM_EXPORT(rt_mp_init);
  116. /**
  117. * @brief This function will detach a memory pool from system object management.
  118. *
  119. * @param mp is the memory pool object.
  120. *
  121. * @return RT_EOK
  122. */
  123. rt_err_t rt_mp_detach(struct rt_mempool *mp)
  124. {
  125. struct rt_thread *thread;
  126. rt_base_t level;
  127. /* parameter check */
  128. RT_ASSERT(mp != RT_NULL);
  129. RT_ASSERT(rt_object_get_type(&mp->parent) == RT_Object_Class_MemPool);
  130. RT_ASSERT(rt_object_is_systemobject(&mp->parent));
  131. /* wake up all suspended threads */
  132. while (!rt_list_isempty(&(mp->suspend_thread)))
  133. {
  134. /* disable interrupt */
  135. level = rt_hw_interrupt_disable();
  136. /* get next suspend thread */
  137. thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
  138. /* set error code to -RT_ERROR */
  139. thread->error = -RT_ERROR;
  140. /*
  141. * resume thread
  142. * In rt_thread_resume function, it will remove current thread from
  143. * suspend list
  144. */
  145. rt_thread_resume(thread);
  146. /* enable interrupt */
  147. rt_hw_interrupt_enable(level);
  148. }
  149. /* detach object */
  150. rt_object_detach(&(mp->parent));
  151. return RT_EOK;
  152. }
  153. RTM_EXPORT(rt_mp_detach);
  154. #ifdef RT_USING_HEAP
  155. /**
  156. * @brief This function will create a mempool object and allocate the memory pool from
  157. * heap.
  158. *
  159. * @param name is the name of memory pool.
  160. *
  161. * @param block_count is the count of blocks in memory pool.
  162. *
  163. * @param block_size is the size for each block.
  164. *
  165. * @return the created mempool object
  166. */
  167. rt_mp_t rt_mp_create(const char *name,
  168. rt_size_t block_count,
  169. rt_size_t block_size)
  170. {
  171. rt_uint8_t *block_ptr;
  172. struct rt_mempool *mp;
  173. rt_size_t offset;
  174. RT_DEBUG_NOT_IN_INTERRUPT;
  175. /* parameter check */
  176. RT_ASSERT(name != RT_NULL);
  177. RT_ASSERT(block_count > 0 && block_size > 0);
  178. /* allocate object */
  179. mp = (struct rt_mempool *)rt_object_allocate(RT_Object_Class_MemPool, name);
  180. /* allocate object failed */
  181. if (mp == RT_NULL)
  182. return RT_NULL;
  183. /* initialize memory pool */
  184. block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
  185. mp->block_size = block_size;
  186. mp->size = (block_size + sizeof(rt_uint8_t *)) * block_count;
  187. /* allocate memory */
  188. mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t *)) *
  189. block_count);
  190. if (mp->start_address == RT_NULL)
  191. {
  192. /* no memory, delete memory pool object */
  193. rt_object_delete(&(mp->parent));
  194. return RT_NULL;
  195. }
  196. mp->block_total_count = block_count;
  197. mp->block_free_count = mp->block_total_count;
  198. /* initialize suspended thread list */
  199. rt_list_init(&(mp->suspend_thread));
  200. /* initialize free block list */
  201. block_ptr = (rt_uint8_t *)mp->start_address;
  202. for (offset = 0; offset < mp->block_total_count; offset ++)
  203. {
  204. *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *)))
  205. = block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *));
  206. }
  207. *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *)))
  208. = RT_NULL;
  209. mp->block_list = block_ptr;
  210. return mp;
  211. }
  212. RTM_EXPORT(rt_mp_create);
  213. /**
  214. * @brief This function will delete a memory pool and release the object memory.
  215. *
  216. * @param mp is the memory pool object.
  217. *
  218. * @return RT_EOK
  219. */
  220. rt_err_t rt_mp_delete(rt_mp_t mp)
  221. {
  222. struct rt_thread *thread;
  223. rt_base_t level;
  224. RT_DEBUG_NOT_IN_INTERRUPT;
  225. /* parameter check */
  226. RT_ASSERT(mp != RT_NULL);
  227. RT_ASSERT(rt_object_get_type(&mp->parent) == RT_Object_Class_MemPool);
  228. RT_ASSERT(rt_object_is_systemobject(&mp->parent) == RT_FALSE);
  229. /* wake up all suspended threads */
  230. while (!rt_list_isempty(&(mp->suspend_thread)))
  231. {
  232. /* disable interrupt */
  233. level = rt_hw_interrupt_disable();
  234. /* get next suspend thread */
  235. thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
  236. /* set error code to -RT_ERROR */
  237. thread->error = -RT_ERROR;
  238. /*
  239. * resume thread
  240. * In rt_thread_resume function, it will remove current thread from
  241. * suspend list
  242. */
  243. rt_thread_resume(thread);
  244. /* enable interrupt */
  245. rt_hw_interrupt_enable(level);
  246. }
  247. /* release allocated room */
  248. rt_free(mp->start_address);
  249. /* detach object */
  250. rt_object_delete(&(mp->parent));
  251. return RT_EOK;
  252. }
  253. RTM_EXPORT(rt_mp_delete);
  254. #endif /* RT_USING_HEAP */
  255. /**
  256. * @brief This function will allocate a block from memory pool.
  257. *
  258. * @param mp is the memory pool object.
  259. *
  260. * @param time is the maximum waiting time for allocating memory.
  261. * - 0 for not waiting, allocating memory immediately.
  262. *
  263. * @return the allocated memory block or RT_NULL on allocated failed.
  264. */
  265. void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
  266. {
  267. rt_uint8_t *block_ptr;
  268. rt_base_t level;
  269. struct rt_thread *thread;
  270. rt_uint32_t before_sleep = 0;
  271. /* parameter check */
  272. RT_ASSERT(mp != RT_NULL);
  273. /* get current thread */
  274. thread = rt_thread_self();
  275. /* disable interrupt */
  276. level = rt_hw_interrupt_disable();
  277. while (mp->block_free_count == 0)
  278. {
  279. /* memory block is unavailable. */
  280. if (time == 0)
  281. {
  282. /* enable interrupt */
  283. rt_hw_interrupt_enable(level);
  284. rt_set_errno(-RT_ETIMEOUT);
  285. return RT_NULL;
  286. }
  287. RT_DEBUG_NOT_IN_INTERRUPT;
  288. thread->error = RT_EOK;
  289. /* need suspend thread */
  290. rt_thread_suspend(thread);
  291. rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
  292. if (time > 0)
  293. {
  294. /* get the start tick of timer */
  295. before_sleep = rt_tick_get();
  296. /* init thread timer and start it */
  297. rt_timer_control(&(thread->thread_timer),
  298. RT_TIMER_CTRL_SET_TIME,
  299. &time);
  300. rt_timer_start(&(thread->thread_timer));
  301. }
  302. /* enable interrupt */
  303. rt_hw_interrupt_enable(level);
  304. /* do a schedule */
  305. rt_schedule();
  306. if (thread->error != RT_EOK)
  307. return RT_NULL;
  308. if (time > 0)
  309. {
  310. time -= rt_tick_get() - before_sleep;
  311. if (time < 0)
  312. time = 0;
  313. }
  314. /* disable interrupt */
  315. level = rt_hw_interrupt_disable();
  316. }
  317. /* memory block is available. decrease the free block counter */
  318. mp->block_free_count--;
  319. /* get block from block list */
  320. block_ptr = mp->block_list;
  321. RT_ASSERT(block_ptr != RT_NULL);
  322. /* Setup the next free node. */
  323. mp->block_list = *(rt_uint8_t **)block_ptr;
  324. /* point to memory pool */
  325. *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
  326. /* enable interrupt */
  327. rt_hw_interrupt_enable(level);
  328. RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook,
  329. (mp, (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *))));
  330. return (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *));
  331. }
  332. RTM_EXPORT(rt_mp_alloc);
  333. /**
  334. * @brief This function will release a memory block.
  335. *
  336. * @param block the address of memory block to be released.
  337. */
  338. void rt_mp_free(void *block)
  339. {
  340. rt_uint8_t **block_ptr;
  341. struct rt_mempool *mp;
  342. struct rt_thread *thread;
  343. rt_base_t level;
  344. /* parameter check */
  345. if (block == RT_NULL) return;
  346. /* get the control block of pool which the block belongs to */
  347. block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *));
  348. mp = (struct rt_mempool *)*block_ptr;
  349. RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block));
  350. /* disable interrupt */
  351. level = rt_hw_interrupt_disable();
  352. /* increase the free block count */
  353. mp->block_free_count ++;
  354. /* link the block into the block list */
  355. *block_ptr = mp->block_list;
  356. mp->block_list = (rt_uint8_t *)block_ptr;
  357. if (!rt_list_isempty(&(mp->suspend_thread)))
  358. {
  359. /* get the suspended thread */
  360. thread = rt_list_entry(mp->suspend_thread.next,
  361. struct rt_thread,
  362. tlist);
  363. /* set error */
  364. thread->error = RT_EOK;
  365. /* resume thread */
  366. rt_thread_resume(thread);
  367. /* enable interrupt */
  368. rt_hw_interrupt_enable(level);
  369. /* do a schedule */
  370. rt_schedule();
  371. return;
  372. }
  373. /* enable interrupt */
  374. rt_hw_interrupt_enable(level);
  375. }
  376. RTM_EXPORT(rt_mp_free);
  377. /**@}*/
  378. #endif /* RT_USING_MEMPOOL */