xref: /trunk/main/salhelper/source/dynload.cxx (revision f3cac8d6)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <salhelper/dynload.hxx>
25 #include <rtl/ustrbuf.hxx>
26 
27 namespace salhelper
28 {
29 
30 typedef	void*	(SAL_CALL *ApiInitFunction) (void);
31 
ORealDynamicLoader(ORealDynamicLoader ** ppSetToZeroInDestructor_,const rtl::OUString & moduleName,const rtl::OUString & initFunction,void * pApi,oslModule pModule)32 ORealDynamicLoader::ORealDynamicLoader(ORealDynamicLoader ** ppSetToZeroInDestructor_,
33 				       const rtl::OUString& moduleName,
34 				       const rtl::OUString& initFunction,
35 				       void* pApi,
36 				       oslModule pModule)
37 	: m_pApi(pApi)
38 	, m_refCount(1)
39 	, m_pModule(pModule)
40 	, m_strModuleName(moduleName)
41 	, m_strInitFunction(initFunction)
42 	, ppSetToZeroInDestructor( ppSetToZeroInDestructor_ )
43 {
44 }
45 
newInstance(ORealDynamicLoader ** ppSetToZeroInDestructor,const rtl::OUString & moduleName,const rtl::OUString & initFunction)46 ORealDynamicLoader* ORealDynamicLoader::newInstance(ORealDynamicLoader ** ppSetToZeroInDestructor,
47 								  const rtl::OUString& moduleName,
48 								  const rtl::OUString& initFunction)
49 {
50 	ApiInitFunction initFunc;
51 	oslModule pModule = osl_loadModule(moduleName.pData, SAL_LOADMODULE_DEFAULT);
52 
53 	if ( !pModule )
54 	{
55 		return NULL;
56 	}
57 
58 	initFunc = (ApiInitFunction)osl_getFunctionSymbol(
59         pModule, initFunction.pData);
60 
61 	if ( !initFunc )
62 	{
63 		osl_unloadModule(pModule);
64 		return NULL;
65 	}
66 
67 	return(new ORealDynamicLoader(ppSetToZeroInDestructor, moduleName,
68 								 initFunction,
69 								 initFunc(),
70 								 pModule));
71 }
72 
~ORealDynamicLoader()73 ORealDynamicLoader::~ORealDynamicLoader()
74 {
75 	// set the address to zero
76 	if( ppSetToZeroInDestructor )
77 		*ppSetToZeroInDestructor = 0;
78 
79 	if (m_pModule)
80 	{
81 		osl_unloadModule(m_pModule);
82 		m_pModule = NULL;
83 	}
84 }
85 
acquire()86 sal_uInt32 ORealDynamicLoader::acquire()
87 {
88 	return ++m_refCount;
89 }
90 
release()91 sal_uInt32 ORealDynamicLoader::release()
92 {
93 	sal_uInt32 nRet = --m_refCount;
94 	if( nRet == 0 )
95 		delete this;
96 	return nRet;
97 }
98 
99 
getApi() const100 void* ORealDynamicLoader::getApi() const
101 {
102 	return m_pApi;
103 }
104 
105 } // namespace salhelper
106 
107