diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt index c63eadab6f5d7..93da271f5e040 100644 --- a/libc/hdr/CMakeLists.txt +++ b/libc/hdr/CMakeLists.txt @@ -81,6 +81,8 @@ add_proxy_header_library( libc.include.signal ) +add_header_library(stdlib_overlay HDRS stdlib_overlay.h) + add_proxy_header_library( stdlib_macros HDRS @@ -193,3 +195,4 @@ add_proxy_header_library( ) add_subdirectory(types) +add_subdirectory(func) diff --git a/libc/hdr/func/CMakeLists.txt b/libc/hdr/func/CMakeLists.txt new file mode 100644 index 0000000000000..714b7ec9b4880 --- /dev/null +++ b/libc/hdr/func/CMakeLists.txt @@ -0,0 +1,52 @@ +add_proxy_header_library( + aligned_alloc + HDRS + aligned_alloc.h + DEPENDS + libc.hdr.stdlib_overlay + FULL_BUILD_DEPENDS + libc.include.stdlib + libc.hdr.types.size_t +) + +add_proxy_header_library( + malloc + HDRS + malloc.h + DEPENDS + libc.hdr.stdlib_overlay + FULL_BUILD_DEPENDS + libc.include.stdlib + libc.hdr.types.size_t +) + +add_proxy_header_library( + realloc + HDRS + realloc.h + DEPENDS + libc.hdr.stdlib_overlay + FULL_BUILD_DEPENDS + libc.include.stdlib + libc.hdr.types.size_t +) + +add_proxy_header_library( + free + HDRS + free.h + DEPENDS + libc.hdr.stdlib_overlay + FULL_BUILD_DEPENDS + libc.include.stdlib +) + +add_proxy_header_library( + _Exit + HDRS + _Exit.h + DEPENDS + libc.hdr.stdlib_overlay + FULL_BUILD_DEPENDS + libc.include.stdlib +) diff --git a/libc/hdr/func/_Exit.h b/libc/hdr/func/_Exit.h new file mode 100644 index 0000000000000..575b0426e508c --- /dev/null +++ b/libc/hdr/func/_Exit.h @@ -0,0 +1,21 @@ +//===-- Definition of the _Exit proxy -------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_HDR_FUNC_EXIT_H +#define LLVM_LIBC_HDR_FUNC_EXIT_H + +#ifdef LIBC_FULL_BUILD +extern "C" void _Exit(int); + +#else // Overlay mode + +#include "hdr/stdlib_overlay.h" + +#endif + +#endif // LLVM_LIBC_HDR_EXIT_H diff --git a/libc/hdr/func/aligned_alloc.h b/libc/hdr/func/aligned_alloc.h new file mode 100644 index 0000000000000..b3436dfee1f23 --- /dev/null +++ b/libc/hdr/func/aligned_alloc.h @@ -0,0 +1,22 @@ +//===-- Definition of the aligned_alloc.h proxy ---------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_HDR_FUNC_ALIGNED_ALLOC_H +#define LLVM_LIBC_HDR_FUNC_ALIGNED_ALLOC_H + +#ifdef LIBC_FULL_BUILD +#include "hdr/types/size_t.h" +extern "C" void *aligned_alloc(size_t, size_t); + +#else // Overlay mode + +#include "hdr/stdlib_overlay.h" + +#endif + +#endif diff --git a/libc/hdr/func/free.h b/libc/hdr/func/free.h new file mode 100644 index 0000000000000..b1190a777da32 --- /dev/null +++ b/libc/hdr/func/free.h @@ -0,0 +1,21 @@ +//===-- Definition of the free.h proxy ------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_HDR_FUNC_FREE_H +#define LLVM_LIBC_HDR_FUNC_FREE_H + +#ifdef LIBC_FULL_BUILD +extern "C" void free(void *); + +#else // Overlay mode + +#include "hdr/stdlib_overlay.h" + +#endif + +#endif diff --git a/libc/hdr/func/malloc.h b/libc/hdr/func/malloc.h new file mode 100644 index 0000000000000..b395f41f2bce2 --- /dev/null +++ b/libc/hdr/func/malloc.h @@ -0,0 +1,22 @@ +//===-- Definition of the malloc.h proxy ----------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_HDR_FUNC_MALLOC_H +#define LLVM_LIBC_HDR_FUNC_MALLOC_H + +#ifdef LIBC_FULL_BUILD +#include "hdr/types/size_t.h" +extern "C" void *malloc(size_t); + +#else // Overlay mode + +#include "hdr/stdlib_overlay.h" + +#endif + +#endif diff --git a/libc/hdr/func/realloc.h b/libc/hdr/func/realloc.h new file mode 100644 index 0000000000000..0096045e8330b --- /dev/null +++ b/libc/hdr/func/realloc.h @@ -0,0 +1,22 @@ +//===-- Definition of the realloc.h proxy ---------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_HDR_FUNC_REALLOC_H +#define LLVM_LIBC_HDR_FUNC_REALLOC_H + +#ifdef LIBC_FULL_BUILD +#include "hdr/types/size_t.h" +extern "C" void *realloc(void *ptr, size_t new_size); + +#else // Overlay mode + +#include "hdr/stdlib_overlay.h" + +#endif + +#endif diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt index 14a3acff8fae9..763ad372a4aa0 100644 --- a/libc/src/__support/CMakeLists.txt +++ b/libc/src/__support/CMakeLists.txt @@ -236,6 +236,9 @@ add_header_library( HDRS char_vector.h DEPENDS + libc.hdr.func.free + libc.hdr.func.malloc + libc.hdr.func.realloc libc.src.__support.common ) diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt index 774668be42e56..15fad9de0ed6d 100644 --- a/libc/src/__support/CPP/CMakeLists.txt +++ b/libc/src/__support/CPP/CMakeLists.txt @@ -80,8 +80,10 @@ add_header_library( HDRS string.h DEPENDS - libc.include.stdlib .string_view + libc.hdr.func.free + libc.hdr.func.malloc + libc.hdr.func.realloc libc.src.__support.common libc.src.__support.integer_to_string libc.src.string.memory_utils.inline_memcpy @@ -199,7 +201,9 @@ add_object_library( HDRS new.h DEPENDS - libc.include.stdlib + libc.hdr.func.free + libc.hdr.func.malloc + libc.hdr.func.aligned_alloc libc.src.__support.common libc.src.__support.macros.properties.os ) diff --git a/libc/src/__support/CPP/new.cpp b/libc/src/__support/CPP/new.cpp index 8316329fb10b0..65f80de373485 100644 --- a/libc/src/__support/CPP/new.cpp +++ b/libc/src/__support/CPP/new.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// #include "new.h" -#include // For free, etc +#include "hdr/func/free.h" void operator delete(void *mem) noexcept { ::free(mem); } diff --git a/libc/src/__support/CPP/new.h b/libc/src/__support/CPP/new.h index c1b6b95033f84..8694d9c475507 100644 --- a/libc/src/__support/CPP/new.h +++ b/libc/src/__support/CPP/new.h @@ -9,12 +9,14 @@ #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H #define LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H +#include "hdr/func/aligned_alloc.h" +#include "hdr/func/free.h" +#include "hdr/func/malloc.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" #include "src/__support/macros/properties/os.h" #include // For size_t -#include // For malloc, free etc. // Defining members in the std namespace is not preferred. But, we do it here // so that we can use it to define the operator new which takes std::align_val_t diff --git a/libc/src/__support/CPP/string.h b/libc/src/__support/CPP/string.h index 64d4c276e8428..dbc0ae04e5e6f 100644 --- a/libc/src/__support/CPP/string.h +++ b/libc/src/__support/CPP/string.h @@ -9,6 +9,9 @@ #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_STRING_H #define LLVM_LIBC_SRC___SUPPORT_CPP_STRING_H +#include "hdr/func/free.h" +#include "hdr/func/malloc.h" +#include "hdr/func/realloc.h" #include "src/__support/CPP/string_view.h" #include "src/__support/integer_to_string.h" // IntegerToString #include "src/__support/macros/config.h" @@ -17,7 +20,6 @@ #include "src/string/string_utils.h" // string_length #include // size_t -#include // malloc, free namespace LIBC_NAMESPACE_DECL { namespace cpp { diff --git a/libc/src/__support/File/CMakeLists.txt b/libc/src/__support/File/CMakeLists.txt index 1b390a12424d0..5a4af5e70e289 100644 --- a/libc/src/__support/File/CMakeLists.txt +++ b/libc/src/__support/File/CMakeLists.txt @@ -18,6 +18,7 @@ add_object_library( libc.src.__support.error_or libc.hdr.types.off_t libc.hdr.stdio_macros + libc.hdr.func.realloc ) add_object_library( diff --git a/libc/src/__support/File/file.cpp b/libc/src/__support/File/file.cpp index 51811a27c1acd..972249fef96bc 100644 --- a/libc/src/__support/File/file.cpp +++ b/libc/src/__support/File/file.cpp @@ -8,6 +8,7 @@ #include "file.h" +#include "hdr/func/realloc.h" #include "hdr/stdio_macros.h" #include "hdr/types/off_t.h" #include "src/__support/CPP/new.h" diff --git a/libc/src/__support/char_vector.h b/libc/src/__support/char_vector.h index df109da5d8127..d0837a8af3f46 100644 --- a/libc/src/__support/char_vector.h +++ b/libc/src/__support/char_vector.h @@ -9,11 +9,13 @@ #ifndef LLVM_LIBC_SRC___SUPPORT_CHARVECTOR_H #define LLVM_LIBC_SRC___SUPPORT_CHARVECTOR_H +#include "hdr/func/free.h" +#include "hdr/func/malloc.h" +#include "hdr/func/realloc.h" #include "src/__support/common.h" // LIBC_INLINE #include "src/__support/macros/config.h" #include // size_t -#include // malloc, realloc, free namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt index 8172fda1e866e..9eaffe2f7ed62 100644 --- a/libc/src/stdio/printf_core/CMakeLists.txt +++ b/libc/src/stdio/printf_core/CMakeLists.txt @@ -129,11 +129,12 @@ add_header_library( HDRS vasprintf_internal.h DEPENDS + libc.hdr.func.malloc + libc.hdr.func.free + libc.hdr.func.realloc libc.src.__support.arg_list libc.src.stdio.printf_core.printf_main libc.src.stdio.printf_core.writer - libc.src.stdlib.malloc - libc.src.stdlib.realloc ) if(NOT (TARGET libc.src.__support.File.file) AND LLVM_LIBC_FULL_BUILD) diff --git a/libc/src/stdio/printf_core/vasprintf_internal.h b/libc/src/stdio/printf_core/vasprintf_internal.h index e3448eebd302b..0e446f856e438 100644 --- a/libc/src/stdio/printf_core/vasprintf_internal.h +++ b/libc/src/stdio/printf_core/vasprintf_internal.h @@ -6,12 +6,14 @@ // //===----------------------------------------------------------------------===// +#include "hdr/func/free.h" +#include "hdr/func/malloc.h" +#include "hdr/func/realloc.h" #include "src/__support/arg_list.h" #include "src/stdio/printf.h" #include "src/stdio/printf_core/core_structs.h" #include "src/stdio/printf_core/printf_main.h" #include "src/stdio/printf_core/writer.h" -#include // malloc, realloc, free namespace LIBC_NAMESPACE_DECL { namespace printf_core { diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt index 2683eefd032b7..4ea894cf48724 100644 --- a/libc/test/src/stdlib/CMakeLists.txt +++ b/libc/test/src/stdlib/CMakeLists.txt @@ -382,7 +382,7 @@ if(LLVM_LIBC_FULL_BUILD) SRCS atexit_test.cpp DEPENDS - libc.include.stdlib + libc.hdr.func._Exit libc.src.stdlib._Exit libc.src.stdlib.exit libc.src.stdlib.atexit @@ -398,6 +398,7 @@ if(LLVM_LIBC_FULL_BUILD) SRCS at_quick_exit_test.cpp DEPENDS + libc.hdr.func._Exit libc.src.stdlib.quick_exit libc.src.stdlib.at_quick_exit libc.src.__support.CPP.array diff --git a/libc/test/src/stdlib/at_quick_exit_test.cpp b/libc/test/src/stdlib/at_quick_exit_test.cpp index e0a258d9fb2d9..8049fe58d29bd 100644 --- a/libc/test/src/stdlib/at_quick_exit_test.cpp +++ b/libc/test/src/stdlib/at_quick_exit_test.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "hdr/func/_Exit.h" #include "src/__support/CPP/array.h" #include "src/__support/CPP/utility.h" #include "src/stdlib/at_quick_exit.h" diff --git a/libc/test/src/stdlib/atexit_test.cpp b/libc/test/src/stdlib/atexit_test.cpp index 8a785ccb8cce2..9e19423f73086 100644 --- a/libc/test/src/stdlib/atexit_test.cpp +++ b/libc/test/src/stdlib/atexit_test.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "hdr/func/_Exit.h" #include "src/__support/CPP/array.h" #include "src/__support/CPP/utility.h" #include "src/stdlib/atexit.h"