提交 dec45642 编写于 作者: D dzzxzz@gmail.com

fixed coding style in src/mempool.c

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2525 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 dc5a1686
...@@ -43,7 +43,7 @@ static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block); ...@@ -43,7 +43,7 @@ static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block);
*/ */
void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block)) void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block))
{ {
rt_mp_alloc_hook = hook; rt_mp_alloc_hook = hook;
} }
/** /**
...@@ -54,7 +54,7 @@ void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block)) ...@@ -54,7 +54,7 @@ void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block))
*/ */
void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block)) void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block))
{ {
rt_mp_free_hook = hook; rt_mp_free_hook = hook;
} }
/*@}*/ /*@}*/
...@@ -67,8 +67,8 @@ void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block)) ...@@ -67,8 +67,8 @@ void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block))
/*@{*/ /*@{*/
/** /**
* This function will initialize a memory pool object, normally which is used for * This function will initialize a memory pool object, normally which is used
* static object. * for static object.
* *
* @param mp the memory pool object * @param mp the memory pool object
* @param name the name of memory pool * @param name the name of memory pool
...@@ -78,46 +78,51 @@ void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block)) ...@@ -78,46 +78,51 @@ void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block))
* *
* @return RT_EOK * @return RT_EOK
*/ */
rt_err_t rt_mp_init(struct rt_mempool *mp, const char *name, void *start, rt_size_t size, rt_size_t block_size) rt_err_t rt_mp_init(struct rt_mempool *mp,
const char *name,
void *start,
rt_size_t size,
rt_size_t block_size)
{ {
rt_uint8_t *block_ptr; rt_uint8_t *block_ptr;
register rt_base_t offset; register rt_base_t offset;
/* parameter check */ /* parameter check */
RT_ASSERT(mp != RT_NULL); RT_ASSERT(mp != RT_NULL);
/* initialize object */ /* initialize object */
rt_object_init(&(mp->parent), RT_Object_Class_MemPool, name); rt_object_init(&(mp->parent), RT_Object_Class_MemPool, name);
/* initialize memory pool */ /* initialize memory pool */
mp->start_address = start; mp->start_address = start;
mp->size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE); mp->size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
/* align the block size */ /* align the block size */
block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE); block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
mp->block_size = block_size; mp->block_size = block_size;
/* align to align size byte */ /* align to align size byte */
mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t *)); mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t *));
mp->block_free_count = mp->block_total_count; mp->block_free_count = mp->block_total_count;
/* initialize suspended thread list */ /* initialize suspended thread list */
rt_list_init(&(mp->suspend_thread)); rt_list_init(&(mp->suspend_thread));
mp->suspend_thread_count = 0; mp->suspend_thread_count = 0;
/* initialize free block list */ /* initialize free block list */
block_ptr = (rt_uint8_t *)mp->start_address; block_ptr = (rt_uint8_t *)mp->start_address;
for (offset = 0; offset < mp->block_total_count; offset ++) 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 * (block_size + sizeof(rt_uint8_t *))) =
= (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_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) = RT_NULL; *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) =
RT_NULL;
mp->block_list = block_ptr; mp->block_list = block_ptr;
return RT_EOK; return RT_EOK;
} }
RTM_EXPORT(rt_mp_init); RTM_EXPORT(rt_mp_init);
...@@ -130,47 +135,48 @@ RTM_EXPORT(rt_mp_init); ...@@ -130,47 +135,48 @@ RTM_EXPORT(rt_mp_init);
*/ */
rt_err_t rt_mp_detach(struct rt_mempool *mp) rt_err_t rt_mp_detach(struct rt_mempool *mp)
{ {
struct rt_thread *thread; struct rt_thread *thread;
register rt_ubase_t temp; register rt_ubase_t temp;
/* parameter check */ /* parameter check */
RT_ASSERT(mp != RT_NULL); RT_ASSERT(mp != RT_NULL);
/* wake up all suspended threads */ /* wake up all suspended threads */
while (!rt_list_isempty(&(mp->suspend_thread))) while (!rt_list_isempty(&(mp->suspend_thread)))
{ {
/* disable interrupt */ /* disable interrupt */
temp = rt_hw_interrupt_disable(); temp = rt_hw_interrupt_disable();
/* get next suspend thread */ /* get next suspend thread */
thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist); thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
/* set error code to RT_ERROR */ /* set error code to RT_ERROR */
thread->error = -RT_ERROR; thread->error = -RT_ERROR;
/* /*
* resume thread * resume thread
* In rt_thread_resume function, it will remove current thread from suspend * In rt_thread_resume function, it will remove current thread from
* list * suspend list
*/ */
rt_thread_resume(thread); rt_thread_resume(thread);
/* decrease suspended thread count */ /* decrease suspended thread count */
mp->suspend_thread_count --; mp->suspend_thread_count --;
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(temp); rt_hw_interrupt_enable(temp);
} }
/* detach object */ /* detach object */
rt_object_detach(&(mp->parent)); rt_object_detach(&(mp->parent));
return RT_EOK; return RT_EOK;
} }
RTM_EXPORT(rt_mp_detach); RTM_EXPORT(rt_mp_detach);
#ifdef RT_USING_HEAP #ifdef RT_USING_HEAP
/** /**
* This function will create a mempool object and allocate the memory pool from heap. * This function will create a mempool object and allocate the memory pool from
* heap.
* *
* @param name the name of memory pool * @param name the name of memory pool
* @param block_count the count of blocks in memory pool * @param block_count the count of blocks in memory pool
...@@ -178,54 +184,59 @@ RTM_EXPORT(rt_mp_detach); ...@@ -178,54 +184,59 @@ RTM_EXPORT(rt_mp_detach);
* *
* @return the created mempool object * @return the created mempool object
*/ */
rt_mp_t rt_mp_create(const char *name, rt_size_t block_count, rt_size_t block_size) rt_mp_t rt_mp_create(const char *name,
rt_size_t block_count,
rt_size_t block_size)
{ {
rt_uint8_t *block_ptr; rt_uint8_t *block_ptr;
struct rt_mempool *mp; struct rt_mempool *mp;
register rt_base_t offset; register rt_base_t offset;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
/* allocate object */ /* allocate object */
mp = (struct rt_mempool *)rt_object_allocate(RT_Object_Class_MemPool, name); mp = (struct rt_mempool *)rt_object_allocate(RT_Object_Class_MemPool, name);
if (mp == RT_NULL) /* allocate object failed */
return RT_NULL; /* allocate object failed */ if (mp == RT_NULL)
return RT_NULL;
/* initialize memory pool */
block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE); /* initialize memory pool */
mp->block_size = block_size; block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
mp->size = (block_size + sizeof(rt_uint8_t *)) * block_count; 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); /* allocate memory */
if (mp->start_address == RT_NULL) mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t *)) *
{ block_count);
/* no memory, delete memory pool object */ if (mp->start_address == RT_NULL)
rt_object_delete(&(mp->parent)); {
/* no memory, delete memory pool object */
return RT_NULL; rt_object_delete(&(mp->parent));
}
return RT_NULL;
mp->block_total_count = block_count; }
mp->block_free_count = mp->block_total_count;
mp->block_total_count = block_count;
/* initialize suspended thread list */ mp->block_free_count = mp->block_total_count;
rt_list_init(&(mp->suspend_thread));
mp->suspend_thread_count = 0; /* initialize suspended thread list */
rt_list_init(&(mp->suspend_thread));
/* initialize free block list */ mp->suspend_thread_count = 0;
block_ptr = (rt_uint8_t *)mp->start_address;
for (offset = 0; offset < mp->block_total_count; offset ++) /* initialize free block list */
{ block_ptr = (rt_uint8_t *)mp->start_address;
*(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *))) for (offset = 0; offset < mp->block_total_count; offset ++)
= block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *)); {
} *(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; *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *)))
= RT_NULL;
return mp;
mp->block_list = block_ptr;
return mp;
} }
RTM_EXPORT(rt_mp_create); RTM_EXPORT(rt_mp_create);
...@@ -238,53 +249,53 @@ RTM_EXPORT(rt_mp_create); ...@@ -238,53 +249,53 @@ RTM_EXPORT(rt_mp_create);
*/ */
rt_err_t rt_mp_delete(rt_mp_t mp) rt_err_t rt_mp_delete(rt_mp_t mp)
{ {
struct rt_thread *thread; struct rt_thread *thread;
register rt_ubase_t temp; register rt_ubase_t temp;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
/* parameter check */ /* parameter check */
RT_ASSERT(mp != RT_NULL); RT_ASSERT(mp != RT_NULL);
/* wake up all suspended threads */ /* wake up all suspended threads */
while (!rt_list_isempty(&(mp->suspend_thread))) while (!rt_list_isempty(&(mp->suspend_thread)))
{ {
/* disable interrupt */ /* disable interrupt */
temp = rt_hw_interrupt_disable(); temp = rt_hw_interrupt_disable();
/* get next suspend thread */ /* get next suspend thread */
thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist); thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
/* set error code to RT_ERROR */ /* set error code to RT_ERROR */
thread->error = -RT_ERROR; thread->error = -RT_ERROR;
/* /*
* resume thread * resume thread
* In rt_thread_resume function, it will remove current thread from suspend * In rt_thread_resume function, it will remove current thread from
* list * suspend list
*/ */
rt_thread_resume(thread); rt_thread_resume(thread);
/* decrease suspended thread count */ /* decrease suspended thread count */
mp->suspend_thread_count --; mp->suspend_thread_count --;
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(temp); rt_hw_interrupt_enable(temp);
} }
#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB) #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
/* the mp object belongs to an application module */ /* the mp object belongs to an application module */
if (mp->parent.flag & RT_OBJECT_FLAG_MODULE) if (mp->parent.flag & RT_OBJECT_FLAG_MODULE)
rt_module_free(mp->parent.module_id, mp->start_address); rt_module_free(mp->parent.module_id, mp->start_address);
else else
#endif #endif
/* release allocated room */ /* release allocated room */
rt_free(mp->start_address); rt_free(mp->start_address);
/* detach object */ /* detach object */
rt_object_delete(&(mp->parent)); rt_object_delete(&(mp->parent));
return RT_EOK; return RT_EOK;
} }
RTM_EXPORT(rt_mp_delete); RTM_EXPORT(rt_mp_delete);
#endif #endif
...@@ -299,83 +310,87 @@ RTM_EXPORT(rt_mp_delete); ...@@ -299,83 +310,87 @@ RTM_EXPORT(rt_mp_delete);
*/ */
void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time) void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
{ {
rt_uint8_t *block_ptr; rt_uint8_t *block_ptr;
register rt_base_t level; register rt_base_t level;
struct rt_thread *thread; struct rt_thread *thread;
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if (mp->block_free_count) if (mp->block_free_count)
{ {
/* memory block is available. decrease the free block counter */ /* memory block is available. decrease the free block counter */
mp->block_free_count --; mp->block_free_count --;
/* get block from block list */ /* get block from block list */
block_ptr = mp->block_list; block_ptr = mp->block_list;
mp->block_list = *(rt_uint8_t **)block_ptr; mp->block_list = *(rt_uint8_t **)block_ptr;
/* point to memory pool */ /* point to memory pool */
*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp; *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
} }
else else
{ {
/* memory block is unavailable. */ /* memory block is unavailable. */
if (time == 0) if (time == 0)
{ {
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
return RT_NULL;
} return RT_NULL;
else }
{ else
RT_DEBUG_NOT_IN_INTERRUPT; {
RT_DEBUG_NOT_IN_INTERRUPT;
/* get current thread */
thread = rt_thread_self(); /* get current thread */
thread = rt_thread_self();
/* need suspend thread */
rt_thread_suspend(thread); /* need suspend thread */
rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist)); rt_thread_suspend(thread);
mp->suspend_thread_count ++; rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
mp->suspend_thread_count ++;
if (time > 0)
{ if (time > 0)
/* init thread timer and start it */ {
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time); /* init thread timer and start it */
rt_timer_start(&(thread->thread_timer)); rt_timer_control(&(thread->thread_timer),
} RT_TIMER_CTRL_SET_TIME,
&time);
/* enable interrupt */ rt_timer_start(&(thread->thread_timer));
rt_hw_interrupt_enable(level); }
/* do a schedule */ /* enable interrupt */
rt_schedule(); rt_hw_interrupt_enable(level);
if (thread->error != RT_EOK) /* do a schedule */
return RT_NULL; rt_schedule();
/* disable interrupt */ if (thread->error != RT_EOK)
level = rt_hw_interrupt_disable(); return RT_NULL;
/* decrease free block */ /* disable interrupt */
mp->block_free_count --; level = rt_hw_interrupt_disable();
/* get block from block list */ /* decrease free block */
block_ptr = mp->block_list; mp->block_free_count --;
mp->block_list = *(rt_uint8_t **)block_ptr;
/* get block from block list */
/* point to memory pool */ block_ptr = mp->block_list;
*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp; mp->block_list = *(rt_uint8_t **)block_ptr;
}
} /* point to memory pool */
*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
/* 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 *)))); /* enable interrupt */
rt_hw_interrupt_enable(level);
return (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *));
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 *));
} }
RTM_EXPORT(rt_mp_alloc); RTM_EXPORT(rt_mp_alloc);
...@@ -386,52 +401,54 @@ RTM_EXPORT(rt_mp_alloc); ...@@ -386,52 +401,54 @@ RTM_EXPORT(rt_mp_alloc);
*/ */
void rt_mp_free(void *block) void rt_mp_free(void *block)
{ {
rt_uint8_t **block_ptr; rt_uint8_t **block_ptr;
struct rt_mempool *mp; struct rt_mempool *mp;
struct rt_thread *thread; struct rt_thread *thread;
register rt_base_t level; register rt_base_t level;
/* get the control block of pool which the block belongs to */ /* get the control block of pool which the block belongs to */
block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *)); block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *));
mp = (struct rt_mempool *)*block_ptr; mp = (struct rt_mempool *)*block_ptr;
RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block)); RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block));
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
/* increase the free block count */ /* increase the free block count */
mp->block_free_count ++; mp->block_free_count ++;
/* link the block into the block list */ /* link the block into the block list */
*block_ptr = mp->block_list; *block_ptr = mp->block_list;
mp->block_list = (rt_uint8_t *)block_ptr; mp->block_list = (rt_uint8_t *)block_ptr;
if (mp->suspend_thread_count > 0) if (mp->suspend_thread_count > 0)
{ {
/* get the suspended thread */ /* get the suspended thread */
thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist); thread = rt_list_entry(mp->suspend_thread.next,
struct rt_thread,
tlist);
/* set error */ /* set error */
thread->error = RT_EOK; thread->error = RT_EOK;
/* resume thread */ /* resume thread */
rt_thread_resume(thread); rt_thread_resume(thread);
/* decrease suspended thread count */ /* decrease suspended thread count */
mp->suspend_thread_count --; mp->suspend_thread_count --;
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
/* do a schedule */ /* do a schedule */
rt_schedule(); rt_schedule();
return; return;
} }
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
} }
RTM_EXPORT(rt_mp_free); RTM_EXPORT(rt_mp_free);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册