File tree Expand file tree Collapse file tree 2 files changed +11
-5
lines changed
libc/test/IntegrationTest Expand file tree Collapse file tree 2 files changed +11
-5
lines changed Original file line number Diff line number Diff line change @@ -13,5 +13,6 @@ add_object_library(
13
13
DEPENDS
14
14
libc.hdr.stdint_proxy
15
15
libc.src.__support.OSUtil.osutil
16
+ libc.src.__support.CPP.atomic
16
17
${arch_specific_deps}
17
18
)
Original file line number Diff line number Diff line change 5
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
-
9
8
#include " hdr/stdint_proxy.h"
9
+ #include " src/__support/CPP/atomic.h"
10
10
#include " src/__support/common.h"
11
11
#include " src/__support/macros/config.h"
12
12
#include < stddef.h>
@@ -65,14 +65,19 @@ int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }
65
65
66
66
static constexpr uint64_t MEMORY_SIZE = 16384 ;
67
67
static uint8_t memory[MEMORY_SIZE];
68
- static uint8_t *ptr = memory ;
68
+ static LIBC_NAMESPACE::cpp::Atomic< size_t > used = 0 ;
69
69
70
70
extern " C" {
71
71
72
+ // For simple test purposes.
72
73
void *malloc (size_t s) {
73
- void *mem = ptr;
74
- ptr += s;
75
- return static_cast <uint64_t >(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
74
+ // Emulate the alignment of std::max_align_t.
75
+ constexpr size_t DEFAULT_ALIGNMENT = alignof (long double );
76
+ s += (-s) & (DEFAULT_ALIGNMENT - 1 ); // Align to default alignment.
77
+ auto res = used.fetch_add (s, LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED);
78
+ if (res + s > MEMORY_SIZE)
79
+ return nullptr ; // Out of memory.
80
+ return &memory[res];
76
81
}
77
82
78
83
void free (void *) {}
You can’t perform that action at this time.
0 commit comments