1/*
2 Copyright (c) 2005-2019 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#include "tbb/tbb_config.h"
18
19// Include this header file before harness.h for HARNESS_SKIP_TEST to take effect
20#if !__TBB_DYNAMIC_LOAD_ENABLED
21#define HARNESS_SKIP_TEST 1
22#else
23
24#if _WIN32 || _WIN64
25#include "tbb/machine/windows_api.h"
26#else
27#include <dlfcn.h>
28#endif
29#include "harness_assert.h"
30
31namespace Harness {
32
33#if TBB_USE_DEBUG
34#define SUFFIX1 "_debug"
35#define SUFFIX2
36#else
37#define SUFFIX1
38#define SUFFIX2 "_debug"
39#endif /* TBB_USE_DEBUG */
40
41#if _WIN32||_WIN64
42#define PREFIX
43#define EXT ".dll"
44#else
45#define PREFIX "lib"
46#if __APPLE__
47#define EXT ".dylib"
48// Android SDK build system does not support .so file name versioning
49#elif __FreeBSD__ || __NetBSD__ || __sun || _AIX || __ANDROID__
50#define EXT ".so"
51#elif __linux__ // Order of these elif's matters!
52#define EXT __TBB_STRING(.so.TBB_COMPATIBLE_INTERFACE_VERSION)
53#else
54#error Unknown OS
55#endif
56#endif
57
58// Form the names of the TBB memory allocator binaries.
59#define MALLOCLIB_NAME1 PREFIX "tbbmalloc" SUFFIX1 EXT
60#define MALLOCLIB_NAME2 PREFIX "tbbmalloc" SUFFIX2 EXT
61
62#if _WIN32 || _WIN64
63typedef HMODULE LIBRARY_HANDLE;
64#else
65typedef void *LIBRARY_HANDLE;
66#endif
67
68#if _WIN32 || _WIN64
69#define TEST_LIBRARY_NAME(base) base".dll"
70#elif __APPLE__
71#define TEST_LIBRARY_NAME(base) base".dylib"
72#else
73#define TEST_LIBRARY_NAME(base) base".so"
74#endif
75
76LIBRARY_HANDLE OpenLibrary(const char *name)
77{
78#if _WIN32 || _WIN64
79#if __TBB_WIN8UI_SUPPORT
80 TCHAR wlibrary[MAX_PATH];
81 if ( MultiByteToWideChar(CP_UTF8, 0, name, -1, wlibrary, MAX_PATH) == 0 ) return false;
82 return :: LoadPackagedLibrary( wlibrary, 0 );
83#else
84 return ::LoadLibrary(name);
85#endif
86#else
87 return dlopen(name, RTLD_NOW|RTLD_GLOBAL);
88#endif
89}
90
91void CloseLibrary(LIBRARY_HANDLE lib)
92{
93#if _WIN32 || _WIN64
94 BOOL ret = FreeLibrary(lib);
95 ASSERT(ret, "FreeLibrary must be successful");
96#else
97 int ret = dlclose(lib);
98 ASSERT(ret == 0, "dlclose must be successful");
99#endif
100}
101
102typedef void (*FunctionAddress)();
103
104template <typename FunctionPointer>
105void GetAddress(Harness::LIBRARY_HANDLE lib, const char *name, FunctionPointer& func)
106{
107#if _WIN32 || _WIN64
108 func = (FunctionPointer)(void*)GetProcAddress(lib, name);
109#else
110 func = (FunctionPointer)dlsym(lib, name);
111#endif
112 ASSERT(func, "Can't find required symbol in dynamic library");
113}
114
115FunctionAddress GetAddress(Harness::LIBRARY_HANDLE lib, const char *name)
116{
117 FunctionAddress func;
118 GetAddress(lib, name, func);
119 return func;
120}
121
122} // namespace Harness
123
124#endif // __TBB_DYNAMIC_LOAD_ENABLED
125