]> git.sesse.net Git - vlc/commitdiff
. Added files needed for the forthcoming module management.
authorSam Hocevar <sam@videolan.org>
Fri, 5 Jan 2001 13:57:12 +0000 (13:57 +0000)
committerSam Hocevar <sam@videolan.org>
Fri, 5 Jan 2001 13:57:12 +0000 (13:57 +0000)
 Notes:

   Plugins are now called modules, because we will be able to compile
   them either as a dynamic plugin, or within the program. And, more
   important, I can commit this without having to break the vlc :)

   I tried to be as clear as possible in my comments, please tell me
   if a few prototypes still have an unclear beahaviour.

   Current features:
    . none, files haven't even been added to the Makefile yet.

   Future features:
    . dynamic loading (done)
    . built-in modules (still to do)
    . automatic unloading of plugins (done)
    . dynamic configuration (still to do)
    . automatic choosing of the most appropriate plugin for a given
       task (still to do but well prepared)

include/modules.h [new file with mode: 0644]
include/modules_config.h [new file with mode: 0644]
include/modules_inner.h [new file with mode: 0644]
src/misc/modules.c [new file with mode: 0644]

diff --git a/include/modules.h b/include/modules.h
new file mode 100644 (file)
index 0000000..f7d12a8
--- /dev/null
@@ -0,0 +1,145 @@
+/*****************************************************************************
+ * modules.h : Module management functions.
+ *****************************************************************************
+ * Copyright (C) 2001 VideoLAN
+ *
+ * Authors: Samuel Hocevar <sam@zoy.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ *****************************************************************************/
+
+/* Number of tries before we unload an unused module */
+#define MODULE_HIDE_DELAY 10
+
+/* The module handle type. */
+#ifdef SYS_BEOS
+typedef int     module_handle_t;
+#else
+typedef void *  module_handle_t;
+#endif
+
+/*****************************************************************************
+ * Configuration structure
+ *****************************************************************************/
+typedef struct module_config_s
+{
+    int         i_type;                         /* Configuration widget type */
+    char *      psz_text;        /* Text commenting or describing the widget */
+    char *      psz_name;                                   /* Variable name */
+    void *      p_getlist;          /* Function to call to get a choice list */
+    void *      p_change;        /* Function to call when commiting a change */
+} module_config_t;
+
+/*****************************************************************************
+ * Bank and module description structures
+ *****************************************************************************/
+
+/* The main module structure */
+typedef struct module_s
+{
+    boolean_t           b_builtin;  /* Set to true if the module is built in */
+
+    module_handle_t     handle;      /* Unique handle to refer to the module */
+    char *              psz_filename;                     /* Module filename */
+
+    char *              psz_name;                    /* Module _unique_ name */
+    char *              psz_longname;             /* Module descriptive name */
+    char *              psz_version;                       /* Module version */
+
+    int                 i_usage;                        /* Reference counter */
+    int                 i_unused_delay;    /* Delay until module is unloaded */
+
+    struct module_s *   next;                                 /* Next module */
+    struct module_s *   prev;                             /* Previous module */
+
+    u32                 i_capabilities;                   /* Capability list */
+
+    module_config_t *   p_config;    /* Module configuration structure table */
+
+} module_t;
+
+/* The module bank structure */
+typedef struct module_bank_s
+{
+    module_t *          first; /* First module of the bank */
+
+    vlc_mutex_t         lock;  /* Global lock -- you can't imagine how awful it
+                                  is to design thread-safe linked lists. */
+} module_bank_t;
+
+/*****************************************************************************
+ * Stuff for handling dynamic modules
+ *****************************************************************************/
+
+/* Function to load a dynamic module, returns 0 if successful. */
+static __inline__ int
+module_load( char * psz_filename, module_handle_t * handle )
+{
+#ifdef SYS_BEOS
+    *handle = load_add_on( psz_filename );
+    return( *handle >= 0 );
+#else
+    *handle = dlopen( psz_filename, RTLD_NOW | RTLD_GLOBAL );
+    return( *handle != NULL );
+#endif
+}
+
+/* Unload a dynamic module. */
+static __inline__ void
+module_unload( module_handle_t handle )
+{
+#ifdef SYS_BEOS
+    unload_add_on( handle );
+#else
+    dlclose( handle );
+#endif
+    return;
+}
+
+/* Get a given symbol from a module. */
+static __inline__ void *
+module_getsymbol( module_handle_t handle, char * psz_function )
+{
+#ifdef SYS_BEOS
+    void * p_symbol;
+    get_image_symbol( handle, psz_function, B_SYMBOL_TYPE_TEXT, &p_symbol );
+    return( p_symbol );
+#else
+    return( dlsym( handle, psz_function ) );
+#endif
+}
+
+/* Wrapper to dlerror() for systems that don't have it. */
+static __inline__ char *
+module_error( void )
+{
+#ifdef SYS_BEOS
+    return( "failed" );
+#else
+    return( dlerror() );
+#endif
+}
+
+/*****************************************************************************
+ * Exported functions.
+ *****************************************************************************/
+module_bank_t * module_InitBank     ( void );
+int             module_DestroyBank  ( module_bank_t * p_bank );
+int             module_ResetBank    ( module_bank_t * p_bank );
+void            module_ManageBank   ( module_bank_t * p_bank );
+
+int module_Need    ( module_t * p_module );
+int module_Unneed  ( module_t * p_module );
+
diff --git a/include/modules_config.h b/include/modules_config.h
new file mode 100644 (file)
index 0000000..7a3a527
--- /dev/null
@@ -0,0 +1,57 @@
+/*****************************************************************************
+ * modules_config.h : Module configuration tools.
+ *****************************************************************************
+ * Copyright (C) 2001 VideoLAN
+ *
+ * Authors: Samuel Hocevar <sam@zoy.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Module capabilities.
+ *****************************************************************************/
+#define MODULE_CAPABILITY_NULL     0       /* The Module can't do anything */
+#define MODULE_CAPABILITY_INTF     1<<0    /* Interface */
+#define MODULE_CAPABILITY_INPUT    1<<1    /* Input */
+#define MODULE_CAPABILITY_DECAPS   1<<2    /* Decaps */
+#define MODULE_CAPABILITY_ADEC     1<<3    /* Audio decoder */
+#define MODULE_CAPABILITY_VDEC     1<<4    /* Video decoder */
+#define MODULE_CAPABILITY_AOUT     1<<5    /* Audio output */
+#define MODULE_CAPABILITY_VOUT     1<<6    /* Video output */
+#define MODULE_CAPABILITY_YUV      1<<7    /* YUV colorspace conversion */
+#define MODULE_CAPABILITY_AFX      1<<8    /* Audio effects */
+#define MODULE_CAPABILITY_VFX      1<<9    /* Video effects */
+
+/*****************************************************************************
+ * Macros used to build the configuration structure.
+ *****************************************************************************/
+
+/* Mandatory first and last parts of the structure */
+#define MODULE_CONFIG_ITEM_START       0x01    /* The main window */
+#define MODULE_CONFIG_ITEM_END         0x00    /* End of the window */
+
+/* Configuration widgets */
+#define MODULE_CONFIG_ITEM_PANE        0x02    /* A notebook pane */
+#define MODULE_CONFIG_ITEM_FRAME       0x03    /* A frame */
+#define MODULE_CONFIG_ITEM_COMMENT     0x04    /* A comment text */
+#define MODULE_CONFIG_ITEM_STRING      0x05    /* A string */
+#define MODULE_CONFIG_ITEM_FILE        0x06    /* A file selector */
+#define MODULE_CONFIG_ITEM_CHECK       0x07    /* A checkbox */
+#define MODULE_CONFIG_ITEM_CHOOSE      0x08    /* A choose box */
+#define MODULE_CONFIG_ITEM_RADIO       0x09    /* A radio box */
+#define MODULE_CONFIG_ITEM_SCALE       0x0a    /* A horizontal ruler */
+#define MODULE_CONFIG_ITEM_SPIN        0x0b    /* A numerical selector */
+
diff --git a/include/modules_inner.h b/include/modules_inner.h
new file mode 100644 (file)
index 0000000..a966569
--- /dev/null
@@ -0,0 +1,98 @@
+/*****************************************************************************
+ * modules_inner.h : Macros used from within a module.
+ *****************************************************************************
+ * Copyright (C) 2001 VideoLAN
+ *
+ * Authors: Samuel Hocevar <sam@zoy.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Check that we are within a module.
+ *****************************************************************************/
+#ifndef MODULE_NAME
+#  error "You must define MODULE_NAME before using modules_inner.h !"
+#endif
+
+/*****************************************************************************
+ * Add a few defines. You do not want to read this section. Really.
+ *****************************************************************************/
+
+/* Explanation:
+ *
+ * if user has #defined MODULE_NAME foo, then we will need:
+ * #define MODULE_STRING "foo"
+ * #define MODULE_VAR(blah) "VLC_MODULE_foo_blah"
+ *
+ * and, if BUILTIN is set, we will also need:
+ * #define InitModule foo_InitModule
+ * #define ActivateModule foo_ActivateModule
+ * #define DeactivateModule foo_DeactivateModule
+ *
+ * this can't easily be done with the C preprocessor, thus a few ugly hacks.
+ */
+
+/* I can't believe I need to do this to change « foo » to « "foo" » */
+#define UGLY_KLUDGE(z) NASTY_CROCK(z)
+#define NASTY_CROCK(z) #z
+/* And I need to do _this_ to change « foo bar » to « foo_bar » ! */
+#define AWFUL_BRITTLE(y,z) CRUDE_HACK(y,z)
+#define CRUDE_HACK(y,z) y##_##z
+
+/* Also, I need to do this to change « blah » to « "VLC_MODULE_foo_blah" » */
+#define MODULE_STRING UGLY_KLUDGE(MODULE_NAME)
+#define MODULE_VAR(z) "VLC_MODULE_" UGLY_KLUDGE(MODULE_NAME) "_" #z
+
+/* If the module is built-in, then we need to define foo_InitModule instead
+ * of InitModule. Same for Activate- and DeactivateModule. */
+#ifdef BUILTIN
+#  define InitModule AWFUL_BRITTLE(MODULE_NAME,InitModule)
+#  define ActivateModule AWFUL_BRITTLE(MODULE_NAME,ActivateModule)
+#  define DeactivateModule AWFUL_BRITTLE(MODULE_NAME,DeactivateModule)
+#endif
+
+/*****************************************************************************
+ * Macros used to build the configuration structure.
+ *****************************************************************************/
+#define MODULE_CONFIG_START( text ) \
+static module_config_t p_config[] = { \
+    { MODULE_CONFIG_ITEM_START, text, NULL, NULL, NULL },
+
+#define MODULE_CONFIG_END   \
+    { MODULE_CONFIG_ITEM_END, NULL, NULL, NULL, NULL } \
+};
+
+#define ADD_FRAME( text ) \
+    { MODULE_CONFIG_ITEM_FRAME, text, NULL, NULL, NULL },
+#define ADD_PANE( text ) \
+    { MODULE_CONFIG_ITEM_PANE, text, NULL, NULL, NULL },
+#define ADD_COMMENT( text ) \
+    { MODULE_CONFIG_ITEM_COMMENT, text, NULL, NULL, NULL },
+#define ADD_STRING( text, name, p_update ) \
+    { MODULE_CONFIG_ITEM_STRING, text, name, NULL, p_update },
+#define ADD_FILE( text, name, p_update ) \
+    { MODULE_CONFIG_ITEM_FILE, text, name, NULL, p_update },
+#define ADD_CHECK( text, name, p_update ) \
+    { MODULE_CONFIG_ITEM_CHECK, text, name, NULL, p_update },
+#define ADD_CHOOSE( text, name, p_getlist, p_update ) \
+    { MODULE_CONFIG_ITEM_CHOOSE, text, name, p_getlist, p_update },
+#define ADD_RADIO( text, name, p_getlist, p_update ) \
+    { MODULE_CONFIG_ITEM_RADIO, text, name, p_getlist, p_update },
+#define ADD_SCALE( text, name, p_getlist, p_update ) \
+    { MODULE_CONFIG_ITEM_SCALE, text, name, p_getlist, p_update },
+#define ADD_SPIN( text, name, p_getlist, p_update ) \
+    { MODULE_CONFIG_ITEM_SPIN, text, name, p_getlist, p_update },
+
diff --git a/src/misc/modules.c b/src/misc/modules.c
new file mode 100644 (file)
index 0000000..0f533f5
--- /dev/null
@@ -0,0 +1,468 @@
+/*****************************************************************************
+ * modules.c : Built-in and dynamic modules management functions
+ *****************************************************************************
+ * Copyright (C) 2001 VideoLAN
+ *
+ * Authors: Samuel Hocevar <sam@zoy.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ *****************************************************************************/
+#include "defs.h"
+
+#include "config.h"
+
+#include <stdlib.h>                                      /* free(), strtol() */
+#include <stdio.h>                                              /* sprintf() */
+#include <string.h>                                            /* strerror() */
+#include <errno.h>                                                 /* ENOMEM */
+#include <sys/types.h>                                               /* open */
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>                                                 /* close */
+
+#if defined(HAVE_DLFCN_H)                                /* Linux, BSD, Hurd */
+#include <dlfcn.h>                           /* dlopen(), dlsym(), dlclose() */
+
+#elif defined(HAVE_IMAGE_H)                                          /* BeOS */
+#include <image.h>
+
+#else
+/* FIXME: this isn't supposed to be an error */
+#error no dynamic plugins available on your system !
+#endif
+
+#ifdef SYS_BEOS
+#include "beos_specific.h"
+#endif
+
+#include "common.h"
+#include "threads.h"
+
+#include "intf_msg.h"
+#include "modules.h"
+
+/* Local prototypes */
+static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename );
+static int HideModule( module_t * p_module );
+static int FreeModule( module_bank_t * p_bank, module_t * p_module );
+static int CallSymbol( module_t * p_module, char * psz_name );
+
+/*****************************************************************************
+ * module_InitBank: create the module bank.
+ *****************************************************************************
+ * This function creates a module bank structure and fills it with the
+ * built-in modules, as well as all the dynamic modules it can find.
+ *****************************************************************************/
+module_bank_t * module_InitBank( void )
+{
+    module_bank_t * p_bank;
+
+    intf_ErrMsg( "FIXME: module_InitBank unimplemented" );
+    p_bank = malloc( sizeof( module_bank_t ) );
+
+    return( p_bank );
+}
+
+/*****************************************************************************
+ * module_DestroyBank: destroy the module bank.
+ *****************************************************************************
+ * This function unloads all unused dynamic modules and removes the module
+ * bank in case of success.
+ *****************************************************************************/
+int module_DestroyBank( module_bank_t * p_bank )
+{
+    intf_ErrMsg( "FIXME: module_DestroyBank unimplemented" );
+    return( -1 );
+}
+
+/*****************************************************************************
+ * module_ResetBank: reset the module bank.
+ *****************************************************************************
+ * This function resets the plugin bank by unloading all unused dynamic
+ * modules.
+ *****************************************************************************/
+int module_ResetBank( module_bank_t * p_bank )
+{
+    intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
+    return( -1 );
+}
+
+/*****************************************************************************
+ * module_ManageBank: manage the module bank.
+ *****************************************************************************
+ * This function parses the module bank and hides modules that have been
+ * unused for a while.
+ *****************************************************************************/
+void module_ManageBank( module_bank_t * p_bank )
+{
+    module_t * p_module;
+
+    /* We take the global lock */
+    vlc_mutex_lock( &p_bank->lock );
+
+    /* Parse the module list to see if any modules need to be unloaded */
+    for( p_module = p_bank->first ;
+         p_module != NULL ;
+         p_module = p_module->next )
+    {
+        /* If the module is unused and if it is a dynamic module... */
+        if( p_module->i_usage == 0 && !p_module->b_builtin )
+        {
+            if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
+            {
+                p_module->i_unused_delay++;
+            }
+            else
+            {
+                intf_Msg( "hiding unused module %s", p_module->psz_name );
+                HideModule( p_module );
+            }
+        }
+    }
+
+    /* We release the global lock */
+    vlc_mutex_lock( &p_bank->lock );
+
+    return;
+}
+
+/*****************************************************************************
+ * module_Need: increase the usage count of a module and load it if needed.
+ *****************************************************************************
+ * This function has to be called before a thread starts using a module. If
+ * the module is already loaded, we just increase its usage count. If it isn't
+ * loaded, we have to dynamically open it and initialize it.
+ * If you successfully call module_Need() at any moment, be careful to call
+ * module_Unneed() when you don't need it anymore.
+ *****************************************************************************/
+int module_Need( module_t * p_module )
+{
+    if( p_module->i_usage >= 0 )
+    {
+        /* This module is already loaded and activated, we can return */
+        p_module->i_usage++;
+        return( 0 );
+    }
+
+    if( p_module->b_builtin )
+    {
+        /* A built-in module should always have a refcount >= 0 ! */
+        intf_ErrMsg( "module error: built-in module %s has refcount %i",
+                     p_module->psz_name, p_module->i_usage );
+        return( -1 );
+    }
+
+    if( p_module->i_usage != -1 )
+    {
+        /* This shouldn't happen. Ever. We have serious problems here. */
+        intf_ErrMsg( "module error: dynamic module %s has refcount %i",
+                     p_module->psz_name, p_module->i_usage );
+        return( -1 );
+    }
+
+    /* i_usage == -1, which means that the module isn't in memory */
+    if( ! module_load( p_module->psz_filename, &p_module->handle ) )
+    {
+        /* The dynamic module couldn't be opened */
+        intf_ErrMsg( "module error: cannot open %s (%s)",
+                     p_module->psz_filename, module_error() );
+        return( -1 );
+    }
+
+    if( CallSymbol( p_module, "ActivateModule" ) != 0 )
+    {
+        /* We couldn't call ActivateModule() -- looks nasty, but
+         * we can't do much about it. Just try to unload module. */
+        module_unload( p_module->handle );
+        p_module->i_usage = -1;
+        return( -1 );
+    }
+
+    /* Everything worked fine ! The module is ready to be used */
+    p_module->i_usage = 1;
+
+    return( 0 );
+}
+
+/*****************************************************************************
+ * module_Unneed: decrease the usage count of a module.
+ *****************************************************************************
+ * This function has to be called before a thread starts using a module. If
+ * the module is already loaded, we just increase its usage count. If it isn't
+ * loaded, we have to dynamically open it and initialize it.
+ * If you successfully call module_Need() at any moment, be careful to call
+ * module_Unneed() when you don't need it anymore.
+ *****************************************************************************/
+int module_Unneed( module_t * p_module )
+{
+    if( p_module->i_usage <= 0 )
+    {
+        /* This shouldn't happen. Ever. We have serious problems here. */
+        intf_ErrMsg( "module error: trying to call module_Unneed() on %s"
+                     " which isn't even in use", p_module->psz_name );
+        return( -1 );
+    }
+
+    /* This module is still in use, we can return */
+    p_module->i_usage--;
+    p_module->i_unused_delay = 0;
+
+    return( 0 );
+}
+
+/*****************************************************************************
+ * Following functions are local.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * AllocateDynModule: load a module into memory and initialize it.
+ *****************************************************************************
+ * This function loads a dynamically loadable module and allocates a structure
+ * for its information data. The module can then be handled by module_Need,
+ * module_Unneed and HideModule. It can be removed by FreeModule.
+ *****************************************************************************/
+static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
+{
+    module_t * p_module;
+    module_handle_t handle;
+
+    /* Try to dynamically load the module. */
+    if( ! module_load( psz_filename, &handle ) )
+    {
+        /* The dynamic module couldn't be opened */
+        intf_ErrMsg( "module error: cannot open %s (%s)",
+                     psz_filename, module_error() );
+        return( -1 );
+    }
+
+    /* Now that we have successfully loaded the module, we can
+     * allocate a structure for it */ 
+    p_module = malloc( sizeof( module_t ) );
+    if( p_module == NULL )
+    {
+        intf_ErrMsg( "module error: can't allocate p_module" );
+        module_unload( handle );
+        return( -1 );
+    }
+
+    /* We need to fill these since they may be needed by CallSymbol() */
+    p_module->psz_filename = psz_filename;
+    p_module->handle = handle;
+
+    if( CallSymbol( p_module, "InitModule" ) != 0 )
+    {
+        /* We couldn't call InitModule() */
+        free( p_module );
+        module_unload( handle );
+        return( -1 );
+    }
+
+    if( CallSymbol( p_module, "ActivateModule" ) != 0 )
+    {
+        /* We couldn't call ActivateModule() */
+        free( p_module );
+        module_unload( handle );
+        return( -1 );
+    }
+
+    /* We strdup() these entries so that they are still valid when the
+     * module is unloaded. */
+    p_module->psz_filename = strdup( p_module->psz_filename );
+    p_module->psz_name = strdup( p_module->psz_name );
+    p_module->psz_longname = strdup( p_module->psz_longname );
+    p_module->psz_version = strdup( p_module->psz_version );
+    if( p_module->psz_filename == NULL 
+            || p_module->psz_name == NULL
+            || p_module->psz_longname == NULL
+            || p_module->psz_version == NULL )
+    {
+        intf_ErrMsg( "module error: can't duplicate strings" );
+        free( p_module->psz_filename );
+        free( p_module->psz_name );
+        free( p_module->psz_longname );
+        free( p_module->psz_version );
+        free( p_module );
+        module_unload( handle );
+        return( -1 );
+    }
+
+    /* Everything worked fine ! The module is ready to be added to the list. */
+    p_module->i_usage = 0;
+    p_module->i_unused_delay = 0;
+
+    p_module->b_builtin = 0;
+
+    /* Link module across linked list */
+    if( p_bank->first != NULL )
+    {
+        p_bank->first->prev = p_module;
+    }
+    p_module->next = p_bank->first;
+    p_bank->first = p_module;
+
+    return( 0 );
+}
+
+/*****************************************************************************
+ * HideModule: remove a module from memory but keep its structure.
+ *****************************************************************************
+ * This function can only be called if i_usage == 0. It will make a call
+ * to the module's inner DeactivateModule() symbol, and then unload it
+ * from memory. A call to module_Need() will automagically load it again.
+ *****************************************************************************/
+static int HideModule( module_t * p_module )
+{
+    if( p_module->b_builtin )
+    {
+        /* A built-in module should never be hidden. */
+        intf_ErrMsg( "module error: trying to hide built-in module %s",
+                     p_module->psz_name );
+        return( -1 );
+    }
+
+    if( p_module->i_usage >= 1 )
+    {
+        intf_ErrMsg( "module error: trying to hide module %s which is still"
+                     " in use", p_module->psz_name );
+        return( -1 );
+    }
+
+    if( p_module->i_usage <= -1 )
+    {
+        intf_ErrMsg( "module error: trying to hide module %s which is already"
+                     " hidden", p_module->psz_name );
+        return( -1 );
+    }
+
+    if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
+    {
+        /* We couldn't call DeactivateModule() -- looks nasty, but
+         * we can't do much about it. Just try to unload module anyway. */
+        module_unload( p_module->handle );
+        p_module->i_usage = -1;
+        return( -1 );
+    }
+
+    /* Everything worked fine, we can safely unload the module. */
+    module_unload( p_module->handle );
+    p_module->i_usage = -1;
+
+    return( 0 );
+}
+
+/*****************************************************************************
+ * FreeModule: delete a module and its structure.
+ *****************************************************************************
+ * This function can only be called if i_usage <= 0.
+ *****************************************************************************/
+static int FreeModule( module_bank_t * p_bank, module_t * p_module )
+{
+    /* If the module is not in use but is still in memory, we first have
+     * to hide it and remove it from memory before we can free the
+     * data structure. */
+    if( p_module->b_builtin )
+    {
+        if( p_module->i_usage != 0 )
+        {
+            intf_ErrMsg( "module error: trying to free builtin module %s with"
+                         " usage %i", p_module->psz_name, p_module->i_usage );
+            return( -1 );
+        }
+    }
+    else
+    {
+        if( p_module->i_usage >= 1 )
+        {
+            intf_ErrMsg( "module error: trying to free module %s which is"
+                         " still in use", p_module->psz_name );
+            return( -1 );
+        }
+
+        /* Two possibilities here: i_usage == -1 and the module is already
+         * unloaded, we can continue, or i_usage == 0, and we have to hide
+         * the module before going on. */
+        if( p_module->i_usage == 0 )
+        {
+            if( HideModule( p_module ) != 0 )
+            {
+                return( -1 );
+            }
+        }
+    }
+
+    /* Unlink the module from the linked list. */
+    if( p_module == p_bank->first )
+    {
+        p_bank->first = p_module->next;
+    }
+
+    if( p_module->prev != NULL )
+    {
+        p_module->prev->next = p_module->next;
+    }
+
+    if( p_module->next != NULL )
+    {
+        p_module->next->prev = p_module->prev;
+    }
+
+    /* We free the structures that we strdup()ed in Allocate*Module(). */
+    free( p_module->psz_filename );
+    free( p_module->psz_name );
+    free( p_module->psz_longname );
+    free( p_module->psz_version );
+
+    free( p_module );
+
+    return( 0 );
+}
+
+/*****************************************************************************
+ * CallSymbol: calls a module symbol.
+ *****************************************************************************
+ * This function calls a symbol given its name and a module structure. The
+ * symbol MUST refer to a function returning int and taking a module_t* as
+ * an argument.
+ *****************************************************************************/
+static int CallSymbol( module_t * p_module, char * psz_name )
+{
+    typedef int ( symbol_t ) ( module_t * p_module );
+    symbol_t * p_symbol;
+
+    /* Try to resolve the symbol */
+    p_symbol = module_getsymbol( p_module->handle, psz_name );
+
+    if( !p_symbol )
+    {
+        /* We couldn't load the symbol */
+        intf_ErrMsg( "module error: cannot find %s in module %s (%s)",
+                     psz_name, p_module->psz_filename, module_error() );
+        return( -1 );
+    }
+
+    /* We can now try to call the symbol */
+    if( p_symbol( p_module ) != 0 )
+    {
+        /* With a well-written module we shouldn't have to print an
+         * additional error message here, but just make sure. */
+        intf_ErrMsg( "module error: failed calling %s in module %s",
+                     psz_name, p_module->psz_filename );
+        return( -1 );
+    }
+
+    /* Everything worked fine, we can return */
+    return( 0 );
+}
+