add a new command list_memheap() into finsh

and unify the coding style in memheap.c

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2151 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
dzzxzz@gmail.com 2012-06-03 06:16:30 +00:00
parent f19d8aa591
commit 43f2bfc013
2 changed files with 168 additions and 127 deletions

View File

@ -26,6 +26,7 @@
* 2009-05-30 Bernard add list_device * 2009-05-30 Bernard add list_device
* 2010-04-21 yi.qiu add list_module * 2010-04-21 yi.qiu add list_module
* 2012-04-29 goprife improve the command line auto-complete feature. * 2012-04-29 goprife improve the command line auto-complete feature.
* 2012-06-02 lgnq add list_memheap
*/ */
#include <rtthread.h> #include <rtthread.h>
@ -35,10 +36,10 @@ rt_inline unsigned int rt_list_len(const rt_list_t *l)
{ {
unsigned int len = 0; unsigned int len = 0;
const rt_list_t *p = l; const rt_list_t *p = l;
while( p->next != l ) while (p->next != l)
{ {
p = p->next; p = p->next;
len++; len ++;
} }
return len; return len;
} }
@ -60,15 +61,13 @@ long version(void)
} }
FINSH_FUNCTION_EXPORT(version, show RT-Thread version information); FINSH_FUNCTION_EXPORT(version, show RT-Thread version information);
#define rt_list_entry(node, type, member) \
((type *)((char *)(node) - (unsigned long)(&((type *)0)->member)))
extern struct rt_object_information rt_object_container[]; extern struct rt_object_information rt_object_container[];
static long _list_thread(struct rt_list_node* list) static long _list_thread(struct rt_list_node *list)
{ {
struct rt_thread *thread; struct rt_thread *thread;
struct rt_list_node *node; struct rt_list_node *node;
rt_uint8_t* ptr; rt_uint8_t *ptr;
rt_kprintf(" thread pri status sp stack size max used left tick error\n"); rt_kprintf(" thread pri status sp stack size max used left tick error\n");
rt_kprintf("-------- ---- ------- ---------- ---------- ---------- ---------- ---\n"); rt_kprintf("-------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
@ -91,8 +90,8 @@ static long _list_thread(struct rt_list_node* list)
thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr), thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
thread->remaining_tick, thread->remaining_tick,
thread->error); thread->error);
} }
return 0; return 0;
} }
@ -102,7 +101,7 @@ long list_thread(void)
} }
FINSH_FUNCTION_EXPORT(list_thread, list thread); FINSH_FUNCTION_EXPORT(list_thread, list thread);
static void show_wait_queue(struct rt_list_node* list) static void show_wait_queue(struct rt_list_node *list)
{ {
struct rt_thread *thread; struct rt_thread *thread;
struct rt_list_node *node; struct rt_list_node *node;
@ -111,7 +110,9 @@ static void show_wait_queue(struct rt_list_node* list)
{ {
thread = rt_list_entry(node, struct rt_thread, tlist); thread = rt_list_entry(node, struct rt_thread, tlist);
rt_kprintf("%s", thread->name); rt_kprintf("%s", thread->name);
if (node->next != list) rt_kprintf("/");
if (node->next != list)
rt_kprintf("/");
} }
} }
@ -125,7 +126,7 @@ static long _list_sem(struct rt_list_node *list)
rt_kprintf("-------- --- --------------\n"); rt_kprintf("-------- --- --------------\n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
sem = (struct rt_semaphore*)(rt_list_entry(node, struct rt_object, list)); sem = (struct rt_semaphore *)(rt_list_entry(node, struct rt_object, list));
if( !rt_list_isempty(&sem->parent.suspend_thread) ) if( !rt_list_isempty(&sem->parent.suspend_thread) )
{ {
rt_kprintf("%-8.*s %03d %d:", RT_NAME_MAX, sem->parent.parent.name, sem->value, rt_kprintf("%-8.*s %03d %d:", RT_NAME_MAX, sem->parent.parent.name, sem->value,
@ -160,8 +161,8 @@ static long _list_event(struct rt_list_node *list)
rt_kprintf("-------- ---------- --------------\n"); rt_kprintf("-------- ---------- --------------\n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
e = (struct rt_event*)(rt_list_entry(node, struct rt_object, list)); e = (struct rt_event *)(rt_list_entry(node, struct rt_object, list));
if( !rt_list_isempty(&e->parent.suspend_thread) ) if (!rt_list_isempty(&e->parent.suspend_thread))
{ {
rt_kprintf("%-8.*s 0x%08x %03d:", RT_NAME_MAX, e->parent.parent.name, rt_kprintf("%-8.*s 0x%08x %03d:", RT_NAME_MAX, e->parent.parent.name,
e->set, rt_list_len(&e->parent.suspend_thread)); e->set, rt_list_len(&e->parent.suspend_thread));
@ -194,7 +195,7 @@ static long _list_mutex(struct rt_list_node *list)
rt_kprintf("-------- -------- ---- --------------\n"); rt_kprintf("-------- -------- ---- --------------\n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
m = (struct rt_mutex*)(rt_list_entry(node, struct rt_object, list)); m = (struct rt_mutex *)(rt_list_entry(node, struct rt_object, list));
rt_kprintf("%-8.*s %-8.*s %04d %d\n", RT_NAME_MAX, m->parent.parent.name, rt_kprintf("%-8.*s %-8.*s %04d %d\n", RT_NAME_MAX, m->parent.parent.name,
RT_NAME_MAX, m->owner->name, m->hold, rt_list_len(&m->parent.suspend_thread)); RT_NAME_MAX, m->owner->name, m->hold, rt_list_len(&m->parent.suspend_thread));
} }
@ -219,8 +220,8 @@ static long _list_mailbox(struct rt_list_node *list)
rt_kprintf("-------- ---- ---- --------------\n"); rt_kprintf("-------- ---- ---- --------------\n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
m = (struct rt_mailbox*)(rt_list_entry(node, struct rt_object, list)); m = (struct rt_mailbox *)(rt_list_entry(node, struct rt_object, list));
if( !rt_list_isempty(&m->parent.suspend_thread) ) if (!rt_list_isempty(&m->parent.suspend_thread))
{ {
rt_kprintf("%-8.*s %04d %04d %d:", RT_NAME_MAX, m->parent.parent.name, rt_kprintf("%-8.*s %04d %04d %d:", RT_NAME_MAX, m->parent.parent.name,
m->entry, m->size, rt_list_len(&m->parent.suspend_thread)); m->entry, m->size, rt_list_len(&m->parent.suspend_thread));
@ -254,8 +255,8 @@ static long _list_msgqueue(struct rt_list_node *list)
rt_kprintf("-------- ---- --------------\n"); rt_kprintf("-------- ---- --------------\n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
m = (struct rt_messagequeue*)(rt_list_entry(node, struct rt_object, list)); m = (struct rt_messagequeue *)(rt_list_entry(node, struct rt_object, list));
if( !rt_list_isempty(&m->parent.suspend_thread) ) if (!rt_list_isempty(&m->parent.suspend_thread))
{ {
rt_kprintf("%-8.*s %04d %d:", RT_NAME_MAX, m->parent.parent.name, rt_kprintf("%-8.*s %04d %d:", RT_NAME_MAX, m->parent.parent.name,
m->entry, rt_list_len(&m->parent.suspend_thread)); m->entry, rt_list_len(&m->parent.suspend_thread));
@ -279,6 +280,32 @@ long list_msgqueue(void)
FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system) FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system)
#endif #endif
#ifdef RT_USING_MEMHEAP
static long _list_memheap(struct rt_list_node *list)
{
struct rt_memheap *mh;
struct rt_list_node *node;
rt_kprintf("memheap pool size available size\n");
rt_kprintf("-------- --------- --------------\n");
for (node = list->next; node != list; node = node->next)
{
mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);
rt_kprintf("%-8.*s %04d %04d\n", RT_NAME_MAX, mh->parent.name,
mh->pool_size, mh->available_size);
}
return 0;
}
long list_memheap(void)
{
return _list_memheap(&rt_object_container[RT_Object_Class_MemHeap].object_list);
}
FINSH_FUNCTION_EXPORT(list_memheap, list memory heap in system)
#endif
#ifdef RT_USING_MEMPOOL #ifdef RT_USING_MEMPOOL
static long _list_mempool(struct rt_list_node *list) static long _list_mempool(struct rt_list_node *list)
{ {
@ -289,7 +316,7 @@ static long _list_mempool(struct rt_list_node *list)
rt_kprintf("-------- ---- ---- ---- --------------\n"); rt_kprintf("-------- ---- ---- ---- --------------\n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
mp = (struct rt_mempool*)rt_list_entry(node, struct rt_object, list); mp = (struct rt_mempool *)rt_list_entry(node, struct rt_object, list);
if (mp->suspend_thread_count > 0) if (mp->suspend_thread_count > 0)
{ {
rt_kprintf("%-8.*s %04d %04d %04d %d:", RT_NAME_MAX, mp->parent.name, rt_kprintf("%-8.*s %04d %04d %04d %d:", RT_NAME_MAX, mp->parent.name,
@ -325,10 +352,12 @@ static long _list_timer(struct rt_list_node *list)
rt_kprintf("-------- ---------- ---------- -----------\n"); rt_kprintf("-------- ---------- ---------- -----------\n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
timer = (struct rt_timer*)(rt_list_entry(node, struct rt_object, list)); timer = (struct rt_timer *)(rt_list_entry(node, struct rt_object, list));
rt_kprintf("%-8.*s 0x%08x 0x%08x ", RT_NAME_MAX, timer->parent.name, timer->init_tick, timer->timeout_tick); rt_kprintf("%-8.*s 0x%08x 0x%08x ", RT_NAME_MAX, timer->parent.name, timer->init_tick, timer->timeout_tick);
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) rt_kprintf("activated\n"); if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
else rt_kprintf("deactivated\n"); rt_kprintf("activated\n");
else
rt_kprintf("deactivated\n");
} }
rt_kprintf("current tick:0x%08x\n", rt_tick_get()); rt_kprintf("current tick:0x%08x\n", rt_tick_get());
@ -371,7 +400,7 @@ static long _list_device(struct rt_list_node *list)
rt_kprintf("-------- ---------- \n"); rt_kprintf("-------- ---------- \n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
device = (struct rt_device*)(rt_list_entry(node, struct rt_object, list)); device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list));
rt_kprintf("%-8.*s %-8s \n", RT_NAME_MAX, device->parent.name, rt_kprintf("%-8.*s %-8s \n", RT_NAME_MAX, device->parent.name,
(device->type <= RT_Device_Class_Unknown)? (device->type <= RT_Device_Class_Unknown)?
device_type_str[device->type]:device_type_str[RT_Device_Class_Unknown]); device_type_str[device->type]:device_type_str[RT_Device_Class_Unknown]);
@ -401,7 +430,7 @@ int list_module(void)
rt_kprintf("------------ --------\n"); rt_kprintf("------------ --------\n");
for (node = list->next; node != list; node = node->next) for (node = list->next; node != list; node = node->next)
{ {
module = (struct rt_module*)(rt_list_entry(node, struct rt_object, list)); module = (struct rt_module *)(rt_list_entry(node, struct rt_object, list));
rt_kprintf("%-16.*s %-04d\n", RT_NAME_MAX, module->parent.name, module->nref); rt_kprintf("%-16.*s %-04d\n", RT_NAME_MAX, module->parent.name, module->nref);
} }
@ -410,23 +439,23 @@ int list_module(void)
FINSH_FUNCTION_EXPORT(list_module, list module in system) FINSH_FUNCTION_EXPORT(list_module, list module in system)
int list_mod_detail(const char* name) int list_mod_detail(const char *name)
{ {
int i; int i;
struct rt_module *module; struct rt_module *module;
/* find module */ /* find module */
if((module = rt_module_find(name)) != RT_NULL) if ((module = rt_module_find(name)) != RT_NULL)
{ {
/* module has entry point */ /* module has entry point */
if(!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY)) if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
{ {
struct rt_thread *thread; struct rt_thread *thread;
struct rt_list_node *tlist; struct rt_list_node *tlist;
rt_uint8_t* ptr; rt_uint8_t *ptr;
/* list main thread in module */ /* list main thread in module */
if(module->module_thread != RT_NULL) if (module->module_thread != RT_NULL)
{ {
rt_kprintf("main thread pri status sp stack size max used left tick error\n"); rt_kprintf("main thread pri status sp stack size max used left tick error\n");
rt_kprintf("------------- ---- ------- ---------- ---------- ---------- ---------- ---\n"); rt_kprintf("------------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
@ -450,52 +479,57 @@ int list_mod_detail(const char* name)
/* list sub thread in module */ /* list sub thread in module */
tlist = &module->module_object[RT_Object_Class_Thread].object_list; tlist = &module->module_object[RT_Object_Class_Thread].object_list;
if(!rt_list_isempty(tlist)) _list_thread(tlist); if (!rt_list_isempty(tlist)) _list_thread(tlist);
#ifdef RT_USING_SEMAPHORE #ifdef RT_USING_SEMAPHORE
/* list semaphored in module */ /* list semaphored in module */
tlist = &module->module_object[RT_Object_Class_Semaphore].object_list; tlist = &module->module_object[RT_Object_Class_Semaphore].object_list;
if(!rt_list_isempty(tlist)) _list_sem(tlist); if (!rt_list_isempty(tlist)) _list_sem(tlist);
#endif #endif
#ifdef RT_USING_MUTEX #ifdef RT_USING_MUTEX
/* list mutex in module */ /* list mutex in module */
tlist = &module->module_object[RT_Object_Class_Mutex].object_list; tlist = &module->module_object[RT_Object_Class_Mutex].object_list;
if(!rt_list_isempty(tlist)) _list_mutex(tlist); if (!rt_list_isempty(tlist)) _list_mutex(tlist);
#endif #endif
#ifdef RT_USING_EVENT #ifdef RT_USING_EVENT
/* list event in module */ /* list event in module */
tlist = &module->module_object[RT_Object_Class_Event].object_list; tlist = &module->module_object[RT_Object_Class_Event].object_list;
if(!rt_list_isempty(tlist)) _list_event(tlist); if (!rt_list_isempty(tlist)) _list_event(tlist);
#endif #endif
#ifdef RT_USING_MAILBOX #ifdef RT_USING_MAILBOX
/* list mailbox in module */ /* list mailbox in module */
tlist = &module->module_object[RT_Object_Class_MailBox].object_list; tlist = &module->module_object[RT_Object_Class_MailBox].object_list;
if(!rt_list_isempty(tlist)) _list_mailbox(tlist); if (!rt_list_isempty(tlist)) _list_mailbox(tlist);
#endif #endif
#ifdef RT_USING_MESSAGEQUEUE #ifdef RT_USING_MESSAGEQUEUE
/* list message queue in module */ /* list message queue in module */
tlist = &module->module_object[RT_Object_Class_MessageQueue].object_list; tlist = &module->module_object[RT_Object_Class_MessageQueue].object_list;
if(!rt_list_isempty(tlist)) _list_msgqueue(tlist); if (!rt_list_isempty(tlist)) _list_msgqueue(tlist);
#endif
#ifdef RT_USING_MEMHEAP
/* list memory heap in module */
tlist = &module->module_object[RT_Object_Class_MemHeap].object_list;
if (!rt_list_isempty(tlist)) _list_memheap(tlist);
#endif #endif
#ifdef RT_USING_MEMPOOL #ifdef RT_USING_MEMPOOL
/* list memory pool in module */ /* list memory pool in module */
tlist = &module->module_object[RT_Object_Class_MemPool].object_list; tlist = &module->module_object[RT_Object_Class_MemPool].object_list;
if(!rt_list_isempty(tlist)) _list_mempool(tlist); if (!rt_list_isempty(tlist)) _list_mempool(tlist);
#endif #endif
#ifdef RT_USING_DEVICE #ifdef RT_USING_DEVICE
/* list device in module */ /* list device in module */
tlist = &module->module_object[RT_Object_Class_Device].object_list; tlist = &module->module_object[RT_Object_Class_Device].object_list;
if(!rt_list_isempty(tlist)) _list_device(tlist); if (!rt_list_isempty(tlist)) _list_device(tlist);
#endif #endif
/* list timer in module */ /* list timer in module */
tlist = &module->module_object[RT_Object_Class_Timer].object_list; tlist = &module->module_object[RT_Object_Class_Timer].object_list;
if(!rt_list_isempty(tlist)) _list_timer(tlist); if (!rt_list_isempty(tlist)) _list_timer(tlist);
} }
rt_kprintf("symbol address \n"); rt_kprintf("symbol address \n");
rt_kprintf("-------- ----------\n"); rt_kprintf("-------- ----------\n");
/* list module export symbols */ /* list module export symbols */
for(i=0; i<module->nsym; i++) for (i=0; i<module->nsym; i++)
{ {
rt_kprintf("%s 0x%x\n", module->symtab[i].name, module->symtab[i].addr); rt_kprintf("%s 0x%x\n", module->symtab[i].name, module->symtab[i].addr);
} }
@ -508,12 +542,12 @@ FINSH_FUNCTION_EXPORT(list_mod_detail, list module objects in system)
long list(void) long list(void)
{ {
struct finsh_syscall_item* syscall_item; struct finsh_syscall_item *syscall_item;
struct finsh_sysvar_item* sysvar_item; struct finsh_sysvar_item *sysvar_item;
rt_kprintf("--Function List:\n"); rt_kprintf("--Function List:\n");
{ {
struct finsh_syscall* index; struct finsh_syscall *index;
for (index = _syscall_table_begin; index < _syscall_table_end; index ++) for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
{ {
#ifdef FINSH_USING_DESCRIPTION #ifdef FINSH_USING_DESCRIPTION
@ -534,7 +568,7 @@ long list(void)
rt_kprintf("--Variable List:\n"); rt_kprintf("--Variable List:\n");
{ {
struct finsh_sysvar* index; struct finsh_sysvar *index;
for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++) for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
{ {
#ifdef FINSH_USING_DESCRIPTION #ifdef FINSH_USING_DESCRIPTION
@ -556,7 +590,7 @@ long list(void)
} }
FINSH_FUNCTION_EXPORT(list, list all symbol in system) FINSH_FUNCTION_EXPORT(list, list all symbol in system)
static int str_is_prefix(const char* prefix, const char* str) static int str_is_prefix(const char *prefix, const char *str)
{ {
while ((*prefix) && (*prefix == *str)) while ((*prefix) && (*prefix == *str))
{ {
@ -564,15 +598,17 @@ static int str_is_prefix(const char* prefix, const char* str)
str ++; str ++;
} }
if (*prefix == 0) return 0; if (*prefix == 0)
return 0;
return -1; return -1;
} }
static int str_common(const char * str1, const char * str2) static int str_common(const char *str1, const char *str2)
{ {
const char * str = str1; const char *str = str1;
while( *str !=0 && *str2 !=0 && (*str == *str2)) while ((*str != 0) && (*str2 != 0) && (*str == *str2))
{ {
str ++; str ++;
str2 ++; str2 ++;
@ -581,13 +617,13 @@ static int str_common(const char * str1, const char * str2)
return (str - str1); return (str - str1);
} }
void list_prefix(char* prefix) void list_prefix(char *prefix)
{ {
struct finsh_syscall_item* syscall_item; struct finsh_syscall_item *syscall_item;
struct finsh_sysvar_item* sysvar_item; struct finsh_sysvar_item *sysvar_item;
rt_uint16_t func_cnt, var_cnt; rt_uint16_t func_cnt, var_cnt;
int length, min_length; int length, min_length;
const char* name_ptr; const char *name_ptr;
func_cnt = 0; func_cnt = 0;
var_cnt = 0; var_cnt = 0;

View File

@ -11,6 +11,7 @@
* Date Author Notes * Date Author Notes
* 2012-04-10 Bernard first implementation * 2012-04-10 Bernard first implementation
*/ */
#include <rtthread.h> #include <rtthread.h>
#ifdef RT_USING_MEMHEAP #ifdef RT_USING_MEMHEAP
@ -36,7 +37,7 @@
* *
* The length of Used Memory Block Tailer is 0, which is prevents block merging across list * The length of Used Memory Block Tailer is 0, which is prevents block merging across list
*/ */
rt_err_t rt_memheap_init(struct rt_memheap* memheap, const char* name, rt_err_t rt_memheap_init(struct rt_memheap *memheap, const char *name,
void *start_addr, void *start_addr,
rt_uint32_t size) rt_uint32_t size)
{ {
@ -53,28 +54,28 @@ rt_err_t rt_memheap_init(struct rt_memheap* memheap, const char* name,
/* initialize the free list header */ /* initialize the free list header */
item = &(memheap->free_header); item = &(memheap->free_header);
item->magic = RT_MEMHEAP_MAGIC; item->magic = RT_MEMHEAP_MAGIC;
item->pool_ptr = memheap; item->pool_ptr = memheap;
item->next = RT_NULL; item->next = RT_NULL;
item->prev = RT_NULL; item->prev = RT_NULL;
item->next_free = item; item->next_free = item;
item->prev_free = item; item->prev_free = item;
/* set the free list to free list header */ /* set the free list to free list header */
memheap->free_list = item; memheap->free_list = item;
/* initialize the first big memory block */ /* initialize the first big memory block */
item = (struct rt_memheap_item*) start_addr; item = (struct rt_memheap_item *)start_addr;
item->magic = RT_MEMHEAP_MAGIC; item->magic = RT_MEMHEAP_MAGIC;
item->pool_ptr = memheap; item->pool_ptr = memheap;
item->next = RT_NULL; item->next = RT_NULL;
item->prev = RT_NULL; item->prev = RT_NULL;
item->next_free = item; item->next_free = item;
item->prev_free = item; item->prev_free = item;
item->next = (struct rt_memheap_item *) item->next = (struct rt_memheap_item *)
((rt_uint8_t*) item + memheap->available_size + RT_MEMHEAP_SIZE); ((rt_uint8_t *)item + memheap->available_size + RT_MEMHEAP_SIZE);
item->prev = item->next; item->prev = item->next;
/* block list header */ /* block list header */
memheap->block_list = item; memheap->block_list = item;
@ -86,30 +87,30 @@ rt_err_t rt_memheap_init(struct rt_memheap* memheap, const char* name,
memheap->free_list->next_free = item; memheap->free_list->next_free = item;
/* move to the end of memory pool to build a small tailer block, which prevents block merging */ /* move to the end of memory pool to build a small tailer block, which prevents block merging */
item = item->next; item = item->next;
/* it's a used memory block */ /* it's a used memory block */
item->magic = RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED; item->magic = RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED;
item->pool_ptr = memheap; item->pool_ptr = memheap;
item->next = (struct rt_memheap_item *) start_addr; item->next = (struct rt_memheap_item *)start_addr;
item->prev = (struct rt_memheap_item *) start_addr; item->prev = (struct rt_memheap_item *)start_addr;
/* not in free list */ /* not in free list */
item->next_free = item->prev_free = RT_NULL; item->next_free = item->prev_free = RT_NULL;
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("memory heap: start addr 0x%08x, size %d, free list header 0x%08x", RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("memory heap: start addr 0x%08x, size %d, free list header 0x%08x",
start_addr, size, &(memheap->free_header))); start_addr, size, &(memheap->free_header)));
return RT_EOK; return RT_EOK;
} }
rt_err_t rt_memheap_detach(struct rt_memheap* heap) rt_err_t rt_memheap_detach(struct rt_memheap *heap)
{ {
rt_object_detach(&(heap->parent)); rt_object_detach(&(heap->parent));
/* Return a successful completion. */ /* Return a successful completion. */
return RT_EOK; return RT_EOK;
} }
void* rt_memheap_alloc(struct rt_memheap *pool_ptr, rt_uint32_t size) void *rt_memheap_alloc(struct rt_memheap *pool_ptr, rt_uint32_t size)
{ {
rt_uint32_t free_size; rt_uint32_t free_size;
struct rt_memheap_item *header_ptr; struct rt_memheap_item *header_ptr;
@ -118,7 +119,8 @@ void* rt_memheap_alloc(struct rt_memheap *pool_ptr, rt_uint32_t size)
/* align allocated size */ /* align allocated size */
size = RT_ALIGN(size, RT_ALIGN_SIZE); size = RT_ALIGN(size, RT_ALIGN_SIZE);
if (size < RT_MEMHEAP_MINIALLOC) size = RT_MEMHEAP_MINIALLOC; if (size < RT_MEMHEAP_MINIALLOC)
size = RT_MEMHEAP_MINIALLOC;
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate %d", size)); RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate %d", size));
@ -140,18 +142,18 @@ void* rt_memheap_alloc(struct rt_memheap *pool_ptr, rt_uint32_t size)
} }
} }
/* determine if the memory is available. */ /* determine if the memory is available. */
if (free_size >= size) if (free_size >= size)
{ {
/* a block that satisfies the request has been found. */ /* a block that satisfies the request has been found. */
/* determine if the block needs to be split. */ /* determine if the block needs to be split. */
if (free_size >= (size + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC)) if (free_size >= (size + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC))
{ {
struct rt_memheap_item* new_ptr; struct rt_memheap_item *new_ptr;
/* split the block. */ /* split the block. */
new_ptr = (struct rt_memheap_item*) (((rt_uint8_t*) header_ptr) + size + RT_MEMHEAP_SIZE); new_ptr = (struct rt_memheap_item *)(((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("split: h[0x%08x] nm[0x%08x] pm[0x%08x] to n[0x%08x]", header_ptr, RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("split: h[0x%08x] nm[0x%08x] pm[0x%08x] to n[0x%08x]", header_ptr,
header_ptr->next, header_ptr->prev, header_ptr->next, header_ptr->prev,
@ -160,7 +162,7 @@ void* rt_memheap_alloc(struct rt_memheap *pool_ptr, rt_uint32_t size)
/* mark the new block as a memory block and freed. */ /* mark the new block as a memory block and freed. */
new_ptr->magic = RT_MEMHEAP_MAGIC; new_ptr->magic = RT_MEMHEAP_MAGIC;
/* put the pool pointer into the new block. */ /* put the pool pointer into the new block. */
new_ptr->pool_ptr = pool_ptr; new_ptr->pool_ptr = pool_ptr;
/* break down the block list */ /* break down the block list */
@ -189,7 +191,7 @@ void* rt_memheap_alloc(struct rt_memheap *pool_ptr, rt_uint32_t size)
else else
{ {
/* decrement the entire free size from the available bytes count. */ /* decrement the entire free size from the available bytes count. */
pool_ptr->available_size = pool_ptr->available_size - free_size; pool_ptr->available_size = pool_ptr->available_size - free_size;
/* remove header_ptr from free list */ /* remove header_ptr from free list */
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("one block: h[0x%08x], nf 0x%08x, pf 0x%08x", header_ptr, RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("one block: h[0x%08x], nf 0x%08x, pf 0x%08x", header_ptr,
@ -202,12 +204,13 @@ void* rt_memheap_alloc(struct rt_memheap *pool_ptr, rt_uint32_t size)
} }
/* Mark the allocated block as not available. */ /* Mark the allocated block as not available. */
header_ptr->magic |= RT_MEMHEAP_USED; header_ptr->magic |= RT_MEMHEAP_USED;
/* Return a memory address to the caller. */ /* Return a memory address to the caller. */
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("am: m[0x%08x], h[0x%08x], size: %d", RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("am: m[0x%08x], h[0x%08x], size: %d",
(void*) ((rt_uint8_t*)header_ptr + RT_MEMHEAP_SIZE), header_ptr, size); (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE), header_ptr, size);
return (void*) ((rt_uint8_t*)header_ptr + RT_MEMHEAP_SIZE));
return (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE));
} }
} }
@ -217,15 +220,16 @@ void* rt_memheap_alloc(struct rt_memheap *pool_ptr, rt_uint32_t size)
return RT_NULL; return RT_NULL;
} }
void rt_memheap_free(void* ptr) void rt_memheap_free(void *ptr)
{ {
struct rt_memheap *pool_ptr; struct rt_memheap *pool_ptr;
struct rt_memheap_item *header_ptr, *new_ptr; struct rt_memheap_item *header_ptr, *new_ptr;
rt_uint32_t insert_header; rt_uint32_t insert_header;
/* set initial status as OK */ /* set initial status as OK */
insert_header = 1; new_ptr = RT_NULL; insert_header = 1;
header_ptr = (struct rt_memheap_item*)((rt_uint8_t*)ptr - RT_MEMHEAP_SIZE); new_ptr = RT_NULL;
header_ptr = (struct rt_memheap_item *)((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: m[0x%08x], h[0x%08x]", ptr, header_ptr)); RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: m[0x%08x], h[0x%08x]", ptr, header_ptr));
@ -235,50 +239,51 @@ void rt_memheap_free(void* ptr)
/* get pool ptr */ /* get pool ptr */
pool_ptr = header_ptr->pool_ptr; pool_ptr = header_ptr->pool_ptr;
/* Mark the memory as available. */ /* Mark the memory as available. */
header_ptr->magic &= ~RT_MEMHEAP_USED; header_ptr->magic &= ~RT_MEMHEAP_USED;
/* Adjust the available number of bytes. */ /* Adjust the available number of bytes. */
pool_ptr->available_size = pool_ptr->available_size + pool_ptr->available_size = pool_ptr->available_size +
((rt_uint32_t)(header_ptr->next) - ((rt_uint32_t)(header_ptr->next) -
(rt_uint32_t)header_ptr) - RT_MEMHEAP_SIZE; (rt_uint32_t)header_ptr) - RT_MEMHEAP_SIZE;
/* Determine if the block can be merged with the previous neighbor. */ /* Determine if the block can be merged with the previous neighbor. */
if (!RT_MEMHEAP_IS_USED(header_ptr->prev)) if (!RT_MEMHEAP_IS_USED(header_ptr->prev))
{ {
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x", header_ptr->prev)); RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x", header_ptr->prev));
/* adjust the available number of bytes. */ /* adjust the available number of bytes. */
pool_ptr->available_size = pool_ptr->available_size + RT_MEMHEAP_SIZE; pool_ptr->available_size = pool_ptr->available_size + RT_MEMHEAP_SIZE;
/* yes, merge block with previous neighbor. */ /* yes, merge block with previous neighbor. */
(header_ptr->prev)->next = header_ptr->next; (header_ptr->prev)->next = header_ptr->next;
(header_ptr->next)->prev = header_ptr->prev; (header_ptr->next)->prev = header_ptr->prev;
/* move header pointer to previous. */ /* move header pointer to previous. */
header_ptr = header_ptr->prev; header_ptr = header_ptr->prev;
insert_header = 0; /* don't insert header to free list */ /* don't insert header to free list */
} insert_header = 0;
}
/* determine if the block can be merged with the next neighbor. */ /* determine if the block can be merged with the next neighbor. */
if (!RT_MEMHEAP_IS_USED(header_ptr->next)) if (!RT_MEMHEAP_IS_USED(header_ptr->next))
{ {
/* adjust the available number of bytes. */ /* adjust the available number of bytes. */
pool_ptr->available_size = pool_ptr->available_size + RT_MEMHEAP_SIZE; pool_ptr->available_size = pool_ptr->available_size + RT_MEMHEAP_SIZE;
/* merge block with next neighbor. */ /* merge block with next neighbor. */
new_ptr = header_ptr->next; new_ptr = header_ptr->next;
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: right node 0x%08x, nf 0x%08x, pf 0x%08x", RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: right node 0x%08x, nf 0x%08x, pf 0x%08x",
new_ptr, new_ptr->next_free, new_ptr->prev_free)); new_ptr, new_ptr->next_free, new_ptr->prev_free));
new_ptr->next->prev = header_ptr; new_ptr->next->prev = header_ptr;
header_ptr->next = new_ptr->next; header_ptr->next = new_ptr->next;
/* remove new ptr from free list */ /* remove new ptr from free list */
new_ptr->next_free->prev_free = new_ptr->prev_free; new_ptr->next_free->prev_free = new_ptr->prev_free;
new_ptr->prev_free->next_free = new_ptr->next_free; new_ptr->prev_free->next_free = new_ptr->next_free;
} }
if (insert_header) if (insert_header)
{ {