mempool.c 11 KB

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