[libc-commits] [libc] 2396c46 - [libc] Add malloc.h header defining mallopt (#110908)

via libc-commits libc-commits at lists.llvm.org
Thu Oct 3 09:45:26 PDT 2024


Author: Fabio D'Urso
Date: 2024-10-03T18:45:23+02:00
New Revision: 2396c469994e094f7a146f4fa9f29990904ed73b

URL: https://github.com/llvm/llvm-project/commit/2396c469994e094f7a146f4fa9f29990904ed73b
DIFF: https://github.com/llvm/llvm-project/commit/2396c469994e094f7a146f4fa9f29990904ed73b.diff

LOG: [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.

Added: 
    libc/include/llvm-libc-macros/malloc-macros.h
    libc/include/malloc.h.def
    libc/newhdrgen/yaml/malloc.yaml

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/aarch64/headers.txt
    libc/config/linux/arm/entrypoints.txt
    libc/config/linux/arm/headers.txt
    libc/config/linux/riscv/entrypoints.txt
    libc/config/linux/riscv/headers.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/config/linux/x86_64/headers.txt
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-macros/CMakeLists.txt
    libc/spec/gnu_ext.td
    libc/src/stdlib/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 283c7bd77b4e8e..72a4d9b65ce990 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 ebe053af99d803..f98af5744685fc 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 1be9a872dd2f7f..2ddb7aeefe48ec 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 84078a947031ac..6576db1f852691 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 8312b2c453f231..b2e1aefccef66f 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 0294f62bc2f7a0..41c343f71998b9 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 806ccbcdf820a0..0daf3ccacbb270 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 881e149d9c40d3..e0c04b381492d0 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 16c2ac5124c843..1f3cb59f69e96e 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 d28d19330e61b5..2ba437c8437f28 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 00000000000000..65eddfc5e86d65
--- /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 00000000000000..2a2bad0f13c3e8
--- /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 00000000000000..8db4f3aebb9b31
--- /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 b2a2b8af104d83..64121aed9574f2 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<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,

diff  --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 1b5b2cb1552646..d997cd41e630fa 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(


        


More information about the libc-commits mailing list