Re: [evolution-patches] Memory improvement for the tokens in camel_folder_summary.c



On Wed, 2006-07-26 at 06:22 -0600, Veerapuram Varadhan wrote:
> On Wed, 2006-07-26 at 10:40 +0000, Philip Van Hoof wrote:
> 
> As pointed out in IRC, when a string that is not in static tokens table,
> but, exists in pstring-table, this patch would leak that *string*.
> 
> Phillip: How about an updated patch with discussed changes? (by removing
> camel_pstring_*)... which will more or less look like the current
> implementation, however, saves some *g_strdups*.

Sure, like this?



-- 
Philip Van Hoof, software developer at x-tend 
home: me at pvanhoof dot be 
gnome: pvanhoof at gnome dot org 
work: vanhoof at x-tend dot be 
http://www.pvanhoof.be - http://www.x-tend.be
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/evolution-data-server/ChangeLog,v
retrieving revision 1.407
diff -u -p -r1.407 ChangeLog
--- ChangeLog	10 Jul 2006 14:23:55 -0000	1.407
+++ ChangeLog	26 Jul 2006 12:34:03 -0000
@@ -1,3 +1,8 @@
+2006-07-20  Philip Van Hoof  <pvanhoof gnome org>
+
+	* camel/camel-folder-summary.c: Better handling of
+	memory for tokens
+
 2006-07-10  Harish Krishnaswamy  <kharish novell com>
 
 	* configure.in, NEWS: EDS 1.7.4 release updates
Index: camel/camel-folder-summary.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/camel/camel-folder-summary.c,v
retrieving revision 1.149
diff -u -p -r1.149 camel-folder-summary.c
--- camel/camel-folder-summary.c	6 Jul 2006 19:43:46 -0000	1.149
+++ camel/camel-folder-summary.c	26 Jul 2006 12:34:10 -0000
@@ -1242,6 +1242,42 @@ static char * tokens[] = {
 
 #define tokens_len (sizeof(tokens)/sizeof(tokens[0]))
 
+
+guint bytes;
+static gchar* 
+token_add (gchar *str)
+{
+	int i;
+
+	if (!str)
+		return NULL;
+
+	for (i=0; i<tokens_len; i++)
+		if (!strcmp (str, tokens[i]))
+		{
+			g_free (str);
+			return tokens[i];
+		}
+
+	return str;
+}
+
+
+static void
+token_free (gchar *token)
+{
+        gint i;
+
+	if (!token)
+		return;
+
+        for (i = 0; i < tokens_len; i ++)
+                if (tokens[i] == token)
+                        return;
+
+        g_free (token);
+}
+
 /* baiscally ...
     0 = null
     1-tokens_len == tokens[id-1]
@@ -1339,7 +1375,7 @@ camel_folder_summary_decode_token(FILE *
 		if (len <= 0) {
 			ret = NULL;
 		} else if (len<= tokens_len) {
-			ret = g_strdup(tokens[len-1]);
+			ret = tokens[len-1];
 		} else {
 			io(printf ("Invalid token encountered: %d", len));
 			*str = NULL;
@@ -1845,10 +1881,10 @@ content_info_new_from_header(CamelFolder
 	ci = camel_folder_summary_content_info_new (s);
 	
 	charset = e_iconv_locale_charset ();
-	ci->id = camel_header_msgid_decode (camel_header_raw_find (&h, "content-id", NULL));
-	ci->description = camel_header_decode_string (camel_header_raw_find (&h, "content-description", NULL), charset);
-	ci->encoding = camel_content_transfer_encoding_decode (camel_header_raw_find (&h, "content-transfer-encoding", NULL));
-	ci->type = camel_content_type_decode(camel_header_raw_find(&h, "content-type", NULL));
+	ci->id = token_add (camel_header_msgid_decode (camel_header_raw_find (&h, "content-id", NULL)));
+	ci->description = token_add (camel_header_decode_string (camel_header_raw_find (&h, "content-description", NULL), charset));
+	ci->encoding = token_add (camel_content_transfer_encoding_decode (camel_header_raw_find (&h, "content-transfer-encoding", NULL)));
+	ci->type = token_add (camel_content_type_decode(camel_header_raw_find(&h, "content-type", NULL)));
 
 	return ci;
 }
@@ -1937,9 +1973,9 @@ static void
 content_info_free(CamelFolderSummary *s, CamelMessageContentInfo *ci)
 {
 	camel_content_type_unref(ci->type);
-	g_free(ci->id);
-	g_free(ci->description);
-	g_free(ci->encoding);
+	token_free (ci->id);
+	token_free (ci->description);
+	token_free (ci->encoding);
 	e_memchunk_free(s->content_info_chunks, ci);
 }
 


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