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_NOW);
47 else {
48 library->libHandle = dlopen(fileName, RTLD_NOW);
49 }
50
51 if (!library->libHandle)
52 {
53 deFree(library);
54 return DE_NULL;
55 }
56
57 return library;
58 }
59
deDynamicLibrary_close(deDynamicLibrary * library)60 void deDynamicLibrary_close (deDynamicLibrary* library)
61 {
62 if (library && library->libHandle)
63 dlclose(library->libHandle);
64 deFree(library);
65 }
66
deDynamicLibrary_getFunction(const deDynamicLibrary * library,const char * symbolName)67 deFunctionPtr deDynamicLibrary_getFunction (const deDynamicLibrary* library, const char* symbolName)
68 {
69 /* C forbids direct cast from object pointer to function pointer */
70 union
71 {
72 deFunctionPtr funcPtr;
73 void* objPtr;
74 } ptr;
75
76 DE_ASSERT(library && library->libHandle && symbolName);
77 ptr.objPtr = dlsym(library->libHandle, symbolName);
78 return ptr.funcPtr;
79 }
80
81 #elif (DE_OS == DE_OS_WIN32)
82 /* Win32 implementation. */
83
84 #define WIN32_LEAN_AND_MEAN
85 #define VC_EXTRALEAN
86 #include <windows.h>
87
88 struct deDynamicLibrary_s
89 {
90 HINSTANCE handle;
91 };
92
deDynamicLibrary_open(const char * fileName)93 deDynamicLibrary* deDynamicLibrary_open (const char* fileName)
94 {
95 deDynamicLibrary* library = (deDynamicLibrary*)deCalloc(sizeof(deDynamicLibrary));
96 if (!library)
97 return DE_NULL;
98
99 library->handle = LoadLibrary(fileName);
100 if (!library->handle)
101 {
102 deFree(library);
103 return DE_NULL;
104 }
105
106 return library;
107 }
108
deDynamicLibrary_close(deDynamicLibrary * library)109 void deDynamicLibrary_close (deDynamicLibrary* library)
110 {
111 if (library && library->handle)
112 FreeLibrary(library->handle);
113 deFree(library);
114 }
115
deDynamicLibrary_getFunction(const deDynamicLibrary * library,const char * symbolName)116 deFunctionPtr deDynamicLibrary_getFunction (const deDynamicLibrary* library, const char* symbolName)
117 {
118 DE_ASSERT(library && library->handle && symbolName);
119 return (deFunctionPtr)GetProcAddress(library->handle, symbolName);
120 }
121
122 #else
123 # error deDynamicLibrary is not implemented on this OS
124 #endif
125