Skip to content

Commit 9cfe302

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

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

0 commit comments

Comments
 (0)