[libc-commits] [libc] 7703770 - [libc] Add mallinfo2() as external entrypoint. (#241662)

via libc-commits libc-commits at lists.llvm.org
Thu Nov 12 09:46:38 PST 2026


Author: Alexey Samsonov
Date: 2026-11-12T09:46:32-08:00
New Revision: 770377056131c2a4d315dd9c918bd063c6627d80

URL: https://github.com/llvm/llvm-project/commit/770377056131c2a4d315dd9c918bd063c6627d80
DIFF: https://github.com/llvm/llvm-project/commit/770377056131c2a4d315dd9c918bd063c6627d80.diff

LOG: [libc] Add mallinfo2() as external entrypoint. (#241662)

`mallinfo2` is the newer interface (available since glibc 2.33) that
replaces
legacy/deprecated `mallinfo`, and uses `struct mallinfo2` with properly
sized struct fields.

Scudo allocator implements this interface, and some of the code on Linux
tests for / uses this functionality from `<malloc.h>` header, so let us
define
it as an external entrypoint in LLVM-libc as well. One of such users
is LLVM itself, in `Process::GetMallocUsage()`.

Assisted-by: automated tooling, human-reviewed.

Added: 
    libc/include/llvm-libc-types/struct_mallinfo2.h

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/arm/entrypoints.txt
    libc/config/linux/riscv/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-types/CMakeLists.txt
    libc/include/malloc.yaml
    libc/src/stdlib/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index dcdea265b5362..aed6852ff9b06 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -571,6 +571,7 @@ set(TARGET_LIBC_ENTRYPOINTS
 if(LLVM_LIBC_INCLUDE_SCUDO)
   list(APPEND TARGET_LIBC_ENTRYPOINTS
     # malloc.h external entrypoints
+    libc.src.stdlib.mallinfo2
     libc.src.stdlib.mallopt
   )
 endif()

diff  --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index ebca30742d203..77edf1dc5d071 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -257,7 +257,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.chown
     libc.src.unistd.chroot
     libc.src.unistd.close
-    libc.src.unistd.close_range
+    libc.src.unistd.close_range
     libc.src.unistd.copy_file_range
     libc.src.unistd.dup
     libc.src.unistd.dup2
@@ -361,6 +361,7 @@ endif()
 if(LLVM_LIBC_INCLUDE_SCUDO)
   list(APPEND TARGET_LIBC_ENTRYPOINTS
     # malloc.h external entrypoints
+    libc.src.stdlib.mallinfo2
     libc.src.stdlib.mallopt
   )
 endif()

diff  --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index e53852bba25c3..dda4f01e36978 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -652,6 +652,7 @@ set(TARGET_LIBC_ENTRYPOINTS
 if(LLVM_LIBC_INCLUDE_SCUDO)
   list(APPEND TARGET_LIBC_ENTRYPOINTS
     # malloc.h external entrypoints
+    libc.src.stdlib.mallinfo2
     libc.src.stdlib.mallopt
   )
 endif()

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9c72f4c2a2f07..eb5dcd44c8fb7 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -652,6 +652,7 @@ set(TARGET_LIBC_ENTRYPOINTS
 if(LLVM_LIBC_INCLUDE_SCUDO)
   list(APPEND TARGET_LIBC_ENTRYPOINTS
     # malloc.h external entrypoints
+    libc.src.stdlib.mallinfo2
     libc.src.stdlib.mallopt
   )
 endif()

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index ddca7cd3749df..69ab3e2e9a4dc 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -227,6 +227,8 @@ add_header_macro(
   DEPENDS
     .llvm_libc_common_h
     .llvm-libc-macros.malloc_macros
+    .llvm-libc-types.size_t
+    .llvm-libc-types.struct_mallinfo2
 )
 
 add_header_macro(

diff  --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 2973e5758b4e1..22add8bdd81f0 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -380,6 +380,7 @@ add_header(
 )
 add_header(locale_t HDR locale_t.h)
 add_header(struct_lconv HDR struct_lconv.h)
+add_header(struct_mallinfo2 HDR struct_mallinfo2.h DEPENDS .size_t)
 add_header(int_hk_t HDR int_hk_t.h)
 add_header(int_hr_t HDR int_hr_t.h)
 add_header(int_k_t HDR int_k_t.h)

diff  --git a/libc/include/llvm-libc-types/struct_mallinfo2.h b/libc/include/llvm-libc-types/struct_mallinfo2.h
new file mode 100644
index 0000000000000..6842e26c98ca6
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_mallinfo2.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of struct mallinfo2.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_MALLINFO2_H
+#define LLVM_LIBC_TYPES_STRUCT_MALLINFO2_H
+
+#include "size_t.h"
+
+struct mallinfo2 {
+  size_t arena;
+  size_t ordblks;
+  size_t smblks;
+  size_t hblks;
+  size_t hblkhd;
+  size_t usmblks;
+  size_t fsmblks;
+  size_t uordblks;
+  size_t fordblks;
+  size_t keepcost;
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_MALLINFO2_H

diff  --git a/libc/include/malloc.yaml b/libc/include/malloc.yaml
index cd84723557bcb..aa71bd65efcaf 100644
--- a/libc/include/malloc.yaml
+++ b/libc/include/malloc.yaml
@@ -8,7 +8,17 @@ macros:
     macro_header: malloc-macros.h
   - macro_name: M_PURGE_ALL
     macro_header: malloc-macros.h
+types:
+  - type_name: struct_mallinfo2
+    standards:
+      - gnu
 functions:
+  - name: mallinfo2
+    standards:
+      - gnu
+    return_type: struct mallinfo2
+    arguments:
+      - type: void
   - name: malloc_usable_size
     standards:
       - gnu

diff  --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index f98912368200c..9a2e9bf36ce5b 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -576,6 +576,11 @@ if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
       DEPENDS
         ${SCUDO_DEPS}
     )
+    add_entrypoint_external(
+      mallinfo2
+      DEPENDS
+        ${SCUDO_DEPS}
+    )
     add_entrypoint_external(
       mallopt
       DEPENDS
@@ -605,6 +610,9 @@ if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
     add_entrypoint_external(
       free
     )
+    add_entrypoint_external(
+      mallinfo2
+    )
     add_entrypoint_external(
       mallopt
     )


        


More information about the libc-commits mailing list