New function that now holds the main logic of the old camel_pstring_strdup
authorJeffrey Stedfast <fejj@novell.com>
Thu, 6 Jul 2006 19:43:47 +0000 (19:43 +0000)
committerJeffrey Stedfast <fejj@src.gnome.org>
Thu, 6 Jul 2006 19:43:47 +0000 (19:43 +0000)
2006-07-06  Jeffrey Stedfast  <fejj@novell.com>

* camel-string-utils.c (camel_pstring_add): New function that now
holds the main logic of the old camel_pstring_strdup function. If
'own' is TRUE, re-use the memory if the string doesn't already
exist and free it otherwise. If FALSE, strdup the value if it
doesn't already exist.
(camel_pstring_strdup): Calls camel_pstring_add() with 'own' as
FALSE.

* camel-folder-summary.c (message_info_new_from_header): Use
camel_pstring_add instead of camel_pstring_strdup here to prevent
unnecessary strdup/freeing.
(message_info_load): Same.

camel/ChangeLog
camel/camel-folder-summary.c
camel/camel-string-utils.c
camel/camel-string-utils.h

index 67ae870..83decf1 100644 (file)
@@ -1,3 +1,18 @@
+2006-07-06  Jeffrey Stedfast  <fejj@novell.com>
+
+       * camel-string-utils.c (camel_pstring_add): New function that now
+       holds the main logic of the old camel_pstring_strdup function. If
+       'own' is TRUE, re-use the memory if the string doesn't already
+       exist and free it otherwise. If FALSE, strdup the value if it
+       doesn't already exist.
+       (camel_pstring_strdup): Calls camel_pstring_add() with 'own' as
+       FALSE.
+
+       * camel-folder-summary.c (message_info_new_from_header): Use
+       camel_pstring_add instead of camel_pstring_strdup here to prevent
+       unnecessary strdup/freeing.
+       (message_info_load): Same.
+
 2006-06-26  Jeffrey Stedfast  <fejj@novell.com>
 
        * camel-gpg-context.c (gpg_ctx_parse_status): Only set name to ""
index 1a72898..4f61c60 100644 (file)
@@ -1624,19 +1624,13 @@ message_info_new_from_header(CamelFolderSummary *s, struct _camel_header_raw *h)
 
        if (ct)
                camel_content_type_unref(ct);
-
-       mi->subject = camel_pstring_strdup(subject);
-       mi->from = camel_pstring_strdup(from);
-       mi->to = camel_pstring_strdup(to);
-       mi->cc = camel_pstring_strdup(cc);
-       mi->mlist = camel_pstring_strdup(mlist);
-
-       g_free(subject);
-       g_free(from);
-       g_free(to);
-       g_free(cc);
-       g_free(mlist);
-
+       
+       mi->subject = camel_pstring_add (subject, TRUE);
+       mi->from = camel_pstring_add (from, TRUE);
+       mi->to = camel_pstring_add (to, TRUE);
+       mi->cc = camel_pstring_add (cc, TRUE);
+       mi->mlist = camel_pstring_add (mlist, TRUE);
+       
        mi->user_flags = NULL;
        mi->user_tags = NULL;
        mi->date_sent = camel_header_decode_date(camel_header_raw_find(&h, "date", NULL), NULL);
@@ -1709,20 +1703,14 @@ message_info_load(CamelFolderSummary *s, FILE *in)
        camel_file_util_decode_string(in, &to);
        camel_file_util_decode_string(in, &cc);
        camel_file_util_decode_string(in, &mlist);
-
+       
        mi->uid = uid;
-       mi->subject = camel_pstring_strdup(subject);
-       mi->from = camel_pstring_strdup(from);
-       mi->to = camel_pstring_strdup(to);
-       mi->cc = camel_pstring_strdup(cc);
-       mi->mlist = camel_pstring_strdup(mlist);
-
-       g_free(subject);
-       g_free(from);
-       g_free(to);
-       g_free(cc);
-       g_free(mlist);
-
+       mi->subject = camel_pstring_add (subject, TRUE);
+       mi->from = camel_pstring_add (from, TRUE);
+       mi->to = camel_pstring_add (to, TRUE);
+       mi->cc = camel_pstring_add (cc, TRUE);
+       mi->mlist = camel_pstring_add (mlist, TRUE);
+       
        mi->content = NULL;
 
        camel_file_util_decode_fixed_int32(in, &mi->message_id.id.part.hi);
