Skip to content

Commit 84b02af

Browse files
parth-07quic-seaswara
authored andcommitted
Add ELD_REGISTER_PLUGIN macro
This commit adds `ELD_REGISTER_PLUGIN` macro. This macro provides good defaults for RegisterAll, getPlugin and Cleanup functions. The goal is to make creating a plugin more convenient by reducing the necessary boilerplate code. Closes #108 Signed-off-by: Parth Arora <[email protected]>
1 parent ab90d7e commit 84b02af

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

include/eld/PluginAPI/PluginBase.h.inc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,28 @@ typedef void *PluginCleanupFuncT();
164164
// LinkerPluginConfig
165165
typedef LinkerPluginConfig *PluginConfigFuncT(PluginType T);
166166
}
167+
168+
/// A helper macro for registering a plugin.
169+
/// It simply provides good defaults for RegisterAll, getPlugin
170+
/// and Cleanup functions.
171+
/// \note It can be only be used once in a source file.
172+
#define ELD_REGISTER_PLUGIN(PluginType) \
173+
PluginType *ThisPlugin = nullptr; \
174+
\
175+
extern "C" { \
176+
bool DLL_A_EXPORT RegisterAll() { \
177+
ThisPlugin = new PluginType(); \
178+
return true; \
179+
} \
180+
\
181+
eld::plugin::PluginBase DLL_A_EXPORT *getPlugin(const char *T) { \
182+
return ThisPlugin; \
183+
} \
184+
\
185+
void DLL_A_EXPORT Cleanup() { \
186+
if (ThisPlugin) \
187+
delete ThisPlugin; \
188+
ThisPlugin = nullptr; \
189+
} \
190+
}
167191
#endif

test/Common/Plugin/AddedSectionOverrides/AddedSectionOverrides.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,4 @@ class DLL_A_EXPORT AddedSectionOverrides
4444
};
4545

4646

47-
std::unordered_map<std::string, Plugin *> Plugins;
48-
49-
extern "C" {
50-
bool DLL_A_EXPORT RegisterAll() {
51-
Plugins["AddedSectionOverrides"] = new AddedSectionOverrides();
52-
return true;
53-
}
54-
55-
PluginBase DLL_A_EXPORT *getPlugin(const char *T) { return Plugins[T]; }
56-
void DLL_A_EXPORT Cleanup() {
57-
for (auto &item : Plugins) {
58-
if (item.second)
59-
delete item.second;
60-
}
61-
Plugins.clear();
62-
}
63-
}
47+
ELD_REGISTER_PLUGIN(AddedSectionOverrides)

test/Common/Plugin/BasicChunkMover/BasicChunkMover.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "LinkerWrapper.h"
44
#include "OutputSectionIteratorPlugin.h"
55
#include "PluginADT.h"
6+
#include "PluginBase.h"
67
#include "PluginVersion.h"
78
#include <cassert>
89

@@ -63,19 +64,4 @@ class DLL_A_EXPORT BasicChunkMover : public eld::plugin::OutputSectionIteratorPl
6364
eld::plugin::OutputSection m_Bar{nullptr};
6465
};
6566

66-
BasicChunkMover *ThisPlugin = nullptr;
67-
68-
extern "C" {
69-
bool DLL_A_EXPORT RegisterAll() {
70-
ThisPlugin = new BasicChunkMover();
71-
return true;
72-
}
73-
74-
eld::plugin::PluginBase DLL_A_EXPORT *getPlugin(const char *T) { return ThisPlugin; }
75-
76-
void DLL_A_EXPORT Cleanup() {
77-
if (ThisPlugin)
78-
delete ThisPlugin;
79-
ThisPlugin = nullptr;
80-
}
81-
}
67+
ELD_REGISTER_PLUGIN(BasicChunkMover)

0 commit comments

Comments
 (0)