From 10f535273f8e96eee4da99642145b7f42ef369d9 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Mon, 29 May 2017 14:05:26 +0000 Subject: [PATCH] [ManagedStatic] Add a way to pass custom creators/deleters. Also add a test case verifying that nested ManagedStatics work correctly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@304155 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/ManagedStatic.h | 7 +++-- unittests/Support/ManagedStatic.cpp | 42 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/include/llvm/Support/ManagedStatic.h b/include/llvm/Support/ManagedStatic.h index 7ce86eee95d..26e20115441 100644 --- a/include/llvm/Support/ManagedStatic.h +++ b/include/llvm/Support/ManagedStatic.h @@ -59,14 +59,15 @@ public: /// libraries that link in LLVM components) and for making destruction be /// explicit through the llvm_shutdown() function call. /// -template +template , + void (*Deleter)(void *) = object_deleter::call> class ManagedStatic : public ManagedStaticBase { public: // Accessors. C &operator*() { void *Tmp = Ptr.load(std::memory_order_acquire); if (!Tmp) - RegisterManagedStatic(object_creator, object_deleter::call); + RegisterManagedStatic(Creator, Deleter); return *static_cast(Ptr.load(std::memory_order_relaxed)); } @@ -76,7 +77,7 @@ public: const C &operator*() const { void *Tmp = Ptr.load(std::memory_order_acquire); if (!Tmp) - RegisterManagedStatic(object_creator, object_deleter::call); + RegisterManagedStatic(Creator, Deleter); return *static_cast(Ptr.load(std::memory_order_relaxed)); } diff --git a/unittests/Support/ManagedStatic.cpp b/unittests/Support/ManagedStatic.cpp index 153884ba429..9c84ce8cb51 100644 --- a/unittests/Support/ManagedStatic.cpp +++ b/unittests/Support/ManagedStatic.cpp @@ -57,4 +57,46 @@ TEST(Initialize, MultipleThreads) { } #endif +namespace NestedStatics { +static ManagedStatic Ms1; +struct Nest { + Nest() { + ++(*Ms1); + } + + ~Nest() { + assert(Ms1.isConstructed()); + ++(*Ms1); + } +}; +static ManagedStatic Ms2; + +TEST(ManagedStaticTest, NestedStatics) { + EXPECT_FALSE(Ms1.isConstructed()); + EXPECT_FALSE(Ms2.isConstructed()); + + *Ms2; + EXPECT_TRUE(Ms1.isConstructed()); + EXPECT_TRUE(Ms2.isConstructed()); + + llvm_shutdown(); + EXPECT_FALSE(Ms1.isConstructed()); + EXPECT_FALSE(Ms2.isConstructed()); +} +} // namespace NestedStatics + +namespace CustomCreatorDeletor { +static void *CustomCreate() { + void *Mem = std::malloc(sizeof(int)); + *((int *)Mem) = 42; + return Mem; +} +static ManagedStatic Custom; +TEST(ManagedStaticTest, CustomCreatorDeletor) { + EXPECT_EQ(42, *Custom); + llvm_shutdown(); + EXPECT_FALSE(Custom.isConstructed()); +} +} // namespace CustomCreatorDeletor + } // anonymous namespace