index 1825ee1..b1f9029 100644 (file)
@@ -146,46 +146,72 @@ static pthread_mutex_t pstring_lock = PTHREAD_MUTEX_INITIALIZER;
 static GHashTable *pstring_table = NULL;
 
 /**
- * camel_pstring_strdup:
- * @s: String to copy.
- * 
- * Create a new pooled string entry for the string @s.  A pooled
- * string is a table where common strings are uniquified to the same
- * pointer value.  They are also refcounted, so freed when no longer
- * in use.  In a thread-safe manner.
- * 
+ * camel_pstring_add:
+ * @str: string to add to the string pool
+ * @own: whether the string pool will own the memory pointed to by @s if @str is not yet in the pool
+ *
+ * Add the string to the pool.
+ *
  * The NULL and empty strings are special cased to constant values.
  *
  * Return value: A pointer to an equivalent string of @s.  Use
  * camel_pstring_free() when it is no longer needed.
  **/
-const char *camel_pstring_strdup(const char *s)
+const char *
+camel_pstring_add (char *str, gboolean own)
 {
-       char *p;
        void *pcount;
+       char *pstr;
        int count;
-
+       
        if (s == NULL)
                return NULL;
-       if (s[0] == 0)
+       
+       if (s[0] == '\0') {
+               if (own)
+                       g_free (str);
                return "";
-
-       pthread_mutex_lock(&pstring_lock);
+       }
+       
+       pthread_mutex_lock (&pstring_lock);
        if (pstring_table == NULL)
-               pstring_table = g_hash_table_new(g_str_hash, g_str_equal);
-
-       if (g_hash_table_lookup_extended(pstring_table, s, (void **)&p, &pcount)) {
-               count = GPOINTER_TO_INT(pcount)+1;
-               g_hash_table_insert(pstring_table, p, GINT_TO_POINTER(count));
+               pstring_table = g_hash_table_new (g_str_hash, g_str_equal);
+       
+       if (g_hash_table_lookup_extended (pstring_table, str, (void **) &pstr, &pcount)) {
+               count = GPOINTER_TO_INT (pcount) + 1;
+               g_hash_table_insert (pstring_table, pstr, GINT_TO_POINTER (count));
        } else {
-               p = g_strdup(s);
-               g_hash_table_insert(pstring_table, p, GINT_TO_POINTER(1));
+               pstr = own ? str : g_strdup (str);
+               g_hash_table_insert (pstring_table, pstr, GINT_TO_POINTER (1));
        }
-       pthread_mutex_unlock(&pstring_lock);
+       
+       pthread_mutex_unlock (&pstring_lock);
+       
+       return pstr;
+}
 
-       return p;
+
+/**
+ * camel_pstring_strdup:
+ * @s: String to copy.
+ * 
+ * Create a new pooled string entry for the string @s.  A pooled
+ * string is a table where common strings are uniquified to the same
+ * pointer value.  They are also refcounted, so freed when no longer
+ * in use.  In a thread-safe manner.
+ * 
+ * The NULL and empty strings are special cased to constant values.
+ *
+ * Return value: A pointer to an equivalent string of @s.  Use
+ * camel_pstring_free() when it is no longer needed.
+ **/
+const char *
+camel_pstring_strdup (const char *s)
+{
+       return camel_pstring_add ((char *) s, FALSE);
 }
 
+
 /**
  * camel_pstring_free:
  * @s: String to free.
@@ -194,7 +220,8 @@ const char *camel_pstring_strdup(const char *s)
  *
  * NULL and the empty string are special cased.
  **/
-void camel_pstring_free(const char *s)
+void
+camel_pstring_free(const char *s)
 {
        char *p;
        void *pcount;
index 5909db0..51dbd79 100644 (file)
@@ -42,6 +42,7 @@ const char *camel_strdown (char *str);
 char camel_tolower(char c);
 char camel_toupper(char c);
 
+const char *camel_pstring_add (char *str, gboolean own);
 const char *camel_pstring_strdup(const char *s);
 void camel_pstring_free(const char *s);