[libc-commits] [libc] [libc] make integration test malloc work properly when threaded (PR #151622)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Fri Aug 1 07:10:24 PDT 2025


================
@@ -65,14 +65,18 @@ int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }
 
 static constexpr uint64_t MEMORY_SIZE = 16384;
 static uint8_t memory[MEMORY_SIZE];
-static uint8_t *ptr = memory;
+static LIBC_NAMESPACE::cpp::Atomic<size_t> used = 0;
 
 extern "C" {
 
+// For simple test purposes.
 void *malloc(size_t s) {
-  void *mem = ptr;
-  ptr += s;
-  return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
+  constexpr size_t MAX_ALIGNMENT = alignof(long double);
+  s += (-s) & (MAX_ALIGNMENT - 1); // Align to max alignment.
+  auto res = used.fetch_add(s);
+  if (res + s > MEMORY_SIZE)
+    return nullptr; // Out of memory.
+  return &memory[res];
----------------
SchrodingerZhu wrote:

Not true

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


More information about the libc-commits mailing list