gnash-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnash-commit] /srv/bzr/gnash/trunk r9545: Cleanup of extension / module


From: Benjamin Wolsey
Subject: [Gnash-commit] /srv/bzr/gnash/trunk r9545: Cleanup of extension / module / plugin loading, part I.
Date: Wed, 30 Jul 2008 09:53:22 +0200
User-agent: Bazaar (1.5)

------------------------------------------------------------
revno: 9545
committer: Benjamin Wolsey <address@hidden>
branch nick: trunk
timestamp: Wed 2008-07-30 09:53:22 +0200
message:
  Cleanup of extension / module / plugin loading, part I.
modified:
  libbase/extension.cpp
  libbase/extension.h
  libbase/sharedlib.cpp
  libbase/sharedlib.h
  libcore/asobj/ClassHierarchy.cpp
    ------------------------------------------------------------
    revno: 9543.1.1
    committer: Benjamin Wolsey <address@hidden>
    branch nick: workingcopy
    timestamp: Wed 2008-07-30 00:45:36 +0200
    message:
      Stop using const char*, pass strings by reference instead. Especially
      don't return pointers to remotely owned characters. Drop multiple 
      constructors in favour of const std::string& ones.
    modified:
      libbase/extension.cpp
      libbase/extension.h
      libbase/sharedlib.cpp
      libbase/sharedlib.h
    ------------------------------------------------------------
    revno: 9543.1.2
    committer: Benjamin Wolsey <address@hidden>
    branch nick: workingcopy
    timestamp: Wed 2008-07-30 09:49:49 +0200
    message:
      Document Extension class. Drop unused functions.
    modified:
      libbase/extension.cpp
      libbase/extension.h
    ------------------------------------------------------------
    revno: 9543.1.3
    committer: Benjamin Wolsey <address@hidden>
    branch nick: workingcopy
    timestamp: Wed 2008-07-30 09:50:17 +0200
    message:
      Drop some unused functions, make mutex a private member. General cleanup.
    modified:
      libbase/sharedlib.cpp
      libbase/sharedlib.h
    ------------------------------------------------------------
    revno: 9543.1.4
    committer: Benjamin Wolsey <address@hidden>
    branch nick: workingcopy
    timestamp: Wed 2008-07-30 09:51:07 +0200
    message:
      No need to use c_str() to load module with function.
    modified:
      libcore/asobj/ClassHierarchy.cpp
=== modified file 'libbase/extension.cpp'
--- a/libbase/extension.cpp     2008-05-07 19:59:28 +0000
+++ b/libbase/extension.cpp     2008-07-30 07:49:49 +0000
@@ -45,7 +45,7 @@
 
 #if HAVE_DIRENT_H || WIN32==1  // win32 hack
 # include <dirent.h>
-# define NAMLEN(dirent) strlen((dirent)->d_name)
+# define NAMLEN(dirent) std::strlen((dirent)->d_name)
 #else
 # define dirent direct
 # define NAMLEN(dirent) (dirent)->d_namlen
@@ -60,7 +60,6 @@
 # endif
 #endif
 
-using namespace std;
 namespace gnash {
 
 Extension::Extension() 
@@ -70,18 +69,19 @@
 //     return lt_dlmutex_register (gnash_mutex_lock, gnash_mutex_unlock,
 //                                 gnash_mutex_seterror, gnash_mutex_geterror);
 #endif
-    char *env = getenv ("GNASH_PLUGINS");
-    if (env == 0) {
+    char *env = std::getenv("GNASH_PLUGINS");
+    if (!env) {
         _pluginsdir = PLUGINSDIR;
-    } else {
+    }
+    else {
         _pluginsdir = env;
     }
 
     log_debug("Plugins path: %s", _pluginsdir);
-    lt_dlsetsearchpath(_pluginsdir);
+    lt_dlsetsearchpath(_pluginsdir.c_str());
 }
 
-Extension::Extension(const char *dir)
+Extension::Extension(const std::string& dir)
 {
 //    GNASH_REPORT_FUNCTION;
 #ifdef LT_DLMUTEX
@@ -89,7 +89,7 @@
 //                                 gnash_mutex_seterror, gnash_mutex_geterror);
 #endif
     _pluginsdir = dir;
-    lt_dlsetsearchpath(_pluginsdir);
+    lt_dlsetsearchpath(_pluginsdir.c_str());
 }
 
 Extension::~Extension()
@@ -98,48 +98,44 @@
 }
 
 bool
