2 * Copyright (C) 2005-2008 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 #include "SectionLoader.h"
24 #include "cores/DllLoader/DllLoaderContainer.h"
25 #include "threads/SingleLock.h"
26 #include "utils/log.h"
27 #include "utils/TimeUtils.h"
31 #define g_sectionLoader XBMC_GLOBAL_USE(CSectionLoader)
33 // delay for unloading dll's
34 #define UNLOAD_DELAY 30*1000 // 30 sec.
36 //Define this to get loggin on all calls to load/unload sections/dlls
39 CSectionLoader::CSectionLoader(void)
42 CSectionLoader::~CSectionLoader(void)
47 bool CSectionLoader::IsLoaded(const CStdString& strSection)
49 CSingleLock lock(g_sectionLoader.m_critSection);
51 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedSections.size(); ++i)
53 CSection& section = g_sectionLoader.m_vecLoadedSections[i];
54 if (section.m_strSectionName == strSection && section.m_lReferenceCount > 0) return true;
59 bool CSectionLoader::Load(const CStdString& strSection)
61 CSingleLock lock(g_sectionLoader.m_critSection);
63 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedSections.size(); ++i)
65 CSection& section = g_sectionLoader.m_vecLoadedSections[i];
66 if (section.m_strSectionName == strSection)
70 CLog::Log(LOGDEBUG,"SECTION:LoadSection(%s) count:%i\n", strSection.c_str(), section.m_lReferenceCount);
73 section.m_lReferenceCount++;
79 if ( NULL == XLoadSection(strSection.c_str() ) )
81 CLog::Log(LOGDEBUG,"SECTION:LoadSection(%s) load failed!!\n", strSection.c_str());
84 HANDLE hHandle = XGetSectionHandle(strSection.c_str());
86 CLog::Log(LOGDEBUG,"SECTION:Section %s loaded count:1 size:%i\n", strSection.c_str(), XGetSectionSize(hHandle) );
90 newSection.m_strSectionName = strSection;
91 newSection.m_lReferenceCount = 1;
92 g_sectionLoader.m_vecLoadedSections.push_back(newSection);
96 void CSectionLoader::Unload(const CStdString& strSection)
98 CSingleLock lock(g_sectionLoader.m_critSection);
99 if (!CSectionLoader::IsLoaded(strSection)) return ;
101 ivecLoadedSections i;
102 i = g_sectionLoader.m_vecLoadedSections.begin();
103 while (i != g_sectionLoader.m_vecLoadedSections.end())
105 CSection& section = *i;
106 if (section.m_strSectionName == strSection)
109 CLog::Log(LOGDEBUG,"SECTION:FreeSection(%s) count:%i\n", strSection.c_str(), section.m_lReferenceCount);
111 section.m_lReferenceCount--;
112 if ( 0 == section.m_lReferenceCount)
114 section.m_unloadDelayStartTick = CTimeUtils::GetTimeMS();
122 LibraryLoader *CSectionLoader::LoadDLL(const CStdString &dllname, bool bDelayUnload /*=true*/, bool bLoadSymbols /*=false*/)
124 CSingleLock lock(g_sectionLoader.m_critSection);
126 if (!dllname) return NULL;
127 // check if it's already loaded, and increase the reference count if so
128 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
130 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
131 if (dll.m_strDllName.Equals(dllname))
133 dll.m_lReferenceCount++;
138 // ok, now load the dll
139 CLog::Log(LOGDEBUG, "SECTION:LoadDLL(%s)\n", dllname.c_str());
140 LibraryLoader* pDll = DllLoaderContainer::LoadModule(dllname.c_str(), NULL, bLoadSymbols);
145 newDLL.m_strDllName = dllname;
146 newDLL.m_lReferenceCount = 1;
147 newDLL.m_bDelayUnload=bDelayUnload;
149 g_sectionLoader.m_vecLoadedDLLs.push_back(newDLL);
151 return newDLL.m_pDll;
154 void CSectionLoader::UnloadDLL(const CStdString &dllname)
156 CSingleLock lock(g_sectionLoader.m_critSection);
158 if (!dllname) return;
159 // check if it's already loaded, and decrease the reference count if so
160 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
162 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
163 if (dll.m_strDllName.Equals(dllname))
165 dll.m_lReferenceCount--;
166 if (0 == dll.m_lReferenceCount)
168 if (dll.m_bDelayUnload)
169 dll.m_unloadDelayStartTick = CTimeUtils::GetTimeMS();
172 CLog::Log(LOGDEBUG,"SECTION:UnloadDll(%s)", dllname.c_str());
174 DllLoaderContainer::ReleaseModule(dll.m_pDll);
175 g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
184 void CSectionLoader::UnloadDelayed()
186 CSingleLock lock(g_sectionLoader.m_critSection);
188 ivecLoadedSections i = g_sectionLoader.m_vecLoadedSections.begin();
189 while( i != g_sectionLoader.m_vecLoadedSections.end() )
191 CSection& section = *i;
192 if( section.m_lReferenceCount == 0 && CTimeUtils::GetTimeMS() - section.m_unloadDelayStartTick > UNLOAD_DELAY)
194 CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(SECTION: %s)", section.m_strSectionName.c_str());
196 XFreeSection(section.m_strSectionName.c_str());
198 i = g_sectionLoader.m_vecLoadedSections.erase(i);
204 // check if we can unload any unreferenced dlls
205 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
207 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
208 if (dll.m_lReferenceCount == 0 && CTimeUtils::GetTimeMS() - dll.m_unloadDelayStartTick > UNLOAD_DELAY)
210 CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(DLL: %s)", dll.m_strDllName.c_str());
213 DllLoaderContainer::ReleaseModule(dll.m_pDll);
214 g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
220 void CSectionLoader::UnloadAll()
222 ivecLoadedSections i;
223 i = g_sectionLoader.m_vecLoadedSections.begin();
224 while (i != g_sectionLoader.m_vecLoadedSections.end())
226 CSection& section = *i;
227 //g_sectionLoader.m_vecLoadedSections.erase(i);
228 CLog::Log(LOGDEBUG,"SECTION:UnloadAll(SECTION: %s)", section.m_strSectionName.c_str());
230 XFreeSection(section.m_strSectionName.c_str());
232 i = g_sectionLoader.m_vecLoadedSections.erase(i);
236 CSingleLock lock(g_sectionLoader.m_critSection);
237 vector<CDll>::iterator it = g_sectionLoader.m_vecLoadedDLLs.begin();
238 while (it != g_sectionLoader.m_vecLoadedDLLs.end())
241 CLog::Log(LOGDEBUG,"SECTION:UnloadAll(DLL: %s)", dll.m_strDllName.c_str());
243 DllLoaderContainer::ReleaseModule(dll.m_pDll);
244 it = g_sectionLoader.m_vecLoadedDLLs.erase(it);