changeset 9508:094ad127d132 HEAD

Added reference counting to struct mail_keywords and related APIs.
author Timo Sirainen <tss@iki.fi>
date Mon, 22 Jun 2009 17:45:56 -0400
parents d7e09c3eaace
children 15e427021619
files src/imap/cmd-append.c src/imap/cmd-copy.c src/imap/cmd-store.c src/lib-index/mail-index-sync.c src/lib-index/mail-index-transaction.c src/lib-index/mail-index.h src/lib-lda/mail-deliver.c src/lib-storage/index/cydir/cydir-storage.c src/lib-storage/index/dbox/dbox-storage.c src/lib-storage/index/dbox/dbox-sync-rebuild.c src/lib-storage/index/index-storage.c src/lib-storage/index/index-storage.h src/lib-storage/index/maildir/maildir-storage.c src/lib-storage/index/maildir/maildir-sync-index.c src/lib-storage/index/mbox/mbox-storage.c src/lib-storage/index/mbox/mbox-sync.c src/lib-storage/index/raw/raw-storage.c src/lib-storage/mail-search.c src/lib-storage/mail-storage-private.h src/lib-storage/mail-storage.c src/lib-storage/mail-storage.h src/lib-storage/test-mailbox.c src/plugins/convert/convert-storage.c src/plugins/lazy-expunge/lazy-expunge-plugin.c src/plugins/virtual/virtual-storage.c src/plugins/virtual/virtual-sync.c
diffstat 26 files changed, 95 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/src/imap/cmd-append.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/imap/cmd-append.c	Mon Jun 22 17:45:56 2009 -0400
@@ -354,7 +354,7 @@
 	ret = mailbox_save_begin(&ctx->save_ctx, ctx->input);
 
 	if (keywords != NULL)
-		mailbox_keywords_free(ctx->box, &keywords);
+		mailbox_keywords_unref(ctx->box, &keywords);
 
 	if (ret < 0) {
 		/* save initialization failed */
--- a/src/imap/cmd-copy.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/imap/cmd-copy.c	Mon Jun 22 17:45:56 2009 -0400
@@ -72,7 +72,8 @@
 
 		if (mailbox_copy(&save_ctx, mail) < 0)
 			ret = mail->expunged ? 0 : -1;
-		mailbox_keywords_free(destbox, &keywords);
+		if (keywords != NULL)
+			mailbox_keywords_unref(destbox, &keywords);
 
 		msgset_generator_next(&srcset_ctx, mail->uid);
 	}
--- a/src/imap/cmd-store.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/imap/cmd-store.c	Mon Jun 22 17:45:56 2009 -0400
@@ -198,7 +198,7 @@
 	mail_free(&mail);
 
 	if (ctx.keywords != NULL)
-		mailbox_keywords_free(client->mailbox, &ctx.keywords);
+		mailbox_keywords_unref(client->mailbox, &ctx.keywords);
 
 	ret = mailbox_search_deinit(&search_ctx);
 	if (ret < 0)
--- a/src/lib-index/mail-index-sync.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-index/mail-index-sync.c	Mon Jun 22 17:45:56 2009 -0400
@@ -89,7 +89,7 @@
 		}
 	}
 
-	mail_index_keywords_free(&keywords);
+	mail_index_keywords_unref(&keywords);
 }
 
 static void mail_index_sync_add_keyword_reset(struct mail_index_sync_ctx *ctx)
@@ -106,7 +106,7 @@
 						   MODIFY_REPLACE, keywords);
 		}
 	}
-	mail_index_keywords_free(&keywords);
+	mail_index_keywords_unref(&keywords);
 }
 
 static void mail_index_sync_add_append(struct mail_index_sync_ctx *ctx)
--- a/src/lib-index/mail-index-transaction.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-index/mail-index-transaction.c	Mon Jun 22 17:45:56 2009 -0400
@@ -1164,6 +1164,7 @@
 	if (count == 0) {
 		k = i_new(struct mail_keywords, 1);
 		k->index = index;
+		k->refcount = 1;
 		return k;
 	}
 
@@ -1171,6 +1172,7 @@
 	k = i_malloc(sizeof(struct mail_keywords) +
 		     (sizeof(k->idx) * (count-1)));
 	k->index = index;
+	k->refcount = 1;
 
 	/* look up the keywords from index. they're never removed from there
 	   so we can permanently store indexes to them. */
@@ -1202,6 +1204,7 @@
 	if (count == 0) {
 		k = i_new(struct mail_keywords, 1);
 		k->index = index;
+		k->refcount = 1;
 		return k;
 	}
 