-Extension::scanAndLoad(const char *dir, as_object &obj)
+Extension::scanAndLoad(const std::string& dir, as_object &obj)
 {
 //    GNASH_REPORT_FUNCTION;
     
-    lt_dlsetsearchpath(_pluginsdir);
+    lt_dlsetsearchpath(_pluginsdir.c_str());
     _pluginsdir = dir;
     
     return scanAndLoad(obj);
 }
 
 bool
-Extension::scanAndLoad(as_object &obj)
+Extension::scanAndLoad(as_object& where)
 {
 //    GNASH_REPORT_FUNCTION;
-    string mod;
+    std::string mod;
     
-    if (_modules.size() == 0) {
+    if (_modules.empty()) {
         scanDir(_pluginsdir);
     }
     
-    vector<string>::iterator it;
+    std::vector<std::string>::iterator it;
     for (it = _modules.begin(); it != _modules.end(); it++) {
         mod = *(it);
-        log_security(_("Loading module: %s"), mod.c_str());
-        SharedLib sl;
-        initModule(mod.c_str(), obj);
+        log_security(_("Loading module: %s"), mod);
+        initModule(mod, where);
     }   
                return true;
 }
 
 bool
-Extension::initModule(const char *module, as_object &obj)
+Extension::initModule(const std::string& module, as_object &where)
 {
-//    GNASH_REPORT_FUNCTION;
 
-    SharedLib::initentry *symptr;
     SharedLib *sl;
-    string symbol;
+    std::string symbol(module);
 
-    log_security(_("Initializing module: \"%s\""), module);
+    log_security(_("Initializing module: \"%s\""), symbol);
     
-    symbol = module;
     if (_plugins[module] == 0) {
         sl = new SharedLib(module);
         sl->openLib();
@@ -148,11 +144,12 @@
         sl = _plugins[module];
     }
     
-    symbol += "_class_init";
-    symptr = sl->getInitEntry(symbol.c_str());
+    symbol.append("_class_init");
+    
+    SharedLib::initentry *symptr = sl->getInitEntry(symbol);
 
     if (symptr) {    
-        symptr(obj);
+        symptr(where);
     } else {
         log_error(_("Couldn't get class_init symbol"));
     }
@@ -161,7 +158,7 @@
 }
 
 bool
-Extension::initModuleWithFunc(const char *module, const char *func,
+Extension::initModuleWithFunc(const std::string& module, const std::string& 
func,
        as_object &obj)
 {
        SharedLib::initentry *symptr;
@@ -193,11 +190,11 @@
 {
 //    GNASH_REPORT_FUNCTION;
     scanDir(_pluginsdir);
-               return true;
+       return true;
 }
 
 bool
-Extension::scanDir(const char *dirlist)
+Extension::scanDir(const std::string& dirlist)
 {
 //    GNASH_REPORT_FUNCTION;
     
@@ -210,9 +207,9 @@
 
 //    scoped_lock lock(lib_mutex);
 
-    dirlistcopy = strdup(dirlist);
+    dirlistcopy = strdup(dirlist.c_str());
     
-    dir = strtok(dirlistcopy, ":");
+    dir = std::strtok(dirlistcopy, ":");
     if (dir == NULL) {
         dir = dirlistcopy;
     }
@@ -237,18 +234,18 @@
                 continue;
             }
 
-            if (strncmp(entry->d_name, ".", 1) == 0) {
+            if (std::strncmp(entry->d_name, ".", 1) == 0) {
                 continue;
             }            
             
-            suffix = strrchr(entry->d_name, '.');
+            suffix = std::strrchr(entry->d_name, '.');
             if (suffix == 0) {
                 continue;
             }
 
             log_debug(_("Gnash Plugin name: %s"), entry->d_name);
             
-            if (strcmp(suffix, ".so") == 0) {
+            if (std::strcmp(suffix, ".so") == 0) {
                 *suffix = 0;
                 log_debug(_("Gnash Plugin name: %s"), entry->d_name);
                 _modules.push_back(entry->d_name);
@@ -260,7 +257,7 @@
         if (closedir(library_dir) != 0) {
             return false;
         }
-        dir = strtok(NULL, ":");
+        dir = std::strtok(NULL, ":");
     }
        return true;
 }
@@ -270,10 +267,10 @@
 {
     GNASH_REPORT_FUNCTION;
     
-    cerr << _modules.size() << " plugin(s) for Gnash installed" << endl;    
-    vector<string>::iterator it;
+    std::cerr << _modules.size() << " plugin(s) for Gnash installed" << 
std::endl;    
+    std::vector<std::string>::iterator it;
     for (it = _modules.begin(); it != _modules.end(); it++) {
-        cerr << "Module name is: \"" << *(it) << "\"" << endl;
+        std::cerr << "Module name is: \"" << *(it) << "\"" << std::endl;
     }
 }
 

=== modified file 'libbase/extension.h'
--- a/libbase/extension.h       2008-03-27 16:12:31 +0000
+++ b/libbase/extension.h       2008-07-30 07:49:49 +0000
@@ -15,8 +15,8 @@
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-#ifndef __EXTENSION_H__
-#define __EXTENSION_H__
+#ifndef GNASH_EXTENSION_H
+#define GNASH_EXTENSION_H
 
 #include <vector>
 #include <string>
@@ -29,32 +29,62 @@
 class DSOEXPORT Extension
 {
   public:
-//    typedef bool init_func_t (as_object &obj);
+
     Extension();
-    Extension(const char *dir);
+
+    Extension(const std::string& dir);
+
     ~Extension();
-    // scan a directory for Gnash modules
+
+    /// Scan a directory for Gnash modules
     bool scanDir();
-    bool scanDir(const char *dir);
-    // scan the directory and open the module
-    bool scanAndLoad(as_object &obj);
-    bool scanAndLoad(const char *dir, as_object &obj);
-    // open a module
-    // initialize the module within Gnash
-    bool initModule(const char *module, as_object &obj);
+
+    /// Scan the given directory for modules
+    //
+    /// @param dir  The directory to scan.
+    bool scanDir(const std::string& dir);
+
+    /// Scan the plugins directory and attach any found modules to
+    /// the given object.
+    //
+    /// @param where     The as_object to which the modules should be
+    ///                  attached (usually the global object)
+    bool scanAndLoad(as_object &where);
+
+    /// Scan the given directory and attach any found modules to
+    /// the given object.
+    //
+    /// @param where     The as_object to which the modules should be
+    ///                  attached (usually the global object)
+    /// @param dir       A directory to scan.
+    bool scanAndLoad(const std::string& dir, as_object &where);
+
        // open a module, initialize the module within Gnash. Known function 
name.
-       bool initModuleWithFunc(const char *module, const char *func, as_object 
&obj);
+       bool initModuleWithFunc(const std::string& module, const std::string& 
func, as_object &obj);
     bool initNewObject(as_object &obj);
     void dumpModules();
 private:
+
+    /// Initialize the named module within Gnash
+    //
+    /// @param symbol   The name of the module to find and
+    ///                 initialize.
+    /// @param obj      The object to attach the module to.
+    bool initModule(const std::string& module, as_object &obj);
+
+    /// A list of modules
     std::vector<std::string> _modules;
-    std::map<const char *, SharedLib *> _plugins;
-    const char *_pluginsdir;
+    
+    /// A map of loaded modules
+    std::map<std::string, SharedLib *> _plugins;
+    
+    /// The default directory to search for modules.
+    std::string _pluginsdir;
 };
 
 } // end of gnash namespace
 
-// __EXTENSION_H__
+// GNASH_EXTENSION_H
 #endif
 
 // Local Variables:

=== modified file 'libbase/sharedlib.cpp'
--- a/libbase/sharedlib.cpp     2008-05-27 15:54:46 +0000
+++ b/libbase/sharedlib.cpp     2008-07-30 07:50:17 +0000
@@ -42,49 +42,13 @@
 #include <boost/thread/mutex.hpp>
 
 #if defined(_WIN32) || defined(WIN32)
-//Get boost !
-//# define lock(lib_mutex) ;
-//# define scoped_lock ;
 #      define PLUGINSDIR "./"
 #endif
 
-typedef boost::mutex::scoped_lock scoped_lock;
-static boost::mutex lib_mutex;
-
-using namespace std;
 
 namespace gnash {
 
-#ifdef LT_DLMUTEX
-//static void
-//gnash_mutex_seterror (void)
-//{
-//    GNASH_REPORT_FUNCTION;
-//}
-//
-//static const char *
-//gnash_mutex_geterror (void)
-//{
-//    GNASH_REPORT_FUNCTION;
-//    return NULL;
-//}
-
-void
-gnash_mutex_lock (void)
-{
-    GNASH_REPORT_FUNCTION;
-}
-
-void
-gnash_mutex_unlock (void)
-{
-    GNASH_REPORT_FUNCTION;
-}
-
-#endif
-
-SharedLib::SharedLib() 
-    : _filespec(0)
+SharedLib::SharedLib()
 {
 //    GNASH_REPORT_FUNCTION;
 #ifdef LT_DLMUTEX
@@ -93,7 +57,7 @@
 #endif
 }
 
-SharedLib::SharedLib(const char *filespec)
+SharedLib::SharedLib(const std::string& filespec)
 {
 //    GNASH_REPORT_FUNCTION;
 #ifdef LT_DLMUTEX
@@ -101,7 +65,7 @@
 //                                 gnash_mutex_seterror, gnash_mutex_geterror);
 #endif
     _filespec = filespec;
-    scoped_lock lock(lib_mutex);
+    scoped_lock lock(_libMutex);
     
     // Initialize libtool's dynamic library loader
     int errors = lt_dlinit ();
@@ -110,13 +74,13 @@
 //     } else {
 //         log_debug ("Initialized ltdl");
     }
-    const char *pluginsdir = PLUGINSDIR;
+    std::string pluginsdir = PLUGINSDIR;
+    
     char *env = std::getenv ("GNASH_PLUGINS");
-    if (env != NULL)
-        pluginsdir = env;
+    if (env) pluginsdir = env;
    
 
-    lt_dlsetsearchpath(pluginsdir);
+    lt_dlsetsearchpath(pluginsdir.c_str());
 }
 
 SharedLib::~SharedLib()
@@ -139,13 +103,7 @@
 }
 
 bool
-SharedLib::openLib (std::string &filespec)
-{
-    return openLib(filespec.c_str());
-}
-
-bool
-SharedLib::openLib (const char *filespec)
+SharedLib::openLib (const std::string& filespec)
 {
 //    GNASH_REPORT_FUNCTION;
     
@@ -159,7 +117,7 @@
     // Make sure preloaded modules are initialised
 //  LTDL_SET_PRELOADED_SYMBOLS();
     
-    scoped_lock lock(lib_mutex);
+    scoped_lock lock(_libMutex);
     
 //     // libtool's dynamic library loader is already initialized in 
constructor
     
@@ -167,7 +125,7 @@
 //          << "for database drivers" << endl;
 
 //    log_debug ("Trying to open shared library \"%s\"", filespec);
-    _dlhandle = lt_dlopenext (filespec);
+    _dlhandle = lt_dlopenext (filespec.c_str());
     
     if (_dlhandle == NULL) {
         log_error ("%s", lt_dlerror());
@@ -190,26 +148,19 @@
 #ifdef WIN32
        return NULL;    //TODO, hack
 #else
-       return basename(const_cast<char *>(_filespec));
+       return basename(const_cast<char *>(_filespec.c_str()));
 #endif
 }
 
-SharedLib::entrypoint *
-SharedLib::getDllSymbol (std::string &symbol)
-{
-    GNASH_REPORT_FUNCTION;
-    return getDllSymbol(symbol.c_str());
-}
-
 SharedLib::initentry *
-SharedLib::getInitEntry (const char *symbol)
+SharedLib::getInitEntry (const std::string& symbol)
 {
 //    GNASH_REPORT_FUNCTION;
     lt_ptr run = NULL;
     
-    scoped_lock lock(lib_mutex);
+    scoped_lock lock(_libMutex);
 
-    run  = lt_dlsym (_dlhandle, symbol);
+    run  = lt_dlsym (_dlhandle, symbol.c_str());
     
     if (run == NULL) {
         log_error (_("Couldn't find symbol: %s"), symbol);
@@ -218,19 +169,19 @@
         log_debug (_("Found symbol %s @ %p"), symbol, (void *)run);
     }
     
-    return (initentry *)run;
+    return (initentry*)(run);
 }
 
 SharedLib::entrypoint *
-SharedLib::getDllSymbol(const char *symbol)
+SharedLib::getDllSymbol(const std::string& symbol)
 {
     GNASH_REPORT_FUNCTION;
     
     lt_ptr run = NULL;
     
-    scoped_lock lock(lib_mutex);
+    scoped_lock lock(_libMutex);
 
-    run  = lt_dlsym (_dlhandle, symbol);
+    run  = lt_dlsym (_dlhandle, symbol.c_str());
     
     /* 
     Realistically, we should never get a valid pointer with a value of 0
@@ -243,7 +194,7 @@
         log_debug (_("Found symbol %s @ %p"), symbol, (void *)run);
     }
     
-    return (entrypoint *)run;
+    return (entrypoint*)(run);
 }
 
 // Get information about the DLL

=== modified file 'libbase/sharedlib.h'
--- a/libbase/sharedlib.h       2008-03-22 02:36:56 +0000
+++ b/libbase/sharedlib.h       2008-07-30 07:50:17 +0000
@@ -15,22 +15,24 @@
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-#ifndef __SHAREDLIB_H__
-#define __SHAREDLIB_H__
+#ifndef GNASH_SHAREDLIB_H
+#define GNASH_SHAREDLIB_H
 
 #ifdef HAVE_CONFIG_H
 #include "gnashconfig.h"
 #endif
 
+#include <boost/thread/mutex.hpp>
+#include <string>
+#include <map>
+#include "as_object.h"
+
 #ifdef _WIN32
 #undef DLL_EXPORT
 #define LIBLTDL_DLL_IMPORT 1
 #endif
 
-#include <string>
-#include <map>
 #include <ltdl.h>
-#include "as_object.h"
 
 // Used on Darwin for basename
 #ifdef HAVE_LIBGEN_H
@@ -44,42 +46,45 @@
 /// TODO: document this class
 class SharedLib
 {
+    typedef boost::mutex::scoped_lock scoped_lock;
+
 public:
     // Typedefs for function pointers to keep the code readable
     typedef bool entrypoint (void *obj);
     typedef void initentry (as_object &obj);
     
     SharedLib();
-    SharedLib(const char *filespec);
+    SharedLib(const std::string& filespec);
     ~SharedLib();
+
     bool openLib();
-    bool openLib(std::string &filespec);
-    bool openLib(const char *filespec);
-    bool closeLib();
+    bool openLib(const std::string &filespec);
     
     // Get a C symbol from the shared library based on the name
-    entrypoint *getDllSymbol (std::string &name);
-    entrypoint *getDllSymbol (const char *name);
-    initentry *getInitEntry (const char *name);
+    entrypoint *getDllSymbol (const std::string& symbol);
+    initentry *getInitEntry (const std::string& symbol);
 
     // Extract file info from the shared library
     const char *getDllFileName();
     const char *getDllModuleName();
     int getDllRefCount();
     const char *moduleName();
-//    lt_dlhandle getDllHandle { return _dlhandle; }
-    const char *getFilespec() { return _filespec; };
+
+    const std::string& getFilespec() { return _filespec; };
     
     
 private:
+
+    bool closeLib();
+
     lt_dlhandle _dlhandle;
-    const char *_filespec;
-    const char *_pluginsdir;    
+    std::string _filespec;
+    boost::mutex _libMutex;    
 };
 
 } // end of gnash namespace
 
-// __SHAREDLIB_H__
+// GNASH_SHAREDLIB_H
 #endif
 
 // Local Variables:

=== modified file 'libcore/asobj/ClassHierarchy.cpp'
--- a/libcore/asobj/ClassHierarchy.cpp  2008-07-21 09:56:09 +0000
+++ b/libcore/asobj/ClassHierarchy.cpp  2008-07-30 07:51:07 +0000
@@ -118,8 +118,8 @@
                                return super;
                        }
                }
-               if 
(mExtension->initModuleWithFunc(mDeclaration.file_name.c_str(),
-                       mDeclaration.init_name.c_str(), *mTarget))
+               if (mExtension->initModuleWithFunc(mDeclaration.file_name,
+                       mDeclaration.init_name, *mTarget))
                {
                        // Successfully loaded it, now find it, set its proto, 
and return.
                        as_value us;


reply via email to

[Prev in Thread] Current Thread [Next in Thread]