dovecot-2.0: Use array_sort() instead of qsort() wherever possible.

dovecot at dovecot.org dovecot at dovecot.org
Wed Jun 17 21:52:59 EEST 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/0059b2381024
changeset: 9491:0059b2381024
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Jun 17 14:52:47 2009 -0400
description:
Use array_sort() instead of qsort() wherever possible.

diffstat:

21 files changed, 156 insertions(+), 215 deletions(-)
src/config/doveconf.c                              |   13 ++----
src/imap/imap-commands.c                           |   10 +---
src/imap/imap-fetch.c                              |   15 +------
src/lib-index/mail-cache-lookup.c                  |    9 +---
src/lib-mail/rfc2231-parser.c                      |   12 ++---
src/lib-settings/settings-parser.c                 |   12 ++---
src/lib-storage/index/dbox/dbox-storage-rebuild.c  |   10 ++--
src/lib-storage/index/dbox/dbox-sync-file.c        |    8 +--
src/lib-storage/index/index-mail-headers.c         |   15 +++----
src/lib-storage/index/index-sort-string.c          |   41 ++++++--------------
src/lib-storage/index/index-sort.c                 |   36 +++++------------
src/lib-storage/index/index-thread-finish.c        |   22 ++++++----
src/lib-storage/index/maildir/maildir-sync-index.c |   18 +++-----
src/lib-storage/index/maildir/maildir-uidlist.c    |   15 +------
src/lib/module-dir.c                               |   11 ++---
src/plugins/acl/acl-backend-vfile.c                |   25 +++++++-----
src/plugins/acl/acl-lookup-dict.c                  |   10 ++--
src/plugins/fts/fts-storage.c                      |   30 ++++++--------
src/plugins/trash/trash-plugin.c                   |   11 +----
src/plugins/virtual/virtual-search.c               |   17 +++-----
src/plugins/virtual/virtual-sync.c                 |   31 +++++----------

diffs (truncated from 1027 to 300 lines):

diff -r a6e1f054a808 -r 0059b2381024 src/config/doveconf.c
--- a/src/config/doveconf.c	Wed Jun 17 14:52:30 2009 -0400
+++ b/src/config/doveconf.c	Wed Jun 17 14:52:47 2009 -0400
@@ -32,10 +32,9 @@ config_request_get_strings(const char *k
 	array_append(&ctx->strings, &value, 1);
 }
 
