00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00043
00044
00045
00046
00047 #include <stdlib.h>
00048 #include <stdio.h>
00049
00050 #include "OSGConfig.h"
00051
00052 #include <iostream>
00053
00054 #include "OSGSharedObjectHandler.h"
00055 #include "OSGBaseFunctions.h"
00056
00057 #include "OSGLog.h"
00058
00059 #ifndef WIN32
00060 #include <dlfcn.h>
00061 #endif
00062
00063 OSG_USING_NAMESPACE
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 Char8 SharedObject::_szApplicationObjectName[] = "Application";
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 SharedObject::SharedObject(const Char8 *szName) :
00110 Inherited( ),
00111 _szName ( ),
00112 _pHandle (NULL ),
00113 _type (Invalid)
00114 {
00115 if(szName == NULL)
00116 {
00117 _szName.assign(_szApplicationObjectName);
00118
00119 _type = Application;
00120 }
00121 else
00122 {
00123 _szName.assign(szName);
00124
00125 _type = SharedLibrary;
00126 }
00127
00128 FINFO(("construct SharedObject %s\n", _szName.c_str()));
00129 }
00130
00131 SharedObject::~SharedObject(void)
00132 {
00133 FINFO(("destroy SharedObject %s\n", _szName.c_str()));
00134 }
00135
00136 bool SharedObject::close(void)
00137 {
00138 bool returnValue = false;
00139
00140 if(_pHandle != NULL)
00141 {
00142 #ifndef WIN32
00143 returnValue = (dlclose(_pHandle) == 0);
00144 #else
00145 returnValue = FreeLibrary(_pHandle);
00146 #endif
00147 _pHandle = NULL;
00148 }
00149
00150 return returnValue;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159 bool SharedObject::open()
00160 {
00161 const Char8 *libName = NULL;
00162
00163 if(_pHandle != NULL)
00164 {
00165 return true;
00166 }
00167
00168 if(_type == SharedLibrary)
00169 {
00170 libName = _szName.c_str();
00171 }
00172
00173 #ifndef WIN32
00174 #ifdef OSG_DLOPEN_LAZY
00175 _pHandle = dlopen(libName, RTLD_LAZY);
00176 #else
00177 _pHandle = dlopen(libName, RTLD_NOW);
00178 #endif
00179
00180 if(_pHandle == NULL)
00181 {
00182 FWARNING(("Could not open shared object : %s\n", dlerror()));
00183 }
00184 #else
00185
00186 if(libName == NULL)
00187 {
00188 Char8 szModuleName[1024];
00189
00190 GetModuleFileName(NULL, szModuleName, 1024);
00191
00192 _pHandle = LoadLibrary(szModuleName);
00193 }
00194 else
00195 {
00196 _pHandle = LoadLibrary(libName);
00197 }
00198 #endif
00199
00200 return (_pHandle != NULL);
00201 }
00202
00203 AnonSymbolHandle SharedObject::getSymbol(const Char8 *szSymbolName)
00204 {
00205 AnonSymbolHandle returnValue = NULL;
00206
00207 if(isOpen() == false)
00208 {
00209 if(open() == false)
00210 {
00211 return returnValue;
00212 }
00213 }
00214
00215 if(_pHandle != NULL && szSymbolName != NULL)
00216 {
00217 #ifndef WIN32
00218 returnValue = dlsym(_pHandle, szSymbolName);
00219 #else
00220 returnValue = GetProcAddress(_pHandle, szSymbolName);
00221 #endif
00222 }
00223
00224 #ifndef WIN32
00225 if(returnValue == NULL)
00226 {
00227 FWARNING(("could not get symbol %s : %s\n", szSymbolName, dlerror()));
00228 }
00229 #else
00230 #endif
00231
00232 return returnValue;
00233 }
00234
00235 bool SharedObject::isOpen(void)
00236 {
00237 return _pHandle != NULL;
00238 }
00239
00240 bool SharedObject::reOpen(void)
00241 {
00242 close();
00243
00244 return open();
00245 }
00246
00247 const std::string &SharedObject::getName(void)
00248 {
00249 return _szName;
00250 }
00251
00252 const Char8 *SharedObject::getCName(void)
00253 {
00254 return _szName.c_str();
00255 }
00256
00257 void SharedObject::dump(void)
00258 {
00259 FINFO(("\tObject %s | %p\n", _szName.c_str(), _pHandle));
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 SharedObjectHandlerP SharedObjectHandler::_the = NULL;
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 SharedObjectHandlerP SharedObjectHandler::the(void)
00305 {
00306 if(_the == NULL)
00307 {
00308 _the = new SharedObjectHandler;
00309 }
00310
00311 return _the;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 SharedObjectHandler::SharedObjectHandler(void) :
00329 _mSharedObjects(),
00330 _vLoadedNames ()
00331 {
00332 FINFO(("create SharedObjectHandler\n"));
00333 }
00334
00335 SharedObjectHandler::~SharedObjectHandler(void)
00336 {
00337 FINFO(("destroy SharedObjectHandler\n"));
00338 }
00339
00340 void SharedObjectHandler::terminate(void)
00341 {
00342 FINFO(("terminate SharedObjectHandler\n"));
00343
00344 this->dump();
00345
00346 SharedObjectMapIt soIt = _mSharedObjects.begin();
00347 SharedObjectMapIt soEnd = _mSharedObjects.end ();
00348
00349 while(soIt != soEnd)
00350 {
00351 soIt->second->close();
00352
00353 soIt->second->subRef();
00354
00355 ++soIt;
00356 }
00357
00358 _the = NULL;
00359 delete this;
00360 }
00361
00362
00363
00364
00365
00366
00367
00368 SharedObjectP SharedObjectHandler::getSharedObject(
00369 const Char8 *szName)
00370 {
00371 SharedObjectP returnValue = NULL;
00372
00373 returnValue = findSharedObject(szName);
00374
00375 if(returnValue == NULL)
00376 {
00377 returnValue = new SharedObject(szName);
00378
00379 returnValue->open ();
00380 returnValue->addRef();
00381
00382 _mSharedObjects[returnValue->getName()] = returnValue;
00383
00384 if(GlobalSystemState == Running)
00385 {
00386 osgPostLoadInit();
00387 }
00388 }
00389
00390 #if 0
00391 for(UInt32 i = 0; i < _vLoadedNames.size(); ++i)
00392 {
00393 FINFO(("Pulled in %s\n", _vLoadedNames[i].c_str()));
00394 }
00395 #endif
00396
00397 _vLoadedNames.clear();
00398
00399 return returnValue;
00400 }
00401
00402 SharedObjectP SharedObjectHandler::getOSGSharedObject(
00403 const Char8 *szName)
00404 {
00405 SharedObjectP returnValue = NULL;
00406
00407 std::string tmpString;
00408
00409 #ifndef WIN32
00410 tmpString.append("lib");
00411 tmpString.append(szName);
00412 tmpString.append(".so");
00413 #else
00414 tmpString.append(szName);
00415 tmpString.append(".dll");
00416 #endif
00417
00418 returnValue = getSharedObject(tmpString.c_str());
00419
00420 return returnValue;
00421 }
00422
00423 SharedObjectP SharedObjectHandler::findSharedObject(
00424 const Char8 *szName) const
00425 {
00426 SharedObjectMapConstIt mapIt;
00427 SharedObjectP returnValue = NULL;
00428 std::string szSearchName;
00429
00430 if(szName != NULL)
00431 {
00432 szSearchName.assign(szName);
00433 }
00434 else
00435 {
00436 szSearchName.assign(SharedObject::_szApplicationObjectName);
00437 }
00438
00439 mapIt = _mSharedObjects.find(szSearchName);
00440
00441 if(mapIt != _mSharedObjects.end())
00442 {
00443 returnValue = mapIt->second;
00444 }
00445
00446 return returnValue;
00447 }
00448
00449 void SharedObjectHandler::removeSharedObject(const Char8 *szName)
00450 {
00451 SharedObjectMapIt mapIt;
00452 std::string szSearchName;
00453
00454 if(szName != NULL)
00455 {
00456 szSearchName.assign(szName);
00457 }
00458 else
00459 {
00460 szSearchName.assign(SharedObject::_szApplicationObjectName);
00461 }
00462
00463 mapIt = _mSharedObjects.find(szSearchName);
00464
00465 _mSharedObjects.erase(mapIt);
00466 }
00467
00468 void SharedObjectHandler::removeSharedObject(SharedObjectP pObject)
00469 {
00470 if(pObject != NULL)
00471 {
00472 removeSharedObject(pObject->getCName());
00473 }
00474 }
00475
00476 void SharedObjectHandler::registerLoadedObject(const Char8 *szName)
00477 {
00478 std::string tmpString;
00479
00480 #ifndef WIN32
00481 tmpString.append("lib");
00482 tmpString.append(szName);
00483 tmpString.append(".so");
00484 #else
00485 tmpString.append(szName);
00486 tmpString.append(".dll");
00487 #endif
00488
00489 _vLoadedNames.push_back(tmpString);
00490 }
00491
00492 bool SharedObjectHandler::initialize(void)
00493 {
00494 SharedObjectP pAppHandle = getSharedObject(NULL);
00495
00496 for(UInt32 i = 0; i < _vLoadedNames.size(); ++i)
00497 {
00498 FINFO(("Preloaded %s %p\n", _vLoadedNames[i].c_str(), pAppHandle));
00499
00500 _mSharedObjects[_vLoadedNames[i]] = pAppHandle;
00501 pAppHandle->addRef();
00502 }
00503
00504 _vLoadedNames.clear();
00505
00506 return true;
00507 }
00508
00509 void SharedObjectHandler::dump(void)
00510 {
00511 SharedObjectMapIt soIt = _mSharedObjects.begin();
00512 SharedObjectMapIt soEnd = _mSharedObjects.end ();
00513
00514 while(soIt != soEnd)
00515 {
00516 FINFO(("SO : %s | %p\n", soIt->first.c_str(), soIt->second));
00517
00518 soIt->second->dump();
00519 ++soIt;
00520 }
00521 }
00522
00523
00524
00525
00526 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00527
00528 #ifdef OSG_SGI_CC
00529 #pragma set woff 1174
00530 #endif
00531
00532 #ifdef OSG_LINUX_ICC
00533 #pragma warning( disable : 177 )
00534 #endif
00535
00536 namespace
00537 {
00538 static Char8 cvsid_cpp[] = "@(#)$Id: $";
00539 static Char8 cvsid_hpp[] = OSGSHAREDOBJECTHANDLER_HEADER_CVSID;
00540 }
00541
00542 #endif