Skip to content

Commit 2396c46

Browse files
authored
[libc] Add malloc.h header defining mallopt (#110908)
This patch adds the malloc.h header, declaring Scudo's mallopt entrypoint when built LLVM_LIBC_INCLUDE_SCUDO, as well as two constants that can be passed to it (M_PURGE and M_PURGE_ALL). Due to limitations of the current build system, only the declaration of mallopt is gated by LLVM_LIBC_INCLUDE_SCUDO, and the two new constants are defined irrespectively of it. We may need to refine this in the future. Note that some allocators other than Scudo may offer a mallopt implementation too (e.g. man 3 mallopt), albeit with different supported input values. This patch only supports the specific case of LLVM_LIBC_INCLUDE_SCUDO.
1 parent ef66936 commit 2396c46

File tree

15 files changed

+111
-0
lines changed

15 files changed

+111
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ set(TARGET_LIBC_ENTRYPOINTS
347347
libc.src.wchar.wctob
348348
)
349349

350+
if(LLVM_LIBC_INCLUDE_SCUDO)
351+
list(APPEND TARGET_LIBC_ENTRYPOINTS
352+
# malloc.h external entrypoints
353+
libc.src.stdlib.mallopt
354+
)
355+
endif()
356+
350357
set(TARGET_LIBM_ENTRYPOINTS
351358
# fenv.h entrypoints
352359
libc.src.fenv.feclearexcept

libc/config/linux/aarch64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(TARGET_PUBLIC_HEADERS
1111
libc.include.inttypes
1212
libc.include.limits
1313
libc.include.link
14+
libc.include.malloc
1415
libc.include.math
1516
libc.include.pthread
1617
libc.include.signal

libc/config/linux/arm/entrypoints.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ if(LLVM_LIBC_FULL_BUILD)
189189
)
190190
endif()
191191

192+
if(LLVM_LIBC_INCLUDE_SCUDO)
193+
list(APPEND TARGET_LIBC_ENTRYPOINTS
194+
# malloc.h external entrypoints
195+
libc.src.stdlib.mallopt
196+
)
197+
endif()
198+
192199
set(TARGET_LIBM_ENTRYPOINTS
193200
# fenv.h entrypoints
194201
libc.src.fenv.feclearexcept

libc/config/linux/arm/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(TARGET_PUBLIC_HEADERS
44
libc.include.fenv
55
libc.include.float
66
libc.include.inttypes
7+
libc.include.malloc
78
libc.include.math
89
libc.include.search
910
libc.include.setjmp

libc/config/linux/riscv/entrypoints.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ set(TARGET_LIBC_ENTRYPOINTS
346346
libc.src.wchar.wctob
347347
)
348348

349+
if(LLVM_LIBC_INCLUDE_SCUDO)
350+
list(APPEND TARGET_LIBC_ENTRYPOINTS
351+
# malloc.h external entrypoints
352+
libc.src.stdlib.mallopt
353+
)
354+
endif()
355+
349356
set(TARGET_LIBM_ENTRYPOINTS
350357
# fenv.h entrypoints
351358
libc.src.fenv.feclearexcept

libc/config/linux/riscv/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(TARGET_PUBLIC_HEADERS
1111
libc.include.stdint
1212
libc.include.inttypes
1313
libc.include.limits
14+
libc.include.malloc
1415
libc.include.math
1516
libc.include.pthread
1617
libc.include.sched

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ set(TARGET_LIBC_ENTRYPOINTS
346346
libc.src.wchar.wctob
347347
)
348348

349+
if(LLVM_LIBC_INCLUDE_SCUDO)
350+
list(APPEND TARGET_LIBC_ENTRYPOINTS
351+
# malloc.h external entrypoints
352+
libc.src.stdlib.mallopt
353+
)
354+
endif()
355+
349356
set(TARGET_LIBM_ENTRYPOINTS
350357
# fenv.h entrypoints
351358
libc.src.fenv.feclearexcept

libc/config/linux/x86_64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(TARGET_PUBLIC_HEADERS
1313
libc.include.inttypes
1414
libc.include.limits
1515
libc.include.link
16+
libc.include.malloc
1617
libc.include.math
1718
libc.include.pthread
1819
libc.include.sched

libc/include/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ add_header_macro(
146146
.llvm-libc-macros.limits_macros
147147
)
148148

149+
add_header_macro(
150+
malloc
151+
../libc/newhdrgen/yaml/malloc.yaml
152+
malloc.h.def
153+
malloc.h
154+
DEPENDS
155+
.llvm_libc_common_h
156+
.llvm-libc-macros.malloc_macros
157+
)
158+
149159
add_header_macro(
150160
math
151161
../libc/newhdrgen/yaml/math.yaml

libc/include/llvm-libc-macros/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ add_macro_header(
109109
link-macros.h
110110
)
111111

112+
add_macro_header(
113+
malloc_macros
114+
HDR
115+
malloc-macros.h
116+
)
117+
112118
add_macro_header(
113119
math_macros
114120
HDR
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Definition of macros to be used with malloc functions -------------===//
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_MACROS_MALLOC_MACROS_H
10+
#define LLVM_LIBC_MACROS_MALLOC_MACROS_H
11+
12+
// Note: these values only make sense when Scudo is used as the memory
13+
// allocator.
14+
#define M_PURGE (-101)
15+
#define M_PURGE_ALL (-104)
16+
17+
#endif // LLVM_LIBC_MACROS_MALLOC_MACROS_H

libc/include/malloc.h.def

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- C standard library header malloc.h --------------------------------===//
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_MALLOC_H
10+
#define LLVM_LIBC_MALLOC_H
11+
12+
#include "__llvm-libc-common.h"
13+
#include "llvm-libc-macros/malloc-macros.h"
14+
15+
%%public_api()
16+
17+
#endif // LLVM_LIBC_MALLOC_H

libc/newhdrgen/yaml/malloc.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
header: malloc.h
2+
macros: []
3+
types: []
4+
enums: []
5+
objects: []
6+
functions:
7+
- name: mallopt
8+
standards:
9+
- GNUExtensions
10+
return_type: int
11+
arguments:
12+
- type: int
13+
- type: int

libc/spec/gnu_ext.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
2222
]
2323
>;
2424

25+
HeaderSpec Malloc = HeaderSpec<
26+
"malloc.h",
27+
[], // Macros
28+
[], // Types
29+
[], // Enumerations
30+
[
31+
FunctionSpec<"mallopt", RetValSpec<IntType>, [ArgSpec<IntType>, ArgSpec<IntType>]>,
32+
]
33+
>;
34+
2535
HeaderSpec Math = HeaderSpec<
2636
"math.h",
2737
[], // Macros
@@ -291,6 +301,7 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
291301
let Headers = [
292302
CType,
293303
FEnv,
304+
Malloc,
294305
Math,
295306
PThread,
296307
Sched,

libc/src/stdlib/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
384384
DEPENDS
385385
${SCUDO_DEPS}
386386
)
387+
add_entrypoint_external(
388+
mallopt
389+
DEPENDS
390+
${SCUDO_DEPS}
391+
)
387392
else()
388393
# Only use freelist malloc for baremetal targets.
389394
add_entrypoint_object(

0 commit comments

Comments
 (0)