xref: /aoo42x/main/salhelper/source/dynload.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <salhelper/dynload.hxx>
29 #include <rtl/ustrbuf.hxx>
30 
31 namespace salhelper
32 {
33 
34 typedef	void*	(SAL_CALL *ApiInitFunction) (void);
35 
36 ORealDynamicLoader::ORealDynamicLoader(ORealDynamicLoader ** ppSetToZeroInDestructor_,
37 				       const rtl::OUString& moduleName,
38 				       const rtl::OUString& initFunction,
39 				       void* pApi,
40 				       oslModule pModule)
41 	: m_pApi(pApi)
42 	, m_refCount(1)
43 	, m_pModule(pModule)
44 	, m_strModuleName(moduleName)
45 	, m_strInitFunction(initFunction)
46 	, ppSetToZeroInDestructor( ppSetToZeroInDestructor_ )
47 {
48 }
49 
50 ORealDynamicLoader* ORealDynamicLoader::newInstance(ORealDynamicLoader ** ppSetToZeroInDestructor,
51 								  const rtl::OUString& moduleName,
52 								  const rtl::OUString& initFunction)
53 {
54 	ApiInitFunction initFunc;
55 	oslModule pModule = osl_loadModule(moduleName.pData, SAL_LOADMODULE_DEFAULT);
56 
57 	if ( !pModule )
58 	{
59 		return NULL;
60 	}
61 
62 	initFunc = (ApiInitFunction)osl_getFunctionSymbol(
63         pModule, initFunction.pData);
64 
65 	if ( !initFunc )
66 	{
67 		osl_unloadModule(pModule);
68 		return NULL;
69 	}
70 
71 	return(new ORealDynamicLoader(ppSetToZeroInDestructor, moduleName,
72 								 initFunction,
73 								 initFunc(),
74 								 pModule));
75 }
76 
77 ORealDynamicLoader::~ORealDynamicLoader()
78 {
79 	// set the address to zero
80 	if( ppSetToZeroInDestructor )
81 		*ppSetToZeroInDestructor = 0;
82 
83 	if (m_pModule)
84 	{
85 		osl_unloadModule(m_pModule);
86 		m_pModule = NULL;
87 	}
88 }
89 
90 sal_uInt32 ORealDynamicLoader::acquire()
91 {
92 	return ++m_refCount;
93 }
94 
95 sal_uInt32 ORealDynamicLoader::release()
96 {
97 	sal_uInt32 nRet = --m_refCount;
98 	if( nRet == 0 )
99 		delete this;
100 	return nRet;
101 }
102 
103 
104 void* ORealDynamicLoader::getApi() const
105 {
106 	return m_pApi;
107 }
108 
109 } // namespace salhelper
110 
111