[libc-commits] [libc] p/libc malloc decls (PR #127292)
Roland McGrath via libc-commits
libc-commits at lists.llvm.org
Fri Feb 14 17:24:12 PST 2025
https://github.com/frobtech created https://github.com/llvm/llvm-project/pull/127292
- **[libc] Elide extra space in hdrgen function declarations**
- **[libc] Fill out generated malloc.h and related stdlib.h extensions**
>From d1d1790c24a1845cb2c6725b15d2a901476d90af Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Thu, 13 Feb 2025 23:33:37 -0800
Subject: [PATCH 1/2] [libc] Elide extra space in hdrgen function declarations
When the return type's rendering already doesn't end with an
identifier character, such as when it's `T *`, then idiomatic
syntax does not include a space before the `(` and arguments.
---
libc/utils/hdrgen/function.py | 5 ++++-
libc/utils/hdrgen/tests/expected_output/subdir/test.h | 2 ++
libc/utils/hdrgen/tests/input/subdir/test.yaml | 3 +++
3 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/libc/utils/hdrgen/function.py b/libc/utils/hdrgen/function.py
index bccd2c2caa2f5..24b28f7e3c7ff 100644
--- a/libc/utils/hdrgen/function.py
+++ b/libc/utils/hdrgen/function.py
@@ -81,4 +81,7 @@ def collapse(type_string):
def __str__(self):
attrs_str = "".join(f"{attr} " for attr in self.attributes)
arguments_str = ", ".join(self.arguments) if self.arguments else "void"
- return attrs_str + f"{self.return_type} {self.name}({arguments_str})"
+ type_str = str(self.return_type)
+ if type_str[-1].isalnum() or type_str[-1] == "_":
+ type_str += " "
+ return attrs_str + type_str + self.name + "(" + arguments_str + ")"
diff --git a/libc/utils/hdrgen/tests/expected_output/subdir/test.h b/libc/utils/hdrgen/tests/expected_output/subdir/test.h
index e968f92bf4e6d..20bab502e6821 100644
--- a/libc/utils/hdrgen/tests/expected_output/subdir/test.h
+++ b/libc/utils/hdrgen/tests/expected_output/subdir/test.h
@@ -19,6 +19,8 @@ type_a func(type_b) __NOEXCEPT;
void gnufunc(type_a) __NOEXCEPT;
+int *ptrfunc(void) __NOEXCEPT;
+
__END_C_DECLS
#endif // LLVM_LIBC_SUBDIR_TEST_H
diff --git a/libc/utils/hdrgen/tests/input/subdir/test.yaml b/libc/utils/hdrgen/tests/input/subdir/test.yaml
index f325363e09cde..5bc8b29e334f8 100644
--- a/libc/utils/hdrgen/tests/input/subdir/test.yaml
+++ b/libc/utils/hdrgen/tests/input/subdir/test.yaml
@@ -12,3 +12,6 @@ functions:
- type: type_a
standards:
- gnu
+ - name: ptrfunc
+ return_type: int *
+ arguments: []
>From d8f824c153296481f0b996c0e0c02f3a29859d87 Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Fri, 14 Feb 2025 00:49:59 -0800
Subject: [PATCH 2/2] [libc] Fill out generated malloc.h and related stdlib.h
extensions
This updates the generated stdlib.h and malloc.h headers to
include the subsets of extenion functions declared by glibc that
are also supported by Scudo and that use only simple types.
Scudo's extensions not declared by glibc are omitted. glibc's
extensions not implemented by Scudo are omitted. The mallinfo
and mallinfo2 functions are omitted (at least for now) since they
need struct definitions for their return types.
---
libc/include/malloc.yaml | 13 +++++++++++++
libc/include/stdlib-malloc.yaml | 28 ++++++++++++++++++++--------
libc/include/stdlib.yaml | 8 ++++++++
3 files changed, 41 insertions(+), 8 deletions(-)
diff --git a/libc/include/malloc.yaml b/libc/include/malloc.yaml
index bf7678797c023..cd84723557bcb 100644
--- a/libc/include/malloc.yaml
+++ b/libc/include/malloc.yaml
@@ -9,6 +9,12 @@ macros:
- macro_name: M_PURGE_ALL
macro_header: malloc-macros.h
functions:
+ - name: malloc_usable_size
+ standards:
+ - gnu
+ return_type: size_t
+ arguments:
+ - type: void *
- name: mallopt
standards:
- gnu
@@ -16,3 +22,10 @@ functions:
arguments:
- type: int
- type: int
+ - name: pvalloc
+ standards:
+ - bsd
+ - gnu
+ return_type: void *
+ arguments:
+ - type: size_t
diff --git a/libc/include/stdlib-malloc.yaml b/libc/include/stdlib-malloc.yaml
index 648a6e58a90a0..51c11f5602e2c 100644
--- a/libc/include/stdlib-malloc.yaml
+++ b/libc/include/stdlib-malloc.yaml
@@ -1,4 +1,10 @@
# This file has declarations that appear both in <stdlib.h> and in <malloc.h>.
+# These include the subset of GNU extensions that Scudo supports.
+#
+# Note: glibc's <stdlib.h> and <malloc.h> both also have `reallocarray`,
+# which Scudo does not support and is omitted here. (Each of those glibc
+# headers also has related functions the other lacks, but those should be
+# covered separately in stdlib.yaml and malloc.yaml instead.)
functions:
- name: aligned_alloc
@@ -27,6 +33,13 @@ functions:
return_type: void *
arguments:
- type: size_t
+ - name: memalign
+ standards:
+ - gnu
+ return_type: void *
+ arguments:
+ - type: size_t
+ - type: size_t
- name: realloc
standards:
- stdc
@@ -34,11 +47,10 @@ functions:
arguments:
- type: void *
- type: size_t
-
-# Note: glibc's <stdlib.h> and <malloc.h> both have these, which are
-# currently missing here:
-# - name: reallocarray
-# - name: memalign
-# - name: valloc
-# Each of those glibc headers also has related functions the other lacks.
-# Only the common subset is mentioned here for future consideration.
+ - name: valloc
+ standards:
+ - bsd
+ - gnu
+ return_type: void *
+ arguments:
+ - type: size_t
diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml
index 6ada26f020179..8d2b3f357e1a9 100644
--- a/libc/include/stdlib.yaml
+++ b/libc/include/stdlib.yaml
@@ -127,6 +127,14 @@ functions:
arguments:
- type: long long
- type: long long
+ - name: posix_memalign
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: void **
+ - type: size_t
+ - type: size_t
- name: qsort
standards:
- stdc
More information about the libc-commits
mailing list