@@ -1209,6 +1212,7 @@
 	k = i_malloc(sizeof(struct mail_keywords) +
 		     (sizeof(k->idx) * (count-1)));
 	k->index = index;
+	k->refcount = 1;
 
 	/* copy but skip duplicates */
 	for (src = dest = 0; src < count; src++) {
@@ -1223,10 +1227,20 @@
 	return k;
 }
 
-void mail_index_keywords_free(struct mail_keywords **keywords)
+void mail_index_keywords_ref(struct mail_keywords *keywords)
+{
+	keywords->refcount++;
+}
+
+void mail_index_keywords_unref(struct mail_keywords **_keywords)
 {
-	i_free(*keywords);
-	*keywords = NULL;
+	struct mail_keywords *keywords = *_keywords;
+
+	i_assert(keywords->refcount > 0);
+
+	*_keywords = NULL;
+	if (--keywords->refcount == 0)
+		i_free(keywords);
 }
 
 static bool
--- a/src/lib-index/mail-index.h	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-index/mail-index.h	Mon Jun 22 17:45:56 2009 -0400
@@ -102,6 +102,7 @@
 struct mail_keywords {
 	struct mail_index *index;
 	unsigned int count;
+	int refcount;
 
         /* variable sized list of keyword indexes */
 	unsigned int idx[1];
@@ -430,8 +431,8 @@
 mail_index_keywords_create_from_indexes(struct mail_index *index,
 					const ARRAY_TYPE(keyword_indexes)
 						*keyword_indexes);
-/* Free the keywords. */
-void mail_index_keywords_free(struct mail_keywords **keywords);
+void mail_index_keywords_ref(struct mail_keywords *keywords);
+void mail_index_keywords_unref(struct mail_keywords **keywords);
 
 /* Update keywords for given message. */
 void mail_index_update_keywords(struct mail_index_transaction *t, uint32_t seq,
--- a/src/lib-lda/mail-deliver.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-lda/mail-deliver.c	Mon Jun 22 17:45:56 2009 -0400
@@ -183,7 +183,8 @@
 	mailbox_save_set_flags(save_ctx, flags, kw);
 	if (mailbox_copy(&save_ctx, ctx->src_mail) < 0)
 		ret = -1;
-	mailbox_keywords_free(box, &kw);
+	if (kw != NULL)
+		mailbox_keywords_unref(box, &kw);
 
 	if (ret < 0)
 		mailbox_transaction_rollback(&t);
--- a/src/lib-storage/index/cydir/cydir-storage.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/index/cydir/cydir-storage.c	Mon Jun 22 17:45:56 2009 -0400
@@ -375,7 +375,8 @@
 		index_transaction_set_max_modseq,
 		index_keywords_create,
 		index_keywords_create_from_indexes,
-		index_keywords_free,
+		index_keywords_ref,
+		index_keywords_unref,
 		index_keyword_is_valid,
 		index_storage_get_seq_range,
 		index_storage_get_uid_range,
--- a/src/lib-storage/index/dbox/dbox-storage.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/index/dbox/dbox-storage.c	Mon Jun 22 17:45:56 2009 -0400
@@ -878,7 +878,8 @@
 		index_transaction_set_max_modseq,
 		index_keywords_create,
 		index_keywords_create_from_indexes,
-		index_keywords_free,
+		index_keywords_ref,
+		index_keywords_unref,
 		index_keyword_is_valid,
 		index_storage_get_seq_range,
 		index_storage_get_uid_range,
--- a/src/lib-storage/index/dbox/dbox-sync-rebuild.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/index/dbox/dbox-sync-rebuild.c	Mon Jun 22 17:45:56 2009 -0400
@@ -104,7 +104,7 @@
 	mail_index_lookup_keywords(view, old_seq, &old_keywords);
 	kw = mail_index_keywords_create_from_indexes(index, &old_keywords);
 	mail_index_update_keywords(ctx->trans, new_seq, MODIFY_REPLACE, kw);
-	mail_index_keywords_free(&kw);
+	mail_index_keywords_unref(&kw);
 
 	dbox_sync_index_copy_cache(ctx, view, old_seq, new_seq);
 }
@@ -125,7 +125,7 @@
 	keywords = mail_index_keywords_create_from_indexes(ctx->mbox->ibox.index,
 							   &keyword_indexes);
 	mail_index_update_keywords(ctx->trans, seq, MODIFY_REPLACE, keywords);
-	mail_index_keywords_free(&keywords);
+	mail_index_keywords_unref(&keywords);
 }
 
 void dbox_sync_rebuild_index_metadata(struct dbox_sync_rebuild_context *ctx,
--- a/src/lib-storage/index/index-storage.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/index/index-storage.c	Mon Jun 22 17:45:56 2009 -0400
@@ -644,9 +644,14 @@
 	return mail_index_keywords_create_from_indexes(ibox->index, idx);
 }
 
