[libc-commits] [libc] [libc] add snmalloc as an alternative allocator to libc (PR #122284)
Schrodinger ZHU Yifan via libc-commits
libc-commits at lists.llvm.org
Sun Jan 19 00:58:14 PST 2025
SchrodingerZhu wrote:
@mjp41 Thanks for the suggestion. However, after investigating, I think libc's implementation is quite wrong currently. I am leaning to keep using pthread-style cleanup because it does not need me to inject `_malloc_thread_cleanup` call into startup and thread init routines.
@lntue @nickdesaulniers
So, right inside startup code, we do a call to `atexit`, even before `call_init_array_callbacks`.
```c++
//...
// We want the fini array callbacks to be run after other atexit
// callbacks are run. So, we register them before running the init
// array callbacks as they can potentially register their own atexit
// callbacks.
atexit(&call_fini_array_callbacks);
call_init_array_callbacks(static_cast<int>(app.args->argc),
reinterpret_cast<char **>(app.args->argv),
reinterpret_cast<char **>(env_ptr));
int retval = main(static_cast<int>(app.args->argc),
reinterpret_cast<char **>(app.args->argv),
reinterpret_cast<char **>(env_ptr));
exit(retval);
```
When `LIBC_COPT_PUBLIC_PACKAING=On` , this call will invoke `malloc` via our `new` operators.
```
#if defined(LIBC_TARGET_ARCH_IS_GPU)
using ExitCallbackList = FixedVector<AtExitUnit, 64>;
#elif defined(LIBC_COPT_PUBLIC_PACKAGING)
using ExitCallbackList = ReverseOrderBlockStore<AtExitUnit, 32>;
#else
using ExitCallbackList = FixedVector<AtExitUnit, CALLBACK_LIST_SIZE_FOR_TESTS>;
#endif
```
This is very wrong in the sense that allocator is not even initialized. It is likely that allocators themselves will need to install `exiting` hooks. I suggest we should use mmapped memory here.
https://github.com/llvm/llvm-project/pull/122284
More information about the libc-commits
mailing list