]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Use array_sort() instead of qsort() wherever possible.
authorTimo Sirainen <tss@iki.fi>
Wed, 17 Jun 2009 18:52:47 +0000 (14:52 -0400)
committerTimo Sirainen <tss@iki.fi>
Wed, 17 Jun 2009 18:52:47 +0000 (14:52 -0400)
--HG--
branch : HEAD

21 files changed:
src/config/doveconf.c
src/imap/imap-commands.c
src/imap/imap-fetch.c
src/lib-index/mail-cache-lookup.c
src/lib-mail/rfc2231-parser.c
src/lib-settings/settings-parser.c
src/lib-storage/index/dbox/dbox-storage-rebuild.c
src/lib-storage/index/dbox/dbox-sync-file.c
src/lib-storage/index/index-mail-headers.c
src/lib-storage/index/index-sort-string.c
src/lib-storage/index/index-sort.c
src/lib-storage/index/index-thread-finish.c
src/lib-storage/index/maildir/maildir-sync-index.c
src/lib-storage/index/maildir/maildir-uidlist.c
src/lib/module-dir.c
src/plugins/acl/acl-backend-vfile.c
src/plugins/acl/acl-lookup-dict.c
src/plugins/fts/fts-storage.c
src/plugins/trash/trash-plugin.c
src/plugins/virtual/virtual-search.c
src/plugins/virtual/virtual-sync.c

index 8c16ea7d4e9f9a541ceea1a971652f79a295e095..4420edc80e369d128f29fee4a4b2a15f3338e114 100644 (file)
@@ -32,10 +32,9 @@ config_request_get_strings(const char *key, const char *value,
        array_append(&ctx->strings, &value, 1);
 }
 
-static int config_string_cmp(const void *p1, const void *p2)
+static int config_string_cmp(const char *const *p1, const char *const *p2)
 {
-       const char *s1 = *(const char *const *)p1;
-       const char *s2 = *(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_human(struct ostream *output,
        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_human(struct ostream *output,
        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 {
index 1fc826d5a3a6f5f651dc27cc2c154a29a3b39980..9aeeb1b17c0b24fe472445c285ed0bcdb9417e4b 100644 (file)
@@ -104,10 +104,8 @@ void command_unregister_array(const struct command *cmdarr, unsigned int count)
        }
 }
 
-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 *name, const void *cmd_p)
 
 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);
 }
index 5d7866882b20a611b3d1def0d447727f3fa6db63..2f5844d57c1ca8f0c11427b13241e3cbcbc00f47 100644 (file)
 
 static ARRAY_DEFINE(fetch_handlers, struct imap_fetch_handler);
 
-static int imap_fetch_handler_cmp(const void *p1, const void *p2)
+static int imap_fetch_handler_cmp(const struct imap_fetch_handler *h1,
+                                 const struct imap_fetch_handler *h2)
 {
-        const struct imap_fetch_handler *h1 = p1, *h2 = p2;
-
        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)
index 29a4e169e8775ce420a77b5dee539fee80666573..21aaf5c96a261e81aaf655c72f834e2dff83f0d5 100644 (file)
@@ -442,10 +442,9 @@ static void header_lines_save(struct header_lookup_context *ctx,
        }
 }
 
-static int header_lookup_line_cmp(const void *p1, const void *p2)
+static int header_lookup_line_cmp(const struct header_lookup_line *l1,
+                                 const struct header_lookup_line *l2)
 {
-       const struct header_lookup_line *l1 = p1, *l2 = p2;
-
        return (int)l1->line_num - (int)l2->line_num;
 }
 
