module.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. /*
  2. * File : module.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-01-09 Bernard first version
  13. * 2010-04-09 yi.qiu implement based on first version
  14. * 2010-10-23 yi.qiu implement module memory allocator
  15. */
  16. #include <rtthread.h>
  17. #include <rtm.h>
  18. #include "string.h"
  19. #include "kservice.h"
  20. /* #define RT_MODULE_DEBUG */
  21. #ifdef RT_USING_MODULE
  22. #include "module.h"
  23. #define elf_module ((Elf32_Ehdr *)module_ptr)
  24. #define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
  25. #define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
  26. #define IS_PROG(s) (s.sh_type == SHT_PROGBITS)
  27. #define IS_NOPROG(s) (s.sh_type == SHT_NOBITS)
  28. #define IS_REL(s) (s.sh_type == SHT_REL)
  29. #define IS_RELA(s) (s.sh_type == SHT_RELA)
  30. #define IS_ALLOC(s) (s.sh_flags == SHF_ALLOC)
  31. #define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
  32. #define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
  33. /* module memory allocator */
  34. struct rt_module_page
  35. {
  36. rt_uint8_t *ptr; /* address of memory block */
  37. rt_size_t npage; /* number of pages */
  38. rt_list_t list;
  39. };
  40. static struct rt_module_page *rt_module_page_list;
  41. /* module memory allocator */
  42. struct rt_mem_head
  43. {
  44. rt_size_t size; /* size of memory block */
  45. struct rt_mem_head *next; /* next valid memory block */
  46. };
  47. extern void *rt_malloc_page(rt_size_t npages);
  48. extern void rt_free_page(void *page_ptr, rt_size_t npages);
  49. static rt_module_t rt_current_module = RT_NULL;
  50. rt_list_t rt_module_symbol_list;
  51. struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL, *_rt_module_symtab_end = RT_NULL;
  52. /**
  53. * @ingroup SystemInit
  54. *
  55. * This function will initialize system module
  56. *
  57. */
  58. void rt_system_module_init(void)
  59. {
  60. extern int __rtmsymtab_start;
  61. extern int __rtmsymtab_end;
  62. #ifdef __GNUC__
  63. _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start;
  64. _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end;
  65. #endif
  66. rt_list_init(&rt_module_symbol_list);
  67. /* init current module */
  68. rt_current_module = RT_NULL;
  69. }
  70. static rt_uint32_t rt_module_symbol_find(const rt_uint8_t* sym_str)
  71. {
  72. /* find in kernel symbol table */
  73. struct rt_module_symtab* index;
  74. for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
  75. {
  76. if (rt_strcmp(index->name, (const char*)sym_str) == 0)
  77. return index->addr;
  78. }
  79. return 0;
  80. }
  81. /**
  82. * This function will return self module object
  83. *
  84. * @return the self module object
  85. *
  86. */
  87. rt_module_t rt_module_self (void)
  88. {
  89. /* return current module */
  90. return rt_current_module;
  91. }
  92. /**
  93. * This function will set current module object
  94. *
  95. * @return RT_EOK
  96. */
  97. rt_err_t rt_module_set (rt_module_t module)
  98. {
  99. /* set current module */
  100. rt_current_module = module;
  101. return RT_EOK;
  102. }
  103. static int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf32_Addr sym_val)
  104. {
  105. Elf32_Addr *where, tmp;
  106. Elf32_Sword addend;
  107. where = (Elf32_Addr *)((rt_uint8_t*)module->module_space + rel->r_offset);
  108. switch (ELF32_R_TYPE(rel->r_info))
  109. {
  110. case R_ARM_NONE:
  111. break;
  112. case R_ARM_ABS32:
  113. *where += (Elf32_Addr)sym_val;
  114. #ifdef RT_MODULE_DEBUG
  115. rt_kprintf("R_ARM_ABS32: %x -> %x\n", where, *where);
  116. #endif
  117. break;
  118. case R_ARM_PC24:
  119. case R_ARM_PLT32:
  120. case R_ARM_CALL:
  121. case R_ARM_JUMP24:
  122. addend = *where & 0x00ffffff;
  123. if (addend & 0x00800000)
  124. addend |= 0xff000000;
  125. tmp = sym_val - (Elf32_Addr)where + (addend << 2);
  126. tmp >>= 2;
  127. *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
  128. #ifdef RT_MODULE_DEBUG
  129. rt_kprintf("R_ARM_PC24: %x -> %x\n", where, *where);
  130. #endif
  131. break;
  132. case R_ARM_V4BX:
  133. *where &= 0xf000000f;
  134. *where |= 0x01a0f000;
  135. break;
  136. case R_ARM_GLOB_DAT:
  137. case R_ARM_JUMP_SLOT:
  138. *where = (Elf32_Addr)sym_val;
  139. #ifdef RT_MODULE_DEBUG
  140. rt_kprintf("R_ARM_JUMP_SLOT: 0x%x -> 0x%x 0x%x\n", where, *where, sym_val);
  141. #endif
  142. break;
  143. default:
  144. return -1;
  145. }
  146. return 0;
  147. }
  148. static void rt_module_init_object_container(struct rt_module* module)
  149. {
  150. RT_ASSERT(module != RT_NULL);
  151. /* initialize object container - thread */
  152. rt_list_init(&(module->module_object[RT_Object_Class_Thread].object_list));
  153. module->module_object[RT_Object_Class_Thread].object_size = sizeof(struct rt_thread);
  154. module->module_object[RT_Object_Class_Thread].type = RT_Object_Class_Thread;
  155. #ifdef RT_USING_SEMAPHORE
  156. /* initialize object container - semaphore */
  157. rt_list_init(&(module->module_object[RT_Object_Class_Semaphore].object_list));
  158. module->module_object[RT_Object_Class_Semaphore].object_size = sizeof(struct rt_semaphore);
  159. module->module_object[RT_Object_Class_Semaphore].type = RT_Object_Class_Semaphore;
  160. #endif
  161. #ifdef RT_USING_MUTEX
  162. /* initialize object container - mutex */
  163. rt_list_init(&(module->module_object[RT_Object_Class_Mutex].object_list));
  164. module->module_object[RT_Object_Class_Mutex].object_size = sizeof(struct rt_mutex);
  165. module->module_object[RT_Object_Class_Mutex].type = RT_Object_Class_Mutex;
  166. #endif
  167. #ifdef RT_USING_EVENT
  168. /* initialize object container - event */
  169. rt_list_init(&(module->module_object[RT_Object_Class_Event].object_list));
  170. module->module_object[RT_Object_Class_Event].object_size = sizeof(struct rt_event);
  171. module->module_object[RT_Object_Class_Event].type = RT_Object_Class_Event;
  172. #endif
  173. #ifdef RT_USING_MAILBOX
  174. /* initialize object container - mailbox */
  175. rt_list_init(&(module->module_object[RT_Object_Class_MailBox].object_list));
  176. module->module_object[RT_Object_Class_MailBox].object_size = sizeof(struct rt_mailbox);
  177. module->module_object[RT_Object_Class_MailBox].type = RT_Object_Class_MailBox;
  178. #endif
  179. #ifdef RT_USING_MESSAGEQUEUE
  180. /* initialize object container - message queue */
  181. rt_list_init(&(module->module_object[RT_Object_Class_MessageQueue].object_list));
  182. module->module_object[RT_Object_Class_MessageQueue].object_size = sizeof(struct rt_messagequeue);
  183. module->module_object[RT_Object_Class_MessageQueue].type = RT_Object_Class_MessageQueue;
  184. #endif
  185. #ifdef RT_USING_MEMPOOL
  186. /* initialize object container - memory pool */
  187. rt_list_init(&(module->module_object[RT_Object_Class_MemPool].object_list));
  188. module->module_object[RT_Object_Class_MemPool].object_size = sizeof(struct rt_mempool);
  189. module->module_object[RT_Object_Class_MemPool].type = RT_Object_Class_MemPool;
  190. #endif
  191. #ifdef RT_USING_DEVICE
  192. /* initialize object container - device */
  193. rt_list_init(&(module->module_object[RT_Object_Class_Device].object_list));
  194. module->module_object[RT_Object_Class_Device].object_size = sizeof(struct rt_device);
  195. module->module_object[RT_Object_Class_Device].type = RT_Object_Class_Device;
  196. #endif
  197. /* initialize object container - timer */
  198. rt_list_init(&(module->module_object[RT_Object_Class_Timer].object_list));
  199. module->module_object[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer);
  200. module->module_object[RT_Object_Class_Timer].type = RT_Object_Class_Timer;
  201. }
  202. /**
  203. * This function will load a module from memory and create a thread for it
  204. *
  205. * @param name the name of module, which shall be unique
  206. * @param module_ptr the memory address of module image
  207. *
  208. * @return the module object
  209. *
  210. */
  211. rt_module_t rt_module_load(const rt_uint8_t* name, void* module_ptr)
  212. {
  213. rt_uint32_t index;
  214. rt_uint32_t module_size = 0;
  215. rt_module_t module = RT_NULL;
  216. rt_uint8_t *ptr, *strtab;
  217. rt_bool_t linked = RT_FALSE;
  218. rt_kprintf("rt_module_load: %s\n", name);
  219. /* check ELF header */
  220. if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) == 0)
  221. {
  222. /* rtmlinke finished */
  223. linked = RT_TRUE;
  224. }
  225. else if (rt_memcmp(elf_module->e_ident, ELFMAG, SELFMAG) != 0)
  226. {
  227. rt_kprintf(" module magic error\n");
  228. return RT_NULL;
  229. }
  230. /* check ELF class */
  231. if(elf_module->e_ident[EI_CLASS] != ELFCLASS32)
  232. {
  233. rt_kprintf(" module class error\n");
  234. return RT_NULL;
  235. }
  236. /* get the ELF image size */
  237. for (index = 0; index < elf_module->e_phnum; index++)
  238. {
  239. if(phdr[index].p_type == PT_LOAD)
  240. module_size += phdr[index].p_memsz;
  241. }
  242. if (module_size == 0)
  243. {
  244. rt_kprintf(" module size error\n");
  245. return module;
  246. }
  247. /* allocate module */
  248. module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, (const char*)name);
  249. if (!module) return RT_NULL;
  250. /* allocate module space */
  251. module->module_space = rt_malloc(module_size);
  252. if (module->module_space == RT_NULL)
  253. {
  254. rt_object_delete(&(module->parent));
  255. return RT_NULL;
  256. }
  257. /* zero all space */
  258. ptr = module->module_space;
  259. rt_memset(ptr, 0, module_size);
  260. for (index = 0; index < elf_module->e_phnum; index++)
  261. {
  262. if(phdr[index].p_type == PT_LOAD)
  263. {
  264. rt_memcpy(ptr, (rt_uint8_t*)elf_module + phdr[index].p_offset, phdr[index].p_filesz);
  265. ptr += phdr[index].p_memsz;
  266. }
  267. }
  268. /* set module entry */
  269. module->module_entry = module->module_space + elf_module->e_entry;
  270. /* handle relocation section */
  271. for (index = 0; index < elf_module->e_shnum; index ++)
  272. {
  273. if (IS_REL(shdr[index]))
  274. {
  275. rt_uint32_t i, nr_reloc;
  276. Elf32_Sym *symtab;
  277. Elf32_Rel *rel;
  278. /* get relocate item */
  279. rel = (Elf32_Rel *) ((rt_uint8_t*)module_ptr + shdr[index].sh_offset);
  280. /* locate .rel.plt and .rel.dyn */
  281. symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr + shdr[shdr[index].sh_link].sh_offset);
  282. strtab = (rt_uint8_t*) module_ptr + shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
  283. nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel));
  284. /* relocate every items */
  285. for (i = 0; i < nr_reloc; i ++)
  286. {
  287. Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
  288. #ifdef RT_MODULE_DEBUG
  289. rt_kprintf("relocate symbol %s shndx %d\n", strtab + sym->st_name, sym->st_shndx);
  290. #endif
  291. if(sym->st_shndx != 0)
  292. {
  293. rt_module_arm_relocate(
  294. module,
  295. rel,
  296. (Elf32_Addr)(module->module_space + sym->st_value));
  297. }
  298. else if(linked == RT_FALSE)
  299. {
  300. Elf32_Addr addr;
  301. #ifdef RT_MODULE_DEBUG
  302. rt_kprintf("unresolved relocate symbol: %s\n", strtab + sym->st_name);
  303. #endif
  304. /* need to resolve symbol in kernel symbol table */
  305. addr = rt_module_symbol_find(strtab + sym->st_name);
  306. if (addr == 0)
  307. {
  308. rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name);
  309. rt_object_delete(&(module->parent));
  310. rt_free(module);
  311. return RT_NULL;
  312. }
  313. rt_module_arm_relocate(module, rel, addr);
  314. }
  315. rel ++;
  316. }
  317. }
  318. }
  319. /* construct module symbol table */
  320. for (index = 0; index < elf_module->e_shnum; index ++)
  321. {
  322. rt_uint8_t* shstrab = (rt_uint8_t*) module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
  323. if (rt_strcmp(shstrab + shdr[index].sh_name, ELF_RTMSYMTAB) == 0)
  324. {
  325. module->symtab = (struct rt_module_symtab *)(module->module_space + shdr[index].sh_addr);
  326. module->nsym = shdr[index].sh_size / sizeof(struct rt_module_symtab);
  327. }
  328. }
  329. /* init module object container */
  330. rt_module_init_object_container(module);
  331. /* increase module reference count */
  332. module->nref++;
  333. if(elf_module->e_entry != 0)
  334. {
  335. /* init module page list */
  336. rt_list_init(&module->page_list);
  337. /* init module memory allocator */
  338. module->mem_list = RT_NULL;
  339. /* create mpool for page node */
  340. module->mpool = rt_mp_create(name, 1024, sizeof(struct rt_module_page));
  341. /* create module thread */
  342. module->stack_size = 2048;
  343. module->thread_priority = 90;
  344. module->module_thread = rt_thread_create(name,
  345. module->module_entry, RT_NULL,
  346. module->stack_size,
  347. module->thread_priority, 10);
  348. module->module_thread->module_id = (void*)module;
  349. /* startup module thread */
  350. rt_thread_startup(module->module_thread);
  351. }
  352. else
  353. {
  354. /* without entry point */
  355. module->parent.flag |= RT_MODULE_FLAG_WITHOUTENTRY;
  356. }
  357. return module;
  358. }
  359. #ifdef RT_USING_DFS
  360. #include <dfs_posix.h>
  361. /**
  362. * This function will load a module from a file
  363. *
  364. * @param filename the file name of application module
  365. *
  366. * @return the module object
  367. *
  368. */
  369. rt_module_t rt_module_open(const char* filename)
  370. {
  371. int fd, length;
  372. struct rt_module* module;
  373. struct stat s;
  374. char *buffer, *offset_ptr;;
  375. if (stat(filename, &s) !=0)
  376. {
  377. rt_kprintf("access file failed\n");
  378. return RT_NULL;
  379. }
  380. buffer = (char *)rt_malloc(s.st_size);
  381. if (buffer == RT_NULL)
  382. {
  383. rt_kprintf("out of memory\n");
  384. return RT_NULL;
  385. }
  386. offset_ptr = buffer;
  387. fd = open(filename, O_RDONLY, 0);
  388. if (fd < 0)
  389. {
  390. rt_kprintf("open file failed\n");
  391. rt_free(buffer);
  392. return RT_NULL;
  393. }
  394. do
  395. {
  396. length = read(fd, offset_ptr, 4096);
  397. if (length > 0)
  398. {
  399. offset_ptr += length;
  400. }
  401. }while (length > 0);
  402. /* close fd */
  403. close(fd);
  404. if ((rt_uint32_t)offset_ptr - (rt_uint32_t)buffer != s.st_size)
  405. {
  406. rt_kprintf("check: read file failed\n");
  407. rt_free(buffer);
  408. return RT_NULL;
  409. }
  410. module = rt_module_load(filename, (void *)buffer);
  411. rt_free(buffer);
  412. return module;
  413. }
  414. #if defined(RT_USING_FINSH)
  415. #include <finsh.h>
  416. FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from file);
  417. #endif
  418. #endif
  419. /**
  420. * This function will unload a module from memory and release resources
  421. *
  422. * @param module the module to be unloaded
  423. *
  424. * @return the operation status, RT_EOK on OK; -RT_ERROR on error
  425. *
  426. */
  427. rt_err_t rt_module_unload(rt_module_t module)
  428. {
  429. struct rt_object* object;
  430. struct rt_list_node *list;
  431. rt_kprintf("rt_module_unload: %s\n", module->parent.name);
  432. /* check parameter */
  433. RT_ASSERT(module != RT_NULL);
  434. /* module has entry point */
  435. if(!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
  436. {
  437. /* suspend module main thread */
  438. if(module->module_thread != RT_NULL)
  439. {
  440. if (module->module_thread->stat == RT_THREAD_READY)
  441. rt_thread_suspend(module->module_thread);
  442. }
  443. /* delete threads */
  444. list = &module->module_object[RT_Object_Class_Thread].object_list;
  445. while(list->next != list)
  446. {
  447. object = rt_list_entry(list->next, struct rt_object, list);
  448. if (rt_object_is_systemobject(object) == RT_EOK)
  449. {
  450. /* detach static object */
  451. rt_thread_detach((rt_thread_t)object);
  452. }
  453. else
  454. {
  455. /* delete dynamic object */
  456. rt_thread_delete((rt_thread_t)object);
  457. }
  458. }
  459. #ifdef RT_USING_SEMAPHORE
  460. /* delete semaphores */
  461. list = &module->module_object[RT_Object_Class_Thread].object_list;
  462. while(list->next != list)
  463. {
  464. object = rt_list_entry(list->next, struct rt_object, list);
  465. if (rt_object_is_systemobject(object) == RT_EOK)
  466. {
  467. /* detach static object */
  468. rt_sem_detach((rt_sem_t)object);
  469. }
  470. else
  471. {
  472. /* delete dynamic object */
  473. rt_sem_delete((rt_sem_t)object);
  474. }
  475. }
  476. #endif
  477. #ifdef RT_USING_MUTEX
  478. /* delete mutexs*/
  479. list = &module->module_object[RT_Object_Class_Mutex].object_list;
  480. while(list->next != list)
  481. {
  482. object = rt_list_entry(list->next, struct rt_object, list);
  483. if (rt_object_is_systemobject(object) == RT_EOK)
  484. {
  485. /* detach static object */
  486. rt_mutex_detach((rt_mutex_t)object);
  487. }
  488. else
  489. {
  490. /* delete dynamic object */
  491. rt_mutex_delete((rt_mutex_t)object);
  492. }
  493. }
  494. #endif
  495. #ifdef RT_USING_EVENT
  496. /* delete mailboxs */
  497. list = &module->module_object[RT_Object_Class_Event].object_list;
  498. while(list->next != list)
  499. {
  500. object = rt_list_entry(list->next, struct rt_object, list);
  501. if (rt_object_is_systemobject(object) == RT_EOK)
  502. {
  503. /* detach static object */
  504. rt_event_detach((rt_event_t)object);
  505. }
  506. else
  507. {
  508. /* delete dynamic object */
  509. rt_event_delete((rt_event_t)object);
  510. }
  511. }
  512. #endif
  513. #ifdef RT_USING_MAILBOX
  514. /* delete mailboxs */
  515. list = &module->module_object[RT_Object_Class_MailBox].object_list;
  516. while(list->next != list)
  517. {
  518. object = rt_list_entry(list->next, struct rt_object, list);
  519. if (rt_object_is_systemobject(object) == RT_EOK)
  520. {
  521. /* detach static object */
  522. rt_mb_detach((rt_mailbox_t)object);
  523. }
  524. else
  525. {
  526. /* delete dynamic object */
  527. rt_mb_delete((rt_mailbox_t)object);
  528. }
  529. }
  530. #endif
  531. #ifdef RT_USING_MESSAGEQUEUE
  532. /* delete msgqueues */
  533. list = &module->module_object[RT_Object_Class_MessageQueue].object_list;
  534. while(list->next != list)
  535. {
  536. object = rt_list_entry(list->next, struct rt_object, list);
  537. if (rt_object_is_systemobject(object) == RT_EOK)
  538. {
  539. /* detach static object */
  540. rt_mq_detach((rt_mq_t)object);
  541. }
  542. else
  543. {
  544. /* delete dynamic object */
  545. rt_mq_delete((rt_mq_t)object);
  546. }
  547. }
  548. #endif
  549. #ifdef RT_USING_MEMPOOL
  550. /* delete mempools */
  551. list = &module->module_object[RT_Object_Class_MemPool].object_list;
  552. while(list->next != list)
  553. {
  554. object = rt_list_entry(list->next, struct rt_object, list);
  555. if (rt_object_is_systemobject(object) == RT_EOK)
  556. {
  557. /* detach static object */
  558. rt_mp_detach((rt_mp_t)object);
  559. }
  560. else
  561. {
  562. /* delete dynamic object */
  563. rt_mp_delete((rt_mp_t)object);
  564. }
  565. }
  566. #endif
  567. #ifdef RT_USING_DEVICE
  568. /* delete devices */
  569. list = &module->module_object[RT_Object_Class_Device].object_list;
  570. while(list->next != list)
  571. {
  572. object = rt_list_entry(list->next, struct rt_object, list);
  573. rt_device_unregister((rt_device_t)object);
  574. }
  575. #endif
  576. /* delete timers */
  577. list = &module->module_object[RT_Object_Class_Timer].object_list;
  578. while(list->next != list)
  579. {
  580. object = rt_list_entry(list->next, struct rt_object, list);
  581. if (rt_object_is_systemobject(object) == RT_EOK)
  582. {
  583. /* detach static object */
  584. rt_timer_detach((rt_timer_t)object);
  585. }
  586. else
  587. {
  588. /* delete dynamic object */
  589. rt_timer_delete((rt_timer_t)object);
  590. }
  591. }
  592. /* free module pages */
  593. list = &module->page_list;
  594. while(list->next != list)
  595. {
  596. struct rt_module_page* page;
  597. /* free page */
  598. page = rt_list_entry(list->next, struct rt_module_page, list);
  599. rt_free_page(page->ptr, page->npage);
  600. rt_list_remove(list->next);
  601. }
  602. /* delete mpool */
  603. if(module->mpool) rt_mp_delete(module->mpool);
  604. }
  605. /* release module space memory */
  606. rt_free(module->module_space);
  607. /* delete module object */
  608. rt_object_delete((rt_object_t)module);
  609. return RT_EOK;
  610. }
  611. /**
  612. * This function will find the specified module.
  613. *
  614. * @param name the name of module finding
  615. *
  616. * @return the module
  617. */
  618. rt_module_t rt_module_find(const char* name)
  619. {
  620. struct rt_object_information *information;
  621. struct rt_object* object;
  622. struct rt_list_node* node;
  623. extern struct rt_object_information rt_object_container[];
  624. /* enter critical */
  625. rt_enter_critical();
  626. /* try to find device object */
  627. information = &rt_object_container[RT_Object_Class_Module];
  628. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  629. {
  630. object = rt_list_entry(node, struct rt_object, list);
  631. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  632. {
  633. /* leave critical */
  634. rt_exit_critical();
  635. return (rt_module_t)object;
  636. }
  637. }
  638. /* leave critical */
  639. rt_exit_critical();
  640. /* not found */
  641. return RT_NULL;
  642. }
  643. static struct rt_mem_head *morepage(rt_size_t nu)
  644. {
  645. rt_uint8_t *cp;
  646. rt_uint32_t npage;
  647. struct rt_mem_head *up;
  648. struct rt_module_page *node;
  649. RT_ASSERT (nu != 0);
  650. /* allocate pages from system heap */
  651. npage = (nu * sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1)/RT_MM_PAGE_SIZE;
  652. cp = rt_malloc_page(npage);
  653. if(!cp) return RT_NULL;
  654. /* allocate page list node from mpool */
  655. node = rt_mp_alloc(rt_current_module->mpool, RT_WAITING_FOREVER);
  656. node->ptr = cp;
  657. node->npage = npage;
  658. /* insert page list node to moudle's page list */
  659. rt_list_insert_after (&rt_current_module->page_list, &node->list);
  660. up = (struct rt_mem_head *) cp;
  661. up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
  662. rt_module_free(rt_current_module, (void *)(up+1));
  663. return up;
  664. }
  665. /*
  666. rt_module_malloc - allocate memory block in free list
  667. */
  668. void *rt_module_malloc(rt_size_t size)
  669. {
  670. struct rt_mem_head *b, *n;
  671. struct rt_mem_head **prev;
  672. rt_size_t nunits;
  673. nunits = (size + sizeof(struct rt_mem_head) -1)/sizeof(struct rt_mem_head) + 1;
  674. RT_ASSERT(size != 0);
  675. RT_ASSERT(nunits != 0);
  676. prev = (struct rt_mem_head **)&rt_current_module->mem_list;
  677. /* if size can be divided by page, allocate page directly */
  678. if(size % RT_MM_PAGE_SIZE == 0)
  679. {
  680. rt_uint8_t *cp;
  681. struct rt_module_page *node;
  682. rt_uint32_t npage = size / RT_MM_PAGE_SIZE;
  683. /* allocate pages from system heap */
  684. cp = rt_malloc_page(npage);
  685. if(!cp) return RT_NULL;
  686. /* allocate page list node from mpool */
  687. node = rt_mp_alloc(rt_current_module->mpool, RT_WAITING_FOREVER);
  688. node->ptr = cp;
  689. node->npage = npage;
  690. /* insert page list node to moudle's page list */
  691. rt_list_insert_after (&rt_current_module->page_list, &node->list);
  692. }
  693. while(RT_TRUE)
  694. {
  695. b = *prev;
  696. if(!b)
  697. {
  698. if ((b = morepage(nunits)) == RT_NULL) return RT_NULL;
  699. else return rt_module_malloc(size); /* To be improved */
  700. }
  701. if (b->size > nunits)
  702. {
  703. /* split memory */
  704. n = b + nunits;
  705. n->next = b->next;
  706. n->size = b->size - nunits;
  707. b->size = nunits;
  708. *prev = n;
  709. break;
  710. }
  711. if (b->size == nunits)
  712. {
  713. /* this node fit, remove this node */
  714. *prev = b->next;
  715. break;
  716. }
  717. prev = &(b->next);
  718. }
  719. return (void *)(b + 1);
  720. }
  721. /*
  722. rt_module_free - insert memory block in free list
  723. */
  724. void rt_module_free(rt_module_t module, void *addr)
  725. {
  726. struct rt_mem_head *b, *n;
  727. struct rt_mem_head **prev;
  728. RT_ASSERT(addr);
  729. RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) -1)) == 0);
  730. n = (struct rt_mem_head *)addr - 1;
  731. prev = (struct rt_mem_head **)&module->mem_list;
  732. while ((b = *prev) != RT_NULL)
  733. {
  734. RT_ASSERT(b->size > 0);
  735. RT_ASSERT(b > n || b + b->size <= n);
  736. if (b + b->size == n)
  737. {
  738. if (b + (b->size += n->size) == b->next)
  739. {
  740. b->size += b->next->size;
  741. b->next = b->next->next;
  742. }
  743. return;
  744. }
  745. if (b == n + n->size)
  746. {
  747. n->size = b->size + n->size;
  748. n->next = b->next;
  749. *prev = n;
  750. return;
  751. }
  752. if (b > n + n->size) break;
  753. prev = &(b->next);
  754. }
  755. n->next = b;
  756. *prev = n;
  757. /* free page, TODO */
  758. }
  759. /*
  760. rt_module_realloc - realloc memory block in free list
  761. */
  762. void *rt_module_realloc(void *ptr, rt_size_t size)
  763. {
  764. struct rt_mem_head *b, *p, *prev, *tmpp;
  765. rt_size_t nunits;
  766. if (!ptr) return rt_module_malloc(size);
  767. if (size == 0)
  768. {
  769. rt_module_free(rt_current_module, ptr);
  770. return RT_NULL;
  771. }
  772. nunits = (size + sizeof(struct rt_mem_head) - 1) / sizeof(struct rt_mem_head) + 1;
  773. b = (struct rt_mem_head *)ptr - 1;
  774. if (nunits <= b->size)
  775. {
  776. /* new size is smaller or equal then before */
  777. if (nunits == b->size) return ptr;
  778. else
  779. {
  780. p = b + nunits;
  781. p->size = b->size - nunits;
  782. b->size = nunits;
  783. rt_module_free(rt_current_module, (void *)(p + 1));
  784. return (void *)(b + 1);
  785. }
  786. }
  787. else
  788. {
  789. /* more space then required */
  790. prev = (struct rt_mem_head *)rt_current_module->mem_list;
  791. for (p = prev->next; p != (b->size + b) && p != RT_NULL; prev = p, p = p->next) break;
  792. /* available block after ap in freelist */
  793. if (p != RT_NULL && (p->size >= (nunits - (b->size))) && p == (b + b->size))
  794. {
  795. /* perfect match */
  796. if (p->size == (nunits - (b->size)))
  797. {
  798. b->size = nunits;
  799. prev->next = p->next;
  800. }
  801. else /* more space then required, split block*/
  802. {
  803. /* pointer to old header */
  804. tmpp = p;
  805. p = b + nunits;
  806. /* restoring old pointer */
  807. p->next = tmpp->next;
  808. /* new size for p */
  809. p->size = tmpp->size + b->size - nunits;
  810. b->size = nunits;
  811. prev->next = p;
  812. }
  813. rt_current_module->mem_list = (void *)prev;
  814. return (void *) (b + 1);
  815. }
  816. else /* allocate new memory and copy old data */
  817. {
  818. if ((p = rt_module_malloc(size)) == RT_NULL) return RT_NULL;
  819. rt_memmove(p, (b+1), ((b->size) * sizeof(struct rt_mem_head)));
  820. rt_module_free(rt_current_module, (void *)(b + 1));
  821. return (void *) (p);
  822. }
  823. }
  824. }
  825. #endif