Ticket #978: osync_plugin_env_free_removed.diff

File osync_plugin_env_free_removed.diff, 6.2 KB (added by ianmartin, 3 years ago)

Patch to add reference counting to osync_plugin_env

  • wrapper/opensync-plugin.i

     
    106106        } 
    107107 
    108108        ~PluginEnv() { 
    109                 osync_plugin_env_free(self); 
     109                osync_plugin_env_unref(self); 
    110110        } 
    111111 
    112112        void load(const char *path = NULL) { 
  • opensync/plugin/opensync_plugin_env_private.h

     
    2727        GList *modules; 
    2828         
    2929        GModule *current_module; 
     30 
     31        int ref_count; 
    3032}; 
    3133 
    3234#endif /* _OPENSYNC_PLUGIN_ENV_PRIVATE_H_ */ 
  • opensync/plugin/opensync_plugin_env.c

     
    3737                return NULL; 
    3838        } 
    3939         
     40        env->ref_count = 1; 
    4041        osync_trace(TRACE_EXIT, "%s: %p", __func__, env); 
    4142        return env; 
    4243} 
    4344 
    44 void osync_plugin_env_free(OSyncPluginEnv *env) 
     45 
     46OSyncPluginEnv *osync_plugin_env_ref(OSyncPluginEnv *env) 
    4547{ 
    4648        osync_trace(TRACE_ENTRY, "%s(%p)", __func__, env); 
    4749        osync_assert(env); 
     50 
     51        g_atomic_int_inc(&(env->ref_count)); 
     52 
     53        osync_trace(TRACE_EXIT, "%s", __func__); 
     54        return env; 
     55 
     56} 
     57 
     58void osync_plugin_env_unref(OSyncPluginEnv *env) 
     59{ 
     60        osync_trace(TRACE_ENTRY, "%s(%p)", __func__, env); 
     61        osync_assert(env); 
    4862         
    49         /* Free the plugins */ 
    50         while (env->plugins) { 
    51                 osync_plugin_unref(env->plugins->data); 
    52                 env->plugins = g_list_remove(env->plugins, env->plugins->data); 
    53         } 
     63        if (g_atomic_int_dec_and_test(&(env->ref_count))) { 
     64                /* Free the plugins */ 
     65                while (env->plugins) { 
     66                        osync_plugin_unref(env->plugins->data); 
     67                        env->plugins = g_list_remove(env->plugins, env->plugins->data); 
     68                } 
    5469         
    55         /* Unload all modules */ 
    56         while (env->modules) { 
    57                 osync_module_unload(env->modules->data); 
    58                 osync_module_free(env->modules->data); 
    59                 env->modules = g_list_remove(env->modules, env->modules->data); 
     70                /* Unload all modules */ 
     71                while (env->modules) { 
     72                        osync_module_unload(env->modules->data); 
     73                        osync_module_free(env->modules->data); 
     74                        env->modules = g_list_remove(env->modules, env->modules->data); 
     75                } 
     76                osync_free(env); 
    6077        } 
    6178         
    62         g_free(env); 
    63          
    6479        osync_trace(TRACE_EXIT, "%s", __func__); 
    6580} 
    6681 
  • opensync/plugin/opensync_plugin_env.h

     
    4747 */ 
    4848OSYNC_EXPORT OSyncPluginEnv *osync_plugin_env_new(OSyncError **error); 
    4949 
    50 /*! @brief Frees a osync environment 
     50/*! @brief Increases the reference counton an opensync plugin environment 
    5151 *  
    52  * Frees a osync environment and all resources. 
     52 * The reference count on the OSyncPluginEnv is incremented. When the 
     53 * reference is no longer needed it should be removed with  
     54 * osync_plugin_env_unref 
    5355 *  
    54  * @param env Pointer to the environment to free 
     56 * @returns The environment passed in 
    5557 *  
    5658 */ 
    57 OSYNC_EXPORT void osync_plugin_env_free(OSyncPluginEnv *env); 
     59OSYNC_EXPORT OSyncPluginEnv *osync_plugin_env_ref(OSyncPluginEnv *env); 
    5860 
     61/*! @brief Removes a reference to an OSyncPluginEnv 
     62 *  
     63 * Decrements the reference count on an osync plugin environment.  If 
     64 * the reference count reaches zero the environment is freed and all 
     65 * resources are unreferenced 
     66 *  
     67 * @param env Pointer to the environment to unreference 
     68 *  
     69 */ 
     70OSYNC_EXPORT void osync_plugin_env_unref(OSyncPluginEnv *env); 
     71 
    5972/*! @brief Loads the sync modules from a given directory 
    6073 *  
    6174 * Loads all sync modules from a directory into a osync environment 
  • opensync/engine/opensync_engine.c

     
    13011301  } 
    13021302         
    13031303  if (engine->pluginenv) { 
    1304     osync_plugin_env_free(engine->pluginenv); 
     1304    osync_plugin_env_unref(engine->pluginenv); 
    13051305    engine->pluginenv = NULL; 
    13061306  } 
    13071307         
  • opensync/client/opensync_client.c

     
    698698  } 
    699699         
    700700  if (client->plugin_env) { 
    701     osync_plugin_env_free(client->plugin_env); 
     701    osync_plugin_env_unref(client->plugin_env); 
    702702    client->plugin_env = NULL; 
    703703  } 
    704704         
  • tools/osyncplugin.c

     
    429429  osync_format_env_free(format_env); 
    430430  format_env = NULL; 
    431431 error_free_pluginenv: 
    432   osync_plugin_env_free(plugin_env); 
     432  osync_plugin_env_unref(plugin_env); 
    433433  plugin_env = NULL; 
    434434 error:  
    435435  return FALSE; 
     
    12181218  osync_format_env_free(format_env); 
    12191219  format_env = NULL; 
    12201220 error_free_pluginenv: 
    1221   osync_plugin_env_free(plugin_env); 
     1221  osync_plugin_env_unref(plugin_env); 
    12221222  plugin_env = NULL; 
    12231223 error:  
    12241224  return FALSE; 
     
    12551255 
    12561256 success: 
    12571257  if (plugin_env) 
    1258     osync_plugin_env_free(plugin_env); 
     1258    osync_plugin_env_unref(plugin_env); 
    12591259 
    12601260  for (o=cmdlist; o; o = o->next) { 
    12611261    Command *cmd = o->data; 
     
    12721272  finalize_plugin(&plugin_data); 
    12731273  //error_free_plugin_env: 
    12741274  if (plugin_env) 
    1275     osync_plugin_env_free(plugin_env); 
     1275    osync_plugin_env_unref(plugin_env); 
    12761276 
    12771277  for (o=cmdlist; o; o = o->next) { 
    12781278    Command *cmd = o->data; 
  • opensync.sym

     
    561561osync_plugin_connection_usb_set_vendorid 
    562562osync_plugin_discover 
    563563osync_plugin_env_find_plugin 
    564 osync_plugin_env_free 
    565564osync_plugin_env_load 
    566565osync_plugin_env_new 
    567566osync_plugin_env_nth_plugin 
    568567osync_plugin_env_num_plugins 
    569568osync_plugin_env_plugin_is_usable 
     569osync_plugin_env_ref 
    570570osync_plugin_env_register_plugin 
     571osync_plugin_env_unref 
    571572osync_plugin_finalize 
    572573osync_plugin_get_config_type 
    573574osync_plugin_get_data