[libc-commits] [libc] [libc] Control freelist malloc buffer size with a config (PR #96248)

via libc-commits libc-commits at lists.llvm.org
Thu Jun 20 16:40:37 PDT 2024


https://github.com/PiJoules updated https://github.com/llvm/llvm-project/pull/96248

>From 41c867d56d694980bae46ffd1f69d25367c85940 Mon Sep 17 00:00:00 2001
From: Leonard Chan <leonardchan at google.com>
Date: Thu, 20 Jun 2024 16:00:21 -0700
Subject: [PATCH] [libc] Control freelist malloc buffer size with a config

Rather than propgating a compile define, add an explicit cmake flag for
controlling the size. The default for baremetal is 100KB and the default
for others is 1GB.
---
 libc/config/baremetal/config.json   | 5 +++++
 libc/config/config.json             | 6 ++++++
 libc/docs/configure.rst             | 2 ++
 libc/src/stdlib/CMakeLists.txt      | 2 ++
 libc/src/stdlib/freelist_malloc.cpp | 7 ++-----
 5 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/libc/config/baremetal/config.json b/libc/config/baremetal/config.json
index 53f232e31cc8a..dda4c42425755 100644
--- a/libc/config/baremetal/config.json
+++ b/libc/config/baremetal/config.json
@@ -12,5 +12,10 @@
     "LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE": {
       "value": false
     }
+  },
+  "malloc": {
+    "LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE": {
+      "value": 102400
+    }
   }
 }
diff --git a/libc/config/config.json b/libc/config/config.json
index 8d6a84e732597..11433c15d762e 100644
--- a/libc/config/config.json
+++ b/libc/config/config.json
@@ -54,5 +54,11 @@
       "value": 100,
       "doc": "Default number of spins before blocking if a rwlock is in contention (default to 100)."
     }
+  },
+  "malloc": {
+    "LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE": {
+      "value": 1073741824,
+      "doc": "Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB)."
+    }
   }
 }
diff --git a/libc/docs/configure.rst b/libc/docs/configure.rst
index bdae6c54052f2..016e2e5aa5876 100644
--- a/libc/docs/configure.rst
+++ b/libc/docs/configure.rst
@@ -28,6 +28,8 @@ to learn about the defaults for your platform and target.
 * **"codegen" options**
     - ``LIBC_CONF_ENABLE_STRONG_STACK_PROTECTOR``: Enable -fstack-protector-strong to defend against stack smashing attack.
     - ``LIBC_CONF_KEEP_FRAME_POINTER``: Keep frame pointer in functions for better debugging experience.
+* **"malloc" options**
+    - ``LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE``: Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB).
 * **"printf" options**
     - ``LIBC_CONF_PRINTF_DISABLE_FIXED_POINT``: Disable printing fixed point values in printf and friends.
     - ``LIBC_CONF_PRINTF_DISABLE_FLOAT``: Disable printing floating point values in printf and friends.
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 3b1ca3ab2fe42..51d53e0d02ea2 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -390,6 +390,8 @@ else()
         malloc.h
       DEPENDS
         libc.src.__support.freelist_heap
+      COMPILE_OPTIONS
+        -DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
     )
   else()
     add_entrypoint_external(
diff --git a/libc/src/stdlib/freelist_malloc.cpp b/libc/src/stdlib/freelist_malloc.cpp
index 0be7f3467e32c..4d3c42ca90bab 100644
--- a/libc/src/stdlib/freelist_malloc.cpp
+++ b/libc/src/stdlib/freelist_malloc.cpp
@@ -17,14 +17,11 @@
 namespace LIBC_NAMESPACE {
 
 namespace {
-// Users can define LIBC_FREELIST_MALLOC_SIZE for setting the default buffer
-// size used by freelist malloc.
 #ifdef LIBC_FREELIST_MALLOC_SIZE
+// This is set via the LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE configuration.
 constexpr size_t SIZE = LIBC_FREELIST_MALLOC_SIZE;
 #else
-// TODO: We should probably have something akin to what scudo/sanitizer
-// allocators do where each platform defines this.
-constexpr size_t SIZE = 0x40000000ULL; // 1GB
+#error "LIBC_FREELIST_MALLOC_SIZE was not defined for this build."
 #endif
 LIBC_CONSTINIT FreeListHeapBuffer<SIZE> freelist_heap_buffer;
 } // namespace



More information about the libc-commits mailing list