From 67e14adc4f0dfbaef4762e383a5328add42f6cf5 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sun, 5 Dec 2021 18:52:59 -0500 Subject: [posix] 按照高级unix环境编程整理IO类别 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/libc/compilers/common/sys/termios.h | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 components/libc/compilers/common/sys/termios.h (limited to 'components/libc/compilers/common/sys') diff --git a/components/libc/compilers/common/sys/termios.h b/components/libc/compilers/common/sys/termios.h deleted file mode 100644 index c4b42083c4..0000000000 --- a/components/libc/compilers/common/sys/termios.h +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - */ -- cgit v1.3.1 From 15435b68507e0a5b6b90341d7c53945487aaf0de Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Tue, 7 Dec 2021 15:47:00 -0500 Subject: [posix][mmap] create the mman folder --- components/libc/compilers/common/sys/ioctl.h | 4 +- components/libc/compilers/common/sys/mman.h | 60 ------------------------- components/libc/posix/Kconfig | 2 +- components/libc/posix/io/SConscript | 4 -- components/libc/posix/io/mman/SConscript | 11 +++++ components/libc/posix/io/mman/mman.c | 67 ++++++++++++++++++++++++++++ components/libc/posix/io/mman/sys/mman.h | 60 +++++++++++++++++++++++++ components/libc/posix/io/mmap.c | 67 ---------------------------- components/libc/posix/io/poll/poll.c | 2 +- 9 files changed, 142 insertions(+), 135 deletions(-) delete mode 100644 components/libc/compilers/common/sys/mman.h create mode 100644 components/libc/posix/io/mman/SConscript create mode 100644 components/libc/posix/io/mman/mman.c create mode 100644 components/libc/posix/io/mman/sys/mman.h delete mode 100644 components/libc/posix/io/mmap.c (limited to 'components/libc/compilers/common/sys') diff --git a/components/libc/compilers/common/sys/ioctl.h b/components/libc/compilers/common/sys/ioctl.h index 29ed3a8b84..a1642199b1 100644 --- a/components/libc/compilers/common/sys/ioctl.h +++ b/components/libc/compilers/common/sys/ioctl.h @@ -7,8 +7,8 @@ * Date Author Notes * 2020-09-01 Meco Man First Version */ -#ifndef _SYS_IOCTL_H -#define _SYS_IOCTL_H +#ifndef __SYS_IOCTL_H__ +#define __SYS_IOCTL_H__ #include #include diff --git a/components/libc/compilers/common/sys/mman.h b/components/libc/compilers/common/sys/mman.h deleted file mode 100644 index 2da2ddf716..0000000000 --- a/components/libc/compilers/common/sys/mman.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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. - */ - -#ifndef _SYS_MMAN_H -#define _SYS_MMAN_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#define MAP_FAILED ((void *) -1) - -#define MAP_SHARED 0x01 -#define MAP_PRIVATE 0x02 -#define MAP_TYPE 0x0f -#define MAP_FIXED 0x10 -#define MAP_ANON 0x20 -#define MAP_ANONYMOUS MAP_ANON -#define MAP_NORESERVE 0x4000 -#define MAP_GROWSDOWN 0x0100 -#define MAP_DENYWRITE 0x0800 -#define MAP_EXECUTABLE 0x1000 -#define MAP_LOCKED 0x2000 -#define MAP_POPULATE 0x8000 -#define MAP_NONBLOCK 0x10000 -#define MAP_STACK 0x20000 -#define MAP_HUGETLB 0x40000 -#define MAP_FILE 0 - -#define PROT_NONE 0 -#define PROT_READ 1 -#define PROT_WRITE 2 -#define PROT_EXEC 4 -#define PROT_GROWSDOWN 0x01000000 -#define PROT_GROWSUP 0x02000000 - -#define MS_ASYNC 1 -#define MS_INVALIDATE 2 -#define MS_SYNC 4 - -#define MCL_CURRENT 1 -#define MCL_FUTURE 2 -#define MCL_ONFAULT 4 - -void *mmap (void *start, size_t len, int prot, int flags, int fd, off_t off); -int munmap (void *start, size_t len); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/components/libc/posix/Kconfig b/components/libc/posix/Kconfig index 52b5fdc948..4534da7118 100644 --- a/components/libc/posix/Kconfig +++ b/components/libc/posix/Kconfig @@ -30,7 +30,7 @@ if RT_USING_POSIX_FS bool "Enable Asynchronous I/O " default n - config RT_USING_POSIX_MMAP + config RT_USING_POSIX_MMAN bool "Enable Memory-Mapped I/O " default n endif diff --git a/components/libc/posix/io/SConscript b/components/libc/posix/io/SConscript index 2a7cd76ded..32654674e8 100644 --- a/components/libc/posix/io/SConscript +++ b/components/libc/posix/io/SConscript @@ -18,10 +18,6 @@ if GetDepend('RT_USING_POSIX_SELECT'): src += ['select.c'] flag = True -if GetDepend('RT_USING_POSIX_MMAP'): - src += ['mmap.c'] - flag = True - if flag == True: group = DefineGroup('POSIX', src, depend = [], CPPPATH = CPPPATH) diff --git a/components/libc/posix/io/mman/SConscript b/components/libc/posix/io/mman/SConscript new file mode 100644 index 0000000000..7dd096c998 --- /dev/null +++ b/components/libc/posix/io/mman/SConscript @@ -0,0 +1,11 @@ +# RT-Thread building script for component + +from building import * + +cwd = GetCurrentDir() +src = ['mman.c'] +CPPPATH = [cwd] + +group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX_MMAN'], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/libc/posix/io/mman/mman.c b/components/libc/posix/io/mman/mman.c new file mode 100644 index 0000000000..827f2cbb14 --- /dev/null +++ b/components/libc/posix/io/mman/mman.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 +#include + +#include +#include + +#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; +} diff --git a/components/libc/posix/io/mman/sys/mman.h b/components/libc/posix/io/mman/sys/mman.h new file mode 100644 index 0000000000..a730f33b6b --- /dev/null +++ b/components/libc/posix/io/mman/sys/mman.h @@ -0,0 +1,60 @@ +/* + * 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. + */ + +#ifndef __SYS_MMAN_H__ +#define __SYS_MMAN_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#define MAP_FAILED ((void *) -1) + +#define MAP_SHARED 0x01 +#define MAP_PRIVATE 0x02 +#define MAP_TYPE 0x0f +#define MAP_FIXED 0x10 +#define MAP_ANON 0x20 +#define MAP_ANONYMOUS MAP_ANON +#define MAP_NORESERVE 0x4000 +#define MAP_GROWSDOWN 0x0100 +#define MAP_DENYWRITE 0x0800 +#define MAP_EXECUTABLE 0x1000 +#define MAP_LOCKED 0x2000 +#define MAP_POPULATE 0x8000 +#define MAP_NONBLOCK 0x10000 +#define MAP_STACK 0x20000 +#define MAP_HUGETLB 0x40000 +#define MAP_FILE 0 + +#define PROT_NONE 0 +#define PROT_READ 1 +#define PROT_WRITE 2 +#define PROT_EXEC 4 +#define PROT_GROWSDOWN 0x01000000 +#define PROT_GROWSUP 0x02000000 + +#define MS_ASYNC 1 +#define MS_INVALIDATE 2 +#define MS_SYNC 4 + +#define MCL_CURRENT 1 +#define MCL_FUTURE 2 +#define MCL_ONFAULT 4 + +void *mmap (void *start, size_t len, int prot, int flags, int fd, off_t off); +int munmap (void *start, size_t len); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/components/libc/posix/io/mmap.c b/components/libc/posix/io/mmap.c deleted file mode 100644 index e68b82e640..0000000000 --- a/components/libc/posix/io/mmap.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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 -#include - -#include -#include - -#include - -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; -} diff --git a/components/libc/posix/io/poll/poll.c b/components/libc/posix/io/poll/poll.c index 0c773b306c..a1500ba109 100644 --- a/components/libc/posix/io/poll/poll.c +++ b/components/libc/posix/io/poll/poll.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include "poll.h" struct rt_poll_node; -- cgit v1.3.1