[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
Thu Jul 31 19:02:09 PDT 2025
================
@@ -65,14 +66,25 @@ 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" {
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);
+ if (s > MEMORY_SIZE)
+ return nullptr; // Not enough memory.
+ s = s + MAX_ALIGNMENT -
+ (s % MAX_ALIGNMENT); // Align the size to the max alignment.
----------------
SchrodingerZhu wrote:
Addressed. I prefer to explicit the modulo operation.
https://github.com/llvm/llvm-project/pull/151622
More information about the libc-commits
mailing list