clean up code

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1106 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
qiuyiuestc 2010-11-24 01:27:45 +00:00
parent b5a2fde359
commit 56c6c301bc
3 changed files with 58 additions and 86 deletions

View File

@ -371,44 +371,6 @@ struct rt_thread
}; };
/*@}*/ /*@}*/
#ifdef RT_USING_MODULE
/*
* module system
*/
#define RT_MODULE_FLAG_WITHENTRY 0x00 /* with entry point */
#define RT_MODULE_FLAG_WITHOUTENTRY 0x01 /* without entry point */
struct rt_module
{
/* inherit from object */
struct rt_object parent;
rt_uint8_t* module_space; /* module memory space */
void* module_entry; /* entry address of module's thread */
rt_thread_t module_thread; /* stack size of module's thread */
rt_uint32_t stack_size; /* priority of module's thread */
rt_uint32_t thread_priority;
/* module memory allocator */
void* module_mem_list; /* module memory free list */
rt_list_t module_page; /* module using page */
void* page_node_pool;
/* module symbol table */
rt_uint32_t nsym;
struct rt_module_symtab *symtab;
/* reference count */
rt_uint32_t nref;
/* object in this module, module object is the last basic object type */
struct rt_object_information module_object[RT_Object_Class_Unknown];
};
typedef struct rt_module* rt_module_t;
#endif
/** /**
* @addtogroup IPC * @addtogroup IPC
*/ */
@ -659,6 +621,44 @@ struct rt_device_blk_geometry
rt_uint32_t block_size; /* size to erase one block */ rt_uint32_t block_size; /* size to erase one block */
}; };
#ifdef RT_USING_MODULE
/*
* module system
*/
#define RT_MODULE_FLAG_WITHENTRY 0x00 /* with entry point */
#define RT_MODULE_FLAG_WITHOUTENTRY 0x01 /* without entry point */
struct rt_module
{
/* inherit from object */
struct rt_object parent;
rt_uint8_t* module_space; /* module memory space */
void* module_entry; /* entry address of module's thread */
rt_thread_t module_thread; /* stack size of module's thread */
rt_uint32_t stack_size; /* priority of module's thread */
rt_uint32_t thread_priority;
/* module memory allocator */
void* mem_list; /* module's free memory list */
rt_list_t page_list; /* module's using page list */
rt_mp_t mpool;
/* module symbol table */
rt_uint32_t nsym;
struct rt_module_symtab *symtab;
/* reference count */
rt_uint32_t nref;
/* object in this module, module object is the last basic object type */
struct rt_object_information module_object[RT_Object_Class_Unknown];
};
typedef struct rt_module* rt_module_t;
#endif
/*@}*/ /*@}*/
#endif #endif

View File

