提交 cea21370 编写于 作者: qiuyiuestc's avatar qiuyiuestc

rewrite module memory allocator

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1618 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 3bc0dd4a
...@@ -759,8 +759,8 @@ struct rt_module ...@@ -759,8 +759,8 @@ struct rt_module
/* module memory allocator */ /* module memory allocator */
void* mem_list; /**< module's free memory list */ void* mem_list; /**< module's free memory list */
rt_list_t page_list; /**< module's using page list */ void* page_array; /**< module's using pages */
rt_mp_t mpool; /**< module's memory pool */ rt_uint32_t page_cnt; /**< module's using pages count */
rt_uint32_t nsym; /**< number of symbol in the module */ rt_uint32_t nsym; /**< number of symbol in the module */
struct rt_module_symtab *symtab; /**< module symbol table */ struct rt_module_symtab *symtab; /**< module symbol table */
......
...@@ -317,6 +317,12 @@ void rt_module_free(rt_module_t module, void *addr); ...@@ -317,6 +317,12 @@ void rt_module_free(rt_module_t module, void *addr);
rt_module_t rt_module_self (void); rt_module_t rt_module_self (void);
rt_err_t rt_module_set (rt_module_t module); rt_err_t rt_module_set (rt_module_t module);
rt_module_t rt_module_find(const char* name); rt_module_t rt_module_find(const char* name);
#ifdef RT_USING_HOOK
void rt_module_load_sethook(void (*hook)(rt_module_t module));
void rt_module_unload_sethook(void (*hook)(rt_module_t module));
#endif
#endif #endif
/*@}*/ /*@}*/
......
...@@ -115,7 +115,7 @@ void rt_thread_idle_excute(void) ...@@ -115,7 +115,7 @@ void rt_thread_idle_excute(void)
rt_hw_interrupt_enable(lock); rt_hw_interrupt_enable(lock);
#ifdef RT_USING_HEAP #ifdef RT_USING_HEAP
#ifdef RT_USING_MODULE #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
/* the thread belongs to an application module */ /* the thread belongs to an application module */
if(thread->flags & RT_OBJECT_FLAG_MODULE) if(thread->flags & RT_OBJECT_FLAG_MODULE)
rt_module_free((rt_module_t)thread->module_id, thread->stack_addr); rt_module_free((rt_module_t)thread->module_id, thread->stack_addr);
...@@ -125,6 +125,7 @@ void rt_thread_idle_excute(void) ...@@ -125,6 +125,7 @@ void rt_thread_idle_excute(void)
rt_free(thread->stack_addr); rt_free(thread->stack_addr);
/* delete thread object */ /* delete thread object */
rt_object_delete((rt_object_t)thread); rt_object_delete((rt_object_t)thread);
#endif
#ifdef RT_USING_MODULE #ifdef RT_USING_MODULE
if(module != RT_NULL) if(module != RT_NULL)
...@@ -139,7 +140,6 @@ void rt_thread_idle_excute(void) ...@@ -139,7 +140,6 @@ void rt_thread_idle_excute(void)
/* unload module */ /* unload module */
if(module->nref == 0) rt_module_unload(module); if(module->nref == 0) rt_module_unload(module);
#endif
#endif #endif
} }
} }
......
...@@ -1286,7 +1286,7 @@ rt_err_t rt_mb_delete (rt_mailbox_t mb) ...@@ -1286,7 +1286,7 @@ rt_err_t rt_mb_delete (rt_mailbox_t mb)
/* also resume all mailbox private suspended thread */ /* also resume all mailbox private suspended thread */
rt_ipc_list_resume_all(&(mb->suspend_sender_thread)); rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
#ifdef RT_USING_MODULE #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
/* the mb object belongs to an application module */ /* the mb object belongs to an application module */
if(mb->parent.parent.flag & RT_OBJECT_FLAG_MODULE) if(mb->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
rt_module_free(mb->parent.parent.module_id, mb->msg_pool); rt_module_free(mb->parent.parent.module_id, mb->msg_pool);
...@@ -1761,7 +1761,7 @@ rt_err_t rt_mq_delete (rt_mq_t mq) ...@@ -1761,7 +1761,7 @@ rt_err_t rt_mq_delete (rt_mq_t mq)
/* resume all suspended thread */ /* resume all suspended thread */
rt_ipc_list_resume_all(&(mq->parent.suspend_thread)); rt_ipc_list_resume_all(&(mq->parent.suspend_thread));
#ifdef RT_USING_MODULE #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
/* the mq object belongs to an application module */ /* the mq object belongs to an application module */
if(mq->parent.parent.flag & RT_OBJECT_FLAG_MODULE) if(mq->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
rt_module_free(mq->parent.parent.module_id, mq->msg_pool); rt_module_free(mq->parent.parent.module_id, mq->msg_pool);
......
...@@ -267,7 +267,7 @@ rt_err_t rt_mp_delete(rt_mp_t mp) ...@@ -267,7 +267,7 @@ rt_err_t rt_mp_delete(rt_mp_t mp)
rt_hw_interrupt_enable(temp); rt_hw_interrupt_enable(temp);
} }
#ifdef RT_USING_MODULE #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);
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
* 2010-01-09 Bernard first version * 2010-01-09 Bernard first version
* 2010-04-09 yi.qiu implement based on first version * 2010-04-09 yi.qiu implement based on first version
* 2010-10-23 yi.qiu implement module memory allocator * 2010-10-23 yi.qiu implement module memory allocator
* 2011-05-25 yi.qiu implement module hook function
* 2011-06-23 yi.qiu rewrite module memory allocator
*/ */
#include <rthw.h> #include <rthw.h>
...@@ -36,13 +38,7 @@ ...@@ -36,13 +38,7 @@
#define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR)) #define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
#define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE)) #define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
/* module memory allocator */ #define PAGE_COUNT_MAX 256
struct rt_module_page
{
rt_uint8_t *ptr; /* address of memory block */
rt_size_t npage; /* number of pages */
rt_list_t list;
};
/* module memory allocator */ /* module memory allocator */
struct rt_mem_head struct rt_mem_head
...@@ -51,12 +47,43 @@ struct rt_mem_head ...@@ -51,12 +47,43 @@ struct rt_mem_head
struct rt_mem_head *next; /* next valid memory block */ struct rt_mem_head *next; /* next valid memory block */
}; };
extern void *rt_malloc_page(rt_size_t npages); struct rt_page_info
extern void rt_free_page(void *page_ptr, rt_size_t npages); {
rt_uint32_t *page_ptr;
rt_uint32_t npage;
};
static void *rt_module_malloc_page(rt_size_t npages);
static void rt_module_free_page(void *page_ptr, rt_size_t npages);
static rt_module_t rt_current_module = RT_NULL; static rt_module_t rt_current_module = RT_NULL;
static struct rt_semaphore mod_sem;
static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL, *_rt_module_symtab_end = RT_NULL;
rt_list_t rt_module_symbol_list; rt_list_t rt_module_symbol_list;
struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL, *_rt_module_symtab_end = RT_NULL;
static char* _strip_name(const char* string)
{
int i = 0, p = 0, q = 0;
const char* str = string;
char* dest = RT_NULL;
while(*str != '\n' && *str != '\0')
{
if(*str =='/' ) p = i + 1;
if(*str == '.') q = i;
str++; i++;
}
if(p < q)
{
int len = q - p;
dest = (char*)rt_malloc(len + 1);
rt_strncpy(dest, &string[p], len);
dest[len] = '\0';
}
return dest;
}
/** /**
* @ingroup SystemInit * @ingroup SystemInit
...@@ -82,6 +109,9 @@ void rt_system_module_init(void) ...@@ -82,6 +109,9 @@ void rt_system_module_init(void)
rt_list_init(&rt_module_symbol_list); rt_list_init(&rt_module_symbol_list);
/* initialize heap semaphore */
rt_sem_init(&mod_sem, "module", 1, RT_IPC_FLAG_FIFO);
/* init current module */ /* init current module */
rt_current_module = RT_NULL; rt_current_module = RT_NULL;
} }
...@@ -248,6 +278,40 @@ static void rt_module_init_object_container(struct rt_module* module) ...@@ -248,6 +278,40 @@ static void rt_module_init_object_container(struct rt_module* module)
module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer; module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer;
} }
#ifdef RT_USING_HOOK
static void (*rt_module_load_hook)(rt_module_t module);
static void (*rt_module_unload_hook)(rt_module_t module);
/**
* @addtogroup Hook
*/
/*@{*/
/**
* This function will set a hook function, which will be invoked when module
* be loaded to system.
*
* @param hook the hook function
*/
void rt_module_load_sethook(void (*hook)(rt_module_t module))
{
rt_module_load_hook = hook;
}
/**
* This function will set a hook function, which will be invoked when module
* be unloaded from system.
*
* @param hook the hook function
*/
void rt_module_unload_sethook(void (*hook)(rt_module_t module))
{
rt_module_unload_hook = hook;
}
/*@}*/
#endif
/** /**
* This function will load a module from memory and create a thread for it * This function will load a module from memory and create a thread for it
* *
...@@ -301,7 +365,7 @@ rt_module_t rt_module_load(const char* name, void* module_ptr) ...@@ -301,7 +365,7 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
} }
/* allocate module */ /* allocate module */
module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, (const char*)name); module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, name);
if (!module) return RT_NULL; if (!module) return RT_NULL;
/* allocate module space */ /* allocate module space */
...@@ -436,24 +500,23 @@ rt_module_t rt_module_load(const char* name, void* module_ptr) ...@@ -436,24 +500,23 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
if(elf_module->e_entry != 0) if(elf_module->e_entry != 0)
{ {
/* init module page list */
rt_list_init(&module->page_list);
/* init module memory allocator */ /* init module memory allocator */
module->mem_list = RT_NULL; module->mem_list = RT_NULL;
/* create mpool for page node */ /* create page array */
module->mpool = rt_mp_create(name, 256, sizeof(struct rt_module_page)); module->page_array = (void *)rt_malloc(PAGE_COUNT_MAX * sizeof(struct rt_page_info));
module->page_cnt = 0;
/* create module thread */ /* create module thread */
module->stack_size = 2048; module->stack_size = 2048;
module->thread_priority = 90; module->thread_priority = 25;
module->module_thread = rt_thread_create(name, module->module_thread = rt_thread_create(name,
module->module_entry, RT_NULL, module->module_entry, RT_NULL,
module->stack_size, module->stack_size,
module->thread_priority, 10); module->thread_priority, 10);
module->module_thread->module_id = (void*)module; module->module_thread->module_id = (void*)module;
module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
/* startup module thread */ /* startup module thread */
rt_thread_startup(module->module_thread); rt_thread_startup(module->module_thread);
...@@ -464,6 +527,13 @@ rt_module_t rt_module_load(const char* name, void* module_ptr) ...@@ -464,6 +527,13 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY; module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
} }
#ifdef RT_USING_HOOK
if(rt_module_load_hook != RT_NULL)
{
rt_module_load_hook(module);
}
#endif
return module; return module;
} }
...@@ -477,19 +547,19 @@ rt_module_t rt_module_load(const char* name, void* module_ptr) ...@@ -477,19 +547,19 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
* @return the module object * @return the module object
* *
*/ */
rt_module_t rt_module_open(const char* filename) rt_module_t rt_module_open(const char* path)
{ {
int fd, length; int fd, length;
struct rt_module* module; struct rt_module* module;
struct stat s; struct stat s;
char *buffer, *offset_ptr;; char *buffer, *offset_ptr, *name;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
/* check parameters */ /* check parameters */
RT_ASSERT(filename != RT_NULL); RT_ASSERT(path != RT_NULL);
if (stat(filename, &s) !=0) if (stat(path, &s) !=0)
{ {
rt_kprintf("access file failed\n"); rt_kprintf("access file failed\n");
return RT_NULL; return RT_NULL;
...@@ -502,7 +572,7 @@ rt_module_t rt_module_open(const char* filename) ...@@ -502,7 +572,7 @@ rt_module_t rt_module_open(const char* filename)
} }
offset_ptr = buffer; offset_ptr = buffer;
fd = open(filename, O_RDONLY, 0); fd = open(path, O_RDONLY, 0);
if (fd < 0) if (fd < 0)
{ {
rt_kprintf("open file failed\n"); rt_kprintf("open file failed\n");
...@@ -529,8 +599,10 @@ rt_module_t rt_module_open(const char* filename) ...@@ -529,8 +599,10 @@ rt_module_t rt_module_open(const char* filename)
return RT_NULL; return RT_NULL;
} }
module = rt_module_load(filename, (void *)buffer); name = _strip_name(path);
module = rt_module_load(name, (void *)buffer);
rt_free(buffer); rt_free(buffer);
rt_free(name);
return module; return module;
} }
...@@ -557,13 +629,13 @@ rt_err_t rt_module_unload(rt_module_t module) ...@@ -557,13 +629,13 @@ rt_err_t rt_module_unload(rt_module_t module)
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
rt_kprintf("rt_module_unload: %s\n", module->parent.name);
/* check parameter */ /* check parameter */
RT_ASSERT(module != RT_NULL); RT_ASSERT(module != RT_NULL);
rt_kprintf("rt_module_unload: %s\n", module->parent.name);
/* module has entry point */ /* module has entry point */
if(!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY)) if((module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY) != RT_MODULE_FLAG_WITHOUTENTRY)
{ {
/* suspend module main thread */ /* suspend module main thread */
if(module->module_thread != RT_NULL) if(module->module_thread != RT_NULL)
...@@ -729,21 +801,21 @@ rt_err_t rt_module_unload(rt_module_t module) ...@@ -729,21 +801,21 @@ rt_err_t rt_module_unload(rt_module_t module)
rt_timer_delete((rt_timer_t)object); rt_timer_delete((rt_timer_t)object);
} }
} }
}
/* free module pages */ if(module->page_cnt > 0)
list = &module->page_list;
while(list->next != list)
{ {
struct rt_module_page* page; int i;
struct rt_page_info* page = (struct rt_page_info*)module->page_array;
/* free page */ rt_kprintf("warning: some module memory still hasn't be freed\n");
page = rt_list_entry(list->next, struct rt_module_page, list);
rt_free_page(page->ptr, page->npage); //list_memlist("tetris");
rt_list_remove(list->next);
}
/* delete mpool */ for(i=0; i<module->page_cnt; i++)
if(module->mpool) rt_mp_delete(module->mpool); {
rt_module_free_page(page[i].page_ptr, page[i].npage);
}
} }
/* release module space memory */ /* release module space memory */
...@@ -753,6 +825,15 @@ rt_err_t rt_module_unload(rt_module_t module) ...@@ -753,6 +825,15 @@ rt_err_t rt_module_unload(rt_module_t module)
for(i=0; i<module->nsym; i++) rt_free((void *)module->symtab[i].name); for(i=0; i<module->nsym; i++) rt_free((void *)module->symtab[i].name);
if(module->symtab != RT_NULL) rt_free(module->symtab); if(module->symtab != RT_NULL) rt_free(module->symtab);
#ifdef RT_USING_HOOK
if(rt_module_unload_hook != RT_NULL)
{
rt_module_unload_hook(module);
}
#endif
rt_free(module->page_array);
/* delete module object */ /* delete module object */
rt_object_delete((rt_object_t)module); rt_object_delete((rt_object_t)module);
...@@ -800,35 +881,79 @@ rt_module_t rt_module_find(const char* name) ...@@ -800,35 +881,79 @@ rt_module_t rt_module_find(const char* name)
return RT_NULL; return RT_NULL;
} }
static struct rt_mem_head *morepage(rt_size_t nu) #ifdef RT_USING_SLAB
/*
* This function will allocate the numbers page with specified size
* in page memory.
*
* @param size the size of memory to be allocated.
* @note this function is used for RT-Thread Application Module
*/
static void *rt_module_malloc_page(rt_size_t npages)
{ {
rt_uint8_t *cp; void* chunk;
rt_uint32_t npage; struct rt_page_info *page;
struct rt_mem_head *up;
struct rt_module_page *node;
RT_DEBUG_NOT_IN_INTERRUPT; chunk = rt_page_alloc(npages);
if (chunk == RT_NULL) return RT_NULL;
RT_ASSERT (nu != 0); page = (struct rt_page_info*)rt_current_module->page_array;
page[rt_current_module->page_cnt].page_ptr = chunk;
page[rt_current_module->page_cnt].npage = npages;
rt_current_module->page_cnt++;
/* allocate pages from system heap */ RT_ASSERT(rt_current_module->page_cnt <= PAGE_COUNT_MAX);
npage = (nu * sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1)/RT_MM_PAGE_SIZE;
cp = rt_malloc_page(npage);
if(!cp) return RT_NULL;
/* allocate page list node from mpool */ return chunk;
node = rt_mp_alloc(rt_current_module->mpool, RT_WAITING_FOREVER); }
node->ptr = cp;
node->npage = npage;
/* insert page list node to moudle's page list */ /*
rt_list_insert_after (&rt_current_module->page_list, &node->list); * This function will release the previously allocated memory page
* by rt_malloc_page.
*
* @param page_ptr the page address to be released.
* @param npages the number of page shall be released.
*
* @note this function is used for RT-Thread Application Module
*/
static void rt_module_free_page(void *page_ptr, rt_size_t npages)
{
int i, index;
struct rt_page_info *page;
up = (struct rt_mem_head *) cp; //rt_kprintf("rt_module_free_page 0x%x %d\n", page_ptr, npages);
up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head); rt_page_free(page_ptr, npages);
rt_module_free(rt_current_module, (void *)(up+1));
page = (struct rt_page_info*)rt_current_module->page_array;
return up; for(i=0; i<rt_current_module->page_cnt; i++)
{
if(page[i].page_ptr == page_ptr)
{
if(page[i].npage == npages + 1)
{
page[i].page_ptr += npages * RT_MM_PAGE_SIZE / sizeof(rt_uint32_t);
}
else if(page[i].npage == npages)
{
for(index=i; index<rt_current_module->page_cnt-1; index++)
{
page[index].page_ptr = page[index + 1].page_ptr;
page[index].npage = page[index + 1].npage;
}
page[rt_current_module->page_cnt - 1].page_ptr = RT_NULL;
page[rt_current_module->page_cnt - 1].npage = 0;
}
else RT_ASSERT(RT_FALSE);
rt_current_module->page_cnt--;
return;
}
}
/* should not be get here */
RT_ASSERT(RT_FALSE);
} }
/* /*
...@@ -836,8 +961,9 @@ static struct rt_mem_head *morepage(rt_size_t nu) ...@@ -836,8 +961,9 @@ static struct rt_mem_head *morepage(rt_size_t nu)
*/ */
void *rt_module_malloc(rt_size_t size) void *rt_module_malloc(rt_size_t size)
{ {
struct rt_mem_head *b, *n; struct rt_mem_head *b, *n, *up;
struct rt_mem_head **prev; struct rt_mem_head **prev;
rt_uint32_t npage;
rt_size_t nunits; rt_size_t nunits;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
...@@ -847,37 +973,10 @@ void *rt_module_malloc(rt_size_t size) ...@@ -847,37 +973,10 @@ 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->mem_list; rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
/* if size can be divided by page, allocate page directly */
if(size % RT_MM_PAGE_SIZE == 0)
{
rt_uint8_t *cp;
struct rt_module_page *node;
rt_uint32_t npage = size / RT_MM_PAGE_SIZE;
/* allocate pages from system heap */
cp = rt_malloc_page(npage);
if(!cp) return RT_NULL;
/* allocate page list node from mpool */
node = rt_mp_alloc(rt_current_module->mpool, RT_WAITING_FOREVER);
node->ptr = cp;
node->npage = npage;
/* insert page list node to moudle's page list */
rt_list_insert_after (&rt_current_module->page_list, &node->list);
}
while(RT_TRUE) for (prev = &rt_current_module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
{ {
b = *prev;
if(!b)
{
if ((b = morepage(nunits)) == RT_NULL) return RT_NULL;
else return rt_module_malloc(size); /* To be improved */
}
if (b->size > nunits) if (b->size > nunits)
{ {
/* split memory */ /* split memory */
...@@ -886,28 +985,50 @@ void *rt_module_malloc(rt_size_t size) ...@@ -886,28 +985,50 @@ void *rt_module_malloc(rt_size_t size)
n->size = b->size - nunits; n->size = b->size - nunits;
b->size = nunits; b->size = nunits;
*prev = n; *prev = n;
break;
//rt_kprintf("rt_module_malloc 0x%x, %d\n",b + 1, size);
rt_sem_release(&mod_sem);
//list_memlist("tetris");
return (void *)(b + 1);
} }
if (b->size == nunits) if (b->size == nunits)
{ {
/* this node fit, remove this node */ /* this node fit, remove this node */
*prev = b->next; *prev = b->next;
break;
rt_kprintf("rt_module_malloc 0x%x, %d\n",b + 1, size);
//list_memlist("tetris");
rt_sem_release(&mod_sem);
return (void *)(b + 1);
}
} }
prev = &(b->next); /* allocate pages from system heap */
npage = (size + sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1)/RT_MM_PAGE_SIZE;
if((up = (struct rt_mem_head*)rt_module_malloc_page(npage)) == RT_NULL) return RT_NULL;
up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
for (prev = &rt_current_module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
{
if (b > up + up->size) break;
} }
return (void *)(b + 1); up->next = b;
*prev = up;
rt_sem_release(&mod_sem);
return rt_module_malloc(size);
} }
/* /*
rt_module_free - insert memory block in free list rt_module_free - free memory block in free list
*/ */
void rt_module_free(rt_module_t module, void *addr) void rt_module_free(rt_module_t module, void *addr)
{ {
struct rt_mem_head *b, *n; struct rt_mem_head *b, *n, *r;
struct rt_mem_head **prev; struct rt_mem_head **prev;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
...@@ -915,6 +1036,10 @@ void rt_module_free(rt_module_t module, void *addr) ...@@ -915,6 +1036,10 @@ void rt_module_free(rt_module_t module, void *addr)
RT_ASSERT(addr); RT_ASSERT(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);
//rt_kprintf("rt_module_free 0x%x\n", addr);
rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
n = (struct rt_mem_head *)addr - 1; n = (struct rt_mem_head *)addr - 1;
prev = (struct rt_mem_head **)&module->mem_list; prev = (struct rt_mem_head **)&module->mem_list;
...@@ -923,13 +1048,41 @@ void rt_module_free(rt_module_t module, void *addr) ...@@ -923,13 +1048,41 @@ void rt_module_free(rt_module_t module, void *addr)
RT_ASSERT(b->size > 0); RT_ASSERT(b->size > 0);
RT_ASSERT(b > n || b + b->size <= n); RT_ASSERT(b > n || b + b->size <= n);
if (b + b->size == n) if (b + b->size == n && ((rt_uint32_t)n % RT_MM_PAGE_SIZE != 0))
{ {
if (b + (b->size += n->size) == b->next) if (b + (b->size + n->size) == b->next)
{ {
b->size += b->next->size; b->size += b->next->size + n->size;
b->next = b->next->next; b->next = b->next->next;
} }
else b->size += n->size;
if((rt_uint32_t)b % RT_MM_PAGE_SIZE == 0)
{
int npage = b->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
if(npage > 0)
{
if((b->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
{
rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
/* split memory */
r = b + nunits;
r->next = b->next;
r->size = b->size - nunits;
*prev = r;
}
else
{
*prev = b->next;
}
rt_module_free_page(b, npage);
}
}
/* unlock */
rt_sem_release(&mod_sem);
////list_memlist("tetris");
return; return;
} }
...@@ -938,7 +1091,34 @@ void rt_module_free(rt_module_t module, void *addr) ...@@ -938,7 +1091,34 @@ void rt_module_free(rt_module_t module, void *addr)
{ {
n->size = b->size + n->size; n->size = b->size + n->size;
n->next = b->next; n->next = b->next;
if((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
{
int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
if(npage > 0)
{
if((n->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
{
rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
/* split memory */
r = n + nunits;
r->next = n->next;
r->size = n->size - nunits;
*prev = r;
}
else *prev = n->next;
rt_module_free_page(n, npage);
}
}
else
{
*prev = n; *prev = n;
}
/* unlock */
rt_sem_release(&mod_sem);
//list_memlist("tetris");
return; return;
} }
...@@ -947,10 +1127,36 @@ void rt_module_free(rt_module_t module, void *addr) ...@@ -947,10 +1127,36 @@ void rt_module_free(rt_module_t module, void *addr)
prev = &(b->next); prev = &(b->next);
} }
if((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
{
int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
if(npage > 0)
{
rt_module_free_page(n, npage);
if(n->size % RT_MM_PAGE_SIZE != 0)
{
rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
/* split memory */
r = n + nunits;
r->next = b;
r->size = n->size - nunits;
*prev = r;
}
else
{
*prev = b;
}
}
}
else
{
n->next = b; n->next = b;
*prev = n; *prev = n;
}
/* free page, TODO */ /* unlock */
rt_sem_release(&mod_sem);
//list_memlist("tetris");
} }
/* /*
...@@ -1027,5 +1233,43 @@ void *rt_module_realloc(void *ptr, rt_size_t size) ...@@ -1027,5 +1233,43 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
} }
} }
} }
#endif
#ifdef RT_USING_FINSH
#include <finsh.h>
void list_memlist(const char* name)
{
rt_module_t module;
struct rt_mem_head **prev;
struct rt_mem_head *b;
module = rt_module_find(name);
if(module == RT_NULL) return RT_NULL;
for (prev = (struct rt_mem_head **)&module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
{
rt_kprintf("0x%x--%d\n", b, b->size * sizeof(struct rt_mem_head));
}
}
FINSH_FUNCTION_EXPORT(list_memlist, list module free memory information)
void list_mempage(const char* name)
{
rt_module_t module;
struct rt_page_info *page;
int i;
module = rt_module_find(name);
if(module == RT_NULL) return RT_NULL;
page = (struct rt_page_info*)module->page_array;
for(i=0; i<module->page_cnt; i++)
{
rt_kprintf("0x%x--%d\n", page[i].page_ptr, page[i].npage);
}
}
FINSH_FUNCTION_EXPORT(list_mempage, list module using memory page information)
#endif
#endif #endif
...@@ -340,7 +340,7 @@ void rt_object_delete(rt_object_t object) ...@@ -340,7 +340,7 @@ void rt_object_delete(rt_object_t object)
/* unlock interrupt */ /* unlock interrupt */
rt_hw_interrupt_enable(temp); rt_hw_interrupt_enable(temp);
#ifdef RT_USING_MODULE #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
if(object->flag & RT_OBJECT_FLAG_MODULE) if(object->flag & RT_OBJECT_FLAG_MODULE)
rt_module_free((rt_module_t)object->module_id, object); rt_module_free((rt_module_t)object->module_id, object);
else else
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#ifdef RT_USING_MODULE #ifdef RT_USING_MODULE
#include <rtm.h> #include <rtm.h>
RTM_EXPORT(rt_object_get_information);
/* /*
* thread interface symbol * thread interface symbol
*/ */
...@@ -147,13 +149,13 @@ RTM_EXPORT(rt_timer_control); ...@@ -147,13 +149,13 @@ RTM_EXPORT(rt_timer_control);
/* /*
* kservice interface symbol * kservice interface symbol
*/ */
RTM_EXPORT(rt_memcpy) RTM_EXPORT(rt_memcpy);
RTM_EXPORT(rt_memcmp) RTM_EXPORT(rt_memcmp);
RTM_EXPORT(rt_memset) RTM_EXPORT(rt_memset);
RTM_EXPORT(rt_kprintf) RTM_EXPORT(rt_kprintf);
RTM_EXPORT(rt_sprintf) RTM_EXPORT(rt_sprintf);
RTM_EXPORT(rt_strstr) RTM_EXPORT(rt_strstr);
RTM_EXPORT(rt_snprintf) RTM_EXPORT(rt_snprintf);
/* /*
* misc interface symbol * misc interface symbol
...@@ -167,43 +169,43 @@ extern int __aeabi_uidivmod; ...@@ -167,43 +169,43 @@ extern int __aeabi_uidivmod;
extern int __aeabi_idivmod; extern int __aeabi_idivmod;
extern int __aeabi_d2iz; extern int __aeabi_d2iz;
RTM_EXPORT(__aeabi_ddiv) RTM_EXPORT(__aeabi_ddiv);
RTM_EXPORT(__aeabi_dmul) RTM_EXPORT(__aeabi_dmul);
RTM_EXPORT(__aeabi_i2d) RTM_EXPORT(__aeabi_i2d);
RTM_EXPORT(__aeabi_uidiv) RTM_EXPORT(__aeabi_uidiv);
RTM_EXPORT(__aeabi_idiv) RTM_EXPORT(__aeabi_idiv);
RTM_EXPORT(__aeabi_idivmod) RTM_EXPORT(__aeabi_idivmod);
RTM_EXPORT(__aeabi_uidivmod) RTM_EXPORT(__aeabi_uidivmod);
RTM_EXPORT(__aeabi_d2iz) RTM_EXPORT(__aeabi_d2iz);
RTM_EXPORT(strcmp) RTM_EXPORT(strcmp);
RTM_EXPORT(strcpy) RTM_EXPORT(strcpy);
RTM_EXPORT(strlen) RTM_EXPORT(strlen);
RTM_EXPORT(rand) RTM_EXPORT(rand);
RTM_EXPORT(memset) RTM_EXPORT(memset);
RTM_EXPORT(memcpy) RTM_EXPORT(memcpy);
#ifdef RT_USING_NEWLIB #ifdef RT_USING_NEWLIB
#include <unistd.h> #include <unistd.h>
RTM_EXPORT(snprintf) RTM_EXPORT(snprintf);
RTM_EXPORT(access) RTM_EXPORT(access);
RTM_EXPORT(__assert_func) RTM_EXPORT(__assert_func);
#include <time.h> #include <time.h>
RTM_EXPORT(localtime) RTM_EXPORT(localtime);
RTM_EXPORT(time) RTM_EXPORT(time);
#endif #endif
#ifdef RT_USING_DFS #ifdef RT_USING_DFS
#include <dfs_posix.h> #include <dfs_posix.h>
RTM_EXPORT(open) RTM_EXPORT(open);
RTM_EXPORT(close) RTM_EXPORT(close);
RTM_EXPORT(read) RTM_EXPORT(read);
RTM_EXPORT(write) RTM_EXPORT(write);
RTM_EXPORT(stat) RTM_EXPORT(stat);
#endif #endif
#ifdef RT_USING_RTGUI #ifdef RT_USING_RTGUI
...@@ -215,45 +217,52 @@ RTM_EXPORT(stat) ...@@ -215,45 +217,52 @@ RTM_EXPORT(stat)
#include <rtgui/widgets/workbench.h> #include <rtgui/widgets/workbench.h>
#include <rtgui/widgets/widget.h> #include <rtgui/widgets/widget.h>
#include <rtgui/widgets/button.h> #include <rtgui/widgets/button.h>
#include <rtgui/widgets/label.h>
#include <rtgui/widgets/list_view.h> #include <rtgui/widgets/list_view.h>
#include <rtgui/widgets/listctrl.h>
#include <rtgui/widgets/filelist_view.h> #include <rtgui/widgets/filelist_view.h>
RTM_EXPORT(rtgui_view_show) RTM_EXPORT(rtgui_label_create);
RTM_EXPORT(rtgui_view_create) RTM_EXPORT(rtgui_view_show);
RTM_EXPORT(rtgui_view_destroy) RTM_EXPORT(rtgui_view_create);
RTM_EXPORT(rtgui_view_event_handler) RTM_EXPORT(rtgui_view_destroy);
RTM_EXPORT(rtgui_dc_draw_text) RTM_EXPORT(rtgui_view_event_handler);
RTM_EXPORT(rtgui_dc_begin_drawing) RTM_EXPORT(rtgui_dc_draw_text);
RTM_EXPORT(rtgui_dc_end_drawing) RTM_EXPORT(rtgui_dc_begin_drawing);
RTM_EXPORT(rtgui_workbench_event_loop) RTM_EXPORT(rtgui_dc_end_drawing);
RTM_EXPORT(rtgui_workbench_event_handler) RTM_EXPORT(rtgui_workbench_event_loop);
RTM_EXPORT(rtgui_workbench_add_view) RTM_EXPORT(rtgui_workbench_event_handler);
RTM_EXPORT(rtgui_workbench_create) RTM_EXPORT(rtgui_workbench_add_view);
RTM_EXPORT(rtgui_workbench_destroy) RTM_EXPORT(rtgui_workbench_create);
RTM_EXPORT(rtgui_workbench_close) RTM_EXPORT(rtgui_workbench_destroy);
RTM_EXPORT(rtgui_timer_start) RTM_EXPORT(rtgui_workbench_close);
RTM_EXPORT(rtgui_timer_create) RTM_EXPORT(rtgui_timer_start);
RTM_EXPORT(rtgui_timer_destory) RTM_EXPORT(rtgui_timer_create);
RTM_EXPORT(rtgui_timer_stop) RTM_EXPORT(rtgui_timer_destory);
RTM_EXPORT(rtgui_thread_register) RTM_EXPORT(rtgui_timer_stop);
RTM_EXPORT(rtgui_thread_deregister) RTM_EXPORT(rtgui_thread_register);
RTM_EXPORT(rtgui_widget_focus) RTM_EXPORT(rtgui_thread_deregister);
RTM_EXPORT(rtgui_widget_set_event_handler) RTM_EXPORT(rtgui_widget_focus);
RTM_EXPORT(rtgui_widget_rect_to_device) RTM_EXPORT(rtgui_widget_set_event_handler);
RTM_EXPORT(rtgui_widget_update) RTM_EXPORT(rtgui_widget_rect_to_device);
RTM_EXPORT(rtgui_widget_get_rect) RTM_EXPORT(rtgui_widget_update);
RTM_EXPORT(rtgui_widget_set_rect) RTM_EXPORT(rtgui_widget_get_rect);
RTM_EXPORT(rtgui_widget_get_toplevel) RTM_EXPORT(rtgui_widget_set_rect);
RTM_EXPORT(rtgui_panel_register) RTM_EXPORT(rtgui_widget_get_toplevel);
RTM_EXPORT(rtgui_panel_set_default_focused) RTM_EXPORT(rtgui_panel_register);
RTM_EXPORT(rtgui_button_create) RTM_EXPORT(rtgui_panel_set_default_focused);
RTM_EXPORT(rtgui_button_destroy) RTM_EXPORT(rtgui_button_create);
RTM_EXPORT(rtgui_button_set_onbutton) RTM_EXPORT(rtgui_button_destroy);
RTM_EXPORT(rtgui_container_add_child) RTM_EXPORT(rtgui_button_set_onbutton);
RTM_EXPORT(rtgui_filelist_view_create) RTM_EXPORT(rtgui_container_add_child);
RTM_EXPORT(rtgui_filelist_view_get_fullpath) RTM_EXPORT(rtgui_filelist_view_create);
RTM_EXPORT(rtgui_list_view_create) RTM_EXPORT(rtgui_filelist_view_get_fullpath);
RTM_EXPORT(rtgui_list_view_destroy) RTM_EXPORT(rtgui_list_view_create);
RTM_EXPORT(rtgui_list_view_destroy);
RTM_EXPORT(rtgui_listctrl_set_onitem);
RTM_EXPORT(rtgui_image_create_from_mem);
RTM_EXPORT(rtgui_listctrl_create);
#endif #endif
#endif #endif
...@@ -256,6 +256,12 @@ void *rt_page_alloc(rt_size_t npages) ...@@ -256,6 +256,12 @@ void *rt_page_alloc(rt_size_t npages)
break; break;
} }
} }
#ifdef RT_MEM_STATS
used_mem += npages * RT_MM_PAGE_SIZE;
if (used_mem > max_mem) max_mem = used_mem;
#endif
/* unlock heap */ /* unlock heap */
rt_sem_release(&heap_sem); rt_sem_release(&heap_sem);
...@@ -275,6 +281,13 @@ void rt_page_free(void *addr, rt_size_t npages) ...@@ -275,6 +281,13 @@ void rt_page_free(void *addr, rt_size_t npages)
/* lock heap */ /* lock heap */
rt_sem_take(&heap_sem, RT_WAITING_FOREVER); rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
/* update memory usage */
#ifdef RT_MEM_STATS
if(rt_page_list != RT_NULL)
used_mem -= npages * RT_MM_PAGE_SIZE;
#endif
for (prev = &rt_page_list; (b = *prev) != RT_NULL; prev = &(b->next)) for (prev = &rt_page_list; (b = *prev) != RT_NULL; prev = &(b->next))
{ {
RT_ASSERT(b->page > 0); RT_ASSERT(b->page > 0);
...@@ -443,52 +456,6 @@ rt_inline int zoneindex(rt_uint32_t *bytes) ...@@ -443,52 +456,6 @@ rt_inline int zoneindex(rt_uint32_t *bytes)
/*@{*/ /*@{*/
/*
* This function will allocate the numbers page with specified size
* in page memory.
*
* @param size the size of memory to be allocated.
* @note this function is used for RT-Thread Application Module
*/
void *rt_malloc_page(rt_size_t npages)
{
void* chunk;
chunk = rt_page_alloc(npages);
if (chunk == RT_NULL) return RT_NULL;
/* update memory usage */
#ifdef RT_MEM_STATS
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
used_mem += npages * RT_MM_PAGE_SIZE;
if (used_mem > max_mem) max_mem = used_mem;
rt_sem_release(&heap_sem);
#endif
return chunk;
}
/*
* This function will release the previously allocated memory page
* by rt_malloc_page.
*
* @param page_ptr the page address to be released.
* @param npages the number of page shall be released.
*
* @note this function is used for RT-Thread Application Module
*/
void rt_free_page(void *page_ptr, rt_size_t npages)
{
rt_page_free(page_ptr, npages);
/* update memory usage */
#ifdef RT_MEM_STATS
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
used_mem -= npages * RT_MM_PAGE_SIZE;
rt_sem_release(&heap_sem);
#endif
}
/** /**
* This function will allocate a block from system heap memory. * This function will allocate a block from system heap memory.
* - If the nbytes is less than zero, * - If the nbytes is less than zero,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册