diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 283c7bd77b4e8..72a4d9b65ce99 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -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 diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt index ebe053af99d80..f98af5744685f 100644 --- a/libc/config/linux/aarch64/headers.txt +++ b/libc/config/linux/aarch64/headers.txt @@ -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 diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt index 1be9a872dd2f7..2ddb7aeefe48e 100644 --- a/libc/config/linux/arm/entrypoints.txt +++ b/libc/config/linux/arm/entrypoints.txt @@ -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 diff --git a/libc/config/linux/arm/headers.txt b/libc/config/linux/arm/headers.txt index 84078a947031a..6576db1f85269 100644 --- a/libc/config/linux/arm/headers.txt +++ b/libc/config/linux/arm/headers.txt @@ -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 diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt index 8312b2c453f23..b2e1aefccef66 100644 --- a/libc/config/linux/riscv/entrypoints.txt +++ b/libc/config/linux/riscv/entrypoints.txt @@ -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 diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt index 0294f62bc2f7a..41c343f71998b 100644 --- a/libc/config/linux/riscv/headers.txt +++ b/libc/config/linux/riscv/headers.txt @@ -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 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 806ccbcdf820a..0daf3ccacbb27 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -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 diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt index 881e149d9c40d..e0c04b381492d 100644 --- a/libc/config/linux/x86_64/headers.txt +++ b/libc/config/linux/x86_64/headers.txt @@ -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 diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt index 16c2ac5124c84..1f3cb59f69e96 100644 --- a/libc/include/CMakeLists.txt +++ b/libc/include/CMakeLists.txt @@ -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 diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt index d28d19330e61b..2ba437c8437f2 100644 --- a/libc/include/llvm-libc-macros/CMakeLists.txt +++ b/libc/include/llvm-libc-macros/CMakeLists.txt @@ -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 diff --git a/libc/include/llvm-libc-macros/malloc-macros.h b/libc/include/llvm-libc-macros/malloc-macros.h new file mode 100644 index 0000000000000..65eddfc5e86d6 --- /dev/null +++ b/libc/include/llvm-libc-macros/malloc-macros.h @@ -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 diff --git a/libc/include/malloc.h.def b/libc/include/malloc.h.def new file mode 100644 index 0000000000000..2a2bad0f13c3e --- /dev/null +++ b/libc/include/malloc.h.def @@ -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 diff --git a/libc/newhdrgen/yaml/malloc.yaml b/libc/newhdrgen/yaml/malloc.yaml new file mode 100644 index 0000000000000..8db4f3aebb9b3 --- /dev/null +++ b/libc/newhdrgen/yaml/malloc.yaml @@ -0,0 +1,13 @@ +header: malloc.h +macros: [] +types: [] +enums: [] +objects: [] +functions: + - name: mallopt + standards: + - GNUExtensions + return_type: int + arguments: + - type: int + - type: int diff --git a/libc/spec/gnu_ext.td b/libc/spec/gnu_ext.td index b2a2b8af104d8..64121aed9574f 100644 --- a/libc/spec/gnu_ext.td +++ b/libc/spec/gnu_ext.td @@ -22,6 +22,16 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> { ] >; + HeaderSpec Malloc = HeaderSpec< + "malloc.h", + [], // Macros + [], // Types + [], // Enumerations + [ + FunctionSpec<"mallopt", RetValSpec, [ArgSpec, ArgSpec]>, + ] + >; + HeaderSpec Math = HeaderSpec< "math.h", [], // Macros @@ -291,6 +301,7 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> { let Headers = [ CType, FEnv, + Malloc, Math, PThread, Sched, diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt index 1b5b2cb155264..d997cd41e630f 100644 --- a/libc/src/stdlib/CMakeLists.txt +++ b/libc/src/stdlib/CMakeLists.txt @@ -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(