+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 ""
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);
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);
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.
*
* 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;