-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Control freelist malloc buffer size with a config #96248
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
[libc] Control freelist malloc buffer size with a config #96248
Conversation
@llvm/pr-subscribers-libc Author: None (PiJoules) ChangesRather 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. Full diff: https://github.com/llvm/llvm-project/pull/96248.diff 5 Files Affected:
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..fb2a83835832e 100644
--- a/libc/src/stdlib/freelist_malloc.cpp
+++ b/libc/src/stdlib/freelist_malloc.cpp
@@ -17,15 +17,8 @@
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
-#endif
LIBC_CONSTINIT FreeListHeapBuffer<SIZE> freelist_heap_buffer;
} // namespace
|
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.
0892341
to
41c867d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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.
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.