1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*
* Copyright (C) 2005-2013 Team XBMC
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "threads/SystemClock.h"
#include "SectionLoader.h"
#include "cores/DllLoader/DllLoaderContainer.h"
#include "threads/SingleLock.h"
#include "utils/log.h"
#include "utils/StringUtils.h"
#define g_sectionLoader XBMC_GLOBAL_USE(CSectionLoader)
// delay for unloading dll's
#define UNLOAD_DELAY 30*1000 // 30 sec.
//Define this to get loggin on all calls to load/unload sections/dlls
//#define LOGALL
CSectionLoader::CSectionLoader(void) = default;
CSectionLoader::~CSectionLoader(void)
{
UnloadAll();
}
LibraryLoader *CSectionLoader::LoadDLL(const std::string &dllname, bool bDelayUnload /*=true*/, bool bLoadSymbols /*=false*/)
{
CSingleLock lock(g_sectionLoader.m_critSection);
if (dllname.empty()) return NULL;
// check if it's already loaded, and increase the reference count if so
for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
{
CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
if (StringUtils::EqualsNoCase(dll.m_strDllName, dllname))
{
dll.m_lReferenceCount++;
return dll.m_pDll;
}
}
// ok, now load the dll
CLog::Log(LOGDEBUG, "SECTION:LoadDLL(%s)\n", dllname.c_str());
LibraryLoader* pDll = DllLoaderContainer::LoadModule(dllname.c_str(), NULL, bLoadSymbols);
if (!pDll)
return NULL;
CDll newDLL;
newDLL.m_strDllName = dllname;
newDLL.m_lReferenceCount = 1;
newDLL.m_bDelayUnload=bDelayUnload;
newDLL.m_pDll=pDll;
g_sectionLoader.m_vecLoadedDLLs.push_back(newDLL);
return newDLL.m_pDll;
}
void CSectionLoader::UnloadDLL(const std::string &dllname)
{
CSingleLock lock(g_sectionLoader.m_critSection);
if (dllname.empty()) return;
// check if it's already loaded, and decrease the reference count if so
for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
{
CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
if (StringUtils::EqualsNoCase(dll.m_strDllName, dllname))
{
dll.m_lReferenceCount--;
if (0 == dll.m_lReferenceCount)
{
if (dll.m_bDelayUnload)
dll.m_unloadDelayStartTick = XbmcThreads::SystemClockMillis();
else
{
CLog::Log(LOGDEBUG,"SECTION:UnloadDll(%s)", dllname.c_str());
if (dll.m_pDll)
DllLoaderContainer::ReleaseModule(dll.m_pDll);
g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
}
return;
}
}
}
}
void CSectionLoader::UnloadDelayed()
{
CSingleLock lock(g_sectionLoader.m_critSection);
// check if we can unload any unreferenced dlls
for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
{
CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
if (dll.m_lReferenceCount == 0 && XbmcThreads::SystemClockMillis() - dll.m_unloadDelayStartTick > UNLOAD_DELAY)
{
CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(DLL: %s)", dll.m_strDllName.c_str());
if (dll.m_pDll)
DllLoaderContainer::ReleaseModule(dll.m_pDll);
g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
return;
}
}
}
void CSectionLoader::UnloadAll()
{
// delete the dll's
CSingleLock lock(g_sectionLoader.m_critSection);
std::vector<CDll>::iterator it = g_sectionLoader.m_vecLoadedDLLs.begin();
while (it != g_sectionLoader.m_vecLoadedDLLs.end())
{
CDll& dll = *it;
if (dll.m_pDll)
DllLoaderContainer::ReleaseModule(dll.m_pDll);
it = g_sectionLoader.m_vecLoadedDLLs.erase(it);
}
}
|