[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.
This commit is contained in:
Fabio D'Urso 2024-10-03 18:45:23 +02:00 committed by GitHub
parent ef66936df4
commit 2396c46999
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 111 additions and 0 deletions

View File

@ -347,6 +347,13 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wctob
)
if(LLVM_LIBC_INCLUDE_SCUDO)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# malloc.h external entrypoints
libc.src.stdlib.mallopt
)
endif()
set(TARGET_LIBM_ENTRYPOINTS
# fenv.h entrypoints
libc.src.fenv.feclearexcept

View File

@ -11,6 +11,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.inttypes
libc.include.limits
libc.include.link
libc.include.malloc
libc.include.math
libc.include.pthread
libc.include.signal

View File

@ -189,6 +189,13 @@ if(LLVM_LIBC_FULL_BUILD)
)
endif()
if(LLVM_LIBC_INCLUDE_SCUDO)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# malloc.h external entrypoints
libc.src.stdlib.mallopt
)
endif()
set(TARGET_LIBM_ENTRYPOINTS
# fenv.h entrypoints
libc.src.fenv.feclearexcept

View File

@ -4,6 +4,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.fenv
libc.include.float
libc.include.inttypes
libc.include.malloc
libc.include.math
libc.include.search
libc.include.setjmp

View File

@ -346,6 +346,13 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wctob
)
if(LLVM_LIBC_INCLUDE_SCUDO)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# malloc.h external entrypoints
libc.src.stdlib.mallopt
)
endif()
set(TARGET_LIBM_ENTRYPOINTS
# fenv.h entrypoints
libc.src.fenv.feclearexcept

View File

@ -11,6 +11,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.stdint
libc.include.inttypes
libc.include.limits
libc.include.malloc
libc.include.math
libc.include.pthread
libc.include.sched

View File

@ -346,6 +346,13 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wctob
)
if(LLVM_LIBC_INCLUDE_SCUDO)
list(APPEND TARGET_LIBC_ENTRYPOINTS
# malloc.h external entrypoints
libc.src.stdlib.mallopt
)
endif()
set(TARGET_LIBM_ENTRYPOINTS
# fenv.h entrypoints
libc.src.fenv.feclearexcept

View File

@ -13,6 +13,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.inttypes
libc.include.limits
libc.include.link
libc.include.malloc
libc.include.math
libc.include.pthread
libc.include.sched

View File

@ -146,6 +146,16 @@ add_header_macro(
.llvm-libc-macros.limits_macros
)
add_header_macro(
malloc
../libc/newhdrgen/yaml/malloc.yaml
malloc.h.def
malloc.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-macros.malloc_macros
)
add_header_macro(
math
../libc/newhdrgen/yaml/math.yaml

View File

@ -109,6 +109,12 @@ add_macro_header(
link-macros.h
)
add_macro_header(
malloc_macros
HDR
malloc-macros.h
)
add_macro_header(
math_macros
HDR

View File

@ -0,0 +1,17 @@
//===-- Definition of macros to be used with malloc functions -------------===//
//
// 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_MACROS_MALLOC_MACROS_H
#define LLVM_LIBC_MACROS_MALLOC_MACROS_H
// Note: these values only make sense when Scudo is used as the memory
// allocator.
#define M_PURGE (-101)
#define M_PURGE_ALL (-104)
#endif // LLVM_LIBC_MACROS_MALLOC_MACROS_H

17
libc/include/malloc.h.def Normal file
View File

@ -0,0 +1,17 @@
//===-- C standard library header malloc.h --------------------------------===//
//
// 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_MALLOC_H
#define LLVM_LIBC_MALLOC_H
#include "__llvm-libc-common.h"
#include "llvm-libc-macros/malloc-macros.h"
%%public_api()
#endif // LLVM_LIBC_MALLOC_H

View File

@ -0,0 +1,13 @@
header: malloc.h
macros: []
types: []
enums: []
objects: []
functions:
- name: mallopt
standards:
- GNUExtensions
return_type: int
arguments:
- type: int
- type: int

View File

@ -22,6 +22,16 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
]
>;
HeaderSpec Malloc = HeaderSpec<
"malloc.h",
[], // Macros
[], // Types
[], // Enumerations
[
FunctionSpec<"mallopt", RetValSpec<IntType>, [ArgSpec<IntType>, ArgSpec<IntType>]>,
]
>;
HeaderSpec Math = HeaderSpec<
"math.h",
[], // Macros
@ -291,6 +301,7 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
let Headers = [
CType,
FEnv,
Malloc,
Math,
PThread,
Sched,

View File

@ -384,6 +384,11 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
DEPENDS
${SCUDO_DEPS}
)
add_entrypoint_external(
mallopt
DEPENDS
${SCUDO_DEPS}
)
else()
# Only use freelist malloc for baremetal targets.
add_entrypoint_object(