mempool.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * File : mempool.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-05-27 Bernard implement memory pool
  13. * 2006-06-03 Bernard fix the thread timer init bug
  14. * 2006-06-30 Bernard fix the allocate/free block bug
  15. * 2006-08-04 Bernard add hook support
  16. * 2006-08-10 Bernard fix interrupt bug in rt_mp_alloc
  17. * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca
  18. * 2010-10-26 yi.qiu add module support in rt_mp_delete
  19. * 2011-01-24 Bernard add object allocation check.
  20. * 2012-03-22 Bernard fix align issue in rt_mp_init and rt_mp_create.
  21. */
  22. #include <rthw.h>
  23. #include <rtthread.h>
  24. #ifdef RT_USING_MEMPOOL
  25. #ifdef RT_USING_HOOK
  26. static void (*rt_mp_alloc_hook)(struct rt_mempool *mp, void *block);
  27. static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block);
  28. /**
  29. * @addtogroup Hook
  30. */
  31. /*@{*/
  32. /**
  33. * This function will set a hook function, which will be invoked when a memory
  34. * block is allocated from memory pool.
  35. *
  36. * @param hook the hook function
  37. */
  38. void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block))
  39. {
  40. rt_mp_alloc_hook = hook;
  41. }
  42. /**
  43. * This function will set a hook function, which will be invoked when a memory
  44. * block is released to memory pool.
  45. *
  46. * @param hook the hook function
  47. */
  48. void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block))
  49. {
  50. rt_mp_free_hook = hook;
  51. }
  52. /*@}*/
  53. #endif
  54. /**
  55. * @addtogroup MM
  56. */
  57. /*@{*/
  58. /**
  59. * This function will initialize a memory pool object, normally which is used for
  60. * static object.
  61. *
  62. * @param mp the memory pool object
  63. * @param name the name of memory pool
  64. * @param start the star address of memory pool
  65. * @param size the total size of memory pool
  66. * @param block_size the size for each block
  67. *
  68. * @return RT_EOK
  69. */
  70. rt_err_t rt_mp_init(struct rt_mempool *mp, const char *name, void *start, rt_size_t size, rt_size_t block_size)
  71. {
  72. rt_uint8_t *block_ptr;
  73. register rt_base_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. mp->suspend_thread_count = 0;
  90. /* initialize free block list */
  91. block_ptr = (rt_uint8_t *)mp->start_address;
  92. for (offset = 0; offset < mp->block_total_count; offset ++)
  93. {
  94. *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *)))
  95. = (rt_uint8_t *)(block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *)));
  96. }
  97. *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) = 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. /* wake up all suspended threads */
  116. while (!rt_list_isempty(&(mp->suspend_thread)))
  117. {
  118. /* disable interrupt */
  119. temp = rt_hw_interrupt_disable();
  120. /* get next suspend thread */
  121. thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
  122. /* set error code to RT_ERROR */
  123. thread->error = -RT_ERROR;
  124. /*
  125. * resume thread
  126. * In rt_thread_resume function, it will remove current thread from suspend
  127. * list
  128. */
  129. rt_thread_resume(thread);
  130. /* decrease suspended thread count */
  131. mp->suspend_thread_count --;
  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 heap.
  143. *
  144. * @param name the name of memory pool
  145. * @param block_count the count of blocks in memory pool
  146. * @param block_size the size for each block
  147. *
  148. * @return the created mempool object
  149. */
  150. rt_mp_t rt_mp_create(const char *name, rt_size_t block_count, rt_size_t block_size)
  151. {
  152. rt_uint8_t *block_ptr;
  153. struct rt_mempool *mp;
  154. register rt_base_t offset;
  155. RT_DEBUG_NOT_IN_INTERRUPT;
  156. /* allocate object */
  157. mp = (struct rt_mempool *)rt_object_allocate(RT_Object_Class_MemPool, name);
  158. if (mp == RT_NULL)
  159. return RT_NULL; /* allocate object failed */
  160. /* initialize memory pool */
  161. block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
  162. mp->block_size = block_size;
  163. mp->size = (block_size + sizeof(rt_uint8_t *)) * block_count;
  164. /* allocate memory */
  165. mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t *)) * block_count);
  166. if (mp->start_address == RT_NULL)
  167. {
  168. /* no memory, delete memory pool object */
  169. rt_object_delete(&(mp->parent));
  170. return RT_NULL;
  171. }
  172. mp->block_total_count = block_count;
  173. mp->block_free_count = mp->block_total_count;
  174. /* initialize suspended thread list */
  175. rt_list_init(&(mp->suspend_thread));
  176. mp->suspend_thread_count = 0;
  177. /* initialize free block list */
  178. block_ptr = (rt_uint8_t *)mp->start_address;
  179. for (offset = 0; offset < mp->block_total_count; offset ++)
  180. {
  181. *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *)))
  182. = block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *));
  183. }
  184. *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) = RT_NULL;
  185. mp->block_list = block_ptr;
  186. return mp;
  187. }
  188. RTM_EXPORT(rt_mp_create);
  189. /**
  190. * This function will delete a memory pool and release the object memory.
  191. *
  192. * @param mp the memory pool object
  193. *
  194. * @return RT_EOK
  195. */
  196. rt_err_t rt_mp_delete(rt_mp_t mp)
  197. {
  198. struct rt_thread *thread;
  199. register rt_ubase_t temp;
  200. RT_DEBUG_NOT_IN_INTERRUPT;
  201. /* parameter check */
  202. RT_ASSERT(mp != RT_NULL);
  203. /* wake up all suspended threads */
  204. while (!rt_list_isempty(&(mp->suspend_thread)))
  205. {
  206. /* disable interrupt */
  207. temp = rt_hw_interrupt_disable();
  208. /* get next suspend thread */
  209. thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
  210. /* set error code to RT_ERROR */
  211. thread->error = -RT_ERROR;
  212. /*
  213. * resume thread
  214. * In rt_thread_resume function, it will remove current thread from suspend
  215. * list
  216. */
  217. rt_thread_resume(thread);
  218. /* decrease suspended thread count */
  219. mp->suspend_thread_count --;
  220. /* enable interrupt */
  221. rt_hw_interrupt_enable(temp);
  222. }
  223. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  224. /* the mp object belongs to an application module */
  225. if (mp->parent.flag & RT_OBJECT_FLAG_MODULE)
  226. rt_module_free(mp->parent.module_id, mp->start_address);
  227. else
  228. #endif
  229. /* release allocated room */
  230. rt_free(mp->start_address);
  231. /* detach object */
  232. rt_object_delete(&(mp->parent));
  233. return RT_EOK;
  234. }
  235. RTM_EXPORT(rt_mp_delete);
  236. #endif
  237. /**
  238. * This function will allocate a block from memory pool
  239. *
  240. * @param mp the memory pool object
  241. * @param time the waiting time
  242. *
  243. * @return the allocated memory block or RT_NULL on allocated failed
  244. */
  245. void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
  246. {
  247. rt_uint8_t *block_ptr;
  248. register rt_base_t level;
  249. struct rt_thread *thread;
  250. /* disable interrupt */
  251. level = rt_hw_interrupt_disable();
  252. if (mp->block_free_count)
  253. {
  254. /* memory block is available. decrease the free block counter */
  255. mp->block_free_count --;
  256. /* get block from block list */
  257. block_ptr = mp->block_list;
  258. mp->block_list = *(rt_uint8_t **)block_ptr;
  259. /* point to memory pool */
  260. *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
  261. }
  262. else
  263. {
  264. /* memory block is unavailable. */
  265. if (time == 0)
  266. {
  267. /* enable interrupt */
  268. rt_hw_interrupt_enable(level);
  269. return RT_NULL;
  270. }
  271. else
  272. {
  273. RT_DEBUG_NOT_IN_INTERRUPT;
  274. /* get current thread */
  275. thread = rt_thread_self();
  276. /* need suspend thread */
  277. rt_thread_suspend(thread);
  278. rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
  279. mp->suspend_thread_count ++;
  280. if (time > 0)
  281. {
  282. /* init thread timer and start it */
  283. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
  284. rt_timer_start(&(thread->thread_timer));
  285. }
  286. /* enable interrupt */
  287. rt_hw_interrupt_enable(level);
  288. /* do a schedule */
  289. rt_schedule();
  290. if (thread->error != RT_EOK)
  291. return RT_NULL;
  292. /* disable interrupt */
  293. level = rt_hw_interrupt_disable();
  294. /* decrease free block */
  295. mp->block_free_count --;
  296. /* get block from block list */
  297. block_ptr = mp->block_list;
  298. mp->block_list = *(rt_uint8_t **)block_ptr;
  299. /* point to memory pool */
  300. *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
  301. }
  302. }
  303. /* enable interrupt */
  304. rt_hw_interrupt_enable(level);
  305. RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook, (mp, (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *))));
  306. return (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *));
  307. }
  308. RTM_EXPORT(rt_mp_alloc);
  309. /**
  310. * This function will release a memory block
  311. *
  312. * @param block the address of memory block to be released
  313. */
  314. void rt_mp_free(void *block)
  315. {
  316. rt_uint8_t **block_ptr;
  317. struct rt_mempool *mp;
  318. struct rt_thread *thread;
  319. register rt_base_t level;
  320. /* get the control block of pool which the block belongs to */
  321. block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *));
  322. mp = (struct rt_mempool *)*block_ptr;
  323. RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block));
  324. /* disable interrupt */
  325. level = rt_hw_interrupt_disable();
  326. /* increase the free block count */
  327. mp->block_free_count ++;
  328. /* link the block into the block list */
  329. *block_ptr = mp->block_list;
  330. mp->block_list = (rt_uint8_t *)block_ptr;
  331. if (mp->suspend_thread_count > 0)
  332. {
  333. /* get the suspended thread */
  334. thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
  335. /* set error */
  336. thread->error = RT_EOK;
  337. /* resume thread */
  338. rt_thread_resume(thread);
  339. /* decrease suspended thread count */
  340. mp->suspend_thread_count --;
  341. /* enable interrupt */
  342. rt_hw_interrupt_enable(level);
  343. /* do a schedule */
  344. rt_schedule();
  345. return;
  346. }
  347. /* enable interrupt */
  348. rt_hw_interrupt_enable(level);
  349. }
  350. RTM_EXPORT(rt_mp_free);
  351. /*@}*/
  352. #endif