Skip to content

Commit 0509238

Browse files
committed
Attempt to avoid static init ordering issues with globalMemCounter
llvm-svn: 305955
1 parent bac3570 commit 0509238

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

libcxx/test/support/count_new.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,17 @@ class MemCounter
231231
const bool MemCounter::disable_checking = false;
232232
#endif
233233

234-
MemCounter globalMemCounter((MemCounter::MemCounterCtorArg_()));
234+
inline MemCounter* getGlobalMemCounter() {
235+
static MemCounter counter((MemCounter::MemCounterCtorArg_()));
236+
return &counter;
237+
}
238+
239+
MemCounter &globalMemCounter = *getGlobalMemCounter();
235240

236241
#ifndef DISABLE_NEW_COUNT
237242
void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
238243
{
239-
globalMemCounter.newCalled(s);
244+
getGlobalMemCounter()->newCalled(s);
240245
void* ret = std::malloc(s);
241246
if (ret == nullptr)
242247
detail::throw_bad_alloc_helper();
@@ -245,21 +250,21 @@ void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
245250

246251
void operator delete(void* p) TEST_NOEXCEPT
247252
{
248-
globalMemCounter.deleteCalled(p);
253+
getGlobalMemCounter()->deleteCalled(p);
249254
std::free(p);
250255
}
251256

252257

253258
void* operator new[](std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
254259
{
255-
globalMemCounter.newArrayCalled(s);
260+
getGlobalMemCounter()->newArrayCalled(s);
256261
return operator new(s);
257262
}
258263

259264

260265
void operator delete[](void* p) TEST_NOEXCEPT
261266
{
262-
globalMemCounter.deleteArrayCalled(p);
267+
getGlobalMemCounter()->deleteArrayCalled(p);
263268
operator delete(p);
264269
}
265270

0 commit comments

Comments
 (0)