1 karl 1.14 //%2006////////////////////////////////////////////////////////////////////////
|
2 chip 1.1 //
|
3 karl 1.7 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
6 karl 1.6 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 karl 1.8 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.14 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 chip 1.1 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
|
20 karl 1.6 //
|
21 chip 1.1 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Chip Vincent (cvincent@us.ibm.com)
33 //
34 // Modified By:
35 //
36 //%/////////////////////////////////////////////////////////////////////////////
37
38 #include "DynamicLibrary.h"
39
40 #include <Pegasus/Common/IPC.h>
|
41 kumpf 1.2 #include <Pegasus/Common/ArrayInternal.h>
42 #include <Pegasus/Common/Pair.h>
|
43 chip 1.1
44 #include <dl.h>
45
46 PEGASUS_NAMESPACE_BEGIN
47
|
48 mike 1.10 // the HPUX dynamic library load and unload routines do not keep a reference
49 // count. this implies that if a library were loaded multiple times, a single
50 // unload will release the library from the process thereby potentially leaving
51 // dangling references behind. this is a tenative implementation that
52 // encapsulates this behavior from users of the DynamicLibrary object. the goal
53 // release the library only after an equal number of loads and unloads have
54 // occured.
|
55 chip 1.1
|
56 mike 1.10 static Array<Pair<DynamicLibrary::LIBRARY_HANDLE, int> > _references;
57 static Mutex _mutex;
|
58 chip 1.1
|
59 mike 1.10 static Uint32 _increment_handle(DynamicLibrary::LIBRARY_HANDLE handle)
|
60 chip 1.1 {
|
61 mike 1.10 AutoMutex autoMutex(_mutex);
62
|
63 chip 1.1 // scoped mutex
64
65 // seek and increment
66 for(Uint32 i = 0, n = _references.size(); i < n; i++)
67 {
|
68 kumpf 1.2 if(handle == _references[i].first)
|
69 chip 1.1 {
|
70 mike 1.10 Uint32 n = ++_references[i].second;
71 return n;
|
72 chip 1.1 }
73 }
74
75 // not found, append and set at 1
|
76 mike 1.10 _references.append(Pair<DynamicLibrary::LIBRARY_HANDLE, int>(handle, 1));
|
77 chip 1.1
|
78 mike 1.10 return 1;
|
79 chip 1.1 }
80
|
81 mike 1.10 static Uint32 _decrement_handle(DynamicLibrary::LIBRARY_HANDLE handle)
|
82 chip 1.1 {
|
83 mike 1.10 AutoMutex autoMutex(_mutex);
84
|
85 chip 1.1 // scoped mutex
86
87 // seek and decrement
88 for(Uint32 i = 0, n = _references.size(); i < n; i++)
89 {
|
90 kumpf 1.2 if(handle == _references[i].first)
|
91 chip 1.1 {
|
92 mike 1.10 Uint32 n = --_references[i].second;
|
93 chip 1.1
94 if(n == 0)
95 {
96 _references.remove(i);
97 }
98
|
99 mike 1.10 return n;
|
100 chip 1.1 }
101 }
102
103 // not found, must be 0
|
104 mike 1.10 return 0;
|
105 chip 1.1 }
106
|
107 chip 1.3 Boolean DynamicLibrary::load(void)
|
108 chip 1.1 {
|
109 chip 1.5 // ensure the module is not already loaded
110 PEGASUS_ASSERT(isLoaded() == false);
111
112 CString cstr = _fileName.getCString();
113
|
114 kumpf 1.13 //_handle = shl_load(cstr,BIND_IMMEDIATE|DYNAMIC_PATH|BIND_VERBOSE, 0L);
115 _handle = shl_load(cstr, BIND_IMMEDIATE | DYNAMIC_PATH, 0L);
|
116 chip 1.5
117 // increment handle if valid
|
118 kumpf 1.13 if(_handle == 0)
|
119 chip 1.1 {
|
120 chip 1.5 _increment_handle(_handle);
|
121 chip 1.1 }
122
|
123 chip 1.5 return(isLoaded());
|
124 chip 1.1 }
125
|
126 chip 1.3 Boolean DynamicLibrary::unload(void)
|
127 chip 1.1 {
|
128 chip 1.5 // ensure the module is loaded
129 PEGASUS_ASSERT(isLoaded() == true);
130
|
131 mike 1.10 // decrement handle is valid and release the library only if the handle
132 // reference count is 0
|
133 chip 1.5 if((_handle != 0) && (_decrement_handle(_handle) == 0))
|
134 chip 1.1 {
|
135 kumpf 1.13 shl_unload(reinterpret_cast<shl_t>(_handle));
|
136 chip 1.5 }
137
138 _handle = 0;
|
139 chip 1.1
|
140 chip 1.5 return(isLoaded());
141 }
|
142 chip 1.1
|
143 chip 1.5 Boolean DynamicLibrary::isLoaded(void) const
144 {
145 return(_handle != 0);
|
146 chip 1.1 }
147
|
148 mike 1.10 DynamicLibrary::LIBRARY_SYMBOL DynamicLibrary::getSymbol(
149 const String & symbolName)
|
150 chip 1.1 {
151 LIBRARY_SYMBOL func = 0;
152
|
153 chip 1.5 if(isLoaded())
|
154 chip 1.1 {
155 CString cstr = symbolName.getCString();
156
|
157 kumpf 1.13 if(shl_findsym((shl_t *)&_handle, cstr, TYPE_UNDEFINED, &func) == 0)
158 {
159 return(func);
160 }
161
162 // NOTE: should the underscore be prepended by the caller or should
163 // this be a compile time option?
164
165 cstr = String(String("_") + symbolName).getCString();
166
167 if(shl_findsym((shl_t *)_handle, cstr, TYPE_UNDEFINED, &func) == 0)
|
168 chip 1.1 {
169 return(func);
170 }
171 }
172
173 return(0);
174 }
175
176 PEGASUS_NAMESPACE_END
|