Skip to content

[libc] Add malloc.h header defining mallopt #110908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2024

Conversation

fabio-d
Copy link
Contributor

@fabio-d fabio-d commented Oct 2, 2024

No description provided.

@llvmbot llvmbot added the libc label Oct 2, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 2, 2024

@llvm/pr-subscribers-libc

Author: Fabio D'Urso (fabio-d)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/110908.diff

15 Files Affected:

  • (modified) libc/config/linux/aarch64/entrypoints.txt (+7)
  • (modified) libc/config/linux/aarch64/headers.txt (+1)
  • (modified) libc/config/linux/arm/entrypoints.txt (+7)
  • (modified) libc/config/linux/arm/headers.txt (+1)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+7)
  • (modified) libc/config/linux/riscv/headers.txt (+1)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+7)
  • (modified) libc/config/linux/x86_64/headers.txt (+1)
  • (modified) libc/include/CMakeLists.txt (+10)
  • (modified) libc/include/llvm-libc-macros/CMakeLists.txt (+6)
  • (added) libc/include/llvm-libc-macros/malloc-macros.h (+17)
  • (added) libc/include/malloc.h.def (+17)
  • (added) libc/newhdrgen/yaml/malloc.yaml (+13)
  • (modified) libc/spec/gnu_ext.td (+11)
  • (modified) libc/src/stdlib/CMakeLists.txt (+5)
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(

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@michaelrj-google
Copy link
Contributor

please add a description before landing

@fabio-d fabio-d merged commit 2396c46 into llvm:main Oct 3, 2024
9 checks passed
@fabio-d fabio-d deleted the libc-malloc-mallopt branch October 3, 2024 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants