2009-07-03 06:48:23 +08:00
|
|
|
/*
|
2021-03-08 11:25:38 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2013-06-24 17:06:09 +08:00
|
|
|
*
|
2018-09-14 22:37:43 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2006-05-27 Bernard implement memory pool
|
2009-12-25 20:18:53 +08:00
|
|
|
* 2006-06-03 Bernard fix the thread timer init bug
|
2009-07-03 06:48:23 +08:00
|
|
|
* 2006-06-30 Bernard fix the allocate/free block bug
|
|
|
|
* 2006-08-04 Bernard add hook support
|
|
|
|
* 2006-08-10 Bernard fix interrupt bug in rt_mp_alloc
|
2010-07-13 15:36:37 +08:00
|
|
|
* 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca
|
2010-10-28 09:21:47 +08:00
|
|
|
* 2010-10-26 yi.qiu add module support in rt_mp_delete
|
2011-01-24 21:25:36 +08:00
|
|
|
* 2011-01-24 Bernard add object allocation check.
|
2012-03-23 15:26:38 +08:00
|
|
|
* 2012-03-22 Bernard fix align issue in rt_mp_init and rt_mp_create.
|
2022-01-08 07:35:44 +08:00
|
|
|
* 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to mempool.c
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
|
|
|
#ifdef RT_USING_MEMPOOL
|
|
|
|
|
2022-01-08 07:35:44 +08:00
|
|
|
#ifndef __on_rt_mp_alloc_hook
|
|
|
|
#define __on_rt_mp_alloc_hook(mp, block) __ON_HOOK_ARGS(rt_mp_alloc_hook, (mp, block))
|
|
|
|
#endif
|
|
|
|
#ifndef __on_rt_mp_free_hook
|
|
|
|
#define __on_rt_mp_free_hook(mp, block) __ON_HOOK_ARGS(rt_mp_free_hook, (mp, block))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
|
2011-09-21 11:56:42 +08:00
|
|
|
static void (*rt_mp_alloc_hook)(struct rt_mempool *mp, void *block);
|
|
|
|
static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup Hook
|
|
|
|
*/
|
2012-03-17 14:43:49 +08:00
|
|
|
|
2016-08-19 10:12:39 +08:00
|
|
|
/**@{*/
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will set a hook function, which will be invoked when a memory
|
2021-09-11 16:40:56 +08:00
|
|
|
* block is allocated from the memory pool.
|
2009-12-25 20:18:53 +08:00
|
|
|
*
|
2009-07-03 06:48:23 +08:00
|
|
|
* @param hook the hook function
|
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 17:10:39 +08:00
|
|
|
rt_mp_alloc_hook = hook;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will set a hook function, which will be invoked when a memory
|
2021-09-11 16:40:56 +08:00
|
|
|
* block is released to the memory pool.
|
2009-12-25 20:18:53 +08:00
|
|
|
*
|
2009-07-03 06:48:23 +08:00
|
|
|
* @param hook the hook function
|
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 17:10:39 +08:00
|
|
|
rt_mp_free_hook = hook;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 10:12:39 +08:00
|
|
|
/**@}*/
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_HOOK */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup MM
|
|
|
|
*/
|
|
|
|
|
2016-08-19 10:12:39 +08:00
|
|
|
/**@{*/
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
2021-09-11 16:40:56 +08:00
|
|
|
* @brief This function will initialize a memory pool object, normally which is used
|
|
|
|
* for static object.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param mp is the memory pool object.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param name is the name of the memory pool.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param start is the start address of the memory pool.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param size is the total size of the memory pool.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param block_size is the size for each block..
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2010-11-29 08:04:55 +08:00
|
|
|
* @return RT_EOK
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2012-12-25 17:10:39 +08:00
|
|
|
rt_err_t rt_mp_init(struct rt_mempool *mp,
|
|
|
|
const char *name,
|
|
|
|
void *start,
|
|
|
|
rt_size_t size,
|
|
|
|
rt_size_t block_size)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 17:10:39 +08:00
|
|
|
rt_uint8_t *block_ptr;
|
2018-04-09 11:57:44 +08:00
|
|
|
register rt_size_t offset;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* parameter check */
|
|
|
|
RT_ASSERT(mp != RT_NULL);
|
2019-07-01 12:55:10 +08:00
|
|
|
RT_ASSERT(name != RT_NULL);
|
|
|
|
RT_ASSERT(start != RT_NULL);
|
|
|
|
RT_ASSERT(size > 0 && block_size > 0);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* initialize object */
|
|
|
|
rt_object_init(&(mp->parent), RT_Object_Class_MemPool, name);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* initialize memory pool */
|
|
|
|
mp->start_address = start;
|
|
|
|
mp->size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* align the block size */
|
|
|
|
block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
|
|
|
|
mp->block_size = block_size;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* align to align size byte */
|
|
|
|
mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t *));
|
|
|
|
mp->block_free_count = mp->block_total_count;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* initialize suspended thread list */
|
|
|
|
rt_list_init(&(mp->suspend_thread));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* initialize free block list */
|
|
|
|
block_ptr = (rt_uint8_t *)mp->start_address;
|
|
|
|
for (offset = 0; offset < mp->block_total_count; offset ++)
|
|
|
|
{
|
|
|
|
*(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *))) =
|
|
|
|
(rt_uint8_t *)(block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *)));
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
*(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) =
|
|
|
|
RT_NULL;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
mp->block_list = block_ptr;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
return RT_EOK;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_mp_init);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2010-11-29 08:04:55 +08:00
|
|
|
/**
|
2021-09-11 16:40:56 +08:00
|
|
|
* @brief This function will detach a memory pool from system object management.
|
2010-11-29 08:04:55 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param mp is the memory pool object.
|
2010-11-29 08:04:55 +08:00
|
|
|
*
|
|
|
|
* @return RT_EOK
|
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
rt_err_t rt_mp_detach(struct rt_mempool *mp)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 17:10:39 +08:00
|
|
|
struct rt_thread *thread;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* parameter check */
|
|
|
|
RT_ASSERT(mp != RT_NULL);
|
2018-07-11 11:47:15 +08:00
|
|
|
RT_ASSERT(rt_object_get_type(&mp->parent) == RT_Object_Class_MemPool);
|
|
|
|
RT_ASSERT(rt_object_is_systemobject(&mp->parent));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* wake up all suspended threads */
|
|
|
|
while (!rt_list_isempty(&(mp->suspend_thread)))
|
|
|
|
{
|
|
|
|
/* disable interrupt */
|
2020-07-23 19:35:31 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* get next suspend thread */
|
|
|
|
thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
|
|
|
|
/* set error code to RT_ERROR */
|
|
|
|
thread->error = -RT_ERROR;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/*
|
|
|
|
* resume thread
|
|
|
|
* In rt_thread_resume function, it will remove current thread from
|
|
|
|
* suspend list
|
|
|
|
*/
|
|
|
|
rt_thread_resume(thread);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* enable interrupt */
|
2020-07-23 19:35:31 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2012-12-25 17:10:39 +08:00
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* detach object */
|
|
|
|
rt_object_detach(&(mp->parent));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
return RT_EOK;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_mp_detach);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will create a mempool object and allocate the memory pool from
|
|
|
|
* heap.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param name is the name of memory pool.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param block_count is the count of blocks in memory pool.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param block_size is the size for each block.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* @return the created mempool object
|
|
|
|
*/
|
2012-12-25 17:10:39 +08:00
|
|
|
rt_mp_t rt_mp_create(const char *name,
|
|
|
|
rt_size_t block_count,
|
|
|
|
rt_size_t block_size)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 17:10:39 +08:00
|
|
|
rt_uint8_t *block_ptr;
|
|
|
|
struct rt_mempool *mp;
|
2018-04-09 11:57:44 +08:00
|
|
|
register rt_size_t offset;
|
2012-12-25 17:10:39 +08:00
|
|
|
|
|
|
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
|
|
|
|
2019-07-01 12:55:10 +08:00
|
|
|
/* parameter check */
|
|
|
|
RT_ASSERT(name != RT_NULL);
|
|
|
|
RT_ASSERT(block_count > 0 && block_size > 0);
|
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* allocate object */
|
|
|
|
mp = (struct rt_mempool *)rt_object_allocate(RT_Object_Class_MemPool, name);
|
|
|
|
/* allocate object failed */
|
|
|
|
if (mp == RT_NULL)
|
|
|
|
return RT_NULL;
|
|
|
|
|
|
|
|
/* initialize memory pool */
|
|
|
|
block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
|
|
|
|
mp->block_size = block_size;
|
|
|
|
mp->size = (block_size + sizeof(rt_uint8_t *)) * block_count;
|
|
|
|
|
|
|
|
/* allocate memory */
|
|
|
|
mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t *)) *
|
|
|
|
block_count);
|
|
|
|
if (mp->start_address == RT_NULL)
|
|
|
|
{
|
|
|
|
/* no memory, delete memory pool object */
|
|
|
|
rt_object_delete(&(mp->parent));
|
|
|
|
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp->block_total_count = block_count;
|
|
|
|
mp->block_free_count = mp->block_total_count;
|
|
|
|
|
|
|
|
/* initialize suspended thread list */
|
|
|
|
rt_list_init(&(mp->suspend_thread));
|
|
|
|
|
|
|
|
/* initialize free block list */
|
|
|
|
block_ptr = (rt_uint8_t *)mp->start_address;
|
|
|
|
for (offset = 0; offset < mp->block_total_count; offset ++)
|
|
|
|
{
|
|
|
|
*(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *)))
|
|
|
|
= block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *));
|
|
|
|
}
|
|
|
|
|
|
|
|
*(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *)))
|
|
|
|
= RT_NULL;
|
|
|
|
|
|
|
|
mp->block_list = block_ptr;
|
|
|
|
|
|
|
|
return mp;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_mp_create);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will delete a memory pool and release the object memory.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param mp is the memory pool object.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2010-11-29 08:04:55 +08:00
|
|
|
* @return RT_EOK
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
rt_err_t rt_mp_delete(rt_mp_t mp)
|
|
|
|
{
|
2012-12-25 17:10:39 +08:00
|
|
|
struct rt_thread *thread;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
2011-06-12 18:01:48 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* parameter check */
|
|
|
|
RT_ASSERT(mp != RT_NULL);
|
2018-07-11 11:47:15 +08:00
|
|
|
RT_ASSERT(rt_object_get_type(&mp->parent) == RT_Object_Class_MemPool);
|
|
|
|
RT_ASSERT(rt_object_is_systemobject(&mp->parent) == RT_FALSE);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* wake up all suspended threads */
|
|
|
|
while (!rt_list_isempty(&(mp->suspend_thread)))
|
|
|
|
{
|
|
|
|
/* disable interrupt */
|
2020-07-23 19:35:31 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* get next suspend thread */
|
|
|
|
thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
|
|
|
|
/* set error code to RT_ERROR */
|
|
|
|
thread->error = -RT_ERROR;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/*
|
|
|
|
* resume thread
|
|
|
|
* In rt_thread_resume function, it will remove current thread from
|
|
|
|
* suspend list
|
|
|
|
*/
|
|
|
|
rt_thread_resume(thread);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* enable interrupt */
|
2020-07-23 19:35:31 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2012-12-25 17:10:39 +08:00
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-06-10 18:46:11 +08:00
|
|
|
/* release allocated room */
|
|
|
|
rt_free(mp->start_address);
|
2009-12-25 20:18:53 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* detach object */
|
|
|
|
rt_object_delete(&(mp->parent));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
return RT_EOK;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_mp_delete);
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_HEAP */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will allocate a block from memory pool.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param mp is the memory pool object.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param time is the maximum waiting time for allocating memory.
|
|
|
|
* - 0 for not waiting, allocating memory immediately.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-10 15:55:08 +08:00
|
|
|
* @return the allocated memory block or RT_NULL on allocated failed.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 17:10:39 +08:00
|
|
|
rt_uint8_t *block_ptr;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2012-12-25 17:10:39 +08:00
|
|
|
struct rt_thread *thread;
|
2014-08-20 10:16:04 +08:00
|
|
|
rt_uint32_t before_sleep = 0;
|
|
|
|
|
2019-07-01 12:55:10 +08:00
|
|
|
/* parameter check */
|
|
|
|
RT_ASSERT(mp != RT_NULL);
|
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
/* get current thread */
|
|
|
|
thread = rt_thread_self();
|
2012-12-25 17:10:39 +08:00
|
|
|
|
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
while (mp->block_free_count == 0)
|
2012-12-25 17:10:39 +08:00
|
|
|
{
|
|
|
|
/* memory block is unavailable. */
|
|
|
|
if (time == 0)
|
|
|
|
{
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
rt_set_errno(-RT_ETIMEOUT);
|
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
2012-12-25 17:10:39 +08:00
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
thread->error = RT_EOK;
|
2014-04-16 10:47:30 +08:00
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
/* need suspend thread */
|
|
|
|
rt_thread_suspend(thread);
|
|
|
|
rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
|
2012-12-25 17:10:39 +08:00
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
if (time > 0)
|
|
|
|
{
|
|
|
|
/* get the start tick of timer */
|
|
|
|
before_sleep = rt_tick_get();
|
|
|
|
|
|
|
|
/* init thread timer and start it */
|
|
|
|
rt_timer_control(&(thread->thread_timer),
|
|
|
|
RT_TIMER_CTRL_SET_TIME,
|
|
|
|
&time);
|
|
|
|
rt_timer_start(&(thread->thread_timer));
|
|
|
|
}
|
2012-12-25 17:10:39 +08:00
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
2012-12-25 17:10:39 +08:00
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
/* do a schedule */
|
|
|
|
rt_schedule();
|
2012-12-25 17:10:39 +08:00
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
if (thread->error != RT_EOK)
|
|
|
|
return RT_NULL;
|
|
|
|
|
|
|
|
if (time > 0)
|
|
|
|
{
|
|
|
|
time -= rt_tick_get() - before_sleep;
|
|
|
|
if (time < 0)
|
|
|
|
time = 0;
|
|
|
|
}
|
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
}
|
2012-12-25 17:10:39 +08:00
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
/* memory block is available. decrease the free block counter */
|
|
|
|
mp->block_free_count--;
|
2012-12-25 17:10:39 +08:00
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
/* get block from block list */
|
|
|
|
block_ptr = mp->block_list;
|
|
|
|
RT_ASSERT(block_ptr != RT_NULL);
|
2012-12-25 17:10:39 +08:00
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
/* Setup the next free node. */
|
|
|
|
mp->block_list = *(rt_uint8_t **)block_ptr;
|
2012-12-25 17:10:39 +08:00
|
|
|
|
2014-08-20 10:16:04 +08:00
|
|
|
/* point to memory pool */
|
|
|
|
*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
|
2012-12-25 17:10:39 +08:00
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook,
|
|
|
|
(mp, (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *))));
|
|
|
|
|
|
|
|
return (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *));
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_mp_alloc);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
2021-09-11 16:40:56 +08:00
|
|
|
* @brief This function will release a memory block.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param block the address of memory block to be released.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_mp_free(void *block)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 17:10:39 +08:00
|
|
|
rt_uint8_t **block_ptr;
|
|
|
|
struct rt_mempool *mp;
|
|
|
|
struct rt_thread *thread;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2019-07-01 12:55:10 +08:00
|
|
|
/* parameter check */
|
|
|
|
if (block == RT_NULL) return;
|
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* get the control block of pool which the block belongs to */
|
|
|
|
block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *));
|
|
|
|
mp = (struct rt_mempool *)*block_ptr;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* increase the free block count */
|
|
|
|
mp->block_free_count ++;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* link the block into the block list */
|
|
|
|
*block_ptr = mp->block_list;
|
|
|
|
mp->block_list = (rt_uint8_t *)block_ptr;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2019-09-16 16:55:40 +08:00
|
|
|
if (!rt_list_isempty(&(mp->suspend_thread)))
|
2012-12-25 17:10:39 +08:00
|
|
|
{
|
|
|
|
/* get the suspended thread */
|
|
|
|
thread = rt_list_entry(mp->suspend_thread.next,
|
|
|
|
struct rt_thread,
|
|
|
|
tlist);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* set error */
|
|
|
|
thread->error = RT_EOK;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* resume thread */
|
|
|
|
rt_thread_resume(thread);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* do a schedule */
|
|
|
|
rt_schedule();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 17:10:39 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_mp_free);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2016-08-19 10:12:39 +08:00
|
|
|
/**@}*/
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_MEMPOOL */
|