Skip to content

[libc] Add proxy headers to handle memory allocation associated with the header `#include <stdlib.h> #114690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
9 commits merged into from Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -193,3 +195,4 @@ add_proxy_header_library(
)

add_subdirectory(types)
add_subdirectory(func)
52 changes: 52 additions & 0 deletions libc/hdr/func/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t header dependency.

libc.hdr.types.size_t
)

add_proxy_header_library(
realloc
HDRS
realloc.h
DEPENDS
libc.hdr.stdlib_overlay
FULL_BUILD_DEPENDS
libc.include.stdlib
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_t header dependency

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
)
21 changes: 21 additions & 0 deletions libc/hdr/func/_Exit.h
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions libc/hdr/func/aligned_alloc.h
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions libc/hdr/func/free.h
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions libc/hdr/func/malloc.h
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions libc/hdr/func/realloc.h
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions libc/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
8 changes: 6 additions & 2 deletions libc/src/__support/CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "new.h"
#include <stdlib.h> // For free, etc
#include "hdr/func/free.h"

void operator delete(void *mem) noexcept { ::free(mem); }

Expand Down
4 changes: 3 additions & 1 deletion libc/src/__support/CPP/new.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stddef.h> // For size_t
#include <stdlib.h> // 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
Expand Down
4 changes: 3 additions & 1 deletion libc/src/__support/CPP/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,7 +20,6 @@
#include "src/string/string_utils.h" // string_length

#include <stddef.h> // size_t
#include <stdlib.h> // malloc, free

namespace LIBC_NAMESPACE_DECL {
namespace cpp {
Expand Down
1 change: 1 addition & 0 deletions libc/src/__support/File/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions libc/src/__support/File/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion libc/src/__support/char_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stddef.h> // size_t
#include <stdlib.h> // malloc, realloc, free

namespace LIBC_NAMESPACE_DECL {

Expand Down
5 changes: 3 additions & 2 deletions libc/src/stdio/printf_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion libc/src/stdio/printf_core/vasprintf_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h> // malloc, realloc, free

namespace LIBC_NAMESPACE_DECL {
namespace printf_core {
Expand Down
3 changes: 2 additions & 1 deletion libc/test/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/test/src/stdlib/at_quick_exit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions libc/test/src/stdlib/atexit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading