File: utils_dynamic_libs.h

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (142 lines) | stat: -rw-r--r-- 3,630 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
    Copyright (c) 2005-2024 Intel Corporation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#ifndef __TBB_test_common_utils_dynamic_libs_H_
#define __TBB_test_common_utils_dynamic_libs_H_

#include "common/test.h"
#include "oneapi/tbb/version.h"

#if __TBB_DYNAMIC_LOAD_ENABLED

#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif

namespace utils {

#if TBB_USE_DEBUG
#define SUFFIX1 "_debug"
#define SUFFIX2
#else
#define SUFFIX1
#define SUFFIX2 "_debug"
#endif /* TBB_USE_DEBUG */

#if (_WIN32||_WIN64)
#if __MINGW32__
    #define PREFIX "lib"
#else
    #define PREFIX
#endif
#define EXT ".dll"
#else
#if TBB_USE_APPLE_FRAMEWORKS
#define PREFIX // When built as Apple* Framework, the binary has no lib prefix
#else
#define PREFIX "lib"
#endif
#if __APPLE__
#if TBB_USE_APPLE_FRAMEWORKS
#define EXT // When built as Apple* Framework, the binary has no extension
#else
#define EXT ".dylib"
#endif
// Android SDK build system does not support .so file name versioning
#elif __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __sun || _AIX || __ANDROID__
#define EXT ".so"
#elif __unix__  // Order of these elif's matters!
#define EXT __TBB_STRING(.so.2)
#else
#error Unknown OS
#endif
#endif
#if TBB_USE_APPLE_FRAMEWORKS
#define MALLOCFRAMEWORK "tbbmalloc.framework/"
#else
#define MALLOCFRAMEWORK
#endif

// Form the names of the TBB memory allocator binaries.
#define MALLOCLIB_NAME1 MALLOCFRAMEWORK PREFIX "tbbmalloc" SUFFIX1 EXT
#define MALLOCLIB_NAME2 MALLOCFRAMEWORK PREFIX "tbbmalloc" SUFFIX2 EXT

#if _WIN32 || _WIN64
using LIBRARY_HANDLE = HMODULE;
#else
using LIBRARY_HANDLE = void*;
#endif

#if _WIN32 || _WIN64
#define TEST_LIBRARY_NAME(base) PREFIX base SUFFIX1 ".dll"
#elif __APPLE__
#define TEST_LIBRARY_NAME(base) PREFIX base SUFFIX1 ".dylib"
#else
#define TEST_LIBRARY_NAME(base) PREFIX base SUFFIX1 ".so"
#endif

LIBRARY_HANDLE OpenLibrary(const char *name)
{
#if _WIN32 || _WIN64
#if __TBB_WIN8UI_SUPPORT
    TCHAR wlibrary[MAX_PATH];
    if (MultiByteToWideChar(CP_UTF8, 0, name, -1, wlibrary, MAX_PATH) == 0) return false;
    return ::LoadPackagedLibrary(wlibrary, 0);
#else
    return ::LoadLibrary(name);
#endif
#else
    return dlopen(name, RTLD_NOW|RTLD_GLOBAL);
#endif
}

void CloseLibrary(LIBRARY_HANDLE lib)
{
#if _WIN32 || _WIN64
    BOOL ret = FreeLibrary(lib);
    REQUIRE_MESSAGE(ret, "FreeLibrary must be successful");
#else
    int ret = dlclose(lib);
    REQUIRE_MESSAGE(ret == 0, "dlclose must be successful");
#endif
}

typedef void (*FunctionAddress)();

template <typename FunctionPointer>
void GetAddress(utils::LIBRARY_HANDLE lib, const char *name, FunctionPointer& func)
{
#if _WIN32 || _WIN64
    func = (FunctionPointer)(void*)GetProcAddress(lib, name);
#else
    func = (FunctionPointer)dlsym(lib, name);
#endif
    REQUIRE_MESSAGE(func, "Can't find required symbol in dynamic library");
}

FunctionAddress GetAddress(utils::LIBRARY_HANDLE lib, const char *name)
{
    FunctionAddress func;
    GetAddress(lib, name, func);
    return func;
}

}  // namespace utils

#endif // __TBB_DYNAMIC_LOAD_ENABLED
#endif // __TBB_test_common_utils_dynamic_libs_H_