-void index_keywords_free(struct mail_keywords *keywords)
+void index_keywords_ref(struct mail_keywords *keywords)
 {
-	mail_index_keywords_free(&keywords);
+	mail_index_keywords_ref(keywords);
+}
+
+void index_keywords_unref(struct mail_keywords *keywords)
+{
+	mail_index_keywords_unref(&keywords);
 }
 
 void index_save_context_free(struct mail_save_context *ctx)
--- a/src/lib-storage/index/index-storage.h	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/index/index-storage.h	Mon Jun 22 17:45:56 2009 -0400
@@ -107,7 +107,8 @@
 struct mail_keywords *
 index_keywords_create_from_indexes(struct mailbox *box,
 				   const ARRAY_TYPE(keyword_indexes) *idx);
-void index_keywords_free(struct mail_keywords *keywords);
+void index_keywords_ref(struct mail_keywords *keywords);
+void index_keywords_unref(struct mail_keywords *keywords);
 bool index_keyword_is_valid(struct mailbox *box, const char *keyword,
 			    const char **error_r);
 
--- a/src/lib-storage/index/maildir/maildir-storage.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/index/maildir/maildir-storage.c	Mon Jun 22 17:45:56 2009 -0400
@@ -1056,7 +1056,8 @@
 		index_transaction_set_max_modseq,
 		index_keywords_create,
 		index_keywords_create_from_indexes,
-		index_keywords_free,
+		index_keywords_ref,
+		index_keywords_unref,
 		index_keyword_is_valid,
 		index_storage_get_seq_range,
 		index_storage_get_uid_range,
--- a/src/lib-storage/index/maildir/maildir-sync-index.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/index/maildir/maildir-sync-index.c	Mon Jun 22 17:45:56 2009 -0400
@@ -326,7 +326,7 @@
 		kw = mail_index_keywords_create_from_indexes(mbox->ibox.index,
 							     &ctx->keywords);
 		mail_index_update_keywords(ctx->trans, seq, MODIFY_REPLACE, kw);
-		mail_index_keywords_free(&kw);
+		mail_index_keywords_unref(&kw);
 		return;
 	}
 
@@ -357,14 +357,14 @@
 		kw = mail_index_keywords_create_from_indexes(mbox->ibox.index,
 							     &ctx->idx_keywords);
 		mail_index_update_keywords(ctx->trans, seq, MODIFY_REMOVE, kw);
-		mail_index_keywords_free(&kw);
+		mail_index_keywords_unref(&kw);
 	}
 
 	if (array_count(&ctx->keywords) > 0) {
 		kw = mail_index_keywords_create_from_indexes(mbox->ibox.index,
 							     &ctx->keywords);
 		mail_index_update_keywords(ctx->trans, seq, MODIFY_ADD, kw);
-		mail_index_keywords_free(&kw);
+		mail_index_keywords_unref(&kw);
 	}
 }
 
@@ -464,7 +464,7 @@
 					mbox->ibox.index, &ctx->keywords);
 				mail_index_update_keywords(trans, seq,
 							   MODIFY_REPLACE, kw);
-				mail_index_keywords_free(&kw);
+				mail_index_keywords_unref(&kw);
 			}
 			continue;
 		}
--- a/src/lib-storage/index/mbox/mbox-storage.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/index/mbox/mbox-storage.c	Mon Jun 22 17:45:56 2009 -0400
@@ -872,7 +872,8 @@
 		index_transaction_set_max_modseq,
 		index_keywords_create,
 		index_keywords_create_from_indexes,
-		index_keywords_free,
+		index_keywords_ref,
+		index_keywords_unref,
 		index_keyword_is_valid,
 		index_storage_get_seq_range,
 		index_storage_get_uid_range,
--- a/src/lib-storage/index/mbox/mbox-sync.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/index/mbox/mbox-sync.c	Mon Jun 22 17:45:56 2009 -0400
@@ -298,7 +298,7 @@
 			sync_ctx->mbox->ibox.index, &mail_ctx->mail.keywords);
 	mail_index_update_keywords(sync_ctx->t, sync_ctx->idx_seq,
 				   MODIFY_REPLACE, keywords);
