Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jun 11, 2011
@psychon psychon Use references instead of pointers for CModInfo
This works fine and reduces the chances of memory leaks.

Signed-off-by: Uli Schlachter <psychon@znc.in>
ca97dca
@psychon psychon Only export a single function from modules
This moves the version check into the function which loads the module info. That
way, we just need a single dlsym() call per module and thus can't get any errors
from the second dlsym() call. And IMHO this feels nicer anyway. ;-)

Signed-off-by: Uli Schlachter <psychon@znc.in>
25ce7de
Showing with 25 additions and 45 deletions.
  1. +14 −33 Modules.cpp
  2. +11 −12 Modules.h
View
47 Modules.cpp
@@ -817,7 +817,7 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, CUser* p
CString sModPath, sDataPath;
bool bVersionMismatch;
- CModInfo* Info;
+ CModInfo Info;
if (!FindModPath(sModule, sModPath, sDataPath)) {
sRetMsg = "Unable to find module [" + sModule + "]";
@@ -835,31 +835,28 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, CUser* p
return false;
}
- if ((pUser == NULL) != Info->IsGlobal()) {
- delete Info;
+ if ((pUser == NULL) != Info.IsGlobal()) {
dlclose(p);
sRetMsg = "Module [" + sModule + "] is ";
- sRetMsg += Info->IsGlobal() ? "" : "not ";
+ sRetMsg += Info.IsGlobal() ? "" : "not ";
sRetMsg += "a global module.";
return false;
}
CModule* pModule = NULL;
if (pUser) {
- pModule = Info->GetLoader()(p, pUser, sModule, sDataPath);
+ pModule = Info.GetLoader()(p, pUser, sModule, sDataPath);
} else {
- pModule = Info->GetGlobalLoader()(p, sModule, sDataPath);
+ pModule = Info.GetGlobalLoader()(p, sModule, sDataPath);
}
- pModule->SetDescription(Info->GetDescription());
- pModule->SetGlobal(Info->IsGlobal());
+ pModule->SetDescription(Info.GetDescription());
+ pModule->SetGlobal(Info.IsGlobal());
pModule->SetArgs(sArgs);
pModule->SetModPath(CDir::ChangeDir(CZNC::Get().GetCurPath(), sModPath));
push_back(pModule);
- delete Info;
-
bool bLoaded;
try {
bLoaded = pModule->OnLoad(sArgs, sRetMsg);
@@ -955,24 +952,19 @@ bool CModules::GetModInfo(CModInfo& ModInfo, const CString& sModule, CString& sR
bool CModules::GetModPathInfo(CModInfo& ModInfo, const CString& sModule, const CString& sModPath, CString& sRetMsg) {
bool bVersionMismatch;
- CModInfo* Info;
- ModHandle p = OpenModule(sModule, sModPath, bVersionMismatch, Info, sRetMsg);
+ ModHandle p = OpenModule(sModule, sModPath, bVersionMismatch, ModInfo, sRetMsg);
if (!p)
return false;
- if (Info) {
- ModInfo = *Info;
- }
ModInfo.SetName(sModule);
ModInfo.SetPath(sModPath);
if (bVersionMismatch) {
ModInfo.SetDescription("--- Version mismatch, recompile this module. ---");
}
- delete Info;
dlclose(p);
return true;
@@ -1057,10 +1049,9 @@ CModules::ModDirList CModules::GetModDirs() {
}
ModHandle CModules::OpenModule(const CString& sModule, const CString& sModPath, bool &bVersionMismatch,
- CModInfo*& Info, CString& sRetMsg) {
+ CModInfo& Info, CString& sRetMsg) {
// Some sane defaults in case anything errors out below
bVersionMismatch = false;
- Info = NULL;
sRetMsg.clear();
for (unsigned int a = 0; a < sModule.length(); a++) {
@@ -1089,16 +1080,7 @@ ModHandle CModules::OpenModule(const CString& sModule, const CString& sModPath,
return NULL;
}
- typedef double (*dFP)();
- dFP Version = (dFP) dlsym(p, "ZNCModVersion");
-
- if (!Version) {
- dlclose(p);
- sRetMsg = "Could not find ZNCModVersion() in module [" + sModule + "]";
- return NULL;
- }
-
- typedef CModInfo* (*InfoFP)();
+ typedef bool (*InfoFP)(double, CModInfo&);
InfoFP ZNCModInfo = (InfoFP) dlsym(p, "ZNCModInfo");
if (!ZNCModInfo) {
@@ -1107,13 +1089,12 @@ ModHandle CModules::OpenModule(const CString& sModule, const CString& sModPath,
return NULL;
}
- if (CModule::GetCoreVersion() != Version()) {
- bVersionMismatch = true;
- sRetMsg = "Version mismatch, recompile this module.";
- } else {
- Info = ZNCModInfo();
+ if (ZNCModInfo(CModule::GetCoreVersion(), Info)) {
sRetMsg = "";
bVersionMismatch = false;
+ } else {
+ bVersionMismatch = true;
+ sRetMsg = "Version mismatch, recompile this module.";
}
return p;
View
23 Modules.h
@@ -55,16 +55,15 @@ template<class M> CGlobalModule* TModLoadGlobal(ModHandle p,
#define MODCOMMONDEFS(CLASS, DESCRIPTION, GLOBAL, LOADER) \
extern "C" { \
- double ZNCModVersion(); \
- double ZNCModVersion() { return VERSION; } \
- CModInfo* ZNCModInfo(); \
- CModInfo* ZNCModInfo() { \
- CModInfo* Info = new CModInfo(); \
- Info->SetDescription(DESCRIPTION); \
- Info->SetGlobal(GLOBAL); \
+ bool ZNCModInfo(double dCoreVersion, CModInfo& Info); \
+ bool ZNCModInfo(double dCoreVersion, CModInfo& Info) { \
+ if (dCoreVersion != VERSION) \
+ return false; \
+ Info.SetDescription(DESCRIPTION); \
+ Info.SetGlobal(GLOBAL); \
LOADER; \
- TModInfo<CLASS>(*Info); \
- return Info; \
+ TModInfo<CLASS>(Info); \
+ return true; \
} \
}
@@ -96,7 +95,7 @@ template<class M> CGlobalModule* TModLoadGlobal(ModHandle p,
* @see For global modules you need GLOBALMODULEDEFS.
*/
#define MODULEDEFS(CLASS, DESCRIPTION) \
- MODCOMMONDEFS(CLASS, DESCRIPTION, false, Info->SetLoader(TModLoad<CLASS>))
+ MODCOMMONDEFS(CLASS, DESCRIPTION, false, Info.SetLoader(TModLoad<CLASS>))
// !User Module Macros
// Global Module Macros
@@ -107,7 +106,7 @@ template<class M> CGlobalModule* TModLoadGlobal(ModHandle p,
/** This works exactly like MODULEDEFS, but for global modules. */
#define GLOBALMODULEDEFS(CLASS, DESCRIPTION) \
- MODCOMMONDEFS(CLASS, DESCRIPTION, true, Info->SetGlobalLoader(TModLoadGlobal<CLASS>))
+ MODCOMMONDEFS(CLASS, DESCRIPTION, true, Info.SetGlobalLoader(TModLoadGlobal<CLASS>))
// !Global Module Macros
// Forward Declarations
@@ -982,7 +981,7 @@ class CModules : public vector<CModule*> {
private:
static ModHandle OpenModule(const CString& sModule, const CString& sModPath,
- bool &bVersionMismatch, CModInfo*& Info, CString& sRetMsg);
+ bool &bVersionMismatch, CModInfo& Info, CString& sRetMsg);
protected:
CUser* m_pUser;

No commit comments for this range

Something went wrong with that request. Please try again.