[libc-commits] [libc] [libc] add snmalloc as an alternative allocator to libc (PR #122284)

Matthew Parkinson via libc-commits libc-commits at lists.llvm.org
Fri Jan 17 23:41:13 PST 2025


================
@@ -0,0 +1,119 @@
+set(SNMALLOC_USE_SELF_VENDORED_STL ON CACHE BOOL "use freestanding snmalloc setup" FORCE)
+set(SNMALLOC_BUILD_TESTING OFF CACHE BOOL "disable snmalloc tests" FORCE)
+set(SNMALLOC_HEADER_ONLY_LIBRARY ON CACHE BOOL "use snmalloc as header only library" FORCE)
+
+# Disable installation
+macro (install)
+endmacro ()
+
+add_subdirectory(${LLVM_LIBC_INCLUDE_SNMALLOC} ${CMAKE_CURRENT_BINARY_DIR}/snmalloc EXCLUDE_FROM_ALL)
+
+target_compile_options(
+  snmalloc 
+  INTERFACE 
+    -ffreestanding
+    -nostdinc 
+    -Wno-newline-eof 
+    -Wno-extra-semi
+    -Wno-unused-command-line-argument
+    -Wno-ctad-maybe-unsupported
+    # TODO: define this
+    -DSTDERR_FILENO=2
+    -DSNMALLOC_USE_PTHREAD_DESTRUCTORS
+    # include_directories does not propagate, use options instead
+    -include ${CMAKE_CURRENT_SOURCE_DIR}/override.h
+    -isystem ${COMPILER_RESOURCE_DIR}/include
+    -isystem ${LIBC_INCLUDE_DIR}
+)
+
+add_dependencies(snmalloc libc-headers)
+
+set(SNMALLOC_DEPS
+  # includes
+  libc.include.errno
+  libc.include.fcntl
+  libc.include.limits
+  libc.include.pthread
+  libc.include.stdio
+  libc.include.stdint
+  libc.include.stdlib
+  libc.include.string
+  libc.include.strings
+  libc.include.sys_mman
+  libc.include.sys_prctl
+  libc.include.sys_random
+  libc.include.sys_syscall
+  libc.include.sys_uio
+  libc.include.unistd
+  # symbols
+  libc.src.errno.errno
----------------
mjp41 wrote:

So `atexit` would only be called inside `register_cleanup`. This occurs when snmalloc detects this thread's allocator has not been initialised. If this is the case it initializes the allocator. Once initialised it calls `register_cleanup`, and then it performs the original operation on the allocator. 

https://github.com/microsoft/snmalloc/blob/main/src%2Fsnmalloc%2Fmem%2Flocalalloc.h#L146-L148

I'm happy to expand the description of this code if helpful. 


https://github.com/llvm/llvm-project/pull/122284


More information about the libc-commits mailing list