/soc/2012/sanket/statscollector-2.x.y: 60699b68d0bc: [ENHANCEMEN...

Sanket Agarwal sanket at soc.pidgin.im
Mon Aug 27 13:04:05 EDT 2012


Changeset: 60699b68d0bcc780d5e3fba6fb942ad4c3957602
Author:	 Sanket Agarwal <sanket at soc.pidgin.im>
Date:	 2012-08-27 10:02 +0530
Branch:	 soc.2012.statscollector
URL: http://hg.pidgin.im/soc/2012/sanket/statscollector-2.x.y/rev/60699b68d0bc

Description:

[ENHANCEMENT] Improve memory management and hash table usage

Thanks Ethan (elb) for the patch.

diffstat:

 libpurple/plugins/statscollector.c |  61 +++++++++++++------------------------
 1 files changed, 22 insertions(+), 39 deletions(-)

diffs (185 lines):

diff --git a/libpurple/plugins/statscollector.c b/libpurple/plugins/statscollector.c
--- a/libpurple/plugins/statscollector.c
+++ b/libpurple/plugins/statscollector.c
@@ -48,7 +48,7 @@ enum
 
 /* Version of XML this plugin supports writing */
 
-#define STATS_XML_MAJOR_V "0" /* 0 -- Dev purposes */
+#define STATS_XML_V "0.2" /* 0 -- Dev purposes */
 
 /* POSIX compliance is an issue that I have looked into some detail now
  * It seems like presence of unistd.h and _POSIX_VERSION being defined
@@ -127,7 +127,7 @@ save_xml(){
 
   GHashTableIter iter;
   gpointer key, value;
-  xmlnode *nroot, *nacc, *nplugins, *nuis, *node;
+  xmlnode *nroot, *nacc, *nplugins, *nuis;
   char *data, *version;
 
   nroot = xmlnode_new("stats");
@@ -137,10 +137,10 @@ save_xml(){
   xmlnode_set_attrib(nroot, "version", version);
 
   /* Load CPUinfo strucutre */
-  xmlnode_insert_child(nroot, cpuinfo_xml);
+  xmlnode_insert_child(nroot, xmlnode_copy(cpuinfo_xml));
 
   /* Load UI info */
-  xmlnode_insert_child(nroot, ui_info);
+  xmlnode_insert_child(nroot, xmlnode_copy(ui_info));
 
   nacc = xmlnode_new_child(nroot, "accounts");
   nplugins = xmlnode_new_child(nroot, "plugins");
@@ -151,33 +151,26 @@ save_xml(){
   g_hash_table_iter_init(&iter, stats_acc_ht);
 
   while(g_hash_table_iter_next(&iter, &key, &value)){
-
-    node = xmlnode_from_str(value, -1);
-    xmlnode_insert_child(nacc, node);
-
+    xmlnode_insert_child(nacc, xmlnode_copy((xmlnode *)value));
   }
 
 
   g_hash_table_iter_init(&iter, stats_plugins_ht);
 
   while(g_hash_table_iter_next(&iter, &key, &value)){
-
-    node = xmlnode_from_str(value, -1);
-    xmlnode_insert_child(nplugins, node);
-
+    xmlnode_insert_child(nplugins, xmlnode_copy((xmlnode *)value));
   }
 
   g_hash_table_iter_init(&iter, stats_uis_ht);
 
   while(g_hash_table_iter_next(&iter, &key, &value)){
-
-    node = xmlnode_from_str(value, -1);
-    xmlnode_insert_child(nuis, node);
-
+    xmlnode_insert_child(nuis, xmlnode_copy((xmlnode *)value));
   }
 
   data = xmlnode_to_formatted_str(nroot, NULL);
   purple_util_write_data_to_file("stats.xml", data, -1);
+  xmlnode_free(nroot);
+  g_free(data);
 
 }
 
@@ -655,7 +648,7 @@ get_ui_info(){
 
   GHashTable *ui_info_ht;
   xmlnode *ui_info_xml, *ui_version_xml;
-  char *ui_name, *ui_version, *data;
+  char *ui_name, *ui_version;
 
   ui_info_ht = purple_core_get_ui_info();
 
@@ -678,8 +671,8 @@ get_ui_info(){
   xmlnode_insert_data(ui_version_xml, ui_version, -1);
   xmlnode_insert_child(ui_info_xml, ui_version_xml);
 
-  data = xmlnode_to_str(ui_info_xml, NULL);
-  g_hash_table_insert(stats_uis_ht, (void *)ui_name, (void *)data);
+  g_hash_table_insert(stats_uis_ht, (void *)ui_name,
+      xmlnode_copy(ui_info_xml));
 
   schedule_stats_save();
 
@@ -732,7 +725,7 @@ acc_sign_on_event(PurpleAccount *account
    * 1.1 If no, then add the account to the list of accounts used
    */
 
-  const char *username, *protocol, *data, *connect_server="";
+  const char *username, *protocol, *connect_server="";
   char *id, *username_dup;
   xmlnode *acc, *p_node, *len_node, *connect_server_node;
   int len;
@@ -822,12 +815,12 @@ acc_sign_on_event(PurpleAccount *account
       (purple_account_get_protocol_id(account)))->user_splits;
   if(user_splits != NULL){
   }
-  data = xmlnode_to_str(acc, NULL);
+
   /* check if the account already exist in our XML file */
   if(g_hash_table_lookup(stats_acc_ht, id)){
-    g_hash_table_replace(stats_acc_ht, (void *)id, (void *)data);
+    g_hash_table_replace(stats_acc_ht, id, (void *)acc);
   } else {
-    g_hash_table_insert(stats_acc_ht, (void *)id, (void *)data);
+    g_hash_table_insert(stats_acc_ht, id, (void *)acc);
   }
   schedule_stats_save();
 }
@@ -841,7 +834,7 @@ plugin_load_event(PurplePlugin *plugin){
    * 1.1 If no, then add the account to the list of accounts used
    */
 
-  const char *id, *name, *version, *author, *summary, *description, *data;
+  const char *id, *name, *version, *author, *summary, *description;
   xmlnode *plugin_xml;
   PurplePluginType plugin_type;
 
@@ -888,13 +881,9 @@ plugin_load_event(PurplePlugin *plugin){
     /* xmlnode_insert_data(description_xml, description, -1); */
     /* xmlnode_insert_child(plugin_xml, description_xml); */
 
-    data = xmlnode_to_str(plugin_xml, NULL);
-    g_hash_table_insert(stats_plugins_ht, (void *)id, (void *)data);
-
+    g_hash_table_insert(stats_plugins_ht, (void *)id, (void *)plugin_xml);
   }
-
   schedule_stats_save();
-
 }
 
 static void
@@ -995,7 +984,7 @@ schedule_send(){
 }
 
 static gboolean
-check_version(char *v){
+check_version(const char *v){
 
   /* Checks if the current stats.xml
    * version matches plugin version
@@ -1074,30 +1063,24 @@ init_stats(){
 
       purple_debug_info("STATS","%s %s", id_node, xmlnode_to_formatted_str(start, NULL));
       g_hash_table_insert(stats_acc_ht, (gpointer)id_node,
-          (char *)xmlnode_to_formatted_str(start, NULL));
+          xmlnode_copy(start));
 
     }
 
     start = xmlnode_get_child(stats_plugins_xml, "plugin");
 
     for(;start;start = xmlnode_get_next_twin(start)){
-
       id_node = xmlnode_get_attrib((const xmlnode *)start, "id");
-
       g_hash_table_insert(stats_plugins_ht, (gpointer)id_node,
-          (char *)xmlnode_to_formatted_str(start, NULL));
-
+          xmlnode_copy(start));
     }
 
     start = xmlnode_get_child(stats_uis_xml, "ui");
 
     for(;start;start = xmlnode_get_next_twin(start)){
-
       id_node = xmlnode_get_attrib((const xmlnode *)start, "id");
-
       g_hash_table_insert(stats_uis_ht, (gpointer)id_node,
-          (char *)xmlnode_to_formatted_str(start, NULL));
-
+          xmlnode_copy(start));
     }
 
 



More information about the Commits mailing list