Skip to content

Commit 74de080

Browse files
committed
[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. llvm-svn: 304175
1 parent af66659 commit 74de080

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

llvm/include/llvm/Support/ManagedStatic.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
namespace llvm {
2121

2222
/// object_creator - Helper method for ManagedStatic.
23-
template <class C> void *object_creator() { return new C(); }
23+
template <class C> struct object_creator {
24+
static void *call() { return new C(); }
25+
};
2426

2527
/// object_deleter - Helper method for ManagedStatic.
2628
///
@@ -54,15 +56,15 @@ class ManagedStaticBase {
5456
/// libraries that link in LLVM components) and for making destruction be
5557
/// explicit through the llvm_shutdown() function call.
5658
///
57-
template <class C, void *(*Creator)() = object_creator<C>,
58-
void (*Deleter)(void *) = object_deleter<C>::call>
59+
template <class C, class Creator = object_creator<C>,
60+
class Deleter = object_deleter<C>>
5961
class ManagedStatic : public ManagedStaticBase {
6062
public:
6163
// Accessors.
6264
C &operator*() {
6365
void *Tmp = Ptr.load(std::memory_order_acquire);
6466
if (!Tmp)
65-
RegisterManagedStatic(Creator, Deleter);
67+
RegisterManagedStatic(Creator::call, Deleter::call);
6668

6769
return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
6870
}
@@ -72,7 +74,7 @@ class ManagedStatic : public ManagedStaticBase {
7274
const C &operator*() const {
7375
void *Tmp = Ptr.load(std::memory_order_acquire);
7476
if (!Tmp)
75-
RegisterManagedStatic(Creator, Deleter);
77+
RegisterManagedStatic(Creator::call, Deleter::call);
7678

7779
return *static_cast<C *>(Ptr.load(std::memory_order_relaxed));
7880
}

llvm/lib/Support/Timer.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
7373
}
7474

7575
namespace {
76-
void *CreateDefaultTimerGroup() {
77-
return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
78-
}
76+
struct CreateDefaultTimerGroup {
77+
static void *call() {
78+
return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
79+
}
80+
};
7981
} // namespace
8082
static ManagedStatic<TimerGroup, CreateDefaultTimerGroup> DefaultTimerGroup;
8183
static TimerGroup *getDefaultTimerGroup() { return &*DefaultTimerGroup; }

llvm/unittests/Support/ManagedStatic.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,17 @@ TEST(ManagedStaticTest, NestedStatics) {
8282
} // namespace NestedStatics
8383

8484
namespace CustomCreatorDeletor {
85-
void *CustomCreate() {
86-
void *Mem = std::malloc(sizeof(int));
87-
*((int *)Mem) = 42;
88-
return Mem;
89-
}
90-
static ManagedStatic<int, CustomCreate, std::free> Custom;
85+
struct CustomCreate {
86+
static void *call() {
87+
void *Mem = std::malloc(sizeof(int));
88+
*((int *)Mem) = 42;
89+
return Mem;
90+
}
91+
};
92+
struct CustomDelete {
93+
static void call(void *P) { std::free(P); }
94+
};
95+
static ManagedStatic<int, CustomCreate, CustomDelete> Custom;
9196
TEST(ManagedStaticTest, CustomCreatorDeletor) {
9297
EXPECT_EQ(42, *Custom);
9398
}

0 commit comments

Comments
 (0)