diff options
| author | Meco Man <[email protected]> | 2021-10-17 17:19:12 -0400 |
|---|---|---|
| committer | Meco Man <[email protected]> | 2021-10-17 17:19:12 -0400 |
| commit | b513d37cf2fc33c2dd20021316533feaac0bef23 (patch) | |
| tree | f14e24d145a40620dbb8fd5a5d807b972ec4a1d5 /components/libc/posix/mmap | |
| parent | 9fe546b0d86b17c64e57447913c8a9f3a9c40534 (diff) | |
[libc][posix] create 'posix' folder and move related files into it
Diffstat (limited to 'components/libc/posix/mmap')
| -rw-r--r-- | components/libc/posix/mmap/SConscript | 13 | ||||
| -rw-r--r-- | components/libc/posix/mmap/posix_mmap.c | 67 |
2 files changed, 80 insertions, 0 deletions
diff --git a/components/libc/posix/mmap/SConscript b/components/libc/posix/mmap/SConscript new file mode 100644 index 0000000000..1a36a15710 --- /dev/null +++ b/components/libc/posix/mmap/SConscript @@ -0,0 +1,13 @@ +# RT-Thread building script for component + +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') + Glob('*.cpp') +CPPPATH = [cwd] + +group = DefineGroup('libc', src, + depend = ['RT_USING_DFS', 'RT_USING_POSIX_MMAP'], + CPPPATH = CPPPATH) + +Return('group') diff --git a/components/libc/posix/mmap/posix_mmap.c b/components/libc/posix/mmap/posix_mmap.c new file mode 100644 index 0000000000..e68b82e640 --- /dev/null +++ b/components/libc/posix/mmap/posix_mmap.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2017/11/30 Bernard The first version. + */ + +#include <stdint.h> +#include <stdio.h> + +#include <rtthread.h> +#include <dfs_posix.h> + +#include <sys/mman.h> + +void *mmap(void *addr, size_t length, int prot, int flags, + int fd, off_t offset) +{ + uint8_t *mem; + + if (addr) + { + mem = addr; + } + else mem = (uint8_t *)malloc(length); + + if (mem) + { + off_t cur; + size_t read_bytes; + + cur = lseek(fd, 0, SEEK_SET); + + lseek(fd, offset, SEEK_SET); + read_bytes = read(fd, mem, length); + if (read_bytes != length) + { + if (addr == RT_NULL) + { + /* read failed */ + free(mem); + mem = RT_NULL; + } + } + lseek(fd, cur, SEEK_SET); + + return mem; + } + + errno = ENOMEM; + + return MAP_FAILED; +} + +int munmap(void *addr, size_t length) +{ + if (addr) + { + free(addr); + return 0; + } + + return -1; +} |
