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