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

via libc-commits libc-commits at lists.llvm.org
Mon Aug 4 20:49:54 PDT 2025


Author: Schrodinger ZHU Yifan
Date: 2025-08-04T23:49:51-04:00
New Revision: 8eab158396d8c627849cc3d5e818725e22434c9c

URL: https://github.com/llvm/llvm-project/commit/8eab158396d8c627849cc3d5e818725e22434c9c
DIFF: https://github.com/llvm/llvm-project/commit/8eab158396d8c627849cc3d5e818725e22434c9c.diff

LOG: [libc] make integration test malloc work properly when threaded (#151622)

Make the simple bump allocation backed by atomic operations.

Added: 
    

Modified: 
    libc/test/IntegrationTest/CMakeLists.txt
    libc/test/IntegrationTest/test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/test/IntegrationTest/CMakeLists.txt b/libc/test/IntegrationTest/CMakeLists.txt
index 3afe354eca986..235e9fe2f55ee 100644
--- a/libc/test/IntegrationTest/CMakeLists.txt
+++ b/libc/test/IntegrationTest/CMakeLists.txt
@@ -13,5 +13,6 @@ add_object_library(
   DEPENDS
     libc.hdr.stdint_proxy
     libc.src.__support.OSUtil.osutil
+    libc.src.__support.CPP.atomic
     ${arch_specific_deps}
 )

diff  --git a/libc/test/IntegrationTest/test.cpp b/libc/test/IntegrationTest/test.cpp
index 8baf74637b309..ec45e28a4e7a6 100644
--- a/libc/test/IntegrationTest/test.cpp
+++ b/libc/test/IntegrationTest/test.cpp
@@ -5,8 +5,8 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-
 #include "hdr/stdint_proxy.h"
+#include "src/__support/CPP/atomic.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 #include <stddef.h>
@@ -65,14 +65,19 @@ 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;
+  // Emulate the alignment of std::max_align_t.
+  constexpr size_t DEFAULT_ALIGNMENT = alignof(long double);
+  s += (-s) & (DEFAULT_ALIGNMENT - 1); // Align to default alignment.
+  auto res = used.fetch_add(s, LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED);
+  if (res + s > MEMORY_SIZE)
+    return nullptr; // Out of memory.
+  return &memory[res];
 }
 
 void free(void *) {}


        


More information about the libc-commits mailing list