@ -229,11 +229,6 @@ static void rt_module_init_object_container(struct rt_module* module)
rt_list_init(&(module->module_object[RT_Object_Class_Timer].object_list)); rt_list_init(&(module->module_object[RT_Object_Class_Timer].object_list));
module->module_object[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer); module->module_object[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer);
module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer; module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer;
/* init object container - module */
rt_list_init(&(module->module_object[RT_Object_Class_Module].object_list));
module->module_object[RT_Object_Class_Module].object_size = sizeof(struct rt_timer);
module->module_object[RT_Object_Class_Module].type = RT_Object_Class_Module;
} }
/** /**
@ -388,11 +383,13 @@ rt_module_t rt_module_load(const rt_uint8_t* name, void* module_ptr)
if(elf_module->e_entry != 0) if(elf_module->e_entry != 0)
{ {
/* init module page list */ /* init module page list */
rt_list_init(&module->module_page); rt_list_init(&module->page_list);
/* init module memory allocator */ /* init module memory allocator */
module->module_mem_list = RT_NULL; module->mem_list = RT_NULL;
module->page_node_pool = RT_NULL;
/* create mpool for page node */
module->mpool = rt_mp_create(name, 1024, sizeof(struct rt_module_page));
/* create module thread */ /* create module thread */
module->stack_size = 2048; module->stack_size = 2048;
@ -648,7 +645,7 @@ rt_err_t rt_module_unload(rt_module_t module)
} }
/* free module pages */ /* free module pages */
list = &module->module_page; list = &module->page_list;
while(list->next != list) while(list->next != list)
{ {
struct rt_module_page* page; struct rt_module_page* page;
@ -659,18 +656,8 @@ rt_err_t rt_module_unload(rt_module_t module)
rt_list_remove(list->next); rt_list_remove(list->next);
} }
if(module->page_node_pool) /* delete mpool */
{ if(module->mpool) rt_mp_delete(module->mpool);
/* free page node mempool */
if(((struct rt_mempool*)module->page_node_pool)->start_address != 0)
rt_page_free(((struct rt_mempool*)module->page_node_pool)->start_address, 1);
/* detach page node mempool */
rt_mp_detach(module->page_node_pool);
/* free page node mempool structure */
rt_free(module->page_node_pool);
}
} }
/* release module space memory */ /* release module space memory */
@ -730,33 +717,18 @@ static struct rt_mem_head *morepage(rt_size_t nu)
RT_ASSERT (nu != 0); RT_ASSERT (nu != 0);
/* alloc pages from system heap */
npage = (nu * sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1)/RT_MM_PAGE_SIZE; npage = (nu * sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1)/RT_MM_PAGE_SIZE;
cp = rt_page_alloc(npage); cp = rt_page_alloc(npage);
if(cp == RT_NULL) return RT_NULL; if(cp == RT_NULL) return RT_NULL;
if(!rt_current_module->page_node_pool) /* allocate page list node from mpool */
{ node = rt_mp_alloc(rt_current_module->mpool, RT_WAITING_FOREVER);
rt_current_module->page_node_pool = rt_malloc(sizeof(struct rt_mempool));
rt_memset(rt_current_module->page_node_pool, 0, sizeof(struct rt_mempool));
/* allocate a page for page node */
void *start = rt_page_alloc(1);
rt_mp_init(
(struct rt_mempool *)rt_current_module->page_node_pool,
"pnp",
start,
RT_MM_PAGE_SIZE,
sizeof(struct rt_module_page));
}
/* allocate page node from mempool */
node = rt_mp_alloc((struct rt_mempool *)rt_current_module->page_node_pool, RT_WAITING_FOREVER);
node->ptr = cp; node->ptr = cp;
node->npage = npage; node->npage = npage;
/* insert page node to moudle's page list */ /* insert page list node to moudle's page list */
if(rt_module_self() != RT_NULL) rt_list_insert_after (&rt_current_module->page_list, &node->list);
rt_list_insert_after (&rt_current_module->module_page, &node->list);
up = (struct rt_mem_head *) cp; up = (struct rt_mem_head *) cp;
up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head); up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
@ -779,7 +751,7 @@ void *rt_module_malloc(rt_size_t size)
RT_ASSERT(size != 0); RT_ASSERT(size != 0);
RT_ASSERT(nunits != 0); RT_ASSERT(nunits != 0);
prev = (struct rt_mem_head **)&rt_current_module->module_mem_list; prev = (struct rt_mem_head **)&rt_current_module->mem_list;
/* if alloc size is the multipal of 1K, TODO */ /* if alloc size is the multipal of 1K, TODO */
@ -828,7 +800,7 @@ void rt_module_free(rt_module_t module, void *addr)
RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) -1)) == 0); RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) -1)) == 0);
n = (struct rt_mem_head *)addr - 1; n = (struct rt_mem_head *)addr - 1;
prev = (struct rt_mem_head **)&module->module_mem_list; prev = (struct rt_mem_head **)&module->mem_list;
while ((b = *prev) != RT_NULL) while ((b = *prev) != RT_NULL)
{ {
@ -899,7 +871,7 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
else else
{ {
/* more space then required */ /* more space then required */
prev = (struct rt_mem_head *)rt_current_module->module_mem_list; prev = (struct rt_mem_head *)rt_current_module->mem_list;
for (p = prev->next; p != (b->size + b) && p != RT_NULL; prev = p, p = p->next) break; for (p = prev->next; p != (b->size + b) && p != RT_NULL; prev = p, p = p->next) break;
/* available block after ap in freelist */ /* available block after ap in freelist */
@ -925,7 +897,7 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
b->size = nunits; b->size = nunits;
prev->next = p; prev->next = p;
} }
rt_current_module->module_mem_list = (void *)prev; rt_current_module->mem_list = (void *)prev;
return (void *) (b + 1); return (void *) (b + 1);
} }
else /* allocate new memory and copy old data */ else /* allocate new memory and copy old data */

View File

@ -257,8 +257,8 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char* name)
struct rt_object_information* information; struct rt_object_information* information;
#ifdef RT_USING_MODULE #ifdef RT_USING_MODULE
/* get module object information */ /* get module object information, module object should be managed by kernel object container */
information = (rt_module_self() != RT_NULL) ? information = (rt_module_self() != RT_NULL && (type != RT_Object_Class_Module)) ?
&rt_module_self()->module_object[type] : &rt_object_container[type]; &rt_module_self()->module_object[type] : &rt_object_container[type];
#else #else
/* get object information */ /* get object information */