diff options
Diffstat (limited to 'libraries/eina/src/include/eina_module.h')
-rw-r--r-- | libraries/eina/src/include/eina_module.h | 343 |
1 files changed, 343 insertions, 0 deletions
diff --git a/libraries/eina/src/include/eina_module.h b/libraries/eina/src/include/eina_module.h new file mode 100644 index 0000000..58e38f9 --- /dev/null +++ b/libraries/eina/src/include/eina_module.h | |||
@@ -0,0 +1,343 @@ | |||
1 | /* EINA - EFL data type library | ||
2 | * Copyright (C) 2007-2008 Jorge Luis Zapata Muga | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) any later version. | ||
8 | * | ||
9 | * This library is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Lesser General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU Lesser General Public | ||
15 | * License along with this library; | ||
16 | * if not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | #ifndef EINA_MODULE_H_ | ||
20 | #define EINA_MODULE_H_ | ||
21 | |||
22 | #include "eina_types.h" | ||
23 | #include "eina_array.h" | ||
24 | #include "eina_error.h" | ||
25 | |||
26 | /** | ||
27 | * @addtogroup Eina_Module_Group Module | ||
28 | * | ||
29 | * @brief These functions provide module management. | ||
30 | */ | ||
31 | |||
32 | /** | ||
33 | * @addtogroup Eina_Tools_Group Tools | ||
34 | * | ||
35 | * @{ | ||
36 | */ | ||
37 | |||
38 | /** | ||
39 | * @defgroup Eina_Module_Group Module | ||
40 | * | ||
41 | * Eina module provides some helpers over POSIX dlopen(). It is not | ||
42 | * meant to replace, abstract or make a "portable" version of the | ||
43 | * POSIX, but enhance its usage by defining some good practices. | ||
44 | * | ||
45 | * Modules are created with eina_module_new() and later loaded with | ||
46 | * eina_module_load(). Loads are reference counted and there must be | ||
47 | * the same number of eina_module_unload() in order to have it to call | ||
48 | * dlclose(). This makes simple to have different users for the same | ||
49 | * module. | ||
50 | * | ||
51 | * The loaded shared objects may have two visible functions that will | ||
52 | * be called and might provide initialization and shutdown | ||
53 | * proceedures. The symbols are @c __eina_module_init and | ||
54 | * @c __eina_module_shutdown and will be defined by the macros | ||
55 | * EINA_MODULE_INIT() and EINA_MODULE_SHUTDOWN(). | ||
56 | * | ||
57 | * There are some helpers to automatically create modules based on | ||
58 | * directory listing. See eina_module_arch_list_get(), | ||
59 | * eina_module_list_get() and eina_module_find(). | ||
60 | * | ||
61 | * @{ | ||
62 | */ | ||
63 | |||
64 | /** | ||
65 | * @typedef Eina_Module | ||
66 | * Dynamic module loader handle. | ||
67 | */ | ||
68 | typedef struct _Eina_Module Eina_Module; | ||
69 | |||
70 | typedef Eina_Bool (*Eina_Module_Cb)(Eina_Module *m, void *data); | ||
71 | |||
72 | /** | ||
73 | * @typedef Eina_Module_Init | ||
74 | * If a function with such signature is exported by module as | ||
75 | * __eina_module_init, it will be called on the first load after | ||
76 | * dlopen() and if #EINA_FALSE is returned, load will fail, #EINA_TRUE | ||
77 | * means the module was successfully initialized. | ||
78 | * @see Eina_Module_Shutdown | ||
79 | */ | ||
80 | typedef Eina_Bool (*Eina_Module_Init)(void); | ||
81 | |||
82 | /** | ||
83 | * @typedef Eina_Module_Shutdown | ||
84 | * If a function with such signature is exported by module as | ||
85 | * __eina_module_shutdown, it will be called before calling dlclose() | ||
86 | * @see Eina_Module_Init | ||
87 | */ | ||
88 | typedef void (*Eina_Module_Shutdown)(void); | ||
89 | |||
90 | /** | ||
91 | * @def EINA_MODULE_INIT | ||
92 | * declares the given function as the module initializer (__eina_module_init). | ||
93 | * It must be of signature #Eina_Module_Init | ||
94 | */ | ||
95 | #define EINA_MODULE_INIT(f) EAPI Eina_Module_Init __eina_module_init = &f | ||
96 | |||
97 | /** | ||
98 | * @def EINA_MODULE_SHUTDOWN | ||
99 | * declares the given function as the module shutdownializer | ||
100 | * (__eina_module_shutdown). It must be of signature | ||
101 | * #Eina_Module_Shutdown | ||
102 | */ | ||
103 | #define EINA_MODULE_SHUTDOWN(f) EAPI Eina_Module_Shutdown __eina_module_shutdown = &f | ||
104 | |||
105 | /** | ||
106 | * @var EINA_ERROR_WRONG_MODULE | ||
107 | * Error identifier corresponding to a wrong module. | ||
108 | */ | ||
109 | extern EAPI Eina_Error EINA_ERROR_WRONG_MODULE; | ||
110 | |||
111 | /** | ||
112 | * @var EINA_ERROR_MODULE_INIT_FAILED | ||
113 | * Error identifier corresponding to a failure during the initialisation of a module. | ||
114 | */ | ||
115 | extern EAPI Eina_Error EINA_ERROR_MODULE_INIT_FAILED; | ||
116 | |||
117 | /** | ||
118 | * @brief Return a new module. | ||
119 | * | ||
120 | * @param file The name of the file module to load. | ||
121 | * | ||
122 | * This function returns a new module. If @p file is @c NULL, the | ||
123 | * function returns @c NULL, otherwise, it allocates an Eina_Module, | ||
124 | * stores a duplicate string of @p file, sets its reference to @c 0 | ||
125 | * and its handle to @c NULL. | ||
126 | * | ||
127 | * When the new module is not needed anymore, use eina_module_free() | ||
128 | * to free the allocated memory. | ||
129 | * | ||
130 | * @see eina_module_load | ||
131 | */ | ||
132 | EAPI Eina_Module * | ||
133 | eina_module_new(const char *file) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1); | ||
134 | |||
135 | /** | ||
136 | * @brief Delete a module. | ||
137 | * | ||
138 | * @param m The module to delete. | ||
139 | * @return EINA_TRUE on success, EINA_FALSE otherwise. | ||
140 | * | ||
141 | * This function calls eina_module_unload() if @p m has been previously | ||
142 | * loaded and frees the allocated memory. On success this function | ||
143 | * returns EINA_TRUE and EINA_FALSE otherwise. If @p m is @c NULL, the | ||
144 | * function returns immediately. | ||
145 | */ | ||
146 | EAPI Eina_Bool | ||
147 | eina_module_free(Eina_Module *m) EINA_ARG_NONNULL(1); | ||
148 | |||
149 | /** | ||
150 | * @brief Load a module. | ||
151 | * | ||
152 | * @param m The module to load. | ||
153 | * @return EINA_TRUE on success, EINA_FALSE otherwise. | ||
154 | * | ||
155 | * This function load the shared file object passed in | ||
156 | * eina_module_new(). If it is a internal Eina module (like the | ||
157 | * mempools), it also initialize it. It the shared file object can not | ||
158 | * be loaded, the error #EINA_ERROR_WRONG_MODULE is set and | ||
159 | * #EINA_FALSE is returned. If it is a internal Eina module and the | ||
160 | * module can not be initialized, the error | ||
161 | * #EINA_ERROR_MODULE_INIT_FAILED is set and #EINA_FALSE is | ||
162 | * returned. If the module has already been loaded, it's refeence | ||
163 | * counter is increased by one and #EINA_TRUE is returned. If @p m is | ||
164 | * @c NULL, the function returns immediately #EINA_FALSE. | ||
165 | * | ||
166 | * When the symbols of the shared file objetcts are not needed | ||
167 | * anymore, call eina_module_unload() to unload the module. | ||
168 | */ | ||
169 | EAPI Eina_Bool | ||
170 | eina_module_load(Eina_Module *module) EINA_ARG_NONNULL(1); | ||
171 | |||
172 | /** | ||
173 | * @brief Unload a module. | ||
174 | * | ||
175 | * @param m The module to load. | ||
176 | * @return EINA_TRUE on success, EINA_FALSE otherwise. | ||
177 | * | ||
178 | * This function unload the module @p m that has been previously | ||
179 | * loaded by eina_module_load(). If the reference counter of @p m is | ||
180 | * strictly greater than @c 1, #EINA_FALSE is returned. Otherwise, the | ||
181 | * shared object file is closed and if it is a internal Eina module, it | ||
182 | * is shutted down just before. In that case, #EINA_TRUE is | ||
183 | * returned. In all case, the reference counter is decreased. If @p m | ||
184 | * is @c NULL, the function returns immediately #EINA_FALSE. | ||
185 | */ | ||
186 | EAPI Eina_Bool | ||
187 | eina_module_unload(Eina_Module *m) EINA_ARG_NONNULL(1); | ||
188 | |||
189 | /** | ||
190 | * @brief Retrive the data associated to a symbol. | ||
191 | * | ||
192 | * @param m The module. | ||
193 | * @param symbol The symbol. | ||
194 | * @return The data associated to the symbol, or @c NULL on failure. | ||
195 | * | ||
196 | * This function returns the data associated to @p symbol of @p m. @p | ||
197 | * m must have been loaded before with eina_module_load(). If @p m | ||
198 | * is @c NULL, or if it has not been correctly loaded before, the | ||
199 | * function returns immediately @c NULL. | ||
200 | */ | ||
201 | EAPI void * | ||
202 | eina_module_symbol_get(const Eina_Module *module, const char *symbol) EINA_PURE EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT; | ||
203 | |||
204 | /** | ||
205 | * @brief Return the file name associated to the module. | ||
206 | * | ||
207 | * @param m The module. | ||
208 | * @return The file name. | ||
209 | * | ||
210 | * This function returns the file name passed in eina_module_new(). If | ||
211 | * @p m is @c NULL, the function returns immediately @c NULL. The | ||
212 | * returned value must no be freed. | ||
213 | */ | ||
214 | EAPI const char * | ||
215 | eina_module_file_get(const Eina_Module *m) EINA_PURE EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1); | ||
216 | |||
217 | |||
218 | /** | ||
219 | * @brief Return the path built from the location of a library and a | ||
220 | * given sub directory. | ||
221 | * | ||
222 | * @param symbol The symbol to search for. | ||
223 | * @param sub_dir The subdirectory to append. | ||
224 | * @return The built path on success, @c NULL otherwise. | ||
225 | * | ||
226 | * This function returns the path built by concatenating the path of | ||
227 | * the library containing the symbol @p symbol and @p sub_dir. @p sub_dir | ||
228 | * can be @c NULL. The returned path must be freed when not used | ||
229 | * anymore. If the symbol is not found, or dl_addr() is not supported, | ||
230 | * or allocation fails, this function returns @c NULL. | ||
231 | */ | ||
232 | EAPI char * | ||
233 | eina_module_symbol_path_get(const void *symbol, const char *sub_dir) EINA_PURE EINA_MALLOC EINA_ARG_NONNULL(1, 2); | ||
234 | |||
235 | /** | ||
236 | * @brief Return the path built from the value of an environment varialbe and a | ||
237 | * given sub directory. | ||
238 | * | ||
239 | * @param env The environment variable to expand. | ||
240 | * @param sub_dir The subdirectory to append. | ||
241 | * @return The built path on success, @c NULL otherwise. | ||
242 | * | ||
243 | * This function returns the path built by concatenating the value of | ||
244 | * the environment variable named @p env and @p sub_dir. @p sub_dir | ||
245 | * can be @c NULL. The returned path must be freed when not used | ||
246 | * anymore. If the symbol is not found, or @p env does not exist, or | ||
247 | * allocation fails, this function returns @c NULL. | ||
248 | */ | ||
249 | EAPI char * | ||
250 | eina_module_environment_path_get(const char *env, const char *sub_dir) EINA_PURE EINA_MALLOC EINA_ARG_NONNULL(1, 2); | ||
251 | |||
252 | |||
253 | /** | ||
254 | * @brief Get an array of modules found on the directory path matching an arch type. | ||
255 | * | ||
256 | * @param array The array that stores the list of the modules. | ||
257 | * @param path The directory's path to search for modules. | ||
258 | * @param arch The architecture string. | ||
259 | * | ||
260 | * This function adds to @p array the module names found in @p path | ||
261 | * which match the cpu architecture @p arch. If @p path or @p arch is | ||
262 | * @c NULL, the function returns immediately @p array. @p array can be | ||
263 | * @c NULL. In that case, it is created with 4 elements. | ||
264 | */ | ||
265 | EAPI Eina_Array * | ||
266 | eina_module_arch_list_get(Eina_Array *array, const char *path, const char *arch); | ||
267 | |||
268 | /** | ||
269 | * @brief Get a list of modules found on the directory path. | ||
270 | * | ||
271 | * @param array The array that stores the list of the modules. | ||
272 | * @param path The directory's path to search for modules. | ||
273 | * @param recursive Iterate recursively on the path. | ||
274 | * @param cb Callback function to call on each module. | ||
275 | * @param data Data passed to the callback function. | ||
276 | * | ||
277 | * This function adds to @p array the list of modules found in | ||
278 | * @p path. If @p recursive is #EINA_TRUE, then recursive search is | ||
279 | * done. The callback @p cb is called on each module found, and @p data | ||
280 | * is passed to @p cb. If @p path is @c NULL, the function returns | ||
281 | * immediately @p array. If the returned value of @p cb is 0, the | ||
282 | * module will not be added to the list, otherwise it will be added. | ||
283 | * @p array can be @c NULL. In that case, it is created with 4 | ||
284 | * elements. @p cb can be @c NULL. | ||
285 | */ | ||
286 | EAPI Eina_Array * | ||
287 | eina_module_list_get(Eina_Array *array, const char *path, Eina_Bool recursive, Eina_Module_Cb cb, void *data) EINA_MALLOC EINA_WARN_UNUSED_RESULT; | ||
288 | |||
289 | /** | ||
290 | * @brief Load every module on the list of modules. | ||
291 | * | ||
292 | * @param array The array of modules to load. | ||
293 | * | ||
294 | * This function calls eina_module_load() on each element found in | ||
295 | * @p array. If @p array is @c NULL, this function does nothing. | ||
296 | */ | ||
297 | EAPI void | ||
298 | eina_module_list_load(Eina_Array *list) EINA_ARG_NONNULL(1); | ||
299 | |||
300 | /** | ||
301 | * @brief Unload every module on the list of modules. | ||
302 | * | ||
303 | * @param array The array of modules to unload. | ||
304 | * | ||
305 | * This function calls eina_module_unload() on each element found in | ||
306 | * @p array. If @p array is @c NULL, this function does nothing. | ||
307 | */ | ||
308 | EAPI void | ||
309 | eina_module_list_unload(Eina_Array *list) EINA_ARG_NONNULL(1); | ||
310 | |||
311 | /** | ||
312 | * @p Free every module on the list of modules. | ||
313 | * | ||
314 | * @param array The array of modules to free. | ||
315 | * | ||
316 | * This function calls eina_module_free() on each element found in | ||
317 | * @p array. If @p array is @c NULL, this function does nothing. | ||
318 | */ | ||
319 | EAPI void | ||
320 | eina_module_list_free(Eina_Array *list) EINA_ARG_NONNULL(1); | ||
321 | |||
322 | /** | ||
323 | * @brief Find an module in array. | ||
324 | * | ||
325 | * @param array The array to find the module. | ||
326 | * @param module The name of module to be searched. | ||
327 | * | ||
328 | * This function finds an @p module in @p array. | ||
329 | * If the element is found the function returns the module, else | ||
330 | * @c NULL is returned. | ||
331 | */ | ||
332 | EAPI Eina_Module * | ||
333 | eina_module_find(const Eina_Array *array, const char *module) EINA_ARG_NONNULL(1, 2); | ||
334 | |||
335 | /** | ||
336 | * @} | ||
337 | */ | ||
338 | |||
339 | /** | ||
340 | * @} | ||
341 | */ | ||
342 | |||
343 | #endif /*EINA_MODULE_H_*/ | ||