• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) || \
28     (DE_OS == DE_OS_IOS) || (DE_OS == DE_OS_QNX) || (DE_OS == DE_OS_FUCHSIA)
29 /* Posix implementation. */
30 
31 #include <dlfcn.h>
32 #include <libgen.h>
33 #include <stdlib.h>
34 
35 struct deDynamicLibrary_s
36 {
37     void *libHandle;
38 };
39 
deDynamicLibrary_open(const char * fileName)40 deDynamicLibrary *deDynamicLibrary_open(const char *fileName)
41 {
42     deDynamicLibrary *library = (deDynamicLibrary *)deCalloc(sizeof(deDynamicLibrary));
43     if (!library)
44         return DE_NULL;
45 
46     if (getenv("LD_LIBRARY_PATH"))
47         library->libHandle = dlopen(basename((char *)fileName), RTLD_LAZY);
48     else
49         library->libHandle = dlopen(fileName, RTLD_LAZY);
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