mempool.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * File : mempool.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, 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. */
  21. #include <rthw.h>
  22. #include <rtthread.h>
  23. #include "kservice.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. */
  71. rt_err_t rt_mp_init(struct rt_mempool* mp, const char* name, void *start, rt_size_t size, rt_size_t block_size)
  72. {
  73. rt_uint8_t *block_ptr;
  74. register rt_base_t offset;
  75. /* parameter check */
  76. RT_ASSERT(mp != RT_NULL);
  77. /* init object */
  78. rt_object_init(&(mp->parent), RT_Object_Class_MemPool, name);
  79. /* init memory pool */
  80. mp->start_address = start;
  81. mp->size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  82. mp->block_size = block_size;
  83. /* align to align size byte */
  84. mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t*));
  85. mp->block_free_count = mp->block_total_count;
  86. /* init suspended thread list */
  87. rt_list_init(&(mp->suspend_thread));
  88. mp->suspend_thread_count = 0;
  89. /* init 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*))) = RT_NULL;
  97. mp->block_list = block_ptr;
  98. return RT_EOK;
  99. }
  100. /**
  101. * This function will detach a memory pool from system object management.
  102. *
  103. * @param mp the memory pool object
  104. *
  105. * @return RT_EOK
  106. */
  107. rt_err_t rt_mp_detach(struct rt_mempool* mp)
  108. {
  109. struct rt_thread* thread;
  110. register rt_ubase_t temp;
  111. /* parameter check */
  112. RT_ASSERT(mp != RT_NULL);
  113. /* wakeup all suspended threads */
  114. while (!rt_list_isempty(&(mp->suspend_thread)))
  115. {
  116. /* disable interrupt */
  117. temp = rt_hw_interrupt_disable();
  118. /* get next suspend thread */
  119. thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
  120. /* set error code to RT_ERROR */
  121. thread->error = -RT_ERROR;
  122. /*
  123. * resume thread
  124. * In rt_thread_resume function, it will remove current thread from suspend
  125. * list
  126. */
  127. rt_thread_resume(thread);
  128. /* decrease suspended thread count */
  129. mp->suspend_thread_count --;
  130. /* enable interrupt */
  131. rt_hw_interrupt_enable(temp);
  132. }
  133. /* detach object */
  134. rt_object_detach(&(mp->parent));
  135. return RT_EOK;
  136. }
  137. #ifdef RT_USING_HEAP
  138. /**
  139. * This function will create a mempool object and allocate the memory pool from heap.
  140. *
  141. * @param name the name of memory pool
  142. * @param block_count the count of blocks in memory pool
  143. * @param block_size the size for each block
  144. *
  145. * @return the created mempool object
  146. *
  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) return RT_NULL; /* allocate object failed */
  157. /* init memory pool */
  158. mp->block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
  159. mp->size = (block_size + sizeof(rt_uint8_t*))* block_count;
  160. /* allocate memory */
  161. mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t*))* block_count);
  162. if (mp->start_address == RT_NULL)
  163. {
  164. /* no memory, delete memory pool object */
  165. rt_object_delete(&(mp->parent));
  166. return RT_NULL;
  167. }
  168. mp->block_total_count = block_count;
  169. mp->block_free_count = mp->block_total_count;
  170. /* init suspended thread list */
  171. rt_list_init(&(mp->suspend_thread));
  172. mp->suspend_thread_count = 0;
  173. /* init free block list */
  174. block_ptr = (rt_uint8_t*) mp->start_address;
  175. for (offset = 0; offset < mp->block_total_count; offset ++)
  176. {
  177. *(rt_uint8_t**)(block_ptr + offset * (block_size + sizeof(rt_uint8_t*)))
  178. = block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t*));
  179. }
  180. *(rt_uint8_t**)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t*))) = RT_NULL;
  181. mp->block_list = block_ptr;
  182. return mp;
  183. }
  184. /**
  185. * This function will delete a memory pool and release the object memory.
  186. *
  187. * @param mp the memory pool object
  188. *
  189. * @return RT_EOK
  190. *
  191. */
  192. rt_err_t rt_mp_delete(rt_mp_t mp)
  193. {
  194. struct rt_thread* thread;
  195. register rt_ubase_t temp;
  196. RT_DEBUG_NOT_IN_INTERRUPT;
  197. /* parameter check */
  198. RT_ASSERT(mp != RT_NULL);
  199. /* wakeup all suspended threads */
  200. while (!rt_list_isempty(&(mp->suspend_thread)))
  201. {
  202. /* disable interrupt */
  203. temp = rt_hw_interrupt_disable();
  204. /* get next suspend thread */
  205. thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
  206. /* set error code to RT_ERROR */
  207. thread->error = -RT_ERROR;
  208. /*
  209. * resume thread
  210. * In rt_thread_resume function, it will remove current thread from suspend
  211. * list
  212. */
  213. rt_thread_resume(thread);
  214. /* decrease suspended thread count */
  215. mp->suspend_thread_count --;
  216. /* enable interrupt */
  217. rt_hw_interrupt_enable(temp);
  218. }
  219. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  220. /* the mp object belongs to an application module */
  221. if(mp->parent.flag & RT_OBJECT_FLAG_MODULE)
  222. rt_module_free(mp->parent.module_id, mp->start_address);
  223. else
  224. #endif
  225. /* release allocated room */
  226. rt_free(mp->start_address);
  227. /* detach object */
  228. rt_object_delete(&(mp->parent));
  229. return RT_EOK;
  230. }
  231. #endif
  232. /**
  233. * This function will allocate a block from memory pool
  234. *
  235. * @param mp the memory pool object
  236. * @param time the waiting time
  237. *
  238. * @return the allocated memory block or RT_NULL on allocated failed
  239. *
  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) return RT_NULL;
  287. /* disable interrupt */
  288. level = rt_hw_interrupt_disable();
  289. /* decrease free block */
  290. mp->block_free_count --;
  291. /* get block from block list */
  292. block_ptr = mp->block_list;
  293. mp->block_list = *(rt_uint8_t**)block_ptr;
  294. /* point to memory pool */
  295. *(rt_uint8_t**)block_ptr = (rt_uint8_t*)mp;
  296. }
  297. }
  298. /* enable interrupt */
  299. rt_hw_interrupt_enable(level);
  300. RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook, (mp, (rt_uint8_t*)(block_ptr + sizeof(rt_uint8_t*))));
  301. return (rt_uint8_t*)(block_ptr + sizeof(rt_uint8_t*));
  302. }
  303. /**
  304. * This function will release a memory block
  305. *
  306. * @param block the address of memory block to be released
  307. *
  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. #endif
  346. /*@}*/