From d7f3aef326378f2e2bbca25b0e8a7910b1c13f30 Mon Sep 17 00:00:00 2001 From: arvinzzz Date: Tue, 17 Jan 2023 19:11:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=8D=E5=90=8CC=E5=BA=93?= =?UTF-8?q?=E7=9A=84malloc=E9=80=82=E9=85=8D=E5=BD=92=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 不同C库的malloc实现归一化,由原先//kal/libc/musl、//kal/libc/newlib、 //../../third_party/musl/porting/liteos_m_iccarm/kernel 各自实现, 统一至//kal/posix/src/malloc.c实现,并实现对三种C库的兼容 2. Kconfig归一化的配套调整,删除Top->"Compat"->"Choose libc implementation"下 malloc API的开关控制,统一归入"Top"->"Compat"->"Enable POSIX API"下 "Enable POSIX MALLOC API"进行能力开关控制 BREAKING CHANGE: Kconfig变更: 删除: LOSCFG_LIBC_ICCARM_MALLOC LOSCFG_LIBC_NEWLIB_MALLOC 新增: LOSCFG_POSIX_MALLOC_API close: I6AGOW Signed-off-by: arvinzzz Change-Id: I584e337cdb36184cf40c36c4f7c73ab780c31a42 --- components/fs/vfs/vfs_fs.c | 16 +++ kal/libc/Kconfig | 4 +- kal/libc/iccarm/Kconfig | 37 ------ kal/libc/musl/BUILD.gn | 3 +- kal/libc/newlib/BUILD.gn | 3 +- kal/libc/newlib/Kconfig | 38 ------ .../src/{other_adapt.c => hook_adapt.c} | 1 + kal/libc/newlib/porting/src/malloc.c | 115 ------------------ kal/posix/BUILD.gn | 6 +- kal/posix/Kconfig | 8 +- kal/{libc/musl => posix/src}/malloc.c | 48 +++++++- kal/posix/src/time.c | 2 + 12 files changed, 80 insertions(+), 201 deletions(-) delete mode 100644 kal/libc/iccarm/Kconfig delete mode 100644 kal/libc/newlib/Kconfig rename kal/libc/newlib/porting/src/{other_adapt.c => hook_adapt.c} (96%) delete mode 100644 kal/libc/newlib/porting/src/malloc.c rename kal/{libc/musl => posix/src}/malloc.c (73%) diff --git a/components/fs/vfs/vfs_fs.c b/components/fs/vfs/vfs_fs.c index 6d929249..50b39eea 100644 --- a/components/fs/vfs/vfs_fs.c +++ b/components/fs/vfs/vfs_fs.c @@ -555,7 +555,9 @@ int open(const char *path, int flags, ...) int ret = VfsOpen(path, flags); return MapToPosixRet(ret); } +#if (LOSCFG_LIBC_NEWLIB == 1) FUNC_ALIAS(open, _open, (const char *path, int flags, ...), int); +#endif int close(int fd) { @@ -581,7 +583,9 @@ int close(int fd) } return MapToPosixRet(ret); } +#if (LOSCFG_LIBC_NEWLIB == 1) FUNC_ALIAS(close, _close, (int fd), int); +#endif ssize_t read(int fd, void *buff, size_t bytes) { @@ -624,7 +628,9 @@ ssize_t read(int fd, void *buff, size_t bytes) return MapToPosixRet(ret); } +#if (LOSCFG_LIBC_NEWLIB == 1) FUNC_ALIAS(read, _read, (int fd, void *buff, size_t bytes), ssize_t); +#endif ssize_t write(int fd, const void *buff, size_t bytes) { @@ -654,7 +660,9 @@ ssize_t write(int fd, const void *buff, size_t bytes) return MapToPosixRet(ret); } +#if (LOSCFG_LIBC_NEWLIB == 1) FUNC_ALIAS(write, _write, (int fd, const void *buff, size_t bytes), ssize_t); +#endif off_t lseek(int fd, off_t off, int whence) { @@ -675,7 +683,9 @@ off_t lseek(int fd, off_t off, int whence) VfsDetachFile(file); return ret; } +#if (LOSCFG_LIBC_NEWLIB == 1) FUNC_ALIAS(lseek, _lseek, (int fd, off_t off, int whence), off_t); +#endif int stat(const char *path, struct stat *stat) { @@ -713,7 +723,9 @@ int stat(const char *path, struct stat *stat) LOS_FsUnlock(); return MapToPosixRet(ret); } +#if (LOSCFG_LIBC_NEWLIB == 1) FUNC_ALIAS(stat, _stat, (const char *path, struct stat *stat), int); +#endif int statfs(const char *path, struct statfs *buf) { @@ -780,7 +792,9 @@ int unlink(const char *path) LOS_FsUnlock(); return MapToPosixRet(ret); } +#if (LOSCFG_LIBC_NEWLIB == 1) FUNC_ALIAS(unlink, _unlink, (const char *path), int); +#endif int rename(const char *oldpath, const char *newpath) { @@ -1074,7 +1088,9 @@ int fstat(int fd, struct stat *buf) VfsDetachFile(filep); return ret; } +#if (LOSCFG_LIBC_NEWLIB == 1) FUNC_ALIAS(fstat, _fstat, (int fd, struct stat *buf), int); +#endif int fcntl(int fd, int cmd, ...) { diff --git a/kal/libc/Kconfig b/kal/libc/Kconfig index 4029373b..a5ecc353 100644 --- a/kal/libc/Kconfig +++ b/kal/libc/Kconfig @@ -1,5 +1,5 @@ # Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -38,11 +38,9 @@ config LIBC_MUSL config LIBC_NEWLIB bool "newlibc" -rsource "newlib/Kconfig" config LIBC_ICCARM bool "iar libc" depends on COMPILER_ICCARM -rsource "iccarm/Kconfig" endchoice diff --git a/kal/libc/iccarm/Kconfig b/kal/libc/iccarm/Kconfig deleted file mode 100644 index 31deaf62..00000000 --- a/kal/libc/iccarm/Kconfig +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -if LIBC_ICCARM - -config LIBC_ICCARM_MALLOC - bool "Enable POSIX malloc/free API support" - default y - help - This enables POSIX malloc/free related APIs. - -endif # LIBC_ICCARM diff --git a/kal/libc/musl/BUILD.gn b/kal/libc/musl/BUILD.gn index 8dcd3aa4..371779c5 100644 --- a/kal/libc/musl/BUILD.gn +++ b/kal/libc/musl/BUILD.gn @@ -1,5 +1,5 @@ # Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -33,7 +33,6 @@ import("//third_party/musl/porting/liteos_m/kernel/musl.gni") module_switch = defined(LOSCFG_LIBC_MUSL) module_name = get_path_info(rebase_path("."), "name") kernel_module(module_name) { - sources = [ "malloc.c" ] configs += [ "$LITEOSTOPDIR:warn_config" ] deps = [ "//third_party/musl/porting/liteos_m/kernel" ] diff --git a/kal/libc/newlib/BUILD.gn b/kal/libc/newlib/BUILD.gn index cd8f1133..5316628e 100644 --- a/kal/libc/newlib/BUILD.gn +++ b/kal/libc/newlib/BUILD.gn @@ -33,12 +33,11 @@ module_switch = defined(LOSCFG_LIBC_NEWLIB) module_name = get_path_info(rebase_path("."), "name") kernel_module(module_name) { sources = [ - "porting/src/malloc.c", + "porting/src/hook_adapt.c", "porting/src/network/htonl.c", "porting/src/network/htons.c", "porting/src/network/ntohl.c", "porting/src/network/ntohs.c", - "porting/src/other_adapt.c", ] configs += [ "$LITEOSTOPDIR:warn_config" ] diff --git a/kal/libc/newlib/Kconfig b/kal/libc/newlib/Kconfig deleted file mode 100644 index b785495e..00000000 --- a/kal/libc/newlib/Kconfig +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -if LIBC_NEWLIB - -config LIBC_NEWLIB_MALLOC - bool "Enable POSIX malloc/free API support" - default y - help - This enables POSIX malloc/free related APIs. - -endif # LIBC_NEWLIB diff --git a/kal/libc/newlib/porting/src/other_adapt.c b/kal/libc/newlib/porting/src/hook_adapt.c similarity index 96% rename from kal/libc/newlib/porting/src/other_adapt.c rename to kal/libc/newlib/porting/src/hook_adapt.c index d7ee0d58..3ad20ea3 100644 --- a/kal/libc/newlib/porting/src/other_adapt.c +++ b/kal/libc/newlib/porting/src/hook_adapt.c @@ -38,6 +38,7 @@ /* * fs adapter interface, such as _read, see component/fs * time adapter interface, such as _gettimeofday, see posix/src/time.c + * malloc adapter interface, such as _malloc_r, see posix/src/malloc.c */ int _isatty(int file) diff --git a/kal/libc/newlib/porting/src/malloc.c b/kal/libc/newlib/porting/src/malloc.c deleted file mode 100644 index 8e840d15..00000000 --- a/kal/libc/newlib/porting/src/malloc.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "securec.h" -#include "los_config.h" -#include "los_memory.h" -#include - -#ifdef LOSCFG_LIBC_NEWLIB_MALLOC -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 __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); -} - -void *__wrap__realloc_r(struct _reent *reent, void *aptr, size_t nbytes) -{ - if (aptr == NULL) { - return malloc(nbytes); - } - - if (nbytes == 0) { - free(aptr); - return NULL; - } - - return LOS_MemRealloc(OS_SYS_MEM_ADDR, aptr, nbytes); -} - -void *__wrap__calloc_r(struct _reent *reent, 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; -} -#endif diff --git a/kal/posix/BUILD.gn b/kal/posix/BUILD.gn index 11fbcfe9..2c2a6766 100644 --- a/kal/posix/BUILD.gn +++ b/kal/posix/BUILD.gn @@ -1,5 +1,5 @@ # Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -67,6 +67,10 @@ kernel_module(module_name) { if (defined(LOSCFG_POSIX_SIGNAL_API)) { sources += [ "src/signal.c" ] } + + if (defined(LOSCFG_POSIX_MALLOC_API)) { + sources += [ "src/malloc.c" ] + } } config("public") { diff --git a/kal/posix/Kconfig b/kal/posix/Kconfig index dd1f37f3..1ae24fe1 100644 --- a/kal/posix/Kconfig +++ b/kal/posix/Kconfig @@ -1,5 +1,5 @@ # Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -80,4 +80,10 @@ config POSIX_FS_API help Answer Y to enable LiteOS support POSIX FS API. +config POSIX_MALLOC_API + bool "Enable POSIX MALLOC API" + default y + help + Answer Y to enable LiteOS support POSIX MALLOC API. + endif # POSIX_API diff --git a/kal/libc/musl/malloc.c b/kal/posix/src/malloc.c similarity index 73% rename from kal/libc/musl/malloc.c rename to kal/posix/src/malloc.c index 36ecfab4..af1fa939 100644 --- a/kal/libc/musl/malloc.c +++ b/kal/posix/src/malloc.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. + * Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: @@ -34,8 +34,15 @@ #include "los_config.h" #include "los_memory.h" +#if (LOSCFG_LIBC_NEWLIB == 1) +void *__wrap__calloc_r(struct _reent *reent, size_t nitems, size_t size) +#else void *calloc(size_t nitems, size_t size) +#endif { +#if (LOSCFG_LIBC_NEWLIB == 1) + (void)reent; +#endif size_t real_size; void *ptr = NULL; @@ -51,8 +58,15 @@ void *calloc(size_t nitems, size_t size) return ptr; } +#if (LOSCFG_LIBC_NEWLIB == 1) +void __wrap__free_r(struct _reent *reent, void *ptr) +#else void free(void *ptr) +#endif { +#if (LOSCFG_LIBC_NEWLIB == 1) + (void)reent; +#endif if (ptr == NULL) { return; } @@ -60,8 +74,24 @@ void free(void *ptr) LOS_MemFree(OS_SYS_MEM_ADDR, ptr); } -void *malloc(size_t size) +#if (LOSCFG_LIBC_NEWLIB == 1) +size_t __wrap__malloc_usable_size_r(struct _reent *reent, void *aptr) { + (void)reent; + (void)aptr; + return 0; +} +#endif + +#if (LOSCFG_LIBC_NEWLIB == 1) +void *__wrap__malloc_r(struct _reent *reent, size_t size) +#else +void *malloc(size_t size) +#endif +{ +#if (LOSCFG_LIBC_NEWLIB == 1) + (void)reent; +#endif if (size == 0) { return NULL; } @@ -84,8 +114,15 @@ void *zalloc(size_t size) return ptr; } +#if (LOSCFG_LIBC_NEWLIB == 1) +void *__wrap__memalign_r(struct _reent *reent, size_t boundary, size_t size) +#else void *memalign(size_t boundary, size_t size) +#endif { +#if (LOSCFG_LIBC_NEWLIB == 1) + (void)reent; +#endif if (size == 0) { return NULL; } @@ -93,8 +130,15 @@ void *memalign(size_t boundary, size_t size) return LOS_MemAllocAlign(OS_SYS_MEM_ADDR, size, boundary); } +#if (LOSCFG_LIBC_NEWLIB == 1) +void *__wrap__realloc_r(struct _reent *reent, void *ptr, size_t size) +#else void *realloc(void *ptr, size_t size) +#endif { +#if (LOSCFG_LIBC_NEWLIB == 1) + (void)reent; +#endif if (ptr == NULL) { return malloc(size); } diff --git a/kal/posix/src/time.c b/kal/posix/src/time.c index f9fc0c8b..b3787723 100644 --- a/kal/posix/src/time.c +++ b/kal/posix/src/time.c @@ -732,7 +732,9 @@ int gettimeofday(struct timeval *tv, void *ptz) } return 0; } +#if (LOSCFG_LIBC_NEWLIB == 1) FUNC_ALIAS(gettimeofday, _gettimeofday, (struct timeval *tv, void *ptz), int); +#endif int settimeofday(const struct timeval *tv, const struct timezone *tz) {