-	mail_index_keywords_free(&keywords);
+	mail_index_keywords_unref(&keywords);
 }
 
 static void
--- a/src/lib-storage/index/raw/raw-storage.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/index/raw/raw-storage.c	Mon Jun 22 17:45:56 2009 -0400
@@ -215,7 +215,8 @@
 		index_transaction_set_max_modseq,
 		index_keywords_create,
 		index_keywords_create_from_indexes,
-		index_keywords_free,
+		index_keywords_ref,
+		index_keywords_unref,
 		index_keyword_is_valid,
 		index_storage_get_seq_range,
 		index_storage_get_uid_range,
--- a/src/lib-storage/mail-search.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/mail-search.c	Mon Jun 22 17:45:56 2009 -0400
@@ -146,7 +146,7 @@
 		case SEARCH_KEYWORDS:
 			if (arg->value.keywords == NULL)
 				break;
-			mailbox_keywords_free(args->box, &arg->value.keywords);
+			mailbox_keywords_unref(args->box, &arg->value.keywords);
 			break;
 		case SEARCH_INTHREAD:
 			i_assert(arg->value.search_args->refcount > 0);
@@ -534,8 +534,8 @@
 		new_kw = mailbox_keywords_create_from_indexes(box,
 							      &new_indexes);
 	} T_END;
-	mailbox_keywords_free(box, _kw1);
-	mailbox_keywords_free(box, _kw2);
+	mailbox_keywords_unref(box, _kw1);
+	mailbox_keywords_unref(box, _kw2);
 	return new_kw;
 }
 
--- a/src/lib-storage/mail-storage-private.h	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/mail-storage-private.h	Mon Jun 22 17:45:56 2009 -0400
@@ -153,7 +153,8 @@
 	struct mail_keywords *
 		(*keywords_create_from_indexes)(struct mailbox *box,
 						const ARRAY_TYPE(keyword_indexes) *idx);
-	void (*keywords_free)(struct mail_keywords *keywords);
+	void (*keywords_ref)(struct mail_keywords *keywords);
+	void (*keywords_unref)(struct mail_keywords *keywords);
 	bool (*keyword_is_valid)(struct mailbox *box, const char *keyword,
 				 const char **error_r);
 
--- a/src/lib-storage/mail-storage.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/mail-storage.c	Mon Jun 22 17:45:56 2009 -0400
@@ -658,13 +658,18 @@
 	return box->v.keywords_create_from_indexes(box, idx);
 }
 
-void mailbox_keywords_free(struct mailbox *box,
-			   struct mail_keywords **_keywords)
+void mailbox_keywords_ref(struct mailbox *box, struct mail_keywords *keywords)
+{
+	box->v.keywords_ref(keywords);
+}
+
+void mailbox_keywords_unref(struct mailbox *box,
+			    struct mail_keywords **_keywords)
 {
 	struct mail_keywords *keywords = *_keywords;
 
 	*_keywords = NULL;
-	box->v.keywords_free(keywords);
+	box->v.keywords_unref(keywords);
 }
 
 bool mailbox_keyword_is_valid(struct mailbox *box, const char *keyword,
--- a/src/lib-storage/mail-storage.h	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/mail-storage.h	Mon Jun 22 17:45:56 2009 -0400
@@ -395,8 +395,9 @@
 struct mail_keywords *
 mailbox_keywords_create_from_indexes(struct mailbox *box,
 				     const ARRAY_TYPE(keyword_indexes) *idx);
-void mailbox_keywords_free(struct mailbox *box,
-			   struct mail_keywords **keywords);
+void mailbox_keywords_ref(struct mailbox *box, struct mail_keywords *keywords);
+void mailbox_keywords_unref(struct mailbox *box,
+			    struct mail_keywords **keywords);
 /* Returns TRUE if keyword is valid, FALSE and error if not. */
 bool mailbox_keyword_is_valid(struct mailbox *box, const char *keyword,
 			      const char **error_r);
--- a/src/lib-storage/test-mailbox.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/lib-storage/test-mailbox.c	Mon Jun 22 17:45:56 2009 -0400
@@ -120,6 +120,7 @@
 			     bool skip_invalid ATTR_UNUSED)
 {
 	*keywords_r = i_new(struct mail_keywords, 1);
+	(*keywords_r)->refcount = 1;
 	return 0;
 }
 
