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

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Wed Jan 15 19:41:28 PST 2025


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

>From ddedf1e4b10a9b386fd334c3babcfd604bed6c62 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Thu, 9 Jan 2025 22:57:57 +0800
Subject: [PATCH] POC: add snmalloc as an alternative allocator to libc

---
 libc/src/stdlib/CMakeLists.txt             | 41 +++++++++++-
 libc/src/stdlib/snmalloc/CMakeLists.txt    | 75 ++++++++++++++++++++++
 libc/src/stdlib/snmalloc/aligned_alloc.cpp |  0
 libc/src/stdlib/snmalloc/calloc.cpp        |  0
 libc/src/stdlib/snmalloc/free.cpp          |  0
 libc/src/stdlib/snmalloc/malloc.cpp        |  9 +++
 libc/src/stdlib/snmalloc/mallopt.cpp       |  0
 libc/src/stdlib/snmalloc/realloc.cpp       |  0
 libc/src/stdlib/snmalloc/support.h         | 14 ++++
 9 files changed, 138 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/stdlib/snmalloc/CMakeLists.txt
 create mode 100644 libc/src/stdlib/snmalloc/aligned_alloc.cpp
 create mode 100644 libc/src/stdlib/snmalloc/calloc.cpp
 create mode 100644 libc/src/stdlib/snmalloc/free.cpp
 create mode 100644 libc/src/stdlib/snmalloc/malloc.cpp
 create mode 100644 libc/src/stdlib/snmalloc/mallopt.cpp
 create mode 100644 libc/src/stdlib/snmalloc/realloc.cpp
 create mode 100644 libc/src/stdlib/snmalloc/support.h

diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 40ba9ead9a7ae6..7d0dfd1aa9da4b 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -324,7 +324,46 @@ add_entrypoint_object(
 )
 
 if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
-  if(LLVM_LIBC_INCLUDE_SCUDO)
+  if (NOT "${LLVM_LIBC_INCLUDE_SNMALLOC}" STREQUAL "")
+    message(STATUS "Including snmalloc as the allocator, source directory: ${LLVM_LIBC_INCLUDE_SNMALLOC}")
+    add_subdirectory(snmalloc)
+    add_entrypoint_object(
+      malloc
+      ALIAS
+      DEPENDS
+        .snmalloc.malloc
+    )
+    add_entrypoint_object(
+      calloc
+      ALIAS
+      DEPENDS
+        .snmalloc.calloc
+    )
+    add_entrypoint_object(
+      realloc
+      ALIAS
+      DEPENDS
+        .snmalloc.realloc
+    )
+    add_entrypoint_object(
+      aligned_alloc
+      ALIAS
+      DEPENDS
+        .snmalloc.aligned_alloc
+    )
+    add_entrypoint_object(
+      free
+      ALIAS
+      DEPENDS
+        .snmalloc.free
+    )
+    add_entrypoint_object(
+      mallopt
+      ALIAS
+      DEPENDS
+        .snmalloc.mallopt
+    )
+  elseif(LLVM_LIBC_INCLUDE_SCUDO)
     set(SCUDO_DEPS "")
 
     include(${LIBC_SOURCE_DIR}/../compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake)
diff --git a/libc/src/stdlib/snmalloc/CMakeLists.txt b/libc/src/stdlib/snmalloc/CMakeLists.txt
new file mode 100644
index 00000000000000..3971ea42db1744
--- /dev/null
+++ b/libc/src/stdlib/snmalloc/CMakeLists.txt
@@ -0,0 +1,75 @@
+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
+    -include ${CMAKE_CURRENT_SOURCE_DIR}/support.h
+    # include_directories does not propagate, use options instead
+    -isystem ${COMPILER_RESOURCE_DIR}/include
+    -isystem ${LIBC_INCLUDE_DIR}
+)
+add_dependencies(snmalloc libc-headers)
+
+add_entrypoint_object(
+  malloc
+  SRCS
+    malloc.cpp
+  DEPENDS
+    snmalloc
+)
+
+add_entrypoint_object(
+  calloc
+  SRCS
+    calloc.cpp
+  DEPENDS
+    snmalloc
+)
+
+add_entrypoint_object(
+  realloc
+  SRCS
+    realloc.cpp
+  DEPENDS
+    snmalloc
+)
+
+add_entrypoint_object(
+  aligned_alloc
+  SRCS
+    aligned_alloc.cpp
+  DEPENDS
+    snmalloc
+)
+
+add_entrypoint_object(
+  free
+  SRCS
+    free.cpp
+  DEPENDS
+    snmalloc
+)
+
+add_entrypoint_object(
+  mallopt
+  SRCS
+    mallopt.cpp
+  DEPENDS
+    snmalloc
+)
diff --git a/libc/src/stdlib/snmalloc/aligned_alloc.cpp b/libc/src/stdlib/snmalloc/aligned_alloc.cpp
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/libc/src/stdlib/snmalloc/calloc.cpp b/libc/src/stdlib/snmalloc/calloc.cpp
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/libc/src/stdlib/snmalloc/free.cpp b/libc/src/stdlib/snmalloc/free.cpp
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/libc/src/stdlib/snmalloc/malloc.cpp b/libc/src/stdlib/snmalloc/malloc.cpp
new file mode 100644
index 00000000000000..644f2eef0ed2bf
--- /dev/null
+++ b/libc/src/stdlib/snmalloc/malloc.cpp
@@ -0,0 +1,9 @@
+#include "src/stdlib/malloc.h"
+#include "snmalloc/snmalloc.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
+  return snmalloc::libc::malloc(size);
+}
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/snmalloc/mallopt.cpp b/libc/src/stdlib/snmalloc/mallopt.cpp
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/libc/src/stdlib/snmalloc/realloc.cpp b/libc/src/stdlib/snmalloc/realloc.cpp
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/libc/src/stdlib/snmalloc/support.h b/libc/src/stdlib/snmalloc/support.h
new file mode 100644
index 00000000000000..4909667c1a50d8
--- /dev/null
+++ b/libc/src/stdlib/snmalloc/support.h
@@ -0,0 +1,14 @@
+#include "src/sys/random/getrandom.h"
+
+// TODO: define this
+inline int getentropy(void *buf, size_t size) {
+  while (size > 0) {
+    ssize_t ret = LIBC_NAMESPACE::getrandom(buf, size, 0);
+    if (ret < 0) {
+      return -1;
+    }
+    buf = (char *)buf + ret;
+    size -= ret;
+  }
+  return 0;
+}



More information about the libc-commits mailing list