module.c 23 KB

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