[llvm] r304175 - [ManagedStatic] Avoid putting function pointers in template args.
Benjamin Kramer via llvm-commits
llvm-commits at lists.llvm.org
Mon May 29 13:56:27 PDT 2017
Author: d0k
Date: Mon May 29 15:56:27 2017
New Revision: 304175
URL: http://llvm.org/viewvc/llvm-project?rev=304175&view=rev
Log:
[ManagedStatic] Avoid putting function pointers in template args.
This is super awkward, but GCC doesn't let us have template visible when
an argument is an inline function and -fvisibility-inlines-hidden is
used.
Modified:
llvm/trunk/include/llvm/Support/ManagedStatic.h
llvm/trunk/lib/Support/Timer.cpp
llvm/trunk/unittests/Support/ManagedStatic.cpp
Modified: llvm/trunk/include/llvm/Support/ManagedStatic.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/ManagedStatic.h?rev=304175&r1=304174&r2=304175&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/ManagedStatic.h (original)
+++ llvm/trunk/include/llvm/Support/ManagedStatic.h Mon May 29 15:56:27 2017
@@ -20,7 +20,9 @@
namespace llvm {
/// object_creator - Helper method for ManagedStatic.
-template <class C> void *object_creator() { return new C(); }
+template <class C> struct object_creator {
+ static void *call() { return new C(); }
+};
/// object_deleter - Helper method for ManagedStatic.
///
@@ -54,15 +56,15 @@ public:
/// libraries that link in LLVM components) and for making destruction be
/// explicit through the llvm_shutdown() function call.
///
-template <class C, void *(*Creator)() = object_creator<C>,
- void (*Deleter)(void *) = object_deleter<C>::call>
+template <class C, class Creator = object_creator<C>,
+ class Deleter = object_deleter<C>>
class ManagedStatic : public ManagedStaticBase {
public:
// Accessors.
C &operator*() {
void *Tmp = Ptr.load(std::memory_order_acquire);
if (!Tmp)
- RegisterManagedStatic(Creator, Deleter);
+ RegisterManagedStatic(Creator::call, Deleter::call);
return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
}
@@ -72,7 +74,7 @@ public:
const C &operator*() const {
void *Tmp = Ptr.load(std::memory_order_acquire);
if (!Tmp)
- RegisterManagedStatic(Creator, Deleter);
+ RegisterManagedStatic(Creator::call, Deleter::call);
return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
}
Modified: llvm/trunk/lib/Support/Timer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Timer.cpp?rev=304175&r1=304174&r2=304175&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Timer.cpp (original)
+++ llvm/trunk/lib/Support/Timer.cpp Mon May 29 15:56:27 2017
@@ -73,9 +73,11 @@ std::unique_ptr<raw_fd_ostream> llvm::Cr
}
namespace {
-void *CreateDefaultTimerGroup() {
- return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
-}
+struct CreateDefaultTimerGroup {
+ static void *call() {
+ return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
+ }
+};
} // namespace
static ManagedStatic<TimerGroup, CreateDefaultTimerGroup> DefaultTimerGroup;
static TimerGroup *getDefaultTimerGroup() { return &*DefaultTimerGroup; }
Modified: llvm/trunk/unittests/Support/ManagedStatic.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ManagedStatic.cpp?rev=304175&r1=304174&r2=304175&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ManagedStatic.cpp (original)
+++ llvm/trunk/unittests/Support/ManagedStatic.cpp Mon May 29 15:56:27 2017
@@ -82,12 +82,17 @@ TEST(ManagedStaticTest, NestedStatics) {
} // namespace NestedStatics
namespace CustomCreatorDeletor {
-void *CustomCreate() {
- void *Mem = std::malloc(sizeof(int));
- *((int *)Mem) = 42;
- return Mem;
-}
-static ManagedStatic<int, CustomCreate, std::free> Custom;
+struct CustomCreate {
+ static void *call() {
+ void *Mem = std::malloc(sizeof(int));
+ *((int *)Mem) = 42;
+ return Mem;
+ }
+};
+struct CustomDelete {
+ static void call(void *P) { std::free(P); }
+};
+static ManagedStatic<int, CustomCreate, CustomDelete> Custom;
TEST(ManagedStaticTest, CustomCreatorDeletor) {
EXPECT_EQ(42, *Custom);
}
More information about the llvm-commits
mailing list