* @brief This function will release a memory block. * * @param block the address of memory block to be released. */
| 371 | * @param block the address of memory block to be released. |
| 372 | */ |
| 373 | void rt_mp_free(void *block) |
| 374 | { |
| 375 | rt_uint8_t **block_ptr; |
| 376 | struct rt_mempool *mp; |
| 377 | rt_base_t level; |
| 378 | |
| 379 | /* parameter check */ |
| 380 | if (block == RT_NULL) return; |
| 381 | |
| 382 | /* get the control block of pool which the block belongs to */ |
| 383 | block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *)); |
| 384 | mp = (struct rt_mempool *)*block_ptr; |
| 385 | |
| 386 | RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block)); |
| 387 | |
| 388 | level = rt_spin_lock_irqsave(&(mp->spinlock)); |
| 389 | |
| 390 | /* increase the free block count */ |
| 391 | mp->block_free_count ++; |
| 392 | |
| 393 | /* link the block into the block list */ |
| 394 | *block_ptr = mp->block_list; |
| 395 | mp->block_list = (rt_uint8_t *)block_ptr; |
| 396 | |
| 397 | if (rt_susp_list_dequeue(&mp->suspend_thread, RT_EOK)) |
| 398 | { |
| 399 | rt_spin_unlock_irqrestore(&(mp->spinlock), level); |
| 400 | |
| 401 | /* do a schedule */ |
| 402 | rt_schedule(); |
| 403 | |
| 404 | return; |
| 405 | } |
| 406 | rt_spin_unlock_irqrestore(&(mp->spinlock), level); |
| 407 | } |
| 408 | RTM_EXPORT(rt_mp_free); |
| 409 | |
| 410 | /**@}*/ |