dovecot-2.0: Use array_bsearch*().

dovecot at dovecot.org dovecot at dovecot.org
Fri Jul 10 20:59:42 EEST 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/d404d34097f2
changeset: 9609:d404d34097f2
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Jul 10 13:59:32 2009 -0400
description:
Use array_bsearch*().

diffstat:

12 files changed, 82 insertions(+), 129 deletions(-)
src/imap/imap-commands.c                          |   11 ---
src/imap/imap-fetch.c                             |   17 ++---
src/lib-index/mail-index-strmap.c                 |   15 ++---
src/lib-index/mailbox-list-index-sync.c           |   21 ++-----
src/lib-mail/istream-header-filter.c              |   14 +---
src/lib-storage/index/dbox/dbox-storage-rebuild.c |   14 +---
src/lib-storage/index/index-thread.c              |    4 -
src/lib-storage/index/maildir/maildir-uidlist.c   |   15 ++---
src/plugins/acl/acl-backend-vfile.c               |   13 +---
src/plugins/fts/fts-storage.c                     |   13 +---
src/plugins/virtual/virtual-storage.c             |   14 +---
src/plugins/virtual/virtual-sync.c                |   60 ++++++++++-----------

diffs (truncated from 508 to 300 lines):

diff -r 5ab09480d6b4 -r d404d34097f2 src/imap/imap-commands.c
--- a/src/imap/imap-commands.c	Fri Jul 10 13:59:24 2009 -0400
+++ b/src/imap/imap-commands.c	Fri Jul 10 13:59:32 2009 -0400
@@ -109,26 +109,19 @@ static int command_cmp(const struct comm
 	return strcasecmp(c1->name, c2->name);
 }
 
-static int command_bsearch(const void *name, const void *cmd_p)
+static int command_bsearch(const char *name, const struct command *cmd)
 {
-        const struct command *cmd = cmd_p;
-
 	return strcasecmp(name, cmd->name);
 }
 
 struct command *command_find(const char *name)
 {
-	const void *base;
-	unsigned int count;
-
 	if (commands_unsorted) {
 		array_sort(&imap_commands, command_cmp);
                 commands_unsorted = FALSE;
 	}
 
-	base = array_get(&imap_commands, &count);
-	return bsearch(name, base, count, sizeof(struct command),
-		       command_bsearch);
+	return array_bsearch(&imap_commands, name, command_bsearch);
 }
 
 void commands_init(void)
diff -r 5ab09480d6b4 -r d404d34097f2 src/imap/imap-fetch.c
--- a/src/imap/imap-fetch.c	Fri Jul 10 13:59:24 2009 -0400
+++ b/src/imap/imap-fetch.c	Fri Jul 10 13:59:32 2009 -0400
@@ -38,28 +38,23 @@ void imap_fetch_handlers_register(const 
 	array_sort(&fetch_handlers, imap_fetch_handler_cmp);
 }
 