@@ -127,12 +128,22 @@
 test_mailbox_keywords_create_from_indexes(struct mailbox *box ATTR_UNUSED,
 					  const ARRAY_TYPE(keyword_indexes) *idx ATTR_UNUSED)
 {
-	return i_new(struct mail_keywords, 1);
+	struct mail_keywords *keywords;
+
+	keywords = i_new(struct mail_keywords, 1);
+	keywords->refcount++;
+	return keywords;
 }
 
-static void test_mailbox_keywords_free(struct mail_keywords *keywords)
+static void test_mailbox_keywords_ref(struct mail_keywords *keywords)
 {
-	i_free(keywords);
+	keywords->refcount++;
+}
+
+static void test_mailbox_keywords_unref(struct mail_keywords *keywords)
+{
+	if (--keywords->refcount == 0)
+		i_free(keywords);
 }
 
 static bool
@@ -297,7 +308,8 @@
 		test_mailbox_transaction_set_max_modseq,
 		test_mailbox_keywords_create,
 		test_mailbox_keywords_create_from_indexes,
-		test_mailbox_keywords_free,
+		test_mailbox_keywords_ref,
+		test_mailbox_keywords_unref,
 		test_mailbox_keyword_is_valid,
 		test_mailbox_get_seq_range,
 		test_mailbox_get_uid_range,
--- a/src/plugins/convert/convert-storage.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/plugins/convert/convert-storage.c	Mon Jun 22 17:45:56 2009 -0400
@@ -77,7 +77,8 @@
 		mailbox_save_set_flags(save_ctx, mail_get_flags(mail),
 				       keywords);
 		ret = mailbox_copy(&save_ctx, mail);
-		mailbox_keywords_free(destbox, &keywords);
+		if (keywords != NULL)
+			mailbox_keywords_unref(destbox, &keywords);
 		if (ret < 0) {
 			*error_r = storage_error(destbox->storage);
 			break;
--- a/src/plugins/lazy-expunge/lazy-expunge-plugin.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/plugins/lazy-expunge/lazy-expunge-plugin.c	Mon Jun 22 17:45:56 2009 -0400
@@ -166,7 +166,8 @@
 
 	if (mailbox_copy(&save_ctx, _mail) < 0 && !_mail->expunged)
 		lt->failed = TRUE;
-	mailbox_keywords_free(lt->dest_box, &keywords);
+	if (keywords != NULL)
+		mailbox_keywords_unref(lt->dest_box, &keywords);
 
 	mmail->super.expunge(_mail);
 }
--- a/src/plugins/virtual/virtual-storage.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/plugins/virtual/virtual-storage.c	Mon Jun 22 17:45:56 2009 -0400
@@ -588,7 +588,8 @@
 		index_transaction_set_max_modseq,
 		index_keywords_create,
 		index_keywords_create_from_indexes,
-		index_keywords_free,
+		index_keywords_ref,
+		index_keywords_unref,
 		index_keyword_is_valid,
 		index_storage_get_seq_range,
 		index_storage_get_uid_range,
--- a/src/plugins/virtual/virtual-sync.c	Mon Jun 22 01:44:42 2009 -0400
+++ b/src/plugins/virtual/virtual-sync.c	Mon Jun 22 17:45:56 2009 -0400
@@ -76,7 +76,7 @@
 	kw_names = mail_get_keywords(bbox->sync_mail);
 	keywords = mail_index_keywords_create(ctx->index, kw_names);
 	mail_index_update_keywords(ctx->trans, vseq, MODIFY_REPLACE, keywords);
-	mail_index_keywords_free(&keywords);
+	mail_index_keywords_unref(&keywords);
 }
 
 static int virtual_sync_mail_cmp(const void *p1, const void *p2)
@@ -380,7 +380,7 @@
 				MODIFY_ADD : MODIFY_REMOVE;
 			mail_update_keywords(bbox->sync_mail,
 					     modify_type, keywords);
-			mailbox_keywords_free(bbox->box, &keywords);
+			mailbox_keywords_unref(bbox->box, &keywords);
 			break;
 		case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
 			kw_names[0] = NULL;
@@ -388,7 +388,7 @@
 								 kw_names);
 			mail_update_keywords(bbox->sync_mail, MODIFY_REPLACE,
 					     keywords);
-			mailbox_keywords_free(bbox->box, &keywords);
+			mailbox_keywords_unref(bbox->box, &keywords);
 			break;
 		case MAIL_INDEX_SYNC_TYPE_APPEND:
 			i_unreached();