]> git.sesse.net Git - vlc/blob - include/modules_core.h
f48333f7fa21d2cbcd697565ab8c785b4c0a8f83
[vlc] / include / modules_core.h
1 /*****************************************************************************
2  * modules_core.h : Module management functions used by the core application.
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Inline functions for handling dynamic modules
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * module_load: load a dynamic library
29  *****************************************************************************
30  * This function loads a dynamically linked library using a system dependant
31  * method, and returns a non-zero value on error, zero otherwise.
32  *****************************************************************************/
33 static __inline__ int
34 module_load( char * psz_filename, module_handle_t * handle )
35 {
36 #ifdef SYS_BEOS
37     *handle = load_add_on( psz_filename );
38     return( *handle < 0 );
39 #else
40     /* Do not open modules with RTLD_GLOBAL, or we are going to get namespace
41      * collisions when two modules have common public symbols */
42     *handle = dlopen( psz_filename, RTLD_NOW );
43
44     return( *handle == NULL );
45 #endif
46 }
47
48 /*****************************************************************************
49  * module_unload: unload a dynamic library
50  *****************************************************************************
51  * This function unloads a previously opened dynamically linked library
52  * using a system dependant method. No return value is taken in consideration,
53  * since some libraries sometimes refuse to close properly.
54  *****************************************************************************/
55 static __inline__ void
56 module_unload( module_handle_t handle )
57 {
58 #ifdef SYS_BEOS
59     unload_add_on( handle );
60 #else
61     dlclose( handle );
62 #endif
63     return;
64 }
65
66 /*****************************************************************************
67  * module_getsymbol: get a symbol from a dynamic library
68  *****************************************************************************
69  * This function queries a loaded library for a symbol specified in a
70  * string, and returns a pointer to it.
71  * FIXME: under Unix we should maybe check for dlerror() instead of the
72  * return value of dlsym, since we could have loaded a symbol really set
73  * to NULL (quite unlikely, though).
74  *****************************************************************************/
75 static __inline__ void *
76 module_getsymbol( module_handle_t handle, char * psz_function )
77 {
78 #ifdef SYS_BEOS
79     void * p_symbol;
80     if( B_OK == get_image_symbol( handle, psz_function,
81                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
82     {
83         return( p_symbol );
84     }
85     else
86     {
87         return( NULL );
88     }
89
90 #elif defined(SYS_DARWIN1_3)
91     /* MacOS X dl library expects symbols to begin with "_". That's
92      * really lame, but hey, what can we do ? */
93     char *  psz_call = malloc( strlen( psz_function ) + 2 );
94     void *  p_return;
95     strcpy( psz_call + 1, psz_function );
96     psz_call[ 0 ] = '_';
97
98     p_return = dlsym( handle, psz_call );
99
100     free( psz_call );
101     return( p_return );
102
103 #else
104     return( dlsym( handle, psz_function ) );
105 #endif
106 }
107
108 /*****************************************************************************
109  * module_error: wrapper for dlerror()
110  *****************************************************************************
111  * This function returns the error message of the last module operation. It
112  * returns the string "failed" on systems which do not have the dlerror()
113  * function.
114  *****************************************************************************/
115 static __inline__ const char *
116 module_error( void )
117 {
118 #ifdef SYS_BEOS
119     return( "failed" );
120 #else
121     return( dlerror() );
122 #endif
123 }
124