自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

鴻蒙輕內(nèi)核M核源碼分析系列之Newlib C

開發(fā) 前端
LiteOS-M內(nèi)核LibC實現(xiàn)有2種,可以根據(jù)需求進行二選一,分別是musl libC和newlibc。本文先學(xué)習(xí)下Newlib C的實現(xiàn)代碼。文中所涉及的源碼,均可以在開源站點。

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

LiteOS-M內(nèi)核LibC實現(xiàn)有2種,可以根據(jù)需求進行二選一,分別是musl libC和newlibc。本文先學(xué)習(xí)下Newlib C的實現(xiàn)代碼。文中所涉及的源碼,均可以在開源站點https://gitee.com/openharmony/kernel_liteos_m 獲取。

使用Musl C庫的時候,內(nèi)核提供了基于LOS_XXX適配實現(xiàn)pthread、mqeue、fs、semaphore、time等模塊的posix接口(//kernel/liteos_m/kal/posix)。內(nèi)核提供的posix接口與musl中的標(biāo)準(zhǔn)C庫接口共同組成LiteOS-M的LibC。編譯時使用arm-none-eabi-gcc,但只使用其工具鏈的編譯功能,通過加上-nostdinc與-nostdlib強制使用我們自己改造后的musl-C。

社區(qū)及三方廠商開發(fā)多使用公版工具鏈arm-none-eabi-gcc加上私有定制優(yōu)化進行編譯,LiteOS-M內(nèi)核也支持公版arm-none-eabi-gcc C庫編譯內(nèi)核運行。newlib是小型C庫,針對posix接口涉及系統(tǒng)調(diào)用的部分,newlib提供一些需要系統(tǒng)適配的鉤子函數(shù),例如_exit(),_open(),_close(),_gettimeofday()等,操作系統(tǒng)適配這些鉤子,就可以使用公版newlib工具鏈編譯運行程序。

1、Newlib C文件系統(tǒng)

在使用Newlib C并且使能支持POSIX FS API時(可以在kernel\liteos-m\目錄下,執(zhí)行make meuconfig彈出配置界面,路徑為Compat-Choose libc implementation),如下圖所示??梢允褂梦募al\libc\newlib\porting\src\fs.c中定義的文件系統(tǒng)操作接口。這些是標(biāo)準(zhǔn)的POSIX接口,如果想了解POSIX用法,可以在linux平臺輸入 man -a 函數(shù)名稱,比如man -a opendir來打開函數(shù)的手冊。

鴻蒙輕內(nèi)核M核源碼分析系列二十 Newlib C-鴻蒙HarmonyOS技術(shù)社區(qū)

1.1 函數(shù)mount、umount和umount2

這些函數(shù)的用法,函數(shù)實現(xiàn)和musl c部分一致。

  1. int mount(const char *source, const char *target, 
  2.           const char *filesystemtype, unsigned long mountflags, 
  3.           const void *data) 
  4.     return LOS_FsMount(source, target, filesystemtype, mountflags, data); 
  5.  
  6. int umount(const char *target) 
  7.     return LOS_FsUmount(target); 
  8.  
  9. int umount2(const char *target, int flag) 
  10.     return LOS_FsUmount2(target, flag); 

1.2 文件操作接口

以下劃線開頭的函數(shù)實現(xiàn)是newlib c的鉤子函數(shù)實現(xiàn)。有關(guān)newlib的鉤子函數(shù)調(diào)用過程下文專門分析下。

  1. int _open(const char *path, int oflag, ...) 
  2.     va_list vaList; 
  3.     va_start(vaList, oflag); 
  4.     int ret; 
  5.     ret = LOS_Open(path, oflag); 
  6.     va_end(vaList); 
  7.     return ret; 
  8.  
  9. int _close(int fd) 
  10.     return LOS_Close(fd); 
  11.  
  12. ssize_t _read(int fd, void *buf, size_t nbyte) 
  13.     return LOS_Read(fd, buf, nbyte); 
  14.  
  15. ssize_t _write(int fd, const void *buf, size_t nbyte) 
  16.     return LOS_Write(fd, buf, nbyte); 
  17.  
  18. off_t _lseek(int fd, off_t offset, int whence) 
  19.     return LOS_Lseek(fd, offset, whence); 
  20.  
  21. int _unlink(const char *path) 
  22.     return LOS_Unlink(path); 
  23.  
  24. int _fstat(int fd, struct stat *buf) 
  25.     return LOS_Fstat(fd, buf); 
  26.  
  27. int _stat(const char *path, struct stat *buf) 
  28.     return LOS_Stat(path, buf); 
  29.  
  30. int fsync(int fd) 
  31.     return LOS_Fsync(fd); 
  32.  
  33. int mkdir(const char *path, mode_t mode) 
  34.     return LOS_Mkdir(path, mode); 
  35.  
  36. DIR *opendir(const char *dirName) 
  37.     return LOS_Opendir(dirName); 
  38.  
  39. struct dirent *readdir(DIR *dir) 
  40.     return LOS_Readdir(dir); 
  41.  
  42. int closedir(DIR *dir) 
  43.     return LOS_Closedir(dir); 
  44.  
  45. int rmdir(const char *path) 
  46.     return LOS_Unlink(path); 
  47.  
  48. int rename(const char *oldName, const char *newName) 
  49.     return LOS_Rename(oldName, newName); 
  50.  
  51. int statfs(const char *path, struct statfs *buf) 
  52.     return LOS_Statfs(path, buf); 
  53.  
  54. int ftruncate(int fd, off_t length) 
  55.     return LOS_Ftruncate(fd, length); 

在newlib沒有使能使能支持POSIX FS API時時,需要提供這些鉤子函數(shù)的空的實現(xiàn),返回-1錯誤碼即可。

  1. int _open(const char *path, int oflag, ...) 
  2.     return -1; 
  3.  
  4. int _close(int fd) 
  5.     return -1; 
  6.  
  7. ssize_t _read(int fd, void *buf, size_t nbyte) 
  8.     return -1; 
  9.  
  10. ssize_t _write(int fd, const void *buf, size_t nbyte) 
  11.     return -1; 
  12.  
  13. off_t _lseek(int fd, off_t offset, int whence) 
  14.     return -1; 
  15.  
  16. int _unlink(const char *path) 
  17.     return -1; 
  18.  
  19. int _fstat(int fd, struct stat *buf) 
  20.     return -1; 
  21.  
  22. int _stat(const char *path, struct stat *buf) 
  23.     return -1; 

2、Newlib C內(nèi)存分配釋放

newlibc 的malloc適配參考The Red Hat newlib C Library-malloc。實現(xiàn)malloc適配有以下兩種方法:

  • 實現(xiàn) _sbrk_r 函數(shù)。這種方法中,內(nèi)存分配函數(shù)使用newlib中的。
  • 實現(xiàn) _malloc_r, _realloc_r, _free_r, _memalign_r, _malloc_usable_size_r等。這種方法中,內(nèi)存分配函數(shù)可以使用內(nèi)核的。

為了方便地根據(jù)業(yè)務(wù)進行內(nèi)存分配算法調(diào)優(yōu)和問題定位,推薦選擇后者。內(nèi)核的內(nèi)存函數(shù)定義在文件kal\libc\newlib\porting\src\malloc.c中。源碼片段如下,代碼實現(xiàn)比較簡單,不再分析源碼。

  1. ...... 
  2. void __wrap__free_r(struct _reent *reent, void *aptr) 
  3.     if (aptr == NULL) { 
  4.         return
  5.     } 
  6.  
  7.     LOS_MemFree(OS_SYS_MEM_ADDR, aptr); 
  8.  
  9. size_t __wrap__malloc_usable_size_r(struct _reent *reent, void *aptr) 
  10.     return 0; 
  11.  
  12. void *__wrap__malloc_r(struct _reent *reent, size_t nbytes) 
  13.     if (nbytes == 0) { 
  14.         return NULL
  15.     } 
  16.  
  17.     return LOS_MemAlloc(OS_SYS_MEM_ADDR, nbytes); 
  18.  
  19. void *__wrap__memalign_r(struct _reent *reent, size_t align, size_t nbytes) 
  20.     if (nbytes == 0) { 
  21.         return NULL
  22.     } 
  23.  
  24.     return LOS_MemAllocAlign(OS_SYS_MEM_ADDR, nbytes, align); 
  25. ...... 

可能已經(jīng)注意到函數(shù)命名由__wrap_加上鉤子函數(shù)名稱兩部分組成。這是因為newlib中已經(jīng)存在這些函數(shù)的符號,因此需要用到gcc的wrap的鏈接選項替換這些函數(shù)符號為內(nèi)核的實現(xiàn),在設(shè)備開發(fā)板的配置文件中,比如//device/board/fnlink/v200zr/liteos_m/config.gni,新增這些函數(shù)的wrap鏈接選項,示例如下:

  1. board_ld_flags += [ 
  2.      "-Wl,--wrap=_malloc_r"
  3.      "-Wl,--wrap=_realloc_r"
  4.      "-Wl,--wrap=_free_r"
  5.      "-Wl,--wrap=_memalign_r"
  6.      "-Wl,--wrap=_malloc_usable_size_r"

3、Newlib鉤子函數(shù)介紹

以open函數(shù)的鉤子函數(shù)_open為例來介紹newlib的鉤子函數(shù)的調(diào)用過程。open()函數(shù)實現(xiàn)在newlib-cygwin\newlib\libc\syscalls\sysopen.c中,該函數(shù)會進一步調(diào)用函數(shù)_open_r,這是個可重入函數(shù)Reentrant Function,支持在多線程中運行。

  1. int 
  2. open (const char *file, 
  3.         int flags, ...) 
  4.   va_list ap; 
  5.   int ret; 
  6.  
  7.   va_start (ap, flags); 
  8.   ret = _open_r (_REENT, file, flags, va_arg (ap, int)); 
  9.   va_end (ap); 
  10.   return ret; 

 所有的可重入函數(shù)定義在文件夾newlib-cygwin\newlib\libc\reent,函數(shù)_open_r定義在該文件夾的文件newlib-cygwin\newlib\libc\reent\openr.c里。函數(shù)代碼如下:

  1. int 
  2. _open_r (struct _reent *ptr, 
  3.      const char *file, 
  4.      int flags, 
  5.      int mode) 
  6.   int ret; 
  7.  
  8.   errno = 0; 
  9.   if ((ret = _open (file, flags, mode)) == -1 && errno != 0) 
  10.     ptr->_errno = errno; 
  11.   return ret; 

 函數(shù)_open_r如上述代碼所示,會進一步調(diào)用函數(shù)_open,該函數(shù),以arm硬件平臺為例,實現(xiàn)在newlib-cygwin\libgloss\arm\syscalls.c文件里。newlib目錄是和硬件平臺無關(guān)的痛毆他那個功能實現(xiàn),libloss目錄是底層的驅(qū)動實現(xiàn),以各個硬件平臺為文件夾進行組織。在特定硬件平臺的目錄下的syscalls.c文件里面實現(xiàn)了newlib需要的各個樁函數(shù):

  1. /* Forward prototypes.  */ 
  2. int _system     (const char *); 
  3. int _rename     (const char *, const char *); 
  4. int _isatty     (int); 
  5. clock_t _times      (struct tms *); 
  6. int _gettimeofday   (struct timeval *, void *); 
  7. int _unlink     (const char *); 
  8. int _link       (const char *, const char *); 
  9. int _stat       (const char *, struct stat *); 
  10. int _fstat      (int, struct stat *); 
  11. int _swistat    (int fd, struct stat * st); 
  12. void *  _sbrk       (ptrdiff_t); 
  13. pid_t   _getpid     (void); 
  14. int _close      (int); 
  15. clock_t _clock      (void); 
  16. int _swiclose   (int); 
  17. int _open       (const char *, int, ...); 
  18. int _swiopen    (const char *, int); 
  19. int _write      (int, const void *, size_t); 
  20. int _swiwrite   (int, const void *, size_t); 
  21. _off_t  _lseek      (int, _off_t, int); 
  22. _off_t  _swilseek   (int, _off_t, int); 
  23. int _read       (int, void *, size_t); 
  24. int _swiread    (int, void *, size_t); 
  25. void    initialise_monitor_handles (void); 

對于上文提到的函數(shù)_open,源碼如下。后續(xù)不再繼續(xù)分析了,LiteOS-M內(nèi)核會提供這些鉤子函數(shù)的實現(xiàn)。

  1. int 
  2. _open (const char * path, int flags, ...) 
  3.   return _swiopen (path, flags); 

小結(jié)

本文學(xué)習(xí)了LiteOS-M內(nèi)核Newlib C的實現(xiàn),特別是文件系統(tǒng)和內(nèi)存分配釋放部分,最后介紹了Newlib鉤子函數(shù)。

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2022-01-10 15:31:44

鴻蒙HarmonyOS應(yīng)用

2021-06-04 09:57:49

鴻蒙HarmonyOS應(yīng)用

2022-03-11 20:23:14

鴻蒙源碼分析進程管理

2021-05-17 09:28:59

鴻蒙HarmonyOS應(yīng)用

2021-06-04 14:15:10

鴻蒙HarmonyOS應(yīng)用

2021-05-08 15:14:50

鴻蒙HarmonyOS應(yīng)用

2021-05-25 09:28:34

鴻蒙HarmonyOS應(yīng)用

2021-10-20 16:08:57

鴻蒙HarmonyOS應(yīng)用

2022-01-14 08:39:47

鴻蒙HarmonyOS應(yīng)用

2021-05-31 20:30:55

鴻蒙HarmonyOS應(yīng)用

2022-03-03 18:28:28

Harmony進程任務(wù)管理模塊

2022-04-13 11:02:12

鴻蒙事件模塊事件Event

2021-07-06 09:45:03

鴻蒙HarmonyOS應(yīng)用

2021-09-22 14:36:32

鴻蒙HarmonyOS應(yīng)用

2021-06-09 09:48:01

鴻蒙HarmonyOS應(yīng)用

2021-05-21 09:25:11

鴻蒙HarmonyOS應(yīng)用

2021-05-27 09:43:56

鴻蒙HarmonyOS應(yīng)用

2021-05-11 09:54:55

鴻蒙HarmonyOS應(yīng)用

2021-12-01 15:59:22

鴻蒙HarmonyOS應(yīng)用

2021-05-10 15:05:56

鴻蒙HarmonyOS應(yīng)用
點贊
收藏

51CTO技術(shù)棧公眾號