[libc-commits] [libc] [libc] Share hdrgen declarations between stdlib.h and malloc.h (PR #127278)

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Fri Feb 14 15:39:34 PST 2025


https://github.com/frobtech created https://github.com/llvm/llvm-project/pull/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.


>From d938a4c726242184a639611dd72ba481d2f206b6 Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Thu, 13 Feb 2025 22:28:42 -0800
Subject: [PATCH] [libc] Share hdrgen declarations between stdlib.h and
 malloc.h

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.
---
 libc/include/malloc.h.def       | 17 -------------
 libc/include/malloc.yaml        | 16 +++++++-----
 libc/include/stdlib-malloc.yaml | 44 +++++++++++++++++++++++++++++++++
 libc/include/stdlib.yaml        | 35 ++------------------------
 4 files changed, 56 insertions(+), 56 deletions(-)
 delete mode 100644 libc/include/malloc.h.def
 create mode 100644 libc/include/stdlib-malloc.yaml

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



More information about the libc-commits mailing list