-static int config_string_cmp(const void *p1, const void *p2)
-{
-	const char *s1 = *(const char *const *)p1;
-	const char *s2 = *(const char *const *)p2;
+static int config_string_cmp(const char *const *p1, const char *const *p2)
+{
+	const char *s1 = *p1, *s2 = *p2;
 	unsigned int i = 0;
 
 	while (s1[i] == s2[i]) {
@@ -71,7 +70,7 @@ static void config_connection_request_hu
 	ARRAY_TYPE(const_string) prefixes_arr;
 	ARRAY_TYPE(uint) prefix_idx_stack;
 	struct config_request_get_string_ctx ctx;
-	const char **strings, *const *args, *p, *str, *const *prefixes;
+	const char *const *strings, *const *args, *p, *str, *const *prefixes;
 	const char *key, *value;
 	unsigned int i, j, count, len, prefix_count, skip_len;
 	unsigned int indent = 0, prefix_idx = -1U;
@@ -81,8 +80,8 @@ static void config_connection_request_hu
 	config_request_handle(filter, module, flags,
 			      config_request_get_strings, &ctx);
 
-	strings = array_get_modifiable(&ctx.strings, &count);
-	qsort(strings, count, sizeof(*strings), config_string_cmp);
+	array_sort(&ctx.strings, config_string_cmp);
+	strings = array_get(&ctx.strings, &count);
 
 	p_array_init(&prefixes_arr, ctx.pool, 32);
 	for (i = 0; i < count && strings[i][0] == '-'; i++) T_BEGIN {
diff -r a6e1f054a808 -r 0059b2381024 src/imap/imap-commands.c
--- a/src/imap/imap-commands.c	Wed Jun 17 14:52:30 2009 -0400
+++ b/src/imap/imap-commands.c	Wed Jun 17 14:52:47 2009 -0400
@@ -104,10 +104,8 @@ void command_unregister_array(const stru
 	}
 }
 
-static int command_cmp(const void *p1, const void *p2)
+static int command_cmp(const struct command *c1, const struct command *c2)
 {
-        const struct command *c1 = p1, *c2 = p2;
-
 	return strcasecmp(c1->name, c2->name);
 }
 
@@ -120,15 +118,15 @@ static int command_bsearch(const void *n
 
 struct command *command_find(const char *name)
 {
-	void *base;
+	const void *base;
 	unsigned int count;
 
-	base = array_get_modifiable(&imap_commands, &count);
 	if (commands_unsorted) {
-		qsort(base, count, sizeof(struct command), command_cmp);
+		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);
 }
diff -r a6e1f054a808 -r 0059b2381024 src/imap/imap-fetch.c
--- a/src/imap/imap-fetch.c	Wed Jun 17 14:52:30 2009 -0400
+++ b/src/imap/imap-fetch.c	Wed Jun 17 14:52:47 2009 -0400
@@ -25,24 +25,17 @@
 
 static ARRAY_DEFINE(fetch_handlers, struct imap_fetch_handler);
 
-static int imap_fetch_handler_cmp(const void *p1, const void *p2)
-{
-        const struct imap_fetch_handler *h1 = p1, *h2 = p2;
-
+static int imap_fetch_handler_cmp(const struct imap_fetch_handler *h1,
+				  const struct imap_fetch_handler *h2)
+{
 	return strcmp(h1->name, h2->name);
 }
 
 void imap_fetch_handlers_register(const struct imap_fetch_handler *handlers,
 				  size_t count)
 {
-	struct imap_fetch_handler *all_handlers;
-	unsigned int all_count;
-
 	array_append(&fetch_handlers, handlers, count);
-
-	all_handlers = array_get_modifiable(&fetch_handlers, &all_count);
-	qsort(all_handlers, all_count, sizeof(*all_handlers),
-	      imap_fetch_handler_cmp);
+	array_sort(&fetch_handlers, imap_fetch_handler_cmp);
 }
 
 static int imap_fetch_handler_bsearch(const void *name_p, const void *handler_p)
diff -r a6e1f054a808 -r 0059b2381024 src/lib-index/mail-cache-lookup.c
--- a/src/lib-index/mail-cache-lookup.c	Wed Jun 17 14:52:30 2009 -0400
+++ b/src/lib-index/mail-cache-lookup.c	Wed Jun 17 14:52:47 2009 -0400
@@ -442,10 +442,9 @@ static void header_lines_save(struct hea
 	}
 }
 
-static int header_lookup_line_cmp(const void *p1, const void *p2)
-{
-	const struct header_lookup_line *l1 = p1, *l2 = p2;
-
+static int header_lookup_line_cmp(const struct header_lookup_line *l1,
+				  const struct header_lookup_line *l2)
+{
 	return (int)l1->line_num - (int)l2->line_num;
 }
 
@@ -518,8 +517,8 @@ mail_cache_lookup_headers_real(struct ma
 
 	/* we need to return headers in the order they existed originally.
 	   we can do this by sorting the messages by their line numbers. */
+	array_sort(&ctx.lines, header_lookup_line_cmp);
 	lines = array_get_modifiable(&ctx.lines, &count);
-	qsort(lines, count, sizeof(*lines), header_lookup_line_cmp);
 
 	/* then start filling dest buffer from the headers */
 	for (i = 0; i < count; i++) {
diff -r a6e1f054a808 -r 0059b2381024 src/lib-mail/rfc2231-parser.c
--- a/src/lib-mail/rfc2231-parser.c	Wed Jun 17 14:52:30 2009 -0400
+++ b/src/lib-mail/rfc2231-parser.c	Wed Jun 17 14:52:47 2009 -0400
@@ -14,9 +14,9 @@ struct rfc2231_parameter {
 	bool extended;
 };
 
-static int rfc2231_parameter_cmp(const void *p1, const void *p2)
+static int rfc2231_parameter_cmp(const struct rfc2231_parameter *r1,
+				 const struct rfc2231_parameter *r2)
 {
-	const struct rfc2231_parameter *r1 = p1, *r2 = p2;
 	int ret;
 
 	ret = strcmp(r1->key, r2->key);
@@ -42,7 +42,8 @@ int rfc2231_parse(struct rfc822_parser_c
 {
 	ARRAY_TYPE(const_string) result;
 	ARRAY_DEFINE(rfc2231_params_arr, struct rfc2231_parameter);
-	struct rfc2231_parameter rfc2231_param, *rfc2231_params;
+	struct rfc2231_parameter rfc2231_param;
+	const struct rfc2231_parameter *rfc2231_params;
 	const char *key, *value, *p, *p2;
 	string_t *str;
 	unsigned int i, j, count, next, next_idx;
@@ -102,9 +103,8 @@ int rfc2231_parse(struct rfc822_parser_c
 
 	/* Merge the RFC 2231 parameters. Since their order isn't guaranteed to
 	   be ascending, start by sorting them. */
-	rfc2231_params = array_get_modifiable(&rfc2231_params_arr, &count);
-	qsort(rfc2231_params, count, sizeof(*rfc2231_params),
-	      rfc2231_parameter_cmp);
+	array_sort(&rfc2231_params_arr, rfc2231_parameter_cmp);
+	rfc2231_params = array_get(&rfc2231_params_arr, &count);
 
 	/* keys are now sorted primarily by their name and secondarily by
 	   their index. If any indexes are missing, fallback to assuming
diff -r a6e1f054a808 -r 0059b2381024 src/lib-settings/settings-parser.c
--- a/src/lib-settings/settings-parser.c	Wed Jun 17 14:52:30 2009 -0400
+++ b/src/lib-settings/settings-parser.c	Wed Jun 17 14:52:47 2009 -0400
@@ -555,10 +555,8 @@ int settings_parse_file(struct setting_p
 	return ret;
 }
 
-static int environ_cmp(const void *p1, const void *p2)
-{
-	const char *const *s1 = p1, *const *s2 = p2;
-
+static int environ_cmp(char *const *s1, char *const *s2)
+{
 	return -strcmp(*s1, *s2);
 }
 
@@ -567,7 +565,7 @@ int settings_parse_environ(struct settin
 	extern char **environ;
 	ARRAY_TYPE(string) sorted_envs_arr;
 	const char *key, *value;
-	char **sorted_envs;
+	char *const *sorted_envs;
 	unsigned int i, count;
 	int ret = 0;
 
@@ -577,8 +575,8 @@ int settings_parse_environ(struct settin
 	i_array_init(&sorted_envs_arr, 128);
 	for (i = 0; environ[i] != NULL; i++)
 		array_append(&sorted_envs_arr, &environ[i], 1);
-	sorted_envs = array_get_modifiable(&sorted_envs_arr, &count);
-	qsort(sorted_envs, count, sizeof(*sorted_envs), environ_cmp);
+	array_sort(&sorted_envs_arr, environ_cmp);
+	sorted_envs = array_get(&sorted_envs_arr, &count);
 
 	for (i = 0; i < count && ret == 0; i++) {
 		value = strchr(sorted_envs[i], '=');
diff -r a6e1f054a808 -r 0059b2381024 src/lib-storage/index/dbox/dbox-storage-rebuild.c
--- a/src/lib-storage/index/dbox/dbox-storage-rebuild.c	Wed Jun 17 14:52:30 2009 -0400
+++ b/src/lib-storage/index/dbox/dbox-storage-rebuild.c	Wed Jun 17 14:52:47 2009 -0400
@@ -261,16 +261,16 @@ static int rebuild_apply_map(struct dbox
 {
 	struct dbox_map *map = ctx->storage->map;
 	const struct mail_index_header *hdr;
-	struct dbox_rebuild_msg **msgs, **pos;
+	struct dbox_rebuild_msg *const *msgs, **pos;
 	struct dbox_rebuild_msg search_msg, *search_msgp = &search_msg;
 	struct dbox_mail_lookup_rec rec;
 	uint32_t seq;
 	unsigned int count;
 
+	if (ctx->msgs_unsorted)
+		array_sort(&ctx->msgs, dbox_rebuild_msg_offset_cmp);
+
 	msgs = array_get_modifiable(&ctx->msgs, &count);
-	if (ctx->msgs_unsorted)
-		qsort(msgs, count, sizeof(*msgs), dbox_rebuild_msg_offset_cmp);
-
 	hdr = mail_index_get_header(ctx->sync_view);
 	for (seq = 1; seq <= hdr->messages_count; seq++) {
 		if (dbox_map_view_lookup_rec(map, ctx->sync_view,
@@ -295,7 +295,7 @@ static int rebuild_apply_map(struct dbox
 
 	/* afterwards we're interested in looking up map_uids.
 	   re-sort the messages to make it easier. */
-	qsort(msgs, count, sizeof(*msgs), dbox_rebuild_msg_uid_cmp);
+	array_sort(&ctx->msgs, dbox_rebuild_msg_uid_cmp);
 	return 0;
 }
 
diff -r a6e1f054a808 -r 0059b2381024 src/lib-storage/index/dbox/dbox-sync-file.c
--- a/src/lib-storage/index/dbox/dbox-sync-file.c	Wed Jun 17 14:52:30 2009 -0400
+++ b/src/lib-storage/index/dbox/dbox-sync-file.c	Wed Jun 17 14:52:47 2009 -0400
@@ -115,7 +115,7 @@ int dbox_sync_file_purge(struct dbox_fil
 	struct ostream *output = NULL;
 	struct dbox_map_append_context *append_ctx;
 	ARRAY_TYPE(dbox_map_file_msg) msgs_arr;
-	struct dbox_map_file_msg *msgs;
+	const struct dbox_map_file_msg *msgs;
 	ARRAY_TYPE(seq_range) expunged_map_uids;
 	ARRAY_TYPE(uint32_t) copied_map_uids;
 	unsigned int i, count;
@@ -145,10 +145,10 @@ int dbox_sync_file_purge(struct dbox_fil
 		dbox_file_unlock(file);
 		return -1;
 	}
-	msgs = array_get_modifiable(&msgs_arr, &count);
 	/* sort messages by their offset */
-	qsort(msgs, count, sizeof(*msgs), dbox_map_file_msg_offset_cmp);
-
+	array_sort(&msgs_arr, dbox_map_file_msg_offset_cmp);
+
+	msgs = array_get(&msgs_arr, &count);
 	append_ctx = dbox_map_append_begin_storage(file->storage);
 	i_array_init(&copied_map_uids, I_MIN(count, 1));
 	i_array_init(&expunged_map_uids, I_MIN(count, 1));
diff -r a6e1f054a808 -r 0059b2381024 src/lib-storage/index/index-mail-headers.c
--- a/src/lib-storage/index/index-mail-headers.c	Wed Jun 17 14:52:30 2009 -0400
+++ b/src/lib-storage/index/index-mail-headers.c	Wed Jun 17 14:52:47 2009 -0400
@@ -32,9 +32,9 @@ static const enum message_parser_flags m
 static const enum message_parser_flags msg_parser_flags =
 	MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK;
 
-static int header_line_cmp(const void *p1, const void *p2)
-{
-	const struct index_mail_line *l1 = p1, *l2 = p2;
+static int header_line_cmp(const struct index_mail_line *l1,
+			   const struct index_mail_line *l2)
+{
 	int diff;
 
 	diff = (int)l1->field_idx - (int)l2->field_idx;
@@ -44,7 +44,7 @@ static int header_line_cmp(const void *p
 
 static void index_mail_parse_header_finish(struct index_mail *mail)
 {
-	struct index_mail_line *lines;
+	const struct index_mail_line *lines;
 	const unsigned char *header, *data;
 	const uint8_t *match;
 	buffer_t *buf;
@@ -52,12 +52,11 @@ static void index_mail_parse_header_fini
 	unsigned int i, j, count, match_idx, match_count;
 	bool noncontiguous;
 
-	lines = array_get_modifiable(&mail->header_lines, &count);
-
 	/* sort it first so fields are grouped together and ordered by
 	   line number */
-	qsort(lines, count, sizeof(*lines), header_line_cmp);
-
+	array_sort(&mail->header_lines, header_line_cmp);
+
+	lines = array_get(&mail->header_lines, &count);


More information about the dovecot-cvs mailing list