Skip to content

Commit eee8340

Browse files
Job Henandez LaraPhilippRados
Job Henandez Lara
authored andcommitted
[libc] Add proxy headers to handle memory allocation associated with the header `#include <stdlib.h> (llvm#114690)
This finishes the work from llvm#114453 by adding proxy headers for `malloc`, `realloc`, `free` and `aligned_alloc`.
1 parent 1c5e8f8 commit eee8340

20 files changed

+194
-10
lines changed

libc/hdr/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ add_proxy_header_library(
8181
libc.include.signal
8282
)
8383

84+
add_header_library(stdlib_overlay HDRS stdlib_overlay.h)
85+
8486
add_proxy_header_library(
8587
stdlib_macros
8688
HDRS
@@ -193,3 +195,4 @@ add_proxy_header_library(
193195
)
194196

195197
add_subdirectory(types)
198+
add_subdirectory(func)

libc/hdr/func/CMakeLists.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
add_proxy_header_library(
2+
aligned_alloc
3+
HDRS
4+
aligned_alloc.h
5+
DEPENDS
6+
libc.hdr.stdlib_overlay
7+
FULL_BUILD_DEPENDS
8+
libc.include.stdlib
9+
libc.hdr.types.size_t
10+
)
11+
12+
add_proxy_header_library(
13+
malloc
14+
HDRS
15+
malloc.h
16+
DEPENDS
17+
libc.hdr.stdlib_overlay
18+
FULL_BUILD_DEPENDS
19+
libc.include.stdlib
20+
libc.hdr.types.size_t
21+
)
22+
23+
add_proxy_header_library(
24+
realloc
25+
HDRS
26+
realloc.h
27+
DEPENDS
28+
libc.hdr.stdlib_overlay
29+
FULL_BUILD_DEPENDS
30+
libc.include.stdlib
31+
libc.hdr.types.size_t
32+
)
33+
34+
add_proxy_header_library(
35+
free
36+
HDRS
37+
free.h
38+
DEPENDS
39+
libc.hdr.stdlib_overlay
40+
FULL_BUILD_DEPENDS
41+
libc.include.stdlib
42+
)
43+
44+
add_proxy_header_library(
45+
_Exit
46+
HDRS
47+
_Exit.h
48+
DEPENDS
49+
libc.hdr.stdlib_overlay
50+
FULL_BUILD_DEPENDS
51+
libc.include.stdlib
52+
)

libc/hdr/func/_Exit.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Definition of the _Exit proxy -------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_FUNC_EXIT_H
10+
#define LLVM_LIBC_HDR_FUNC_EXIT_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
extern "C" void _Exit(int);
14+
15+
#else // Overlay mode
16+
17+
#include "hdr/stdlib_overlay.h"
18+
19+
#endif
20+
21+
#endif // LLVM_LIBC_HDR_EXIT_H

libc/hdr/func/aligned_alloc.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of the aligned_alloc.h proxy ---------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_FUNC_ALIGNED_ALLOC_H
10+
#define LLVM_LIBC_HDR_FUNC_ALIGNED_ALLOC_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
#include "hdr/types/size_t.h"
14+
extern "C" void *aligned_alloc(size_t, size_t);
15+
16+
#else // Overlay mode
17+
18+
#include "hdr/stdlib_overlay.h"
19+
20+
#endif
21+
22+
#endif

libc/hdr/func/free.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Definition of the free.h proxy ------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_FUNC_FREE_H
10+
#define LLVM_LIBC_HDR_FUNC_FREE_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
extern "C" void free(void *);
14+
15+
#else // Overlay mode
16+
17+
#include "hdr/stdlib_overlay.h"
18+
19+
#endif
20+
21+
#endif

libc/hdr/func/malloc.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of the malloc.h proxy ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_FUNC_MALLOC_H
10+
#define LLVM_LIBC_HDR_FUNC_MALLOC_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
#include "hdr/types/size_t.h"
14+
extern "C" void *malloc(size_t);
15+
16+
#else // Overlay mode
17+
18+
#include "hdr/stdlib_overlay.h"
19+
20+
#endif
21+
22+
#endif

libc/hdr/func/realloc.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of the realloc.h proxy ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_FUNC_REALLOC_H
10+
#define LLVM_LIBC_HDR_FUNC_REALLOC_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
#include "hdr/types/size_t.h"
14+
extern "C" void *realloc(void *ptr, size_t new_size);
15+
16+
#else // Overlay mode
17+
18+
#include "hdr/stdlib_overlay.h"
19+
20+
#endif
21+
22+
#endif

libc/src/__support/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ add_header_library(
242242
HDRS
243243
char_vector.h
244244
DEPENDS
245+
libc.hdr.func.free
246+
libc.hdr.func.malloc
247+
libc.hdr.func.realloc
245248
libc.src.__support.common
246249
)
247250

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ add_header_library(
8080
HDRS
8181
string.h
8282
DEPENDS
83-
libc.include.stdlib
8483
.string_view
84+
libc.hdr.func.free
85+
libc.hdr.func.malloc
86+
libc.hdr.func.realloc
8587
libc.src.__support.common
8688
libc.src.__support.integer_to_string
8789
libc.src.string.memory_utils.inline_memcpy
@@ -199,7 +201,9 @@ add_object_library(
199201
HDRS
200202
new.h
201203
DEPENDS
202-
libc.include.stdlib
204+
libc.hdr.func.free
205+
libc.hdr.func.malloc
206+
libc.hdr.func.aligned_alloc
203207
libc.src.__support.common
204208
libc.src.__support.macros.properties.os
205209
)

libc/src/__support/CPP/new.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "new.h"
10-
#include <stdlib.h> // For free, etc
10+
#include "hdr/func/free.h"
1111

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

libc/src/__support/CPP/new.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H
1010
#define LLVM_LIBC_SRC___SUPPORT_CPP_NEW_H
1111

12+
#include "hdr/func/aligned_alloc.h"
13+
#include "hdr/func/free.h"
14+
#include "hdr/func/malloc.h"
1215
#include "src/__support/common.h"
1316
#include "src/__support/macros/config.h"
1417
#include "src/__support/macros/properties/os.h"
1518

1619
#include <stddef.h> // For size_t
17-
#include <stdlib.h> // For malloc, free etc.
1820

1921
// Defining members in the std namespace is not preferred. But, we do it here
2022
// so that we can use it to define the operator new which takes std::align_val_t

libc/src/__support/CPP/string.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_STRING_H
1010
#define LLVM_LIBC_SRC___SUPPORT_CPP_STRING_H
1111

12+
#include "hdr/func/free.h"
13+
#include "hdr/func/malloc.h"
14+
#include "hdr/func/realloc.h"
1215
#include "src/__support/CPP/string_view.h"
1316
#include "src/__support/integer_to_string.h" // IntegerToString
1417
#include "src/__support/macros/config.h"
@@ -17,7 +20,6 @@
1720
#include "src/string/string_utils.h" // string_length
1821

1922
#include <stddef.h> // size_t
20-
#include <stdlib.h> // malloc, free
2123

2224
namespace LIBC_NAMESPACE_DECL {
2325
namespace cpp {

libc/src/__support/File/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_object_library(
1818
libc.src.__support.error_or
1919
libc.hdr.types.off_t
2020
libc.hdr.stdio_macros
21+
libc.hdr.func.realloc
2122
)
2223

2324
add_object_library(

libc/src/__support/File/file.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "file.h"
1010

11+
#include "hdr/func/realloc.h"
1112
#include "hdr/stdio_macros.h"
1213
#include "hdr/types/off_t.h"
1314
#include "src/__support/CPP/new.h"

libc/src/__support/char_vector.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_CHARVECTOR_H
1010
#define LLVM_LIBC_SRC___SUPPORT_CHARVECTOR_H
1111

12+
#include "hdr/func/free.h"
13+
#include "hdr/func/malloc.h"
14+
#include "hdr/func/realloc.h"
1215
#include "src/__support/common.h" // LIBC_INLINE
1316
#include "src/__support/macros/config.h"
1417

1518
#include <stddef.h> // size_t
16-
#include <stdlib.h> // malloc, realloc, free
1719

1820
namespace LIBC_NAMESPACE_DECL {
1921

libc/src/stdio/printf_core/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,12 @@ add_header_library(
129129
HDRS
130130
vasprintf_internal.h
131131
DEPENDS
132+
libc.hdr.func.malloc
133+
libc.hdr.func.free
134+
libc.hdr.func.realloc
132135
libc.src.__support.arg_list
133136
libc.src.stdio.printf_core.printf_main
134137
libc.src.stdio.printf_core.writer
135-
libc.src.stdlib.malloc
136-
libc.src.stdlib.realloc
137138
)
138139

139140
if(NOT (TARGET libc.src.__support.File.file) AND LLVM_LIBC_FULL_BUILD)

libc/src/stdio/printf_core/vasprintf_internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "hdr/func/free.h"
10+
#include "hdr/func/malloc.h"
11+
#include "hdr/func/realloc.h"
912
#include "src/__support/arg_list.h"
1013
#include "src/stdio/printf.h"
1114
#include "src/stdio/printf_core/core_structs.h"
1215
#include "src/stdio/printf_core/printf_main.h"
1316
#include "src/stdio/printf_core/writer.h"
14-
#include <stdlib.h> // malloc, realloc, free
1517

1618
namespace LIBC_NAMESPACE_DECL {
1719
namespace printf_core {

libc/test/src/stdlib/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ if(LLVM_LIBC_FULL_BUILD)
382382
SRCS
383383
atexit_test.cpp
384384
DEPENDS
385-
libc.include.stdlib
385+
libc.hdr.func._Exit
386386
libc.src.stdlib._Exit
387387
libc.src.stdlib.exit
388388
libc.src.stdlib.atexit
@@ -398,6 +398,7 @@ if(LLVM_LIBC_FULL_BUILD)
398398
SRCS
399399
at_quick_exit_test.cpp
400400
DEPENDS
401+
libc.hdr.func._Exit
401402
libc.src.stdlib.quick_exit
402403
libc.src.stdlib.at_quick_exit
403404
libc.src.__support.CPP.array

libc/test/src/stdlib/at_quick_exit_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "hdr/func/_Exit.h"
910
#include "src/__support/CPP/array.h"
1011
#include "src/__support/CPP/utility.h"
1112
#include "src/stdlib/at_quick_exit.h"

libc/test/src/stdlib/atexit_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "hdr/func/_Exit.h"
910
#include "src/__support/CPP/array.h"
1011
#include "src/__support/CPP/utility.h"
1112
#include "src/stdlib/atexit.h"

0 commit comments

Comments
 (0)