[libc-commits] [libc] Revert "Revert "[libc] make integration test malloc work properly when threaded"" (PR #152236)
Schrodinger ZHU Yifan via libc-commits
libc-commits at lists.llvm.org
Tue Aug 5 19:05:12 PDT 2025
https://github.com/SchrodingerZhu updated https://github.com/llvm/llvm-project/pull/152236
>From 021d954acee76dede0815722ae2b85de18e9b712 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Tue, 5 Aug 2025 21:22:23 -0400
Subject: [PATCH 1/3] =?UTF-8?q?Revert=20"Revert=20"[libc]=20make=20integra?=
=?UTF-8?q?tion=20test=20malloc=20work=20properly=20when=20thread=E2=80=A6?=
=?UTF-8?q?"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This reverts commit 6393a9edde6da41d0e927b34a6c9ba965289c6da.
---
libc/test/IntegrationTest/CMakeLists.txt | 1 +
libc/test/IntegrationTest/test.cpp | 15 ++++++++++-----
2 files changed, 11 insertions(+), 5 deletions(-)
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 *) {}
>From 898849c51b92d407d743c480cc7bb63aa145d4e2 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Tue, 5 Aug 2025 21:25:44 -0400
Subject: [PATCH 2/3] skip gpu
---
libc/test/IntegrationTest/test.cpp | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/libc/test/IntegrationTest/test.cpp b/libc/test/IntegrationTest/test.cpp
index ec45e28a4e7a6..627e068f9e30b 100644
--- a/libc/test/IntegrationTest/test.cpp
+++ b/libc/test/IntegrationTest/test.cpp
@@ -65,11 +65,22 @@ int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }
static constexpr uint64_t MEMORY_SIZE = 16384;
static uint8_t memory[MEMORY_SIZE];
+
+#ifdef LIBC_TARGET_ARCH_IS_GPU
+static size_t used = 0;
+#else
static LIBC_NAMESPACE::cpp::Atomic<size_t> used = 0;
+#endif
extern "C" {
-// For simple test purposes.
+#ifdef LIBC_TARGET_ARCH_IS_GPU
+void *malloc(size_t s) {
+ void *mem = ptr;
+ ptr += s;
+ return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
+}
+#else
void *malloc(size_t s) {
// Emulate the alignment of std::max_align_t.
constexpr size_t DEFAULT_ALIGNMENT = alignof(long double);
@@ -79,6 +90,7 @@ void *malloc(size_t s) {
return nullptr; // Out of memory.
return &memory[res];
}
+#endif
void free(void *) {}
>From 1ced5ca71a26c912e855fc6fce66ddefa099d698 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Tue, 5 Aug 2025 21:58:53 -0400
Subject: [PATCH 3/3] use jhuber6's patch
---
libc/test/IntegrationTest/test.cpp | 36 ++++++++++--------------------
1 file changed, 12 insertions(+), 24 deletions(-)
diff --git a/libc/test/IntegrationTest/test.cpp b/libc/test/IntegrationTest/test.cpp
index 627e068f9e30b..0e4825202a9e2 100644
--- a/libc/test/IntegrationTest/test.cpp
+++ b/libc/test/IntegrationTest/test.cpp
@@ -63,34 +63,22 @@ int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }
// which just hands out continuous blocks from a statically allocated chunk of
// memory.
-static constexpr uint64_t MEMORY_SIZE = 16384;
-static uint8_t memory[MEMORY_SIZE];
-
-#ifdef LIBC_TARGET_ARCH_IS_GPU
-static size_t used = 0;
-#else
-static LIBC_NAMESPACE::cpp::Atomic<size_t> used = 0;
-#endif
+static constexpr uint64_t ALIGNMENT = alignof(long double);
+static constexpr uint64_t MEMORY_SIZE = 65336;
+alignas(ALIGNMENT) static uint8_t memory[MEMORY_SIZE];
+static size_t ptr = 0;
extern "C" {
-#ifdef LIBC_TARGET_ARCH_IS_GPU
-void *malloc(size_t s) {
- void *mem = ptr;
- ptr += s;
- return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
+void *malloc(size_t size) {
+ LIBC_NAMESPACE::cpp::AtomicRef<size_t> ref(ptr);
+ size = (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
+ size_t old_ptr =
+ ref.fetch_add(size, LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED);
+ if (static_cast<size_t>(old_ptr + size) > MEMORY_SIZE)
+ return nullptr;
+ return &memory[old_ptr];
}
-#else
-void *malloc(size_t s) {
- // 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];
-}
-#endif
void free(void *) {}
More information about the libc-commits
mailing list