织梦网站怎么做伪静态页面,新余教育网站建设,游戏网站建设方案书,企业标准网上备案网站LiteOS-M内核LibC实现有2种#xff0c;可以根据需求进行二选一#xff0c;分别是musl libC和newlibc。本文先学习下Newlib C的实现代码。文中所涉及的源码#xff0c;均可以在开源站点https://gitee.com/openharmony/kernel_liteos_m 获取。
使用Musl C库的时候#xff0c…LiteOS-M内核LibC实现有2种可以根据需求进行二选一分别是musl libC和newlibc。本文先学习下Newlib C的实现代码。文中所涉及的源码均可以在开源站点https://gitee.com/openharmony/kernel_liteos_m 获取。
使用Musl C库的时候内核提供了基于LOS_XXX适配实现pthread、mqeue、fs、semaphore、time等模块的posix接口//kernel/liteos_m/kal/posix。内核提供的posix接口与musl中的标准C库接口共同组成LiteOS-M的LibC。编译时使用arm-none-eabi-gcc但只使用其工具链的编译功能通过加上-nostdinc与-nostdlib强制使用我们自己改造后的musl-C。
社区及三方厂商开发多使用公版工具链arm-none-eabi-gcc加上私有定制优化进行编译LiteOS-M内核也支持公版arm-none-eabi-gcc C库编译内核运行。newlib是小型C库针对posix接口涉及系统调用的部分newlib提供一些需要系统适配的钩子函数例如_exit()_open()_close()_gettimeofday()等操作系统适配这些钩子就可以使用公版newlib工具链编译运行程序。 1、Newlib C文件系统
在使用Newlib C并且使能支持POSIX FS API时可以在kernel\liteos-m\目录下执行make meuconfig弹出配置界面路径为Compat-Choose libc implementation如下图所示。可以使用文件kal\libc\newlib\porting\src\fs.c中定义的文件系统操作接口。这些是标准的POSIX接口如果想了解POSIX用法可以在linux平台输入 man -a 函数名称比如man -a opendir来打开函数的手册。 1.1 函数mount、umount和umount2
这些函数的用法函数实现和musl c部分一致。
int mount(const char *source, const char *target,const char *filesystemtype, unsigned long mountflags,const void *data)
{return LOS_FsMount(source, target, filesystemtype, mountflags, data);
}int umount(const char *target)
{return LOS_FsUmount(target);
}int umount2(const char *target, int flag)
{return LOS_FsUmount2(target, flag);
}1.2 文件操作接口
以下划线开头的函数实现是newlib c的钩子函数实现。有关newlib的钩子函数调用过程下文专门分析下。
int _open(const char *path, int oflag, ...)
{va_list vaList;va_start(vaList, oflag);int ret;ret LOS_Open(path, oflag);va_end(vaList);return ret;
}int _close(int fd)
{return LOS_Close(fd);
}ssize_t _read(int fd, void *buf, size_t nbyte)
{return LOS_Read(fd, buf, nbyte);
}ssize_t _write(int fd, const void *buf, size_t nbyte)
{return LOS_Write(fd, buf, nbyte);
}off_t _lseek(int fd, off_t offset, int whence)
{return LOS_Lseek(fd, offset, whence);
}int _unlink(const char *path)
{return LOS_Unlink(path);
}int _fstat(int fd, struct stat *buf)
{return LOS_Fstat(fd, buf);
}int _stat(const char *path, struct stat *buf)
{return LOS_Stat(path, buf);
}int fsync(int fd)
{return LOS_Fsync(fd);
}int mkdir(const char *path, mode_t mode)
{return LOS_Mkdir(path, mode);
}DIR *opendir(const char *dirName)
{return LOS_Opendir(dirName);
}struct dirent *readdir(DIR *dir)
{return LOS_Readdir(dir);
}int closedir(DIR *dir)
{return LOS_Closedir(dir);
}int rmdir(const char *path)
{return LOS_Unlink(path);
}int rename(const char *oldName, const char *newName)
{return LOS_Rename(oldName, newName);
}int statfs(const char *path, struct statfs *buf)
{return LOS_Statfs(path, buf);
}int ftruncate(int fd, off_t length)
{return LOS_Ftruncate(fd, length);
}在newlib没有使能使能支持POSIX FS API时时需要提供这些钩子函数的空的实现返回-1错误码即可。
int _open(const char *path, int oflag, ...)
{return -1;
}int _close(int fd)
{return -1;
}ssize_t _read(int fd, void *buf, size_t nbyte)
{return -1;
}ssize_t _write(int fd, const void *buf, size_t nbyte)
{return -1;
}off_t _lseek(int fd, off_t offset, int whence)
{return -1;
}int _unlink(const char *path)
{return -1;
}int _fstat(int fd, struct stat *buf)
{return -1;
}int _stat(const char *path, struct stat *buf)
{return -1;
}2、Newlib C内存分配释放
newlibc 的malloc适配参考The Red Hat newlib C Library-malloc。实现malloc适配有以下两种方法 实现 _sbrk_r 函数。这种方法中内存分配函数使用newlib中的。 实现 _malloc_r, _realloc_r, _free_r, _memalign_r, _malloc_usable_size_r等。这种方法中内存分配函数可以使用内核的。
为了方便地根据业务进行内存分配算法调优和问题定位推荐选择后者。内核的内存函数定义在文件kal\libc\newlib\porting\src\malloc.c中。源码片段如下代码实现比较简单不再分析源码。
......
void __wrap__free_r(struct _reent *reent, void *aptr)
{if (aptr NULL) {return;}LOS_MemFree(OS_SYS_MEM_ADDR, aptr);
}size_t __wrap__malloc_usable_size_r(struct _reent *reent, void *aptr)
{return 0;
}void *__wrap__malloc_r(struct _reent *reent, size_t nbytes)
{if (nbytes 0) {return NULL;}return LOS_MemAlloc(OS_SYS_MEM_ADDR, nbytes);
}void *__wrap__memalign_r(struct _reent *reent, size_t align, size_t nbytes)
{if (nbytes 0) {return NULL;}return LOS_MemAllocAlign(OS_SYS_MEM_ADDR, nbytes, align);
}
......可能已经注意到函数命名由__wrap_加上钩子函数名称两部分组成。这是因为newlib中已经存在这些函数的符号因此需要用到gcc的wrap的链接选项替换这些函数符号为内核的实现在设备开发板的配置文件中比如//device/board/fnlink/v200zr/liteos_m/config.gni新增这些函数的wrap链接选项示例如下
board_ld_flags [-Wl,--wrap_malloc_r,-Wl,--wrap_realloc_r,-Wl,--wrap_free_r,-Wl,--wrap_memalign_r,-Wl,--wrap_malloc_usable_size_r,
]3、Newlib钩子函数介绍
以open函数的钩子函数_open为例来介绍newlib的钩子函数的调用过程。open()函数实现在newlib-cygwin\newlib\libc\syscalls\sysopen.c中该函数会进一步调用函数_open_r这是个可重入函数Reentrant Function支持在多线程中运行。
int
open (const char *file,int flags, ...)
{va_list ap;int ret;va_start (ap, flags);ret _open_r (_REENT, file, flags, va_arg (ap, int));va_end (ap);return ret;
}所有的可重入函数定义在文件夹newlib-cygwin\newlib\libc\reent函数_open_r定义在该文件夹的文件newlib-cygwin\newlib\libc\reent\openr.c里。函数代码如下
int
_open_r (struct _reent *ptr,const char *file,int flags,int mode)
{int ret;errno 0;if ((ret _open (file, flags, mode)) -1 errno ! 0)ptr-_errno errno;return ret;
}函数_open_r如上述代码所示会进一步调用函数_open该函数以arm硬件平台为例实现在newlib-cygwin\libgloss\arm\syscalls.c文件里。newlib目录是和硬件平台无关的痛殴他那个功能实现libloss目录是底层的驱动实现以各个硬件平台为文件夹进行组织。在特定硬件平台的目录下的syscalls.c文件里面实现了newlib需要的各个桩函数
/* Forward prototypes. */
int _system (const char *);
int _rename (const char *, const char *);
int _isatty (int);
clock_t _times (struct tms *);
int _gettimeofday (struct timeval *, void *);
int _unlink (const char *);
int _link (const char *, const char *);
int _stat (const char *, struct stat *);
int _fstat (int, struct stat *);
int _swistat (int fd, struct stat * st);
void * _sbrk (ptrdiff_t);
pid_t _getpid (void);
int _close (int);
clock_t _clock (void);
int _swiclose (int);
int _open (const char *, int, ...);
int _swiopen (const char *, int);
int _write (int, const void *, size_t);
int _swiwrite (int, const void *, size_t);
_off_t _lseek (int, _off_t, int);
_off_t _swilseek (int, _off_t, int);
int _read (int, void *, size_t);
int _swiread (int, void *, size_t);
void initialise_monitor_handles (void);对于上文提到的函数_open源码如下。后续不再继续分析了LiteOS-M内核会提供这些钩子函数的实现。
int
_open (const char * path, int flags, ...)
{return _swiopen (path, flags);
}
小结
本文学习了LiteOS-M内核Newlib C的实现特别是文件系统和内存分配释放部分最后介绍了Newlib钩子函数。
如果大家想更加深入的学习 OpenHarmony 开发的内容不妨可以参考以下相关学习文档进行学习助你快速提升自己
OpenHarmony 开发环境搭建https://qr18.cn/CgxrRy 《OpenHarmony源码解析》https://qr18.cn/CgxrRy
搭建开发环境Windows 开发环境的搭建Ubuntu 开发环境搭建Linux 与 Windows 之间的文件共享……
系统架构分析https://qr18.cn/CgxrRy
构建子系统启动流程子系统分布式任务调度子系统分布式通信子系统驱动子系统…… OpenHarmony 设备开发学习手册https://qr18.cn/CgxrRy OpenHarmony面试题内含参考答案https://qr18.cn/CgxrRy