mempool.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-05-27 Bernard implement memory pool
  23. * 2006-06-03 Bernard fix the thread timer init bug
  24. * 2006-06-30 Bernard fix the allocate/free block bug
  25. * 2006-08-04 Bernard add hook support
  26. * 2006-08-10 Bernard fix interrupt bug in rt_mp_alloc
  27. * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca
  28. * 2010-10-26 yi.qiu add module support in rt_mp_delete
  29. * 2011-01-24 Bernard add object allocation check.
  30. * 2012-03-22 Bernard fix align issue in rt_mp_init and rt_mp_create.
  31. */
  32. #include <rthw.h>
  33. #include <rtthread.h>
  34. #ifdef RT_USING_MEMPOOL
  35. #ifdef RT_USING_HOOK
  36. static void (*rt_mp_alloc_hook)(struct rt_mempool *mp, void *block);
  37. static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block);
  38. /**
  39. * @addtogroup Hook
  40. */
  41. /*@{*/
  42. /**
  43. * This function will set a hook function, which will be invoked when a memory
  44. * block is allocated from memory pool.
  45. *
  46. * @param hook the hook function
  47. */
  48. void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block))
  49. {
  50. rt_mp_alloc_hook = hook;
  51. }
  52. /**
  53. * This function will set a hook function, which will be invoked when a memory
  54. * block is released to memory pool.
  55. *
  56. * @param hook the hook function
  57. */
  58. void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block))
  59. {
  60. rt_mp_free_hook = hook;
  61. }
  62. /*@}*/
  63. #endif
  64. /**
  65. * @addtogroup MM
  66. */
  67. /*@{*/
  68. /**
  69. * This function will initialize a memory pool object, normally which is used
  70. * for static object.
  71. *
  72. * @param mp the memory pool object
  73. * @param name the name of memory pool
  74. * @param start the star address of memory pool
  75. * @param size the total size of memory pool
  76. * @param block_size the size for each block
  77. *
  78. * @return RT_EOK
  79. */
  80. rt_err_t rt_mp_init(struct rt_mempool *mp,
  81. const char *name,
  82. void *start,
  83. rt_size_t size,
  84. rt_size_t block_size)
  85. {
  86. rt_uint8_t *block_ptr;
  87. register rt_base_t offset;
  88. /* parameter check */
  89. RT_ASSERT(mp != RT_NULL);
  90. /* initialize object */
  91. rt_object_init(&(mp->parent), RT_Object_Class_MemPool, name);
  92. /* initialize memory pool */
  93. mp->start_address = start;
  94. mp->size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  95. /* align the block size */
  96. block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
  97. mp->block_size = block_size;
  98. /* align to align size byte */
  99. mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t *));
  100. mp->block_free_count = mp->block_total_count;
  101. /* initialize suspended thread list */
  102. rt_list_init(&(mp->suspend_thread));
  103. mp->suspend_thread_count = 0;
  104. /* initialize free block list */
  105. block_ptr = (rt_uint8_t *)mp->start_address;
  106. for (offset = 0; offset < mp->block_total_count; offset ++)
  107. {
  108. *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *))) =
  109. (rt_uint8_t *)(block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *)));
  110. }
  111. *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) =
  112. RT_NULL;
  113. mp->block_list = block_ptr;
  114. return RT_EOK;
  115. }
  116. RTM_EXPORT(rt_mp_init);
  117. /**
  118. * This function will detach a memory pool from system object management.
  119. *
  120. * @param mp the memory pool object
  121. *
  122. * @return RT_EOK
  123. */
  124. rt_err_t rt_mp_detach(struct rt_mempool *mp)
  125. {
  126. struct rt_thread *thread;
  127. register rt_ubase_t temp;
  128. /* parameter check */
  129. RT_ASSERT(mp != RT_NULL);
  130. /* wake up all suspended threads */
  131. while (!rt_list_isempty(&(mp->suspend_thread)))
  132. {
  133. /* disable interrupt */
  134. temp = rt_hw_interrupt_disable();
  135. /* get next suspend thread */
  136. thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
  137. /* set error code to RT_ERROR */
  138. thread->error = -RT_ERROR;
  139. /*
  140. * resume thread
  141. * In rt_thread_resume function, it will remove current thread from
  142. * suspend list
  143. */
  144. rt_thread_resume(thread);
  145. /* decrease suspended thread count */
  146. mp->suspend_thread_count --;
  147. /* enable interrupt */
  148. rt_hw_interrupt_enable(temp);
  149. }
  150. /* detach object */
  151. rt_object_detach(&(mp->parent));
  152. return RT_EOK;
  153. }
  154. RTM_EXPORT(rt_mp_detach);
  155. #ifdef RT_USING_HEAP
  156. /**
  157. * This function will create a mempool object and allocate the memory pool from
  158. * heap.
  159. *
  160. * @param name the name of memory pool
  161. * @param block_count the count of blocks in memory pool
  162. * @param block_size the size for each block
  163. *
  164. * @return the created mempool object
  165. */
  166. rt_mp_t rt_mp_create(const char *name,
  167. rt_size_t block_count,
  168. rt_size_t block_size)
  169. {
  170. rt_uint8_t *block_ptr;
  171. struct rt_mempool *mp;
  172. register rt_base_t offset;
  173. RT_DEBUG_NOT_IN_INTERRUPT;
  174. /* allocate object */
  175. mp = (struct rt_mempool *)rt_object_allocate(RT_Object_Class_MemPool, name);
  176. /* allocate object failed */
  177. if (mp == RT_NULL)
  178. return RT_NULL;
  179. /* initialize memory pool */
  180. block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
  181. mp->block_size = block_size;
  182. mp->size = (block_size + sizeof(rt_uint8_t *)) * block_count;
  183. /* allocate memory */
  184. mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t *)) *
  185. block_count);
  186. if (mp->start_address == RT_NULL)
  187. {
  188. /* no memory, delete memory pool object */
  189. rt_object_delete(&(mp->parent));
  190. return RT_NULL;
  191. }
  192. mp->block_total_count = block_count;
  193. mp->block_free_count = mp->block_total_count;
  194. /* initialize suspended thread list */
  195. rt_list_init(&(mp->suspend_thread));
  196. mp->suspend_thread_count = 0;
  197. /* initialize free block list */
  198. block_ptr = (rt_uint8_t *)mp->start_address;
  199. for (offset = 0; offset < mp->block_total_count; offset ++)
  200. {
  201. *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *)))
  202. = block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *));
  203. }
  204. *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *)))
  205. = RT_NULL;
  206. mp->block_list = block_ptr;
  207. return mp;
  208. }
  209. RTM_EXPORT(rt_mp_create);
  210. /**
  211. * This function will delete a memory pool and release the object memory.
  212. *
  213. * @param mp the memory pool object
  214. *
  215. * @return RT_EOK
  216. */
  217. rt_err_t rt_mp_delete(rt_mp_t mp)
  218. {
  219. struct rt_thread *thread;
  220. register rt_ubase_t temp;
  221. RT_DEBUG_NOT_IN_INTERRUPT;
  222. /* parameter check */
  223. RT_ASSERT(mp != RT_NULL);
  224. /* wake up all suspended threads */
  225. while (!rt_list_isempty(&(mp->suspend_thread)))
  226. {
  227. /* disable interrupt */
  228. temp = rt_hw_interrupt_disable();
  229. /* get next suspend thread */
  230. thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
  231. /* set error code to RT_ERROR */
  232. thread->error = -RT_ERROR;
  233. /*
  234. * resume thread
  235. * In rt_thread_resume function, it will remove current thread from
  236. * suspend list
  237. */
  238. rt_thread_resume(thread);
  239. /* decrease suspended thread count */
  240. mp->suspend_thread_count --;
  241. /* enable interrupt */
  242. rt_hw_interrupt_enable(temp);
  243. }
  244. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  245. /* the mp object belongs to an application module */
  246. if (mp->parent.flag & RT_OBJECT_FLAG_MODULE)
  247. rt_module_free(mp->parent.module_id, mp->start_address);
  248. else
  249. #endif
  250. /* release allocated room */
  251. rt_free(mp->start_address);
  252. /* detach object */
  253. rt_object_delete(&(mp->parent));
  254. return RT_EOK;
  255. }
  256. RTM_EXPORT(rt_mp_delete);
  257. #endif
  258. /**
  259. * This function will allocate a block from memory pool
  260. *
  261. * @param mp the memory pool object
  262. * @param time the waiting time
  263. *
  264. * @return the allocated memory block or RT_NULL on allocated failed
  265. */
  266. void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
  267. {
  268. rt_uint8_t *block_ptr;
  269. register rt_base_t level;
  270. struct rt_thread *thread;
  271. rt_uint32_t before_sleep = 0;
  272. /* get current thread */
  273. thread = rt_thread_self();
  274. /* disable interrupt */
  275. level = rt_hw_interrupt_disable();
  276. while (mp->block_free_count == 0)
  277. {
  278. /* memory block is unavailable. */
  279. if (time == 0)
  280. {
  281. /* enable interrupt */
  282. rt_hw_interrupt_enable(level);
  283. rt_set_errno(-RT_ETIMEOUT);
  284. return RT_NULL;
  285. }
  286. RT_DEBUG_NOT_IN_INTERRUPT;
  287. thread->error = RT_EOK;
  288. /* need suspend thread */
  289. rt_thread_suspend(thread);
  290. rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
  291. mp->suspend_thread_count++;
  292. if (time > 0)
  293. {
  294. /* get the start tick of timer */
  295. before_sleep = rt_tick_get();
  296. /* init thread timer and start it */
  297. rt_timer_control(&(thread->thread_timer),
  298. RT_TIMER_CTRL_SET_TIME,
  299. &time);
  300. rt_timer_start(&(thread->thread_timer));
  301. }
  302. /* enable interrupt */
  303. rt_hw_interrupt_enable(level);
  304. /* do a schedule */
  305. rt_schedule();
  306. if (thread->error != RT_EOK)
  307. return RT_NULL;
  308. if (time > 0)
  309. {
  310. time -= rt_tick_get() - before_sleep;
  311. if (time < 0)
  312. time = 0;
  313. }
  314. /* disable interrupt */
  315. level = rt_hw_interrupt_disable();
  316. }
  317. /* memory block is available. decrease the free block counter */
  318. mp->block_free_count--;
  319. /* get block from block list */
  320. block_ptr = mp->block_list;
  321. RT_ASSERT(block_ptr != RT_NULL);
  322. /* Setup the next free node. */
  323. mp->block_list = *(rt_uint8_t **)block_ptr;
  324. /* point to memory pool */
  325. *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
  326. /* enable interrupt */
  327. rt_hw_interrupt_enable(level);
  328. RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook,
  329. (mp, (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *))));
  330. return (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *));
  331. }
  332. RTM_EXPORT(rt_mp_alloc);
  333. /**
  334. * This function will release a memory block
  335. *
  336. * @param block the address of memory block to be released
  337. */
  338. void rt_mp_free(void *block)
  339. {
  340. rt_uint8_t **block_ptr;
  341. struct rt_mempool *mp;
  342. struct rt_thread *thread;
  343. register rt_base_t level;
  344. /* get the control block of pool which the block belongs to */
  345. block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *));
  346. mp = (struct rt_mempool *)*block_ptr;
  347. RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block));
  348. /* disable interrupt */
  349. level = rt_hw_interrupt_disable();
  350. /* increase the free block count */
  351. mp->block_free_count ++;
  352. /* link the block into the block list */
  353. *block_ptr = mp->block_list;
  354. mp->block_list = (rt_uint8_t *)block_ptr;
  355. if (mp->suspend_thread_count > 0)
  356. {
  357. /* get the suspended thread */
  358. thread = rt_list_entry(mp->suspend_thread.next,
  359. struct rt_thread,
  360. tlist);
  361. /* set error */
  362. thread->error = RT_EOK;
  363. /* resume thread */
  364. rt_thread_resume(thread);
  365. /* decrease suspended thread count */
  366. mp->suspend_thread_count --;
  367. /* enable interrupt */
  368. rt_hw_interrupt_enable(level);
  369. /* do a schedule */
  370. rt_schedule();
  371. return;
  372. }
  373. /* enable interrupt */
  374. rt_hw_interrupt_enable(level);
  375. }
  376. RTM_EXPORT(rt_mp_free);
  377. /*@}*/
  378. #endif