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
* 2010-04-21 yi.qiu add list_module
* 2012-04-29 goprife improve the command line auto-complete feature.
* 2012-06-02 lgnq add list_memheap
*/
#include <rtthread.h>
@ -35,10 +36,10 @@ rt_inline unsigned int rt_list_len(const rt_list_t *l)
{
unsigned int len = 0;
const rt_list_t *p = l;
while( p->next != l )
while (p->next != l)
{
p = p->next;
len++;
len ++;
}
return len;
}
@ -60,15 +61,13 @@ long version(void)
}
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[];
static long _list_thread(struct rt_list_node* list)
static long _list_thread(struct rt_list_node *list)
{
struct rt_thread *thread;
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("-------- ---- ------- ---------- ---------- ---------- ---------- ---\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->remaining_tick,
thread->error);
}
return 0;
}
@ -102,7 +101,7 @@ long list_thread(void)
}
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_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);
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");
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) )
{
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");
for (node = list->next; node != list; node = node->next)
{
e = (struct rt_event*)(rt_list_entry(node, struct rt_object, list));
if( !rt_list_isempty(&e->parent.suspend_thread) )
e = (struct rt_event *)(rt_list_entry(node, struct rt_object, list));
if (!rt_list_isempty(&e->parent.suspend_thread))
{
rt_kprintf("%-8.*s 0x%08x %03d:", RT_NAME_MAX, e->parent.parent.name,
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");
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_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");
for (node = list->next; node != list; node = node->next)
{
m = (struct rt_mailbox*)(rt_list_entry(node, struct rt_object, list));
if( !rt_list_isempty(&m->parent.suspend_thread) )
m = (struct rt_mailbox *)(rt_list_entry(node, struct rt_object, list));
if (!rt_list_isempty(&m->parent.suspend_thread))
{
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));
@ -254,8 +255,8 @@ static long _list_msgqueue(struct rt_list_node *list)
rt_kprintf("-------- ---- --------------\n");
for (node = list->next; node != list; node = node->next)
{
m = (struct rt_messagequeue*)(rt_list_entry(node, struct rt_object, list));
if( !rt_list_isempty(&m->parent.suspend_thread) )
m = (struct rt_messagequeue *)(rt_list_entry(node, struct rt_object, list));
if (!rt_list_isempty(&m->parent.suspend_thread))
{
rt_kprintf("%-8.*s %04d %d:", RT_NAME_MAX, m->parent.parent.name,
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)
#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
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");
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)
{
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");
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);
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) rt_kprintf("activated\n");
else rt_kprintf("deactivated\n");
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
rt_kprintf("activated\n");
else
rt_kprintf("deactivated\n");
}
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");
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,
(device->type <= 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");
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);
}
@ -410,23 +439,23 @@ int list_module(void)
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;
struct rt_module *module;
/* find module */
if((module = rt_module_find(name)) != RT_NULL)
if ((module = rt_module_find(name)) != RT_NULL)
{
/* 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_list_node *tlist;
rt_uint8_t* ptr;
rt_uint8_t *ptr;
/* 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("------------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
@ -450,52 +479,57 @@ int list_mod_detail(const char* name)
/* list sub thread in module */
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
/* list semaphored in module */
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
#ifdef RT_USING_MUTEX
/* list mutex in module */
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
#ifdef RT_USING_EVENT
/* list event in module */
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
#ifdef RT_USING_MAILBOX
/* list mailbox in module */
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
#ifdef RT_USING_MESSAGEQUEUE
/* list message queue in module */
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
#ifdef RT_USING_MEMPOOL
/* list memory pool in module */
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
#ifdef RT_USING_DEVICE
/* list device in module */
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
/* list timer in module */
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("-------- ----------\n");
/* 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);
}
@ -508,12 +542,12 @@ FINSH_FUNCTION_EXPORT(list_mod_detail, list module objects in system)
long list(void)
{
struct finsh_syscall_item* syscall_item;
struct finsh_sysvar_item* sysvar_item;
struct finsh_syscall_item *syscall_item;
struct finsh_sysvar_item *sysvar_item;
rt_kprintf("--Function List:\n");
{
struct finsh_syscall* index;
struct finsh_syscall *index;
for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
{
#ifdef FINSH_USING_DESCRIPTION
@ -534,7 +568,7 @@ long list(void)
rt_kprintf("--Variable List:\n");
{
struct finsh_sysvar* index;
struct finsh_sysvar *index;
for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
{
#ifdef FINSH_USING_DESCRIPTION
@ -556,7 +590,7 @@ long list(void)
}
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))
{
@ -564,15 +598,17 @@ static int str_is_prefix(const char* prefix, const char* str)
str ++;
}
if (*prefix == 0) return 0;
if (*prefix == 0)
return 0;
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 ++;
str2 ++;
@ -581,13 +617,13 @@ static int str_common(const char * str1, const char * str2)
return (str - str1);
}
void list_prefix(char* prefix)
void list_prefix(char *prefix)
{
struct finsh_syscall_item* syscall_item;
struct finsh_sysvar_item* sysvar_item;
struct finsh_syscall_item *syscall_item;
struct finsh_sysvar_item *sysvar_item;
rt_uint16_t func_cnt, var_cnt;
int length, min_length;
const char* name_ptr;
const char *name_ptr;
func_cnt = 0;
var_cnt = 0;

View File

@ -11,6 +11,7 @@
* Date Author Notes
* 2012-04-10 Bernard first implementation
*/
#include <rtthread.h>
#ifdef RT_USING_MEMHEAP
@ -36,7 +37,7 @@
*
* 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,
rt_uint32_t size)
{
@ -64,7 +65,7 @@ rt_err_t rt_memheap_init(struct rt_memheap* memheap, const char* name,
memheap->free_list = item;
/* 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->pool_ptr = memheap;
item->next = RT_NULL;
@ -73,7 +74,7 @@ rt_err_t rt_memheap_init(struct rt_memheap* memheap, const char* name,
item->prev_free = 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;
/* block list header */
@ -90,8 +91,8 @@ rt_err_t rt_memheap_init(struct rt_memheap* memheap, const char* name,
/* it's a used memory block */
item->magic = RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED;
item->pool_ptr = memheap;
item->next = (struct rt_memheap_item *) start_addr;
item->prev = (struct rt_memheap_item *) start_addr;
item->next = (struct rt_memheap_item *)start_addr;
item->prev = (struct rt_memheap_item *)start_addr;
/* not in free list */
item->next_free = item->prev_free = RT_NULL;
@ -101,7 +102,7 @@ rt_err_t rt_memheap_init(struct rt_memheap* memheap, const char* name,
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));
@ -109,7 +110,7 @@ rt_err_t rt_memheap_detach(struct rt_memheap* heap)
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;
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 */
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));
@ -148,10 +150,10 @@ void* rt_memheap_alloc(struct rt_memheap *pool_ptr, rt_uint32_t size)
/* determine if the block needs to be split. */
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. */
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,
header_ptr->next, header_ptr->prev,
@ -206,8 +208,9 @@ void* rt_memheap_alloc(struct rt_memheap *pool_ptr, rt_uint32_t size)
/* Return a memory address to the caller. */
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);
return (void*) ((rt_uint8_t*)header_ptr + RT_MEMHEAP_SIZE));
(void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE), header_ptr, 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;
}
void rt_memheap_free(void* ptr)
void rt_memheap_free(void *ptr)
{
struct rt_memheap *pool_ptr;
struct rt_memheap_item *header_ptr, *new_ptr;
rt_uint32_t insert_header;
/* set initial status as OK */
insert_header = 1; new_ptr = RT_NULL;
header_ptr = (struct rt_memheap_item*)((rt_uint8_t*)ptr - RT_MEMHEAP_SIZE);
insert_header = 1;
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));
@ -257,7 +261,8 @@ void rt_memheap_free(void* ptr)
/* move header pointer to previous. */
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. */