Commit 6eb6044c authored by Sam Hocevar's avatar Sam Hocevar

 . src/misc/modules.c is in a workable state now.

  To test it, run ./configure with the --enable-null flag, which will
  compile the Null Module as a plugin. Watch the memory usage at the
  launch and when the plugin is released.
parent 2a8ef11d
...@@ -245,6 +245,7 @@ VIDEO_DECODER = src/video_decoder/video_decoder.o \ ...@@ -245,6 +245,7 @@ VIDEO_DECODER = src/video_decoder/video_decoder.o \
MISC = src/misc/mtime.o \ MISC = src/misc/mtime.o \
src/misc/tests.o \ src/misc/tests.o \
src/misc/rsc_files.o \ src/misc/rsc_files.o \
src/misc/modules.o \
src/misc/netutils.o \ src/misc/netutils.o \
src/misc/playlist.o \ src/misc/playlist.o \
src/misc/plugins.o src/misc/plugins.o
......
...@@ -51,8 +51,9 @@ typedef struct ...@@ -51,8 +51,9 @@ typedef struct
/* Shared data - these structures are accessed directly from p_main by /* Shared data - these structures are accessed directly from p_main by
* several modules */ * several modules */
struct module_bank_s * p_module_bank; /* module bank */
p_plugin_bank_t p_bank; /* plugin bank */ p_plugin_bank_t p_bank; /* plugin bank */
p_playlist_t p_playlist; /* plugin bank */ p_playlist_t p_playlist; /* playlist */
p_intf_msg_t p_msg; /* messages interface data */ p_intf_msg_t p_msg; /* messages interface data */
p_input_vlan_t p_vlan; /* vlan library data */ p_input_vlan_t p_vlan; /* vlan library data */
} main_t; } main_t;
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
*****************************************************************************/ *****************************************************************************/
/* Number of tries before we unload an unused module */ /* Number of tries before we unload an unused module */
#define MODULE_HIDE_DELAY 10 #define MODULE_HIDE_DELAY 50
/* The module handle type. */ /* The module handle type. */
#ifdef SYS_BEOS #ifdef SYS_BEOS
...@@ -82,9 +82,10 @@ typedef struct module_bank_s ...@@ -82,9 +82,10 @@ typedef struct module_bank_s
/***************************************************************************** /*****************************************************************************
* Exported functions. * Exported functions.
*****************************************************************************/ *****************************************************************************/
module_bank_t * module_InitBank ( void ); module_bank_t * module_CreateBank ( void );
int module_DestroyBank ( module_bank_t * p_bank ); void module_InitBank ( module_bank_t * p_bank );
int module_ResetBank ( module_bank_t * p_bank ); void module_DestroyBank ( module_bank_t * p_bank );
void module_ResetBank ( module_bank_t * p_bank );
void module_ManageBank ( module_bank_t * p_bank ); void module_ManageBank ( module_bank_t * p_bank );
int module_Need ( module_t * p_module ); int module_Need ( module_t * p_module );
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include "threads.h" #include "threads.h"
#include "mtime.h" #include "mtime.h"
#include "plugins.h" #include "plugins.h"
#include "modules.h"
#include "playlist.h" #include "playlist.h"
#include "stream_control.h" #include "stream_control.h"
...@@ -235,6 +236,9 @@ void intf_Run( intf_thread_t *p_intf ) ...@@ -235,6 +236,9 @@ void intf_Run( intf_thread_t *p_intf )
/* Manage specific interface */ /* Manage specific interface */
p_intf->p_sys_manage( p_intf ); p_intf->p_sys_manage( p_intf );
/* Manage module bank */
module_ManageBank( p_main->p_module_bank );
/* Check attached threads status */ /* Check attached threads status */
if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error ) if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
{ {
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
#include "mtime.h" #include "mtime.h"
#include "tests.h" /* TestMMX() */ #include "tests.h" /* TestMMX() */
#include "plugins.h" #include "plugins.h"
#include "modules.h"
#include "playlist.h" #include "playlist.h"
#include "stream_control.h" #include "stream_control.h"
#include "input_ext-intf.h" #include "input_ext-intf.h"
...@@ -251,6 +252,20 @@ int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] ) ...@@ -251,6 +252,20 @@ int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
} }
bank_Init( p_main->p_bank ); bank_Init( p_main->p_bank );
/*
* Initialize module bank
*/
p_main->p_module_bank = module_CreateBank( );
if( !p_main->p_module_bank )
{
intf_Msg( "Module bank initialization failed" );
bank_Destroy( p_main->p_bank );
playlist_Destroy( p_main->p_playlist );
intf_MsgDestroy();
return( errno );
}
module_InitBank( p_main->p_module_bank );
/* /*
* Initialize shared resources and libraries * Initialize shared resources and libraries
*/ */
...@@ -312,6 +327,11 @@ int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] ) ...@@ -312,6 +327,11 @@ int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
} }
#endif #endif
/*
* Free module bank
*/
module_DestroyBank( p_main->p_module_bank );
/* /*
* Free plugin bank * Free plugin bank
*/ */
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
* Copyright (C) 2001 VideoLAN * Copyright (C) 2001 VideoLAN
* *
* Authors: Samuel Hocevar <sam@zoy.org> * Authors: Samuel Hocevar <sam@zoy.org>
* Ethan C. Baldridge <BaldridgeE@cadmus.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
...@@ -25,12 +26,8 @@ ...@@ -25,12 +26,8 @@
#include <stdlib.h> /* free(), strtol() */ #include <stdlib.h> /* free(), strtol() */
#include <stdio.h> /* sprintf() */ #include <stdio.h> /* sprintf() */
#include <string.h> /* strerror() */ #include <string.h> /* strdup() */
#include <errno.h> /* ENOMEM */ #include <dirent.h>
#include <sys/types.h> /* open */
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> /* close */
#if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */ #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
#include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */ #include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
...@@ -61,43 +58,136 @@ static int FreeModule( module_bank_t * p_bank, module_t * p_module ); ...@@ -61,43 +58,136 @@ static int FreeModule( module_bank_t * p_bank, module_t * p_module );
static int CallSymbol( module_t * p_module, char * psz_name ); static int CallSymbol( module_t * p_module, char * psz_name );
/***************************************************************************** /*****************************************************************************
* module_InitBank: create the module bank. * module_CreateBank: create the module bank.
***************************************************************************** *****************************************************************************
* This function creates a module bank structure and fills it with the * This function creates a module bank structure.
* built-in modules, as well as all the dynamic modules it can find.
*****************************************************************************/ *****************************************************************************/
module_bank_t * module_InitBank( void ) module_bank_t * module_CreateBank( void )
{ {
module_bank_t * p_bank; module_bank_t * p_bank;
intf_ErrMsg( "FIXME: module_InitBank unimplemented" );
p_bank = malloc( sizeof( module_bank_t ) ); p_bank = malloc( sizeof( module_bank_t ) );
return( p_bank ); return( p_bank );
} }
/*****************************************************************************
* 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.
*****************************************************************************/
void module_InitBank( module_bank_t * p_bank )
{
static char * path[] = { ".", "lib", PLUGIN_PATH, NULL } ;
char ** ppsz_path = path;
char * psz_file;
#ifdef SYS_BEOS
char * psz_program_path = beos_GetProgramPath();
int i_programlen = strlen( psz_program_path );
#endif
DIR * dir;
struct dirent * file;
p_bank->first = NULL;
vlc_mutex_init( &p_bank->lock );
intf_Msg( "module: module bank initialized" );
for( ; *ppsz_path != NULL ; ppsz_path++ )
{
if( (dir = opendir( *ppsz_path )) )
{
/* Store strlen(*ppsz_path) for later use. */
int i_dirlen = strlen( *ppsz_path );
/* Parse the directory and try to load all files it contains. */
while( (file = readdir( dir )) )
{
if( memcmp( file->d_name, ".", 1 ) )
{
#ifdef SYS_BEOS
/* Under BeOS, we need to add beos_GetProgramPath() to
* access files under the current directory */
if( memcmp( file->d_name, "/", 1 ) )
{
psz_file = malloc( i_programlen + i_dirlen
+ strlen( file->d_name ) + 3 );
if( psz_file == NULL )
{
continue;
}
sprintf( psz_file, "%s/%s/%s", psz_programlen,
*ppsz_path, file->d_name );
}
else
#endif
{
psz_file = malloc( i_dirlen
+ strlen( file->d_name ) + 2 );
if( psz_file == NULL )
{
continue;
}
sprintf( psz_file, "%s/%s", *ppsz_path, file->d_name );
}
/* We created a nice filename -- now we just try to load
* it as a dynamic module. */
AllocateDynModule( p_bank, psz_file );
/* We don't care if the allocation succeeded */
free( psz_file );
}
}
}
}
return;
}
/***************************************************************************** /*****************************************************************************
* module_DestroyBank: destroy the module bank. * module_DestroyBank: destroy the module bank.
***************************************************************************** *****************************************************************************
* This function unloads all unused dynamic modules and removes the module * This function unloads all unused dynamic modules and removes the module
* bank in case of success. * bank in case of success.
*****************************************************************************/ *****************************************************************************/
int module_DestroyBank( module_bank_t * p_bank ) void module_DestroyBank( module_bank_t * p_bank )
{ {
intf_ErrMsg( "FIXME: module_DestroyBank unimplemented" ); module_t * p_next;
return( -1 );
while( p_bank->first != NULL )
{
if( FreeModule( p_bank, p_bank->first ) )
{
/* Module deletion failed */
intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
p_bank->first->psz_name );
/* We just free the module by hand. Niahahahahaha. */
p_next = p_bank->first->next;
free(p_bank->first);
p_bank->first = p_next;
}
}
/* We can free the module bank */
free( p_bank );
return;
} }
/***************************************************************************** /*****************************************************************************
* module_ResetBank: reset the module bank. * module_ResetBank: reset the module bank.
***************************************************************************** *****************************************************************************
* This function resets the plugin bank by unloading all unused dynamic * This function resets the module bank by unloading all unused dynamic
* modules. * modules.
*****************************************************************************/ *****************************************************************************/
int module_ResetBank( module_bank_t * p_bank ) void module_ResetBank( module_bank_t * p_bank )
{ {
intf_ErrMsg( "FIXME: module_ResetBank unimplemented" ); intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
return( -1 ); return;
} }
/***************************************************************************** /*****************************************************************************
...@@ -127,14 +217,15 @@ void module_ManageBank( module_bank_t * p_bank ) ...@@ -127,14 +217,15 @@ void module_ManageBank( module_bank_t * p_bank )
} }
else else
{ {
intf_Msg( "hiding unused module %s", p_module->psz_name ); intf_Msg( "module: hiding unused module `%s'",
p_module->psz_name );
HideModule( p_module ); HideModule( p_module );
} }
} }
} }
/* We release the global lock */ /* We release the global lock */
vlc_mutex_lock( &p_bank->lock ); vlc_mutex_unlock( &p_bank->lock );
return; return;
} }
...@@ -160,7 +251,7 @@ int module_Need( module_t * p_module ) ...@@ -160,7 +251,7 @@ int module_Need( module_t * p_module )
if( p_module->b_builtin ) if( p_module->b_builtin )
{ {
/* A built-in module should always have a refcount >= 0 ! */ /* A built-in module should always have a refcount >= 0 ! */
intf_ErrMsg( "module error: built-in module %s has refcount %i", intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
p_module->psz_name, p_module->i_usage ); p_module->psz_name, p_module->i_usage );
return( -1 ); return( -1 );
} }
...@@ -168,7 +259,7 @@ int module_Need( module_t * p_module ) ...@@ -168,7 +259,7 @@ int module_Need( module_t * p_module )
if( p_module->i_usage != -1 ) if( p_module->i_usage != -1 )
{ {
/* This shouldn't happen. Ever. We have serious problems here. */ /* This shouldn't happen. Ever. We have serious problems here. */
intf_ErrMsg( "module error: dynamic module %s has refcount %i", intf_ErrMsg( "module error: dynamic module `%s' has refcount %i",
p_module->psz_name, p_module->i_usage ); p_module->psz_name, p_module->i_usage );
return( -1 ); return( -1 );
} }
...@@ -211,7 +302,7 @@ int module_Unneed( module_t * p_module ) ...@@ -211,7 +302,7 @@ int module_Unneed( module_t * p_module )
if( p_module->i_usage <= 0 ) if( p_module->i_usage <= 0 )
{ {
/* This shouldn't happen. Ever. We have serious problems here. */ /* This shouldn't happen. Ever. We have serious problems here. */
intf_ErrMsg( "module error: trying to call module_Unneed() on %s" intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
" which isn't even in use", p_module->psz_name ); " which isn't even in use", p_module->psz_name );
return( -1 ); return( -1 );
} }
...@@ -243,7 +334,7 @@ static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename ) ...@@ -243,7 +334,7 @@ static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
if( ! module_load( psz_filename, &handle ) ) if( ! module_load( psz_filename, &handle ) )
{ {
/* The dynamic module couldn't be opened */ /* The dynamic module couldn't be opened */
intf_ErrMsg( "module error: cannot open %s (%s)", intf_DbgMsg( "module error: cannot open %s (%s)",
psz_filename, module_error() ); psz_filename, module_error() );
return( -1 ); return( -1 );
} }
...@@ -305,14 +396,18 @@ static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename ) ...@@ -305,14 +396,18 @@ static int AllocateDynModule( module_bank_t * p_bank, char * psz_filename )
p_module->b_builtin = 0; p_module->b_builtin = 0;
/* Link module across linked list */ /* Link module into the linked list */
if( p_bank->first != NULL ) if( p_bank->first != NULL )
{ {
p_bank->first->prev = p_module; p_bank->first->prev = p_module;
} }
p_module->next = p_bank->first; p_module->next = p_bank->first;
p_module->prev = NULL;
p_bank->first = p_module; p_bank->first = p_module;
intf_Msg( "module: dynamic module `%s', %s",
p_module->psz_name, p_module->psz_longname );
return( 0 ); return( 0 );
} }
...@@ -328,21 +423,21 @@ static int HideModule( module_t * p_module ) ...@@ -328,21 +423,21 @@ static int HideModule( module_t * p_module )
if( p_module->b_builtin ) if( p_module->b_builtin )
{ {
/* A built-in module should never be hidden. */ /* A built-in module should never be hidden. */
intf_ErrMsg( "module error: trying to hide built-in module %s", intf_ErrMsg( "module error: trying to hide built-in module `%s'",
p_module->psz_name ); p_module->psz_name );
return( -1 ); return( -1 );
} }
if( p_module->i_usage >= 1 ) if( p_module->i_usage >= 1 )
{ {
intf_ErrMsg( "module error: trying to hide module %s which is still" intf_ErrMsg( "module error: trying to hide module `%s' which is still"
" in use", p_module->psz_name ); " in use", p_module->psz_name );
return( -1 ); return( -1 );
} }
if( p_module->i_usage <= -1 ) if( p_module->i_usage <= -1 )
{ {
intf_ErrMsg( "module error: trying to hide module %s which is already" intf_ErrMsg( "module error: trying to hide module `%s' which is already"
" hidden", p_module->psz_name ); " hidden", p_module->psz_name );
return( -1 ); return( -1 );
} }
...@@ -377,7 +472,7 @@ static int FreeModule( module_bank_t * p_bank, module_t * p_module ) ...@@ -377,7 +472,7 @@ static int FreeModule( module_bank_t * p_bank, module_t * p_module )
{ {
if( p_module->i_usage != 0 ) if( p_module->i_usage != 0 )
{ {
intf_ErrMsg( "module error: trying to free builtin module %s with" intf_ErrMsg( "module error: trying to free builtin module `%s' with"
" usage %i", p_module->psz_name, p_module->i_usage ); " usage %i", p_module->psz_name, p_module->i_usage );
return( -1 ); return( -1 );
} }
...@@ -386,7 +481,7 @@ static int FreeModule( module_bank_t * p_bank, module_t * p_module ) ...@@ -386,7 +481,7 @@ static int FreeModule( module_bank_t * p_bank, module_t * p_module )
{ {
if( p_module->i_usage >= 1 ) if( p_module->i_usage >= 1 )
{ {
intf_ErrMsg( "module error: trying to free module %s which is" intf_ErrMsg( "module error: trying to free module `%s' which is"
" still in use", p_module->psz_name ); " still in use", p_module->psz_name );
return( -1 ); return( -1 );
} }
...@@ -448,7 +543,7 @@ static int CallSymbol( module_t * p_module, char * psz_name ) ...@@ -448,7 +543,7 @@ static int CallSymbol( module_t * p_module, char * psz_name )
if( !p_symbol ) if( !p_symbol )
{ {
/* We couldn't load the symbol */ /* We couldn't load the symbol */
intf_ErrMsg( "module error: cannot find %s in module %s (%s)", intf_DbgMsg( "module warning: cannot find symbol %s in module %s (%s)",
psz_name, p_module->psz_filename, module_error() ); psz_name, p_module->psz_filename, module_error() );
return( -1 ); return( -1 );
} }
...@@ -458,7 +553,7 @@ static int CallSymbol( module_t * p_module, char * psz_name ) ...@@ -458,7 +553,7 @@ static int CallSymbol( module_t * p_module, char * psz_name )
{ {
/* With a well-written module we shouldn't have to print an /* With a well-written module we shouldn't have to print an
* additional error message here, but just make sure. */ * additional error message here, but just make sure. */
intf_ErrMsg( "module error: failed calling %s in module %s", intf_ErrMsg( "module error: failed calling symbol %s in module %s",
psz_name, p_module->psz_filename ); psz_name, p_module->psz_filename );
return( -1 ); return( -1 );
} }
......
...@@ -115,7 +115,6 @@ void bank_Init( plugin_bank_t * p_bank ) ...@@ -115,7 +115,6 @@ void bank_Init( plugin_bank_t * p_bank )
/* Dummy plugin */ /* Dummy plugin */
SEEK_PLUGIN( "dummy" ); SEEK_PLUGIN( "dummy" );
SEEK_PLUGIN( "null" );
#undef SEEK_PLUGIN #undef SEEK_PLUGIN
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment