Skip to content

Commit d050aa9

Browse files
PiJoulesAlexisPerry
authored andcommitted
[libc] Control freelist malloc buffer size with a config (llvm#96248)
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.
1 parent 8336991 commit d050aa9

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

libc/config/baremetal/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
"LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE": {
1313
"value": false
1414
}
15+
},
16+
"malloc": {
17+
"LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE": {
18+
"value": 102400
19+
}
1520
}
1621
}

libc/config/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,11 @@
5454
"value": 100,
5555
"doc": "Default number of spins before blocking if a rwlock is in contention (default to 100)."
5656
}
57+
},
58+
"malloc": {
59+
"LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE": {
60+
"value": 1073741824,
61+
"doc": "Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB)."
62+
}
5763
}
5864
}

libc/docs/configure.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ to learn about the defaults for your platform and target.
2828
* **"codegen" options**
2929
- ``LIBC_CONF_ENABLE_STRONG_STACK_PROTECTOR``: Enable -fstack-protector-strong to defend against stack smashing attack.
3030
- ``LIBC_CONF_KEEP_FRAME_POINTER``: Keep frame pointer in functions for better debugging experience.
31+
* **"malloc" options**
32+
- ``LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE``: Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB).
3133
* **"printf" options**
3234
- ``LIBC_CONF_PRINTF_DISABLE_FIXED_POINT``: Disable printing fixed point values in printf and friends.
3335
- ``LIBC_CONF_PRINTF_DISABLE_FLOAT``: Disable printing floating point values in printf and friends.

libc/src/stdlib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ else()
390390
malloc.h
391391
DEPENDS
392392
libc.src.__support.freelist_heap
393+
COMPILE_OPTIONS
394+
-DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
393395
)
394396
else()
395397
add_entrypoint_external(

libc/src/stdlib/freelist_malloc.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717
namespace LIBC_NAMESPACE {
1818

1919
namespace {
20-
// Users can define LIBC_FREELIST_MALLOC_SIZE for setting the default buffer
21-
// size used by freelist malloc.
2220
#ifdef LIBC_FREELIST_MALLOC_SIZE
21+
// This is set via the LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE configuration.
2322
constexpr size_t SIZE = LIBC_FREELIST_MALLOC_SIZE;
2423
#else
25-
// TODO: We should probably have something akin to what scudo/sanitizer
26-
// allocators do where each platform defines this.
27-
constexpr size_t SIZE = 0x40000000ULL; // 1GB
24+
#error "LIBC_FREELIST_MALLOC_SIZE was not defined for this build."
2825
#endif
2926
LIBC_CONSTINIT FreeListHeapBuffer<SIZE> freelist_heap_buffer;
3027
} // namespace

0 commit comments

Comments
 (0)