Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/gl/GLLibraryLoader.cpp
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "GLLibraryLoader.h"
6
7
#include "nsDebug.h"
8
9
#ifdef WIN32
10
#include <windows.h>
11
#endif
12
13
namespace mozilla {
14
namespace gl {
15
16
bool
17
GLLibraryLoader::OpenLibrary(const char* library)
18
0
{
19
0
    PRLibSpec lspec;
20
0
    lspec.type = PR_LibSpec_Pathname;
21
0
    lspec.value.pathname = library;
22
0
23
0
    mLibrary = PR_LoadLibraryWithFlags(lspec, PR_LD_LAZY | PR_LD_LOCAL);
24
0
    if (!mLibrary)
25
0
        return false;
26
0
27
0
    return true;
28
0
}
29
30
bool
31
GLLibraryLoader::LoadSymbols(const SymLoadStruct* firstStruct,
32
                             bool tryplatform,
33
                             const char* prefix,
34
                             bool warnOnFailure)
35
0
{
36
0
    return LoadSymbols(mLibrary,
37
0
                       firstStruct,
38
0
                       tryplatform ? mLookupFunc : nullptr,
39
0
                       prefix,
40
0
                       warnOnFailure);
41
0
}
42
43
PRFuncPtr
44
GLLibraryLoader::LookupSymbol(const char* sym)
45
0
{
46
0
    return LookupSymbol(mLibrary, sym, mLookupFunc);
47
0
}
48
49
PRFuncPtr
50
GLLibraryLoader::LookupSymbol(PRLibrary* lib,
51
                              const char* sym,
52
                              PlatformLookupFunction lookupFunction)
53
0
{
54
0
    PRFuncPtr res = 0;
55
0
56
0
    // try finding it in the library directly, if we have one
57
0
    if (lib) {
58
0
        res = PR_FindFunctionSymbol(lib, sym);
59
0
    }
60
0
61
0
    // then try looking it up via the lookup symbol
62
0
    if (!res && lookupFunction) {
63
0
        res = lookupFunction(sym);
64
0
    }
65
0
66
0
    // finally just try finding it in the process
67
0
    if (!res) {
68
0
        PRLibrary* leakedLibRef;
69
0
        res = PR_FindFunctionSymbolAndLibrary(sym, &leakedLibRef);
70
0
    }
71
0
72
0
    return res;
73
0
}
74
75
bool
76
GLLibraryLoader::LoadSymbols(PRLibrary* lib,
77
                             const SymLoadStruct* firstStruct,
78
                             PlatformLookupFunction lookupFunction,
79
                             const char* prefix,
80
                             bool warnOnFailure)
81
0
{
82
0
    char sbuf[MAX_SYMBOL_LENGTH * 2];
83
0
    int failCount = 0;
84
0
85
0
    const SymLoadStruct* ss = firstStruct;
86
0
    while (ss->symPointer) {
87
0
        *ss->symPointer = 0;
88
0
89
0
        for (int i = 0; i < MAX_SYMBOL_NAMES; i++) {
90
0
            if (ss->symNames[i] == nullptr)
91
0
                break;
92
0
93
0
            const char* s = ss->symNames[i];
94
0
            if (prefix && *prefix != 0) {
95
0
                strcpy(sbuf, prefix);
96
0
                strcat(sbuf, ss->symNames[i]);
97
0
                s = sbuf;
98
0
            }
99
0
100
0
            PRFuncPtr p = LookupSymbol(lib, s, lookupFunction);
101
0
            if (p) {
102
0
                *ss->symPointer = p;
103
0
                break;
104
0
            }
105
0
        }
106
0
107
0
        if (*ss->symPointer == 0) {
108
0
            if (warnOnFailure) {
109
0
                printf_stderr("Can't find symbol '%s'.\n", ss->symNames[0]);
110
0
            }
111
0
112
0
            failCount++;
113
0
        }
114
0
115
0
        ss++;
116
0
    }
117
0
118
0
    return failCount == 0 ? true : false;
119
0
}
120
121
/*static*/ void
122
GLLibraryLoader::ClearSymbols(const SymLoadStruct* const firstStruct)
123
0
{
124
0
    for (auto cur = firstStruct; cur->symPointer; ++cur) {
125
0
        *cur->symPointer = nullptr;
126
0
    }
127
0
}
128
129
} /* namespace gl */
130
} /* namespace mozilla */
131