Skip to content

[libc] Share hdrgen declarations between stdlib.h and malloc.h #127278

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 2 commits into from
Feb 15, 2025

Conversation

frobtech
Copy link
Contributor

This uses the new merge_yaml_files feature in hdrgen to share the
source of truth for the malloc suite of functions declared in
both stdlib.h and in malloc.h (without either header including
the other). It also modernizes the malloc.yaml definition a bit,
including dropping the custom template malloc.h.def file in favor
of using the explicit macros list to generate the includes.

This uses the new merge_yaml_files feature in hdrgen to share the
source of truth for the malloc suite of functions declared in
both stdlib.h and in malloc.h (without either header including
the other).  It also modernizes the malloc.yaml definition a bit,
including dropping the custom template malloc.h.def file in favor
of using the explicit macros list to generate the includes.
@frobtech frobtech requested review from Caslyn and fabio-d February 14, 2025 23:39
@frobtech frobtech marked this pull request as ready for review February 14, 2025 23:40
@llvmbot llvmbot added the libc label Feb 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 14, 2025

@llvm/pr-subscribers-libc

Author: Roland McGrath (frobtech)

Changes

This uses the new merge_yaml_files feature in hdrgen to share the
source of truth for the malloc suite of functions declared in
both stdlib.h and in malloc.h (without either header including
the other). It also modernizes the malloc.yaml definition a bit,
including dropping the custom template malloc.h.def file in favor
of using the explicit macros list to generate the includes.


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

4 Files Affected:

  • (removed) libc/include/malloc.h.def (-17)
  • (modified) libc/include/malloc.yaml (+10-6)
  • (added) libc/include/stdlib-malloc.yaml (+44)
  • (modified) libc/include/stdlib.yaml (+2-33)
diff --git a/libc/include/malloc.h.def b/libc/include/malloc.h.def
deleted file mode 100644
index 2a2bad0f13c3e..0000000000000
--- a/libc/include/malloc.h.def
+++ /dev/null
@@ -1,17 +0,0 @@
-//===-- 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/include/malloc.yaml b/libc/include/malloc.yaml
index ec73c9090f729..bf7678797c023 100644
--- a/libc/include/malloc.yaml
+++ b/libc/include/malloc.yaml
@@ -1,13 +1,17 @@
 header: malloc.h
-header_template: malloc.h.def
-macros: []
-types: []
-enums: []
-objects: []
+standards:
+  - gnu
+merge_yaml_files:
+  - stdlib-malloc.yaml
+macros:
+  - macro_name: M_PURGE
+    macro_header: malloc-macros.h
+  - macro_name: M_PURGE_ALL
+    macro_header: malloc-macros.h
 functions:
   - name: mallopt
     standards:
-      - GNUExtensions
+      - gnu
     return_type: int
     arguments:
       - type: int
diff --git a/libc/include/stdlib-malloc.yaml b/libc/include/stdlib-malloc.yaml
new file mode 100644
index 0000000000000..648a6e58a90a0
--- /dev/null
+++ b/libc/include/stdlib-malloc.yaml
@@ -0,0 +1,44 @@
+# This file has declarations that appear both in <stdlib.h> and in <malloc.h>.
+
+functions:
+  - name: aligned_alloc
+    standards:
+      - stdc
+    return_type: void *
+    arguments:
+      - type: size_t
+      - type: size_t
+  - name: calloc
+    standards:
+      - stdc
+    return_type: void *
+    arguments:
+      - type: size_t
+      - type: size_t
+  - name: free
+    standards:
+      - stdc
+    return_type: void
+    arguments:
+      - type: void *
+  - name: malloc
+    standards:
+      - stdc
+    return_type: void *
+    arguments:
+      - type: size_t
+  - name: realloc
+    standards:
+      - stdc
+    return_type: void *
+    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.
diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml
index c231a797bd445..6ada26f020179 100644
--- a/libc/include/stdlib.yaml
+++ b/libc/include/stdlib.yaml
@@ -2,6 +2,8 @@ header: stdlib.h
 header_template: stdlib.h.def
 standards:
   - stdc
+merge_yaml_files:
+  - stdlib-malloc.yaml
 macros: []
 types:
   - type_name: __atexithandler_t
@@ -34,13 +36,6 @@ functions:
     return_type: int
     arguments:
       - type: int
-  - name: aligned_alloc
-    standards:
-      - stdc
-    return_type: void *
-    arguments:
-      - type: size_t
-      - type: size_t
   - name: at_quick_exit
     standards:
       - stdc
@@ -87,13 +82,6 @@ functions:
       - type: size_t
       - type: size_t
       - type: __bsearchcompare_t
-  - name: calloc
-    standards:
-      - stdc
-    return_type: void *
-    arguments:
-      - type: size_t
-      - type: size_t
   - name: div
     standards:
       - stdc
@@ -107,12 +95,6 @@ functions:
     return_type: _Noreturn void
     arguments:
       - type: int
-  - name: free
-    standards:
-      - stdc
-    return_type: void
-    arguments:
-      - type: void *
   - name: getenv
     standards:
       - stdc
@@ -145,12 +127,6 @@ functions:
     arguments:
       - type: long long
       - type: long long
-  - name: malloc
-    standards:
-      - stdc
-    return_type: void *
-    arguments:
-      - type: size_t
   - name: qsort
     standards:
       - stdc
@@ -182,13 +158,6 @@ functions:
     return_type: int
     arguments:
       - type: void
-  - name: realloc
-    standards:
-      - stdc
-    return_type: void *
-    arguments:
-      - type: void *
-      - type: size_t
   - name: srand
     standards:
       - stdc

# - 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for this comment!

@frobtech frobtech merged commit 625cb5a into llvm:main Feb 15, 2025
14 of 16 checks passed
@frobtech frobtech deleted the p/libc-malloc-stdlib branch February 15, 2025 00:46
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…127278)

This uses the new merge_yaml_files feature in hdrgen to share the
source of truth for the malloc suite of functions declared in
both stdlib.h and in malloc.h (without either header including
the other).  It also modernizes the malloc.yaml definition a bit,
including dropping the custom template malloc.h.def file in favor
of using the explicit macros list to generate the includes.
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