!1016 refactor: 不同C库malloc归一

Merge pull request !1016 from Zhaotianyu/20230117libc_malloc
This commit is contained in:
openharmony_ci 2023-01-18 09:56:58 +00:00 committed by Gitee
commit c6d021bede
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
12 changed files with 80 additions and 201 deletions

View File

@ -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, ...)
{

View File

@ -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

View File

@ -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

View File

@ -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" ]

View File

@ -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" ]

View File

@ -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

View File

@ -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)

View File

@ -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 <malloc.h>
#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

View File

@ -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") {

View File

@ -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

View File

@ -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);
}

View File

@ -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)
{