1 /*-------------------------------------------------------------------------
2 * drawElements Utility Library
3 * ----------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Dynamic link library abstraction.
22 *//*--------------------------------------------------------------------*/
23
24 #include "deDynamicLibrary.h"
25 #include "deMemory.h"
26
27 #if (DE_OS == DE_OS_UNIX) || (DE_OS == DE_OS_ANDROID) || (DE_OS == DE_OS_OSX) || (DE_OS == DE_OS_SYMBIAN) || (DE_OS == DE_OS_IOS) || (DE_OS == DE_OS_QNX) || (DE_OS == DE_OS_FUCHSIA)
28 /* Posix implementation. */
29
30 #include <dlfcn.h>
31 #include <libgen.h>
32 #include <stdlib.h>
33
34 struct deDynamicLibrary_s
35 {
36 void* libHandle;
37 };
38
deDynamicLibrary_open(const char * fileName)39 deDynamicLibrary* deDynamicLibrary_open (const char* fileName)
40 {
41 deDynamicLibrary* library = (deDynamicLibrary*)deCalloc(sizeof(deDynamicLibrary));
42 if (!library)
43 return DE_NULL;
44
45 if (getenv("LD_LIBRARY_PATH"))
46 library->libHandle = dlopen(basename((char*)fileName), RTLD_LAZY);
47 else
48 library->libHandle = dlopen(fileName, RTLD_LAZY);
49
50 if (!library->libHandle)
51 {
52 deFree(library);
53 return DE_NULL;
54 }
55
56 return library;
57 }
58
deDynamicLibrary_close(deDynamicLibrary * library)59 void deDynamicLibrary_close (deDynamicLibrary* library)
60 {
61 if (library && library->libHandle)
62 dlclose(library->libHandle);
63 deFree(library);
64 }
65
deDynamicLibrary_getFunction(const deDynamicLibrary * library,const char * symbolName)66 deFunctionPtr deDynamicLibrary_getFunction (const deDynamicLibrary* library, const char* symbolName)
67 {
68 /* C forbids direct cast from object pointer to function pointer */
69 union
70 {
71 deFunctionPtr funcPtr;
72 void* objPtr;
73 } ptr;
74
75 DE_ASSERT(library && library->libHandle && symbolName);
76 ptr.objPtr = dlsym(library->libHandle, symbolName);
77 return ptr.funcPtr;
78 }
79
80 #elif (DE_OS == DE_OS_WIN32)
81 /* Win32 implementation. */
82
83 #define WIN32_LEAN_AND_MEAN
84 #define VC_EXTRALEAN
85 #include <windows.h>
86
87 struct deDynamicLibrary_s
88 {
89 HINSTANCE handle;
90 };
91
deDynamicLibrary_open(const char * fileName)92 deDynamicLibrary* deDynamicLibrary_open (const char* fileName)
93 {
94 deDynamicLibrary* library = (deDynamicLibrary*)deCalloc(sizeof(deDynamicLibrary));
95 if (!library)
96 return DE_NULL;
97
98 library->handle = LoadLibrary(fileName);
99 if (!library->handle)
100 {
101 deFree(library);
102 return DE_NULL;
103 }
104
105 return library;
106 }
107
deDynamicLibrary_close(deDynamicLibrary * library)108 void deDynamicLibrary_close (deDynamicLibrary* library)
109 {
110 if (library && library->handle)
111 FreeLibrary(library->handle);
112 deFree(library);
113 }
114
deDynamicLibrary_getFunction(const deDynamicLibrary * library,const char * symbolName)115 deFunctionPtr deDynamicLibrary_getFunction (const deDynamicLibrary* library, const char* symbolName)
116 {
117 DE_ASSERT(library && library->handle && symbolName);
118 return (deFunctionPtr)GetProcAddress(library->handle, symbolName);
119 }
120
121 #else
122 # error deDynamicLibrary is not implemented on this OS
123 #endif
124