gfx/gl/GLLibraryLoader.cpp
author Mozilla Releng Treescript <release+treescript@mozilla.org>
Fri, 16 Nov 2018 16:05:12 +0000
changeset 501264 f7da5f0188e1cdb842cef460b4e8bfd9af67f2fd
parent 396746 a2624aa679c189d22240dc3dabedccae97e246f5
child 508163 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
No bug - Tagging 7473fdd1c21c00299314892ad74b10aa5af67179 with FIREFOX_64_0b10_RELEASE a=release CLOSED TREE DONTBUILD

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "GLLibraryLoader.h"

#include "nsDebug.h"

#ifdef WIN32
#include <windows.h>
#endif

namespace mozilla {
namespace gl {

bool
GLLibraryLoader::OpenLibrary(const char* library)
{
    PRLibSpec lspec;
    lspec.type = PR_LibSpec_Pathname;
    lspec.value.pathname = library;

    mLibrary = PR_LoadLibraryWithFlags(lspec, PR_LD_LAZY | PR_LD_LOCAL);
    if (!mLibrary)
        return false;

    return true;
}

bool
GLLibraryLoader::LoadSymbols(const SymLoadStruct* firstStruct,
                             bool tryplatform,
                             const char* prefix,
                             bool warnOnFailure)
{
    return LoadSymbols(mLibrary,
                       firstStruct,
                       tryplatform ? mLookupFunc : nullptr,
                       prefix,
                       warnOnFailure);
}

PRFuncPtr
GLLibraryLoader::LookupSymbol(const char* sym)
{
    return LookupSymbol(mLibrary, sym, mLookupFunc);
}

PRFuncPtr
GLLibraryLoader::LookupSymbol(PRLibrary* lib,
                              const char* sym,
                              PlatformLookupFunction lookupFunction)
{
    PRFuncPtr res = 0;

    // try finding it in the library directly, if we have one
    if (lib) {
        res = PR_FindFunctionSymbol(lib, sym);
    }

    // then try looking it up via the lookup symbol
    if (!res && lookupFunction) {
        res = lookupFunction(sym);
    }

    // finally just try finding it in the process
    if (!res) {
        PRLibrary* leakedLibRef;
        res = PR_FindFunctionSymbolAndLibrary(sym, &leakedLibRef);
    }

    return res;
}

bool
GLLibraryLoader::LoadSymbols(PRLibrary* lib,
                             const SymLoadStruct* firstStruct,
                             PlatformLookupFunction lookupFunction,
                             const char* prefix,
                             bool warnOnFailure)
{
    char sbuf[MAX_SYMBOL_LENGTH * 2];
    int failCount = 0;

    const SymLoadStruct* ss = firstStruct;
    while (ss->symPointer) {
        *ss->symPointer = 0;

        for (int i = 0; i < MAX_SYMBOL_NAMES; i++) {
            if (ss->symNames[i] == nullptr)
                break;

            const char* s = ss->symNames[i];
            if (prefix && *prefix != 0) {
                strcpy(sbuf, prefix);
                strcat(sbuf, ss->symNames[i]);
                s = sbuf;
            }

            PRFuncPtr p = LookupSymbol(lib, s, lookupFunction);
            if (p) {
                *ss->symPointer = p;
                break;
            }
        }

        if (*ss->symPointer == 0) {
            if (warnOnFailure) {
                printf_stderr("Can't find symbol '%s'.\n", ss->symNames[0]);
            }

            failCount++;
        }

        ss++;
    }

    return failCount == 0 ? true : false;
}

/*static*/ void
GLLibraryLoader::ClearSymbols(const SymLoadStruct* const firstStruct)
{
    for (auto cur = firstStruct; cur->symPointer; ++cur) {
        *cur->symPointer = nullptr;
    }
}

} /* namespace gl */
} /* namespace mozilla */