mempool.c 10 KB

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