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

Fabio D'Urso via libc-commits libc-commits at lists.llvm.org
Wed Oct 2 11:31:21 PDT 2024


https://github.com/fabio-d created https://github.com/llvm/llvm-project/pull/110908

None

>From 8939130894edad03fbab5523c343efab87444775 Mon Sep 17 00:00:00 2001
From: Fabio D'Urso <fdurso at google.com>
Date: Mon, 30 Sep 2024 16:22:56 +0200
Subject: [PATCH] [libc] Add malloc.h header defining mallopt

---
 libc/config/linux/aarch64/entrypoints.txt     |  7 +++++++
 libc/config/linux/aarch64/headers.txt         |  1 +
 libc/config/linux/arm/entrypoints.txt         |  7 +++++++
 libc/config/linux/arm/headers.txt             |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  7 +++++++
 libc/config/linux/riscv/headers.txt           |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  7 +++++++
 libc/config/linux/x86_64/headers.txt          |  1 +
 libc/include/CMakeLists.txt                   | 10 ++++++++++
 libc/include/llvm-libc-macros/CMakeLists.txt  |  6 ++++++
 libc/include/llvm-libc-macros/malloc-macros.h | 17 +++++++++++++++++
 libc/include/malloc.h.def                     | 17 +++++++++++++++++
 libc/newhdrgen/yaml/malloc.yaml               | 13 +++++++++++++
 libc/spec/gnu_ext.td                          | 11 +++++++++++
 libc/src/stdlib/CMakeLists.txt                |  5 +++++
 15 files changed, 111 insertions(+)
 create mode 100644 libc/include/llvm-libc-macros/malloc-macros.h
 create mode 100644 libc/include/malloc.h.def
 create mode 100644 libc/newhdrgen/yaml/malloc.yaml

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