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)
45 bool CSectionLoader::IsLoaded(const CStdString& strSection)
47 CSingleLock lock(g_sectionLoader.m_critSection);
49 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedSections.size(); ++i)
51 CSection& section = g_sectionLoader.m_vecLoadedSections[i];
52 if (section.m_strSectionName == strSection && section.m_lReferenceCount > 0) return true;
57 bool CSectionLoader::Load(const CStdString& strSection)
59 CSingleLock lock(g_sectionLoader.m_critSection);
61 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedSections.size(); ++i)
63 CSection& section = g_sectionLoader.m_vecLoadedSections[i];
64 if (section.m_strSectionName == strSection)
68 CLog::Log(LOGDEBUG,"SECTION:LoadSection(%s) count:%i\n", strSection.c_str(), section.m_lReferenceCount);
71 section.m_lReferenceCount++;
77 if ( NULL == XLoadSection(strSection.c_str() ) )
79 CLog::Log(LOGDEBUG,"SECTION:LoadSection(%s) load failed!!\n", strSection.c_str());
82 HANDLE hHandle = XGetSectionHandle(strSection.c_str());
84 CLog::Log(LOGDEBUG,"SECTION:Section %s loaded count:1 size:%i\n", strSection.c_str(), XGetSectionSize(hHandle) );
88 newSection.m_strSectionName = strSection;
89 newSection.m_lReferenceCount = 1;
90 g_sectionLoader.m_vecLoadedSections.push_back(newSection);
94 void CSectionLoader::Unload(const CStdString& strSection)
96 CSingleLock lock(g_sectionLoader.m_critSection);
97 if (!CSectionLoader::IsLoaded(strSection)) return ;
100 i = g_sectionLoader.m_vecLoadedSections.begin();
101 while (i != g_sectionLoader.m_vecLoadedSections.end())
103 CSection& section = *i;
104 if (section.m_strSectionName == strSection)
107 CLog::Log(LOGDEBUG,"SECTION:FreeSection(%s) count:%i\n", strSection.c_str(), section.m_lReferenceCount);
109 section.m_lReferenceCount--;
110 if ( 0 == section.m_lReferenceCount)
112 section.m_unloadDelayStartTick = CTimeUtils::GetTimeMS();
120 LibraryLoader *CSectionLoader::LoadDLL(const CStdString &dllname, bool bDelayUnload /*=true*/, bool bLoadSymbols /*=false*/)
122 CSingleLock lock(g_sectionLoader.m_critSection);
124 if (!dllname) return NULL;
125 // check if it's already loaded, and increase the reference count if so
126 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
128 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
129 if (dll.m_strDllName.Equals(dllname))
131 dll.m_lReferenceCount++;
136 // ok, now load the dll
137 CLog::Log(LOGDEBUG, "SECTION:LoadDLL(%s)\n", dllname.c_str());
138 LibraryLoader* pDll = DllLoaderContainer::LoadModule(dllname.c_str(), NULL, bLoadSymbols);
143 newDLL.m_strDllName = dllname;
144 newDLL.m_lReferenceCount = 1;
145 newDLL.m_bDelayUnload=bDelayUnload;
147 g_sectionLoader.m_vecLoadedDLLs.push_back(newDLL);
149 return newDLL.m_pDll;
152 void CSectionLoader::UnloadDLL(const CStdString &dllname)
154 CSingleLock lock(g_sectionLoader.m_critSection);
156 if (!dllname) return;
157 // check if it's already loaded, and decrease the reference count if so
158 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
160 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
161 if (dll.m_strDllName.Equals(dllname))
163 dll.m_lReferenceCount--;
164 if (0 == dll.m_lReferenceCount)
166 if (dll.m_bDelayUnload)
167 dll.m_unloadDelayStartTick = CTimeUtils::GetTimeMS();
170 CLog::Log(LOGDEBUG,"SECTION:UnloadDll(%s)", dllname.c_str());
172 DllLoaderContainer::ReleaseModule(dll.m_pDll);
173 g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
182 void CSectionLoader::UnloadDelayed()
184 CSingleLock lock(g_sectionLoader.m_critSection);
186 ivecLoadedSections i = g_sectionLoader.m_vecLoadedSections.begin();
187 while( i != g_sectionLoader.m_vecLoadedSections.end() )
189 CSection& section = *i;
190 if( section.m_lReferenceCount == 0 && CTimeUtils::GetTimeMS() - section.m_unloadDelayStartTick > UNLOAD_DELAY)
192 CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(SECTION: %s)", section.m_strSectionName.c_str());
194 XFreeSection(section.m_strSectionName.c_str());
196 i = g_sectionLoader.m_vecLoadedSections.erase(i);
202 // check if we can unload any unreferenced dlls
203 for (int i = 0; i < (int)g_sectionLoader.m_vecLoadedDLLs.size(); ++i)
205 CDll& dll = g_sectionLoader.m_vecLoadedDLLs[i];
206 if (dll.m_lReferenceCount == 0 && CTimeUtils::GetTimeMS() - dll.m_unloadDelayStartTick > UNLOAD_DELAY)
208 CLog::Log(LOGDEBUG,"SECTION:UnloadDelayed(DLL: %s)", dll.m_strDllName.c_str());
211 DllLoaderContainer::ReleaseModule(dll.m_pDll);
212 g_sectionLoader.m_vecLoadedDLLs.erase(g_sectionLoader.m_vecLoadedDLLs.begin() + i);
218 void CSectionLoader::UnloadAll()
220 ivecLoadedSections i;
221 i = g_sectionLoader.m_vecLoadedSections.begin();
222 while (i != g_sectionLoader.m_vecLoadedSections.end())
224 CSection& section = *i;
225 //g_sectionLoader.m_vecLoadedSections.erase(i);
226 CLog::Log(LOGDEBUG,"SECTION:UnloadAll(SECTION: %s)", section.m_strSectionName.c_str());
228 XFreeSection(section.m_strSectionName.c_str());
230 i = g_sectionLoader.m_vecLoadedSections.erase(i);
234 CSingleLock lock(g_sectionLoader.m_critSection);
235 vector<CDll>::iterator it = g_sectionLoader.m_vecLoadedDLLs.begin();
236 while (it != g_sectionLoader.m_vecLoadedDLLs.end())
239 CLog::Log(LOGDEBUG,"SECTION:UnloadAll(DLL: %s)", dll.m_strDllName.c_str());
241 DllLoaderContainer::ReleaseModule(dll.m_pDll);
242 it = g_sectionLoader.m_vecLoadedDLLs.erase(it);