[libc-commits] [libc] 9cfe302 - [libc] Add proxy headers to handle memory allocation associated with the header `#include <stdlib.h> (#114690)

via libc-commits libc-commits at lists.llvm.org
Sun Nov 3 11:28:27 PST 2024


Author: Job Henandez Lara
Date: 2024-11-03T11:28:24-08:00
New Revision: 9cfe3028ca7977fb582fa3b15b875e8772fc8fc0

URL: https://github.com/llvm/llvm-project/commit/9cfe3028ca7977fb582fa3b15b875e8772fc8fc0
DIFF: https://github.com/llvm/llvm-project/commit/9cfe3028ca7977fb582fa3b15b875e8772fc8fc0.diff

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

This finishes the work from
https://github.com/llvm/llvm-project/pull/114453 by adding proxy headers
for `malloc`, `realloc`, `free` and `aligned_alloc`.

Added: 
    libc/hdr/func/CMakeLists.txt
    libc/hdr/func/_Exit.h
    libc/hdr/func/aligned_alloc.h
    libc/hdr/func/free.h
    libc/hdr/func/malloc.h
    libc/hdr/func/realloc.h

Modified: 
    libc/hdr/CMakeLists.txt
    libc/src/__support/CMakeLists.txt
    libc/src/__support/CPP/CMakeLists.txt
    libc/src/__support/CPP/new.cpp
    libc/src/__support/CPP/new.h
    libc/src/__support/CPP/string.h
    libc/src/__support/File/CMakeLists.txt
    libc/src/__support/File/file.cpp
    libc/src/__support/char_vector.h
    libc/src/stdio/printf_core/CMakeLists.txt
    libc/src/stdio/printf_core/vasprintf_internal.h
    libc/test/src/stdlib/CMakeLists.txt
    libc/test/src/stdlib/at_quick_exit_test.cpp
    libc/test/src/stdlib/atexit_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index c63eadab6f5d77..93da271f5e040b 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 00000000000000..714b7ec9b48809
--- /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 00000000000000..575b0426e508c6
--- /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 00000000000000..b3436dfee1f233
--- /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 00000000000000..b1190a777da327
--- /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 00000000000000..b395f41f2bce26
--- /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 00000000000000..0096045e8330b5
--- /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 bdf839e7622d5f..c44e333645e22e 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -242,6 +242,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 774668be42e56d..15fad9de0ed6d2 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 8316329fb10b0b..65f80de3734851 100644
--- a/libc/src/__support/CPP/new.cpp
+++ b/libc/src/__support/CPP/new.cpp
@@ -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); }
 

diff  --git a/libc/src/__support/CPP/new.h b/libc/src/__support/CPP/new.h
index c1b6b95033f84c..8694d9c4755075 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 <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

diff  --git a/libc/src/__support/CPP/string.h b/libc/src/__support/CPP/string.h
index 64d4c276e84281..dbc0ae04e5e6ff 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 <stddef.h> // size_t
-#include <stdlib.h> // 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 1b390a12424d04..5a4af5e70e2894 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 51811a27c1acd6..972249fef96bcf 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 df109da5d81278..d0837a8af3f467 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 <stddef.h> // size_t
-#include <stdlib.h> // 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 8172fda1e866ed..9eaffe2f7ed621 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 e3448eebd302b7..0e446f856e4381 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 <stdlib.h> // 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 2683eefd032b76..4ea894cf48724d 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 e0a258d9fb2d96..8049fe58d29bd3 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 8a785ccb8cce20..9e19423f730860 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"


        


More information about the libc-commits mailing list