1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/gl/GLLibraryLoader.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,112 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "GLLibraryLoader.h" 1.9 + 1.10 +#include "nsDebug.h" 1.11 + 1.12 +namespace mozilla { 1.13 +namespace gl { 1.14 + 1.15 +bool 1.16 +GLLibraryLoader::OpenLibrary(const char *library) 1.17 +{ 1.18 + PRLibSpec lspec; 1.19 + lspec.type = PR_LibSpec_Pathname; 1.20 + lspec.value.pathname = library; 1.21 + 1.22 + mLibrary = PR_LoadLibraryWithFlags(lspec, PR_LD_LAZY | PR_LD_LOCAL); 1.23 + if (!mLibrary) 1.24 + return false; 1.25 + 1.26 + return true; 1.27 +} 1.28 + 1.29 +bool 1.30 +GLLibraryLoader::LoadSymbols(SymLoadStruct *firstStruct, 1.31 + bool tryplatform, 1.32 + const char *prefix, 1.33 + bool warnOnFailure) 1.34 +{ 1.35 + return LoadSymbols(mLibrary, 1.36 + firstStruct, 1.37 + tryplatform ? mLookupFunc : nullptr, 1.38 + prefix, 1.39 + warnOnFailure); 1.40 +} 1.41 + 1.42 +PRFuncPtr 1.43 +GLLibraryLoader::LookupSymbol(PRLibrary *lib, 1.44 + const char *sym, 1.45 + PlatformLookupFunction lookupFunction) 1.46 +{ 1.47 + PRFuncPtr res = 0; 1.48 + 1.49 + // try finding it in the library directly, if we have one 1.50 + if (lib) { 1.51 + res = PR_FindFunctionSymbol(lib, sym); 1.52 + } 1.53 + 1.54 + // then try looking it up via the lookup symbol 1.55 + if (!res && lookupFunction) { 1.56 + res = lookupFunction(sym); 1.57 + } 1.58 + 1.59 + // finally just try finding it in the process 1.60 + if (!res) { 1.61 + PRLibrary *leakedLibRef; 1.62 + res = PR_FindFunctionSymbolAndLibrary(sym, &leakedLibRef); 1.63 + } 1.64 + 1.65 + return res; 1.66 +} 1.67 + 1.68 +bool 1.69 +GLLibraryLoader::LoadSymbols(PRLibrary *lib, 1.70 + SymLoadStruct *firstStruct, 1.71 + PlatformLookupFunction lookupFunction, 1.72 + const char *prefix, 1.73 + bool warnOnFailure) 1.74 +{ 1.75 + char sbuf[MAX_SYMBOL_LENGTH * 2]; 1.76 + int failCount = 0; 1.77 + 1.78 + SymLoadStruct *ss = firstStruct; 1.79 + while (ss->symPointer) { 1.80 + *ss->symPointer = 0; 1.81 + 1.82 + for (int i = 0; i < MAX_SYMBOL_NAMES; i++) { 1.83 + if (ss->symNames[i] == nullptr) 1.84 + break; 1.85 + 1.86 + const char *s = ss->symNames[i]; 1.87 + if (prefix && *prefix != 0) { 1.88 + strcpy(sbuf, prefix); 1.89 + strcat(sbuf, ss->symNames[i]); 1.90 + s = sbuf; 1.91 + } 1.92 + 1.93 + PRFuncPtr p = LookupSymbol(lib, s, lookupFunction); 1.94 + if (p) { 1.95 + *ss->symPointer = p; 1.96 + break; 1.97 + } 1.98 + } 1.99 + 1.100 + if (*ss->symPointer == 0) { 1.101 + if (warnOnFailure) 1.102 + printf_stderr("Can't find symbol '%s'.\n", ss->symNames[0]); 1.103 + 1.104 + failCount++; 1.105 + } 1.106 + 1.107 + ss++; 1.108 + } 1.109 + 1.110 + return failCount == 0 ? true : false; 1.111 +} 1.112 + 1.113 +} /* namespace gl */ 1.114 +} /* namespace mozilla */ 1.115 +