mempool.c 12 KB

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