@@ -518,8 +517,8 @@ mail_cache_lookup_headers_real(struct mail_cache_view *view, string_t *dest,
 
        /* 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++) {
index 8a4d280646bed81f06147e549dfbe7720f4fa0e6..9d35d4b86ef8234a4b6b9c5d53b202f37a70acd9 100644 (file)
@@ -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_context *ctx,
 {
        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_context *ctx,
 
        /* 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
index 07a9291ea5d435073cb31a3aee2596f55337a97c..e9a734c992ebba2fa906d11b0b6de5a40fc1a17e 100644 (file)
@@ -555,10 +555,8 @@ int settings_parse_file(struct setting_parser_context *ctx,
        return ret;
 }
 
-static int environ_cmp(const void *p1, const void *p2)
+static int environ_cmp(char *const *s1, char *const *s2)
 {
-       const char *const *s1 = p1, *const *s2 = p2;
-
        return -strcmp(*s1, *s2);
 }
 
@@ -567,7 +565,7 @@ int settings_parse_environ(struct setting_parser_context *ctx)
        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 setting_parser_context *ctx)
        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], '=');
index 3da61c6842e53dc33c490d568307b51a0b65d352..090aadb67714406a0b1c7f4a4fadb7c63e611cd6 100644 (file)
@@ -261,16 +261,16 @@ static int rebuild_apply_map(struct dbox_storage_rebuild_context *ctx)
 {
        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;
 
-       msgs = array_get_modifiable(&ctx->msgs, &count);
        if (ctx->msgs_unsorted)
-               qsort(msgs, count, sizeof(*msgs), dbox_rebuild_msg_offset_cmp);
+               array_sort(&ctx->msgs, dbox_rebuild_msg_offset_cmp);
 
+       msgs = array_get_modifiable(&ctx->msgs, &count);
        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_storage_rebuild_context *ctx)
 
        /* 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;
 }
 
index 1e765250cfe692cefce63193146637656d318cdd..948452373def797c9383b3d6ffe78dafe656a26c 100644 (file)
@@ -115,7 +115,7 @@ int dbox_sync_file_purge(struct dbox_file *file)
        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_file *file)
                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));
index b9f13768f72f95c41c153980767b876166c18686..6724a2d3029e5370a270697fa6d8b7a0e9e34c4f 100644 (file)
@@ -32,9 +32,9 @@ static const enum message_header_parser_flags hdr_parser_flags =
 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)
+static int header_line_cmp(const struct index_mail_line *l1,
+                          const struct index_mail_line *l2)
 {
-       const struct index_mail_line *l1 = p1, *l2 = p2;
        int diff;
 
        diff = (int)l1->field_idx - (int)l2->field_idx;
@@ -44,7 +44,7 @@ static int header_line_cmp(const void *p1, const void *p2)
 
 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_finish(struct index_mail *mail)
        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);
        match = array_get(&mail->header_match, &match_count);
        header = buffer_get_data(mail->header_data, NULL);
        buf = buffer_create_dynamic(pool_datastack_create(), 256);
index 6ee18ace06e79f322d199523fbaf36d8802727de..9f072d0170cf9b394658961000b46625dcdd83b9 100644 (file)
@@ -93,18 +93,6 @@ static int sort_node_seq_cmp(const void *p1, const void *p2)
        return 0;
 }
 
-static void index_sort_nodes_by_seq(struct sort_string_context *ctx)
-{
-       struct mail_sort_node *nodes;
-       unsigned int count;
-
-       nodes = array_get_modifiable(&ctx->zero_nodes, &count);
-       qsort(nodes, count, sizeof(struct mail_sort_node), sort_node_seq_cmp);
-
-       nodes = array_get_modifiable(&ctx->nonzero_nodes, &count);
-       qsort(nodes, count, sizeof(struct mail_sort_node), sort_node_seq_cmp);
-}
-
 static void index_sort_generate_seqs(struct sort_string_context *ctx)
 {
        struct mail_sort_node *nodes, *nodes2;
@@ -241,10 +229,10 @@ void index_sort_list_add_string(struct mail_search_sort_program *program,
        index_sort_node_add(ctx, &node);
 }
 
-static int sort_node_zero_string_cmp(const void *p1, const void *p2)
+static int sort_node_zero_string_cmp(const struct mail_sort_node *n1,
+                                    const struct mail_sort_node *n2)
 {
        struct sort_string_context *ctx = static_zero_cmp_context;
-       const struct mail_sort_node *n1 = p1, *n2 = p2;
        int ret;
 
        ret = strcmp(ctx->sort_strings[n1->seq], ctx->sort_strings[n2->seq]);
@@ -288,8 +276,7 @@ static void index_sort_zeroes(struct sort_string_context *ctx)
 
        /* we have all strings, sort nodes based on them */
        static_zero_cmp_context = ctx;
-       qsort(nodes, count, sizeof(struct mail_sort_node),
-             sort_node_zero_string_cmp);
+       array_sort(&ctx->zero_nodes, sort_node_zero_string_cmp);
 }
 
 static const char *
@@ -713,10 +700,10 @@ static void index_sort_write_changed_sort_ids(struct sort_string_context *ctx)
        }
 }
 
