1.5 函数lseek

函数lseek用于重新定位文件读写的偏移位置。参数whence取值为SEEK_SET、SEEK_CUR或SEEK_END,定义在文件third_party\musl\porting\liteos_m\kernel\include\fcntl.h。

函数执行成功时,返回值为从文件开头的偏移字节数值。

off_t lseek(int fd, off_t offset, int whence)
{
    return LOS_Lseek(fd, offset, whence);
}}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

1.6 函数fstat、stat和statfs

函数fstat和stat用于获取文件的状态state,参数参数分别是文件描述符和文件路径。参数中的struct stat结构体定义在文件third_party\musl\porting\liteos_m\kernel\include\bits\stat.h中。
函数statfs返回文件系统统计statistics数据,结构体struct statfs定义在文件third_party\musl\porting\liteos_m\kernel\include\bits\statfs.h中。

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 statfs(const char *path, struct statfs *buf)
{
    return LOS_Statfs(path, buf);
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

1.7 函数mkdir、opendir、readir、closedir和rmdrir

函数mkdir用于创建一个目录,目录名称由参数path指定。参数mode指定目录权限。创建成功返回0,否则返回-1。
函数opendir用于打开一个目录流a directory stream,目录名称由参数dirName指定,返回一个执行目录刘的指针。发生错误时,返回NULL,并设置errno。返回值类型DIR是struct __dirstream的别名,定义在文件中third_party\musl\porting\liteos_m\kernel\include\dirent.h。可以访问https://linux.die.net/man/3/opendir了解更多关于该函数的信息。
函数readdir用于读取一个目录,返回一个struct dirent结构体指针,代表目录流DIR *dir中的下一个目录条目directory entry。到达目录流尾部或错误时,返回NULL。结构体定义在文件third_party\musl\porting\liteos_m\kernel\include\bits\dirent.h中。 可以访问https://linux.die.net/man/3/readdir了解更多关于该函数的信息。
函数closedir用于关闭一个目录。函数rmdir用于删除一个目录,只有空目录才会被删除。

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);
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

1.8 函数fsync

函数mkdir用于同步内存中所有已修改的文件数据到储存设备。可以访问https://linux.die.net/man/3/fsync了解更多关于该函数的信息。

int fsync(int fd)
{
    return LOS_Fsync(fd);
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

1.9 函数rename

函数rename用于重命名一个文件。可以访问https://linux.die.net/man/3/rename了解更多关于该函数的信息。

int rename(const char *oldName, const char *newName)
{
    return LOS_Rename(oldName, newName);
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

1.10 函数ftruncate

函数ftruncate用于截断一个文件到指定的长度。可以访问https://linux.die.net/man/3/ftruncate了解更多关于该函数的信息。

int ftruncate(int fd, off_t length)
{
    return LOS_Ftruncate(fd, length);
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

2、Musl LibC内存分配释放

LiteOS-M内核提供了内存分配释放函数。这些是标准的POSIX接口,如果想了解其用法,可以参考Section 3: library functions。可以在网页上搜索,也可以直接把上述网址和函数名称进行拼接,如对于malloc()函数,可以直接访问 https://linux.die.net/man/3/malloc 。opendir等部分函数需要在网页上查看。下文快速记录下各个函数的使用方法。

1.1 函数malloc、free和memalign

函数malloc和free分别调用内核内存模块的接口来实现内存申请和释放。函数memalign可以以指定的内存对齐大小来申请内存。

void free(void *ptr)
{
    if (ptr == NULL) {
        return;
    }

    LOS_MemFree(OS_SYS_MEM_ADDR, ptr);
}

void *malloc(size_t size)
{
    if (size == 0) {
        return NULL;
    }

    return LOS_MemAlloc(OS_SYS_MEM_ADDR, size);
}
void *memalign(size_t boundary, size_t size)
{
    if (size == 0) {
        return NULL;
    }

    return LOS_MemAllocAlign(OS_SYS_MEM_ADDR, size, boundary);
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

1.2 函数malloc、free和memalign

函数calloc在内存的动态存储区中分配nitems个长度为size的连续空间,函数返回一个指向分配起始地址的指针;如果分配不成功,返回NULL。
函数zalloc和malloc的区别是,申请成功后,对申请的内存区域置0。函数realloc用于重新申请一块内存区域。

void *calloc(size_t nitems, size_t size)
{
    size_t real_size;
    void *ptr = NULL;

    if (nitems == 0 || size == 0) {
        return NULL;
    }

    real_size = (size_t)(nitems * size);
    ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, real_size);
    if (ptr != NULL) {
        (void)memset_s(ptr, real_size, 0, real_size);
    }
    return ptr;
}
void *zalloc(size_t size)
{
    void *ptr = NULL;

    if (size == 0) {
        return NULL;
    }

    ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, size);
    if (ptr != NULL) {
        (void)memset_s(ptr, size, 0, size);
    }
    return ptr;
}

void *realloc(void *ptr, size_t size)
{
    if (ptr == NULL) {
        return malloc(size);
    }

    if (size == 0) {
        free(ptr);
        return NULL;
    }

    return LOS_MemRealloc(OS_SYS_MEM_ADDR, ptr, size);
}
 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

小结

本文学习了LiteOS-M内核Musl LibC的实现,特别是文件系统和内存分配释放部分。时间仓促和能力关系,如有失误,欢迎指正。

如果大家想更加深入的学习 OpenHarmony 开发的内容,不妨可以参考以下相关学习文档进行学习,助你快速提升自己:

OpenHarmony 开发环境搭建:https://qr18.cn/CgxrRy

《OpenHarmony源码解析》:https://qr18.cn/CgxrRy

系统架构分析:https://qr18.cn/CgxrRy

OpenHarmony 设备开发学习手册:https://qr18.cn/CgxrRy

在这里插入图片描述

OpenHarmony面试题(内含参考答案):https://qr18.cn/CgxrRy

data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/maniuT/article/details/139535703","extend1":"pc","ab":"new"}">> id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"> class="blog_extension blog_extension_type2" id="blog_extension"> class="extension_official" data-report-click="{"spm":"1001.2101.3001.6471"}" data-report-view="{"spm":"1001.2101.3001.6471"}"> class="blog_extension_card_left"> class="blog_extension_card_cont"> 鸿蒙开发学习资料领取!!! class="blog_extension_card_cont_r"> 微信名片
注:本文转载自blog.csdn.net的沧海一笑-dj的文章"https://blog.csdn.net/dengjin20104042056/article/details/98378311"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接

评论记录:

未查询到任何数据!