-static int imap_fetch_handler_bsearch(const void *name_p, const void *handler_p)
-{
-	const char *name = name_p;
-        const struct imap_fetch_handler *h = handler_p;
-
+static int
+imap_fetch_handler_bsearch(const char *name, const struct imap_fetch_handler *h)
+{
 	return strcmp(name, h->name);
 }
 
 bool imap_fetch_init_handler(struct imap_fetch_context *ctx, const char *name,
 			     const struct imap_arg **args)
 {
-	const struct imap_fetch_handler *handler, *handlers;
+	const struct imap_fetch_handler *handler;
 	const char *lookup_name, *p;
-	unsigned int count;
 
 	for (p = name; i_isalnum(*p); p++) ;
 	lookup_name = t_strdup_until(name, p);
 
-	handlers = array_get_modifiable(&fetch_handlers, &count);
-	handler = bsearch(lookup_name, handlers, count,
-			  sizeof(struct imap_fetch_handler),
-			  imap_fetch_handler_bsearch);
+	handler = array_bsearch(&fetch_handlers, lookup_name,
+				imap_fetch_handler_bsearch);
 	if (handler == NULL) {
 		client_send_command_error(ctx->cmd,
 			t_strconcat("Unknown parameter ", name, NULL));
diff -r 5ab09480d6b4 -r d404d34097f2 src/lib-index/mail-index-strmap.c
--- a/src/lib-index/mail-index-strmap.c	Fri Jul 10 13:59:24 2009 -0400
+++ b/src/lib-index/mail-index-strmap.c	Fri Jul 10 13:59:32 2009 -0400
@@ -1072,11 +1072,9 @@ static void mail_index_strmap_unlock(str
 		file_dotlock_delete(&strmap->dotlock);
 }
 
-static int strmap_rec_cmp(const void *key, const void *value)
-{
-	const uint32_t *uid = key;
-	const struct mail_index_strmap_rec *rec = value;
-
+static int
+strmap_rec_cmp(const uint32_t *uid, const struct mail_index_strmap_rec *rec)
+{
 	return *uid < rec->uid ? -1 :
 		(*uid > rec->uid ? 1 : 0);
 }
@@ -1112,10 +1110,11 @@ mail_index_strmap_write_append(struct ma
 	   already have internally given it another index). So the only
 	   sensible choice is to write nothing and hope that the message goes
 	   away soon. */
+	next_uid = view->last_read_uid + 1;
+	(void)array_bsearch_insert_pos(&view->recs, &next_uid,
+				       strmap_rec_cmp, &i);
+
 	old_recs = array_get(&view->recs, &old_count);
-	next_uid = view->last_read_uid + 1;
-	(void)bsearch_insert_pos(&next_uid, old_recs, old_count,
-				 sizeof(*old_recs), strmap_rec_cmp, &i);
 	if (i < old_count) {
 		while (i > 0 && old_recs[i-1].uid == old_recs[i].uid)
 			i--;
diff -r 5ab09480d6b4 -r d404d34097f2 src/lib-index/mailbox-list-index-sync.c
--- a/src/lib-index/mailbox-list-index-sync.c	Fri Jul 10 13:59:24 2009 -0400
+++ b/src/lib-index/mailbox-list-index-sync.c	Fri Jul 10 13:59:32 2009 -0400
@@ -136,11 +136,10 @@ mailbox_list_copy_sync_dir(struct mailbo
 	return 0;
 }
 
-static int mailbox_list_sync_record_cmp(const void *_key, const void *_rec)
-{
-	const struct mailbox_list_sync_lookup_key *key = _key;
-	const struct mailbox_list_sync_record *rec = _rec;
-
+static int
+mailbox_list_sync_record_cmp(const struct mailbox_list_sync_lookup_key *key,
+			     const struct mailbox_list_sync_record *rec)
+{
 	if (key->name_hash < rec->name_hash)
 		return -1;
 	if (key->name_hash > rec->name_hash)
@@ -154,9 +153,6 @@ mailbox_list_sync_dir_lookup(struct mail
 			     const char *name, unsigned int *idx_r)
 {
 	struct mailbox_list_sync_lookup_key key;
-	struct mailbox_list_sync_record *recs;
-	unsigned int count;
-	bool match;
 
 	/* binary search the current hierarchy level name. the values are
 	   sorted primarily by their hash value and secondarily by the actual
@@ -164,14 +160,11 @@ mailbox_list_sync_dir_lookup(struct mail
 	key.name = name;
 	key.name_hash = crc32_str(name);
 
-	recs = array_get_modifiable(&dir->records, &count);
-	match = bsearch_insert_pos(&key, recs, count, sizeof(*recs),
-				   mailbox_list_sync_record_cmp,
-				   idx_r);
-	if (!match)
+	if (!array_bsearch_insert_pos(&dir->records, &key,
+				      mailbox_list_sync_record_cmp, idx_r))
 		return NULL;
 
-	return &recs[*idx_r];
+	return array_idx_modifiable(&dir->records, *idx_r);
 }
 
 static struct mailbox_list_sync_record *
diff -r 5ab09480d6b4 -r d404d34097f2 src/lib-mail/istream-header-filter.c
--- a/src/lib-mail/istream-header-filter.c	Fri Jul 10 13:59:24 2009 -0400
+++ b/src/lib-mail/istream-header-filter.c	Fri Jul 10 13:59:32 2009 -0400
@@ -86,25 +86,19 @@ read_mixed(struct header_filter_istream 
 	return ret;
 }
 
-static int cmp_uint(const void *p1, const void *p2)
-{
-	const unsigned int *i1 = p1, *i2 = p2;
-
+static int cmp_uint(const unsigned int *i1, const unsigned int *i2)
+{
 	return *i1 < *i2 ? -1 :
 		(*i1 > *i2 ? 1 : 0);
 }
 
 static bool match_line_changed(struct header_filter_istream *mstream)
 {
-	const unsigned int *lines;
-	unsigned int count;
-
 	if (!array_is_created(&mstream->match_change_lines))
 		return FALSE;
 
-	lines = array_get(&mstream->match_change_lines, &count);
-	return bsearch(&mstream->cur_line, lines, count, sizeof(*lines),
-		       cmp_uint) != NULL;
+	return array_bsearch(&mstream->match_change_lines, &mstream->cur_line,
+			     cmp_uint) != NULL;
 }
 
 static void add_eol(struct header_filter_istream *mstream)
diff -r 5ab09480d6b4 -r d404d34097f2 src/lib-storage/index/dbox/dbox-storage-rebuild.c
--- a/src/lib-storage/index/dbox/dbox-storage-rebuild.c	Fri Jul 10 13:59:24 2009 -0400
+++ b/src/lib-storage/index/dbox/dbox-storage-rebuild.c	Fri Jul 10 13:59:32 2009 -0400
@@ -116,10 +116,9 @@ static int dbox_rebuild_msg_offset_cmp(c
 	return 0;
 }
 
-static int dbox_rebuild_msg_uid_cmp(const void *p1, const void *p2)
-{
-	const struct dbox_rebuild_msg *const *m1 = p1, *const *m2 = p2;
-
+static int dbox_rebuild_msg_uid_cmp(struct dbox_rebuild_msg *const *m1,
+				    struct dbox_rebuild_msg *const *m2)
+{
 	if ((*m1)->map_uid < (*m2)->map_uid)
 		return -1;
 	if ((*m1)->map_uid > (*m2)->map_uid)
@@ -296,13 +295,10 @@ rebuild_lookup_map_uid(struct dbox_stora
 		       uint32_t map_uid)
 {
 	struct dbox_rebuild_msg search_msg, *search_msgp = &search_msg;
-	struct dbox_rebuild_msg *const *msgs, **pos;
-	unsigned int count;
+	struct dbox_rebuild_msg **pos;
 
 	search_msg.map_uid = map_uid;
-	msgs = array_get(&ctx->msgs, &count);
-	pos = bsearch(&search_msgp, msgs, count, sizeof(*msgs),
-		      dbox_rebuild_msg_uid_cmp);
+	pos = array_bsearch(&ctx->msgs, &search_msgp, dbox_rebuild_msg_uid_cmp);
 	return pos == NULL ? NULL : *pos;
 }
 
diff -r 5ab09480d6b4 -r d404d34097f2 src/lib-storage/index/index-thread.c
--- a/src/lib-storage/index/index-thread.c	Fri Jul 10 13:59:24 2009 -0400
+++ b/src/lib-storage/index/index-thread.c	Fri Jul 10 13:59:32 2009 -0400
@@ -436,9 +436,9 @@ static void mail_thread_cache_update_add
 	if (uid_count == 0)
 		return;
 
+	(void)array_bsearch_insert_pos(tbox->msgid_map, &uids[0].seq1,
+				       msgid_map_cmp, &j);
 	msgid_map = array_get(tbox->msgid_map, &map_count);
-	(void)bsearch_insert_pos(&uids[0].seq1, msgid_map, map_count,
-				 sizeof(*msgid_map), msgid_map_cmp, &j);
 	i_assert(j < map_count);
 	while (j > 0 && msgid_map[j-1].uid == msgid_map[j].uid)
 		j--;
diff -r 5ab09480d6b4 -r d404d34097f2 src/lib-storage/index/maildir/maildir-uidlist.c
--- a/src/lib-storage/index/maildir/maildir-uidlist.c	Fri Jul 10 13:59:24 2009 -0400
+++ b/src/lib-storage/index/maildir/maildir-uidlist.c	Fri Jul 10 13:59:32 2009 -0400
@@ -339,10 +339,9 @@ void maildir_uidlist_deinit(struct maild
 	i_free(uidlist);
 }
 
-static int maildir_uid_cmp(const void *p1, const void *p2)
-{
-	const struct maildir_uidlist_rec *const *rec1 = p1, *const *rec2 = p2;
-
+static int maildir_uid_cmp(struct maildir_uidlist_rec *const *rec1,
+			   struct maildir_uidlist_rec *const *rec2)
+{
 	return (*rec1)->uid < (*rec2)->uid ? -1 :
 		(*rec1)->uid > (*rec2)->uid ? 1 : 0;
 }
@@ -400,12 +399,12 @@ maildir_uidlist_records_array_delete(str
 				     struct maildir_uidlist_rec *rec)
 {
 	struct maildir_uidlist_rec *const *recs, *const *pos;
-	unsigned int idx, count;
-
-	recs = array_get(&uidlist->records, &count);
-	pos = bsearch(&rec, recs, count, sizeof(*recs), maildir_uid_cmp);
+	unsigned int idx;
+
+	pos = array_bsearch(&uidlist->records, &rec, maildir_uid_cmp);
 	i_assert(pos != NULL);
 
+	recs = array_idx(&uidlist->records, 0);
 	idx = pos - recs;
 	array_delete(&uidlist->records, idx, 1);
 	return idx;
diff -r 5ab09480d6b4 -r d404d34097f2 src/plugins/acl/acl-backend-vfile.c
--- a/src/plugins/acl/acl-backend-vfile.c	Fri Jul 10 13:59:24 2009 -0400
+++ b/src/plugins/acl/acl-backend-vfile.c	Fri Jul 10 13:59:32 2009 -0400
@@ -662,11 +662,6 @@ static int acl_rights_cmp(const struct a
 		return ret;
 
 	return null_strcmp(r1->identifier, r2->identifier);
-}
-
-static int acl_rights_bsearch_cmp(const void *p1, const void *p2)
-{
-	return acl_rights_cmp(p1, p2);
 }
 
 static void
@@ -1092,10 +1087,9 @@ acl_backend_vfile_object_update(struct a
 	struct acl_object_vfile *aclobj = (struct acl_object_vfile *)_aclobj;
 	struct acl_backend_vfile *backend =
 		(struct acl_backend_vfile *)_aclobj->backend;
-	const struct acl_rights *rights;
 	struct dotlock *dotlock;
 	const char *path;
-	unsigned int i, count;
+	unsigned int i;
 	int fd;
 	bool changed;
 
@@ -1106,9 +1100,8 @@ acl_backend_vfile_object_update(struct a
 	if (fd == -1)
 		return -1;
 
-	rights = array_get(&aclobj->rights, &count);
-	if (!bsearch_insert_pos(&update->rights, rights, count, sizeof(*rights),
-				acl_rights_bsearch_cmp, &i))
+	if (!array_bsearch_insert_pos(&aclobj->rights, &update->rights,


More information about the dovecot-cvs mailing list