-static int sort_node_cmp(const void *p1, const void *p2)
+static int sort_node_cmp(const struct mail_sort_node *n1,
+                        const struct mail_sort_node *n2)
 {
        struct sort_string_context *ctx = static_zero_cmp_context;
-       const struct mail_sort_node *n1 = p1, *n2 = p2;
 
        if (n1->sort_id < n2->sort_id)
                return !ctx->reverse ? -1 : 1;
@@ -782,22 +769,21 @@ static void index_sort_list_reset_broken(struct sort_string_context *ctx)
 void index_sort_list_finish_string(struct mail_search_sort_program *program)
 {
        struct sort_string_context *ctx = program->context;
-       struct mail_sort_node *nodes;
+       const struct mail_sort_node *nodes;
        unsigned int i, count;
        uint32_t seq;
 
-       nodes = array_get_modifiable(&ctx->nonzero_nodes, &count);
-
        static_zero_cmp_context = ctx;
        if (array_count(&ctx->zero_nodes) == 0) {
                /* fast path: we have all sort IDs */
-               qsort(nodes, count, sizeof(struct mail_sort_node),
-                     sort_node_cmp);
+               array_sort(&ctx->nonzero_nodes, sort_node_cmp);
 
                if (!array_is_created(&program->seqs))
                        i_array_init(&program->seqs, count);
                else
                        array_clear(&program->seqs);
+
+               nodes = array_get(&ctx->nonzero_nodes, &count);
                for (i = 0; i < count; i++) {
                        seq = nodes[i].seq;
                        array_append(&program->seqs, &seq, 1);
@@ -806,7 +792,8 @@ void index_sort_list_finish_string(struct mail_search_sort_program *program)
        } else {
                if (ctx->seqs_nonsorted) {
                        /* the nodes need to be sorted by sequence initially */
-                       index_sort_nodes_by_seq(ctx);
+                       array_sort(&ctx->zero_nodes, sort_node_seq_cmp);
+                       array_sort(&ctx->nonzero_nodes, sort_node_seq_cmp);
                }
 
                /* we have to add some sort IDs. we'll do this for all
@@ -816,9 +803,7 @@ void index_sort_list_finish_string(struct mail_search_sort_program *program)
                /* add messages not in seqs list */
                index_sort_add_missing(ctx);
                /* sort all messages with sort IDs */
-               nodes = array_get_modifiable(&ctx->nonzero_nodes, &count);
-               qsort(nodes, count, sizeof(struct mail_sort_node),
-                     sort_node_cmp);
+               array_sort(&ctx->nonzero_nodes, sort_node_cmp);
                for (;;) {
                        /* sort all messages without sort IDs */
                        index_sort_zeroes(ctx);
@@ -851,7 +836,7 @@ void index_sort_list_finish_string(struct mail_search_sort_program *program)
                        array_reverse(&ctx->sorted_nodes);
                }
 
-               nodes = array_get_modifiable(&ctx->sorted_nodes, &count);
+               nodes = array_get(&ctx->sorted_nodes, &count);
                array_clear(&program->seqs);
                for (i = 0; i < count; i++) {
                        if (nodes[i].wanted) {
index 3f0641683446c4ec43a8e67edc2037d5cf3f2b2c..ab9af130e92a4096d1ae2b78775d58a1d688af42 100644 (file)
@@ -110,10 +110,10 @@ void index_sort_list_add(struct mail_search_sort_program *program,
        program->sort_list_add(program, mail);
 }
 
-static int sort_node_date_cmp(const void *p1, const void *p2)
+static int sort_node_date_cmp(const struct mail_sort_node_date *n1,
+                             const struct mail_sort_node_date *n2)
 {
        struct sort_cmp_context *ctx = &static_node_cmp_context;
-       const struct mail_sort_node_date *n1 = p1, *n2 = p2;
 
        if (n1->date < n2->date)
                return !ctx->reverse ? -1 : 1;
@@ -129,21 +129,17 @@ static void
 index_sort_list_finish_date(struct mail_search_sort_program *program)
 {
        ARRAY_TYPE(mail_sort_node_date) *nodes = program->context;
-       struct mail_sort_node_date *date_nodes;
-       unsigned int count;
 
-       date_nodes = array_get_modifiable(nodes, &count);
-       qsort(date_nodes, count, sizeof(struct mail_sort_node_date),
-             sort_node_date_cmp);
+       array_sort(nodes, sort_node_date_cmp);
        memcpy(&program->seqs, nodes, sizeof(program->seqs));
        i_free(nodes);
        program->context = NULL;
 }
 
-static int sort_node_size_cmp(const void *p1, const void *p2)
+static int sort_node_size_cmp(const struct mail_sort_node_size *n1,
+                             const struct mail_sort_node_size *n2)
 {
        struct sort_cmp_context *ctx = &static_node_cmp_context;
-       const struct mail_sort_node_size *n1 = p1, *n2 = p2;
 
        if (n1->size < n2->size)
                return !ctx->reverse ? -1 : 1;
@@ -159,21 +155,17 @@ static void
 index_sort_list_finish_size(struct mail_search_sort_program *program)
 {
        ARRAY_TYPE(mail_sort_node_size) *nodes = program->context;
-       struct mail_sort_node_size *size_nodes;
-       unsigned int count;
 
-       size_nodes = array_get_modifiable(nodes, &count);
-       qsort(size_nodes, count, sizeof(struct mail_sort_node_size),
-             sort_node_size_cmp);
+       array_sort(nodes, sort_node_size_cmp);
        memcpy(&program->seqs, nodes, sizeof(program->seqs));
        i_free(nodes);
        program->context = NULL;
 }
 
-static int sort_node_float_cmp(const void *p1, const void *p2)
+static int sort_node_float_cmp(const struct mail_sort_node_float *n1,
+                              const struct mail_sort_node_float *n2)
 {
        struct sort_cmp_context *ctx = &static_node_cmp_context;
-       const struct mail_sort_node_float *n1 = p1, *n2 = p2;
 
        if (n1->num < n2->num)
                return !ctx->reverse ? -1 : 1;
@@ -189,12 +181,8 @@ static void
 index_sort_list_finish_float(struct mail_search_sort_program *program)
 {
        ARRAY_TYPE(mail_sort_node_float) *nodes = program->context;
-       struct mail_sort_node_float *float_nodes;
-       unsigned int count;
 
-       float_nodes = array_get_modifiable(nodes, &count);
-       qsort(float_nodes, count, sizeof(struct mail_sort_node_float),
-             sort_node_float_cmp);
+       array_sort(nodes, sort_node_float_cmp);
        memcpy(&program->seqs, nodes, sizeof(program->seqs));
        i_free(nodes);
        program->context = NULL;
index 283a4803d284519d2f3e4a8ad54b1e6aca8e77f5..ecbf21fb292a643adb108f2dad3103fbabb68b46 100644 (file)
@@ -112,10 +112,9 @@ add_base_subject(struct subject_gather_context *ctx, const char *subject,
        node->reply_or_forward = is_reply_or_forward;
 }
 
-static int mail_thread_child_node_cmp(const void *p1, const void *p2)
+static int mail_thread_child_node_cmp(const struct mail_thread_child_node *c1,
+                                     const struct mail_thread_child_node *c2)
 {
-       const struct mail_thread_child_node *c1 = p1, *c2 = p2;
-
        if (c1->sort_date < c2->sort_date)
                return -1;
        if (c1->sort_date > c2->sort_date)
@@ -128,6 +127,12 @@ static int mail_thread_child_node_cmp(const void *p1, const void *p2)
        return 0;
 }
 
+static int mail_thread_root_node_cmp(const struct mail_thread_root_node *r1,
+                                    const struct mail_thread_root_node *r2)
+{
+       return mail_thread_child_node_cmp(&r1->node, &r2->node);
+}
+
 static uint32_t
 thread_lookup_existing(struct thread_finish_context *ctx, uint32_t idx)
 {
@@ -170,7 +175,7 @@ thread_sort_children(struct thread_finish_context *ctx, uint32_t parent_idx,
                     ARRAY_TYPE(mail_thread_child_node) *sorted_children)
 {
        const struct mail_thread_shadow_node *shadows;
-       struct mail_thread_child_node child, *children;
+       struct mail_thread_child_node child;
        unsigned int count;
 
        memset(&child, 0, sizeof(child));
@@ -195,8 +200,7 @@ thread_sort_children(struct thread_finish_context *ctx, uint32_t parent_idx,
        }
 
        /* sort the children */
-       children = array_get_modifiable(sorted_children, &count);
-       qsort(children, count, sizeof(*children), mail_thread_child_node_cmp);
+       array_sort(sorted_children, mail_thread_child_node_cmp);
 }
 
 static void gather_base_subjects(struct thread_finish_context *ctx)
@@ -402,7 +406,7 @@ static void sort_root_nodes(struct thread_finish_context *ctx)
                }
        }
        array_free(&sorted_children);
-       qsort(roots, count, sizeof(*roots), mail_thread_child_node_cmp);
+       array_sort(&ctx->roots, mail_thread_root_node_cmp);
 }
 
 static int mail_thread_root_node_idx_cmp(const void *key, const void *value)
@@ -455,7 +459,7 @@ static void sort_root_nodes_ref2(struct thread_finish_context *ctx,
                if (root->node.sort_date < child.sort_date)
                        root->node.sort_date = child.sort_date;
        }
-       qsort(roots, root_count, sizeof(*roots), mail_thread_child_node_cmp);
+       array_sort(&ctx->roots, mail_thread_root_node_cmp);
 }
 
 static void mail_thread_create_shadows(struct thread_finish_context *ctx,
index b0834db66e676d1ac96184c7d1a583b3f6e545a4..97e090ebfc881c18f8e607ac7815c39c5f6a1339 100644 (file)
@@ -295,7 +295,8 @@ maildir_sync_mail_keywords(struct maildir_index_sync_context *ctx, uint32_t seq)
 {
        struct maildir_mailbox *mbox = ctx->mbox;
        struct mail_keywords *kw;
-       unsigned int i, j, old_count, new_count, *old_indexes, *new_indexes;
+       unsigned int i, j, old_count, new_count;
+       const unsigned int *old_indexes, *new_indexes;
        bool have_indexonly_keywords;
        int diff;
 
@@ -306,12 +307,11 @@ maildir_sync_mail_keywords(struct maildir_index_sync_context *ctx, uint32_t seq)
        }
 
        /* sort the keywords */
-       old_indexes = array_get_modifiable(&ctx->idx_keywords, &old_count);
-       qsort(old_indexes, old_count, sizeof(*old_indexes), uint_cmp);
-       new_indexes = array_get_modifiable(&ctx->keywords, &new_count);
-       qsort(new_indexes, new_count, sizeof(*new_indexes), uint_cmp);
+       array_sort(&ctx->idx_keywords, uint_cmp);
+       array_sort(&ctx->keywords, uint_cmp);
 
        /* drop keywords that are in index-only. we don't want to touch them. */
+       old_indexes = array_get(&ctx->idx_keywords, &old_count);
        have_indexonly_keywords = FALSE;
        for (i = old_count; i > 0; i--) {
                if (old_indexes[i-1] < MAILDIR_MAX_KEYWORDS)
@@ -338,16 +338,14 @@ maildir_sync_mail_keywords(struct maildir_index_sync_context *ctx, uint32_t seq)
           so first remove the unwanted keywords and then add back the wanted
           ones. we can get these lists easily by removing common elements
           from old and new keywords. */
-       new_indexes = array_get_modifiable(&ctx->keywords, &new_count);
+       new_indexes = array_get(&ctx->keywords, &new_count);
        for (i = j = 0; i < old_count && j < new_count; ) {
                diff = (int)old_indexes[i] - (int)new_indexes[j];
                if (diff == 0) {
                        array_delete(&ctx->keywords, j, 1);
                        array_delete(&ctx->idx_keywords, i, 1);
-                       old_indexes = array_get_modifiable(&ctx->idx_keywords,
-                                                          &old_count);
-                       new_indexes = array_get_modifiable(&ctx->keywords,
-                                                          &new_count);
+                       old_indexes = array_get(&ctx->idx_keywords, &old_count);
+                       new_indexes = array_get(&ctx->keywords, &new_count);
                } else if (diff < 0) {
                        i++;
                } else {
index 9a5838f4a82c2ea725472b57b412295a58415516..b1bf3cb808f4ffad5b06e92b481231060c689fde 100644 (file)
@@ -671,12 +671,7 @@ static int maildir_uidlist_read_header(struct maildir_uidlist *uidlist,
 
 static void maildir_uidlist_records_sort_by_uid(struct maildir_uidlist *uidlist)
 {
-       struct maildir_uidlist_rec **recs;
-       unsigned int count;
-
-       recs = array_get_modifiable(&uidlist->records, &count);
-       qsort(recs, count, sizeof(*recs), maildir_uid_cmp);
-
+       array_sort(&uidlist->records, maildir_uid_cmp);
        uidlist->unsorted = FALSE;
 }
 
@@ -1762,12 +1757,9 @@ static void maildir_uidlist_assign_uids(struct maildir_uidlist_sync_ctx *ctx)
 static void maildir_uidlist_swap(struct maildir_uidlist_sync_ctx *ctx)
 {
        struct maildir_uidlist *uidlist = ctx->uidlist;
-       struct maildir_uidlist_rec **recs;
-       unsigned int count;
 
        /* buffer is unsorted, sort it by UID */
-       recs = array_get_modifiable(&ctx->records, &count);
-       qsort(recs, count, sizeof(*recs), maildir_uid_cmp);
+       array_sort(&ctx->records, maildir_uid_cmp);
 
        array_free(&uidlist->records);
        uidlist->records = ctx->records;
@@ -1783,7 +1775,8 @@ static void maildir_uidlist_swap(struct maildir_uidlist_sync_ctx *ctx)
        ctx->record_pool = NULL;
 
        if (ctx->new_files_count != 0) {
-               ctx->first_nouid_pos = count - ctx->new_files_count;
+               ctx->first_nouid_pos = array_count(&uidlist->records) -
+                       ctx->new_files_count;
                maildir_uidlist_assign_uids(ctx);
        } else {
                ctx->uidlist->change_counter++;
index aa369b8a01f1169b9f61bfe8e55d7b90a4e3bdca..a02520064815256d59e3fadbf8c6ff170111d04f 100644 (file)
@@ -133,9 +133,8 @@ module_load(const char *path, const char *name, bool require_init_funcs,
        return module;
 }
 
-static int module_name_cmp(const void *p1, const void *p2)
+static int module_name_cmp(const char *const *n1, const char *const *n2)
 {
-       const char *const *n1 = p1, *const *n2 = p2;
        const char *s1 = *n1, *s2 = *n2;
 
        if (strncmp(s1, "lib", 3) == 0)
@@ -189,7 +188,7 @@ module_dir_load_real(const char *dir, const char *module_names,
 {
        DIR *dirp;
        struct dirent *d;
-       const char *name, *p, **names_p;
+       const char *name, *p, *const *names_p;
        const char **module_names_arr;
        struct module *modules, *module, **module_pos;
        unsigned int i, count;
@@ -233,8 +232,8 @@ module_dir_load_real(const char *dir, const char *module_names,
                array_append(&names, &name, 1);
        }
 
-       names_p = array_get_modifiable(&names, &count);
-       qsort(names_p, count, sizeof(const char *), module_name_cmp);
+       array_sort(&names, module_name_cmp);
+       names_p = array_get(&names, &count);
 
        if (module_names == NULL)
                module_names_arr = NULL;
index ca880563fadd062323951867b93592443a158905..542de61f5e2131ac6c3f2048044e225f1e6dec9d 100644 (file)
@@ -283,14 +283,14 @@ static const char *const *
 acl_rights_alloc(pool_t pool, ARRAY_TYPE(const_string) *rights_arr,
                 bool dup_strings)
 {
-       const char **ret, **rights;
+       const char **ret, *const *rights;
        unsigned int i, dest, count;
 
        /* sort the rights first so we can easily drop duplicates */
-       rights = array_get_modifiable(rights_arr, &count);
-       qsort(rights, count, sizeof(*rights), i_strcmp_p);
+       array_sort(rights_arr, i_strcmp_p);
 
        /* @UNSAFE */
+       rights = array_get(rights_arr, &count);
        ret = p_new(pool, const char *, count + 1);
        if (count > 0) {
                ret[0] = rights[0];
@@ -639,9 +639,9 @@ int acl_backend_vfile_object_get_mtime(struct acl_object *aclobj,
        return 0;
 }
 
-static int acl_rights_cmp(const void *p1, const void *p2)
+static int acl_rights_cmp(const struct acl_rights *r1,
+                         const struct acl_rights *r2)
 {
-       const struct acl_rights *r1 = p1, *r2 = p2;
        int ret;
 
        if (r1->global != r2->global) {
@@ -656,6 +656,11 @@ static int acl_rights_cmp(const void *p1, const void *p2)
        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
 acl_rights_merge(pool_t pool, const char *const **destp, const char *const *src,
                 bool dup_strings)
@@ -685,10 +690,10 @@ static void acl_backend_vfile_rights_sort(struct acl_object_vfile *aclobj)
        if (!array_is_created(&aclobj->rights))
                return;
 
-       rights = array_get_modifiable(&aclobj->rights, &count);
-       qsort(rights, count, sizeof(*rights), acl_rights_cmp);
+       array_sort(&aclobj->rights, acl_rights_cmp);
 
        /* merge identical identifiers */
+       rights = array_get_modifiable(&aclobj->rights, &count);
        for (dest = 0, i = 1; i < count; i++) {
                if (acl_rights_cmp(&rights[i], &rights[dest]) == 0) {
                        /* add i's rights to dest and delete i */
@@ -1095,7 +1100,7 @@ acl_backend_vfile_object_update(struct acl_object *_aclobj,
 
        rights = array_get(&aclobj->rights, &count);
        if (!bsearch_insert_pos(&update->rights, rights, count, sizeof(*rights),
-                               acl_rights_cmp, &i))
+                               acl_rights_bsearch_cmp, &i))
                changed = vfile_object_add_right(aclobj, i, update);
        else
                changed = vfile_object_modify_right(aclobj, i, update);
index 2706242f75d324f2487410186f9272fe223d03fd..f0296d09fa93cc244998a0ec8b8bc28f45b9996b 100644 (file)
@@ -137,7 +137,7 @@ acl_lookup_dict_rebuild_update(struct acl_lookup_dict *dict,
        const char *username = dict->user->username;
        struct dict_iterate_context *iter;
        struct dict_transaction_context *dt;
-       const char *prefix, *key, *value, **old_ids, *const *new_ids, *p;
+       const char *prefix, *key, *value, *const *old_ids, *const *new_ids, *p;
        ARRAY_TYPE(const_string) old_ids_arr;
        unsigned int newi, oldi, old_count, new_count;
        string_t *path;
@@ -169,14 +169,14 @@ acl_lookup_dict_rebuild_update(struct acl_lookup_dict *dict,
        }
 
        /* sort the existing identifiers */
-       old_ids = array_get_modifiable(&old_ids_arr, &old_count);
-       qsort(old_ids, old_count, sizeof(*old_ids), i_strcmp_p);
+       array_sort(&old_ids_arr, i_strcmp_p);
 
        /* sync the identifiers */
        path = t_str_new(256);
        str_append(path, prefix);
 
        dt = dict_transaction_begin(dict->dict);
+       old_ids = array_get(&old_ids_arr, &old_count);
        new_ids = array_get(new_ids_arr, &new_count);
        for (newi = oldi = 0; newi < new_count || oldi < old_count; ) {
                ret = newi == new_count ? 1 :
@@ -226,9 +226,9 @@ int acl_lookup_dict_rebuild(struct acl_lookup_dict *dict)
        }
 
        /* sort identifiers and remove duplicates */
-       ids = array_get_modifiable(&ids_arr, &count);
-       qsort(ids, count, sizeof(*ids), i_strcmp_p);
+       array_sort(&ids_arr, i_strcmp_p);
 
+       ids = array_get_modifiable(&ids_arr, &count);
        for (i = 1, dest = 0; i < count; i++) {
                if (strcmp(ids[dest], ids[i]) != 0) {
                        if (++dest != i)
index 8766b86d4a3cc4a075fb93ca64b4692fd7f4c54f..71a5284eeb4b52d297a434b915e5dfc77253a2dd 100644 (file)
@@ -268,9 +268,9 @@ fts_build_init_box(struct fts_search_context *fctx, struct mailbox *box,
                                  seq1, seq2, last_uid);
 }
 
-static int mailbox_name_cmp(const void *p1, const void *p2)
+static int mailbox_name_cmp(const struct fts_orig_mailboxes *box1,
+                           const struct fts_orig_mailboxes *box2)
 {
-       const struct fts_orig_mailboxes *box1 = p1, *box2 = p2;
        int ret;
 
        T_BEGIN {
@@ -286,10 +286,10 @@ static int mailbox_name_cmp(const void *p1, const void *p2)
        return ret;
 }
 
-static int fts_backend_uid_map_mailbox_cmp(const void *p1, const void *p2)
+static int
+fts_backend_uid_map_mailbox_cmp(const struct fts_backend_uid_map *map1,
+                               const struct fts_backend_uid_map *map2)
 {
-       const struct fts_backend_uid_map *map1 = p1, *map2 = p2;
-
        return strcmp(map1->mailbox, map2->mailbox);
 }
 
@@ -372,12 +372,11 @@ fts_box_get_root(struct mailbox *box, struct mail_namespace **ns_r)
 static int fts_build_init_virtual(struct fts_search_context *fctx)
 {
        struct fts_search_virtual_context *vctx = &fctx->virtual_ctx;
-       struct fts_backend_uid_map *last_uids;
        ARRAY_TYPE(mailboxes) mailboxes;
        struct mailbox *const *boxes;
-       struct fts_orig_mailboxes *orig_boxes;
+       const struct fts_orig_mailboxes *orig_boxes;
        struct fts_orig_mailboxes orig_box;
-       unsigned int i, box_count, last_uid_count;
+       unsigned int i, box_count;
        int ret;
 
        t_array_init(&mailboxes, 64);
@@ -393,8 +392,7 @@ static int fts_build_init_virtual(struct fts_search_context *fctx)
                array_append(&vctx->orig_mailboxes, &orig_box, 1);
        }
 
-       orig_boxes = array_get_modifiable(&vctx->orig_mailboxes, &box_count);
-
+       orig_boxes = array_get(&vctx->orig_mailboxes, &box_count);
        if (box_count <= 0) {
                if (box_count == 0) {
                        /* empty virtual mailbox */
@@ -416,11 +414,9 @@ static int fts_build_init_virtual(struct fts_search_context *fctx)
                pool_unref(&vctx->pool);
                return -1;
        }
-       last_uids = array_get_modifiable(&vctx->last_uids, &last_uid_count);
 
-       qsort(orig_boxes, box_count, sizeof(*orig_boxes), mailbox_name_cmp);
-       qsort(last_uids, last_uid_count, sizeof(*last_uids),
-             fts_backend_uid_map_mailbox_cmp);
+       array_sort(&vctx->orig_mailboxes, mailbox_name_cmp);
+       array_sort(&vctx->last_uids, fts_backend_uid_map_mailbox_cmp);
 
        ret = fts_build_init_virtual_next(fctx);
        return ret < 0 ? -1 : 0;
index fee57e44f13cd2405c9b5660c60a864e8e78fe7c..1cd3d3f952871d85f244a3989c9f874ea421fb55 100644 (file)
@@ -241,10 +241,9 @@ static bool trash_find_storage(struct mail_user *user,
        return FALSE;
 }
 
-static int trash_mailbox_priority_cmp(const void *p1, const void *p2)
+static int trash_mailbox_priority_cmp(const struct trash_mailbox *t1,
+                                     const struct trash_mailbox *t2)
 {
-       const struct trash_mailbox *t1 = p1, *t2 = p2;
-
        return t1->priority - t2->priority;
 }
 
@@ -254,7 +253,6 @@ static int read_configuration(struct mail_user *user, const char *path)
        struct istream *input;
        const char *line, *name;
        struct trash_mailbox *trash;
-       unsigned int count;
        int fd, ret = 0;
 
        fd = open(path, O_RDONLY);
@@ -291,8 +289,7 @@ static int read_configuration(struct mail_user *user, const char *path)
        i_stream_destroy(&input);
        (void)close(fd);
 
-       trash = array_get_modifiable(&tuser->trash_boxes, &count);
-       qsort(trash, count, sizeof(*trash), trash_mailbox_priority_cmp);
+       array_sort(&tuser->trash_boxes, trash_mailbox_priority_cmp);
        return ret;
 }
 
index 61a4b2351b96047c7c94ef29b04f6f47ef11788c..4923ca2b29b42dd2d604cfb5a3ffcb810872323b 100644 (file)
@@ -34,10 +34,9 @@ struct virtual_search_context {
        unsigned int next_record_idx;
 };
 
-static int virtual_search_record_cmp(const void *p1, const void *p2)
+static int virtual_search_record_cmp(const struct virtual_search_record *r1,
+                                    const struct virtual_search_record *r2)
 {
-       const struct virtual_search_record *r1 = p1, *r2 = p2;
-
        if (r1->mailbox_id < r2->mailbox_id)
                return -1;
        if (r1->mailbox_id > r2->mailbox_id)
@@ -70,8 +69,7 @@ static int virtual_search_get_records(struct mail_search_context *ctx,
        struct virtual_mailbox *mbox =
                (struct virtual_mailbox *)ctx->transaction->box;
        const struct virtual_mail_index_record *vrec;
-       struct virtual_search_record srec, *srecs;
-       unsigned int count;
+       struct virtual_search_record srec;
        const void *data;
        bool expunged;
        int ret, result;
@@ -97,10 +95,9 @@ static int virtual_search_get_records(struct mail_search_context *ctx,
                }
                mail_search_args_reset(ctx->args->args, FALSE);
        }
-       srecs = array_get_modifiable(&vctx->records, &count);
-       qsort(srecs, count, sizeof(*srecs), virtual_search_record_cmp);
+       array_sort(&vctx->records, virtual_search_record_cmp);
 
-       ctx->progress_max = count;
+       ctx->progress_max = array_count(&vctx->records);
        return ret;
 }
 
index dbe90fccce7f77ed27601783b40c4b8ca69fb635..538a49e78d89f505ccb39bf32e08c586a58a5ce8 100644 (file)
@@ -119,10 +119,9 @@ virtual_backend_box_sync_mail_unset(struct virtual_backend_box *bbox)
        }
 }
 
-static int bbox_mailbox_id_cmp(const void *p1, const void *p2)
+static int bbox_mailbox_id_cmp(struct virtual_backend_box *const *b1,
+                              struct virtual_backend_box *const *b2)
 {
-       const struct virtual_backend_box *const *b1 = p1, *const *b2 = p2;
-
        if ((*b1)->mailbox_id < (*b2)->mailbox_id)
                return -1;
        if ((*b1)->mailbox_id > (*b2)->mailbox_id)
@@ -235,7 +234,7 @@ static bool virtual_sync_ext_header_read(struct virtual_sync_context *ctx)
                }
        }
        /* sort the backend mailboxes by mailbox_id. */
-       qsort(bboxes, count, sizeof(*bboxes), bbox_mailbox_id_cmp);
+       array_sort(&ctx->mbox->backend_boxes, bbox_mailbox_id_cmp);
        return ret;
 }
 
@@ -589,10 +588,9 @@ virtual_sync_mailbox_box_add(struct virtual_sync_context *ctx,
        }
 }
 
-static int virtual_backend_uidmap_cmp(const void *p1, const void *p2)
+static int virtual_backend_uidmap_cmp(const struct virtual_backend_uidmap *u1,
+                                     const struct virtual_backend_uidmap *u2)
 {
-       const struct virtual_backend_uidmap *u1 = p1, *u2 = p2;
-
        if (u1->real_uid < u2->real_uid)
                return -1;
        if (u1->real_uid > u2->real_uid)
@@ -602,12 +600,8 @@ static int virtual_backend_uidmap_cmp(const void *p1, const void *p2)
 
 static void virtual_sync_bbox_uids_sort(struct virtual_backend_box *bbox)
 {
-       struct virtual_backend_uidmap *uids;
-       unsigned int uid_count;
-
        /* the uidmap must be sorted by real_uids */
-       uids = array_get_modifiable(&bbox->uids, &uid_count);
-       qsort(uids, uid_count, sizeof(*uids), virtual_backend_uidmap_cmp);
+       array_sort(&bbox->uids, virtual_backend_uidmap_cmp);
        bbox->uids_nonsorted = FALSE;
 }
 
@@ -1145,10 +1139,9 @@ static void virtual_sync_backend_map_uids(struct virtual_sync_context *ctx)
        }
 }
 
-static int virtual_add_record_cmp(const void *p1, const void *p2)
+static int virtual_add_record_cmp(const struct virtual_add_record *add1,
+                                 const struct virtual_add_record *add2)
 {
-       const struct virtual_add_record *add1 = p1, *add2 = p2;
-
        if (add1->received_date < add2->received_date)
                return -1;
        if (add1->received_date > add2->received_date)
@@ -1192,7 +1185,7 @@ static void virtual_sync_backend_sort_new(struct virtual_sync_context *ctx)
                }
        }
 
-       qsort(adds, count, sizeof(*adds), virtual_add_record_cmp);
+       array_sort(&ctx->all_adds, virtual_add_record_cmp);
 }
 
 static void virtual_sync_backend_add_new(struct virtual_sync_context *ctx)