Mercurial > dovecot > core-2.2
changeset 9609:d404d34097f2 HEAD
Use array_bsearch*().
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Fri, 10 Jul 2009 13:59:32 -0400 |
parents | 5ab09480d6b4 |
children | cf930dc6a7fe |
files | src/imap/imap-commands.c src/imap/imap-fetch.c src/lib-index/mail-index-strmap.c src/lib-index/mailbox-list-index-sync.c src/lib-mail/istream-header-filter.c src/lib-storage/index/dbox/dbox-storage-rebuild.c src/lib-storage/index/index-thread.c src/lib-storage/index/maildir/maildir-uidlist.c src/plugins/acl/acl-backend-vfile.c src/plugins/fts/fts-storage.c src/plugins/virtual/virtual-storage.c src/plugins/virtual/virtual-sync.c |
diffstat | 12 files changed, 71 insertions(+), 118 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ 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)
--- 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 @@ array_sort(&fetch_handlers, imap_fetch_handler_cmp); } -static int imap_fetch_handler_bsearch(const void *name_p, const void *handler_p) +static int +imap_fetch_handler_bsearch(const char *name, const struct imap_fetch_handler *h) { - const char *name = name_p; - const struct imap_fetch_handler *h = handler_p; - 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));
--- 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 @@ file_dotlock_delete(&strmap->dotlock); } -static int strmap_rec_cmp(const void *key, const void *value) +static int +strmap_rec_cmp(const uint32_t *uid, const struct mail_index_strmap_rec *rec) { - const uint32_t *uid = key; - const struct mail_index_strmap_rec *rec = value; - return *uid < rec->uid ? -1 : (*uid > rec->uid ? 1 : 0); } @@ -1112,10 +1110,11 @@ 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--;
--- 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 @@ return 0; } -static int mailbox_list_sync_record_cmp(const void *_key, const void *_rec) +static int +mailbox_list_sync_record_cmp(const struct mailbox_list_sync_lookup_key *key, + const struct mailbox_list_sync_record *rec) { - const struct mailbox_list_sync_lookup_key *key = _key; - const struct mailbox_list_sync_record *rec = _rec; - if (key->name_hash < rec->name_hash) return -1; if (key->name_hash > rec->name_hash) @@ -154,9 +153,6 @@ 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 @@ 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 *
--- 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 @@ return ret; } -static int cmp_uint(const void *p1, const void *p2) +static int cmp_uint(const unsigned int *i1, const unsigned int *i2) { - const unsigned int *i1 = p1, *i2 = p2; - 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)
--- 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 @@ return 0; } -static int dbox_rebuild_msg_uid_cmp(const void *p1, const void *p2) +static int dbox_rebuild_msg_uid_cmp(struct dbox_rebuild_msg *const *m1, + struct dbox_rebuild_msg *const *m2) { - const struct dbox_rebuild_msg *const *m1 = p1, *const *m2 = p2; - if ((*m1)->map_uid < (*m2)->map_uid) return -1; if ((*m1)->map_uid > (*m2)->map_uid) @@ -296,13 +295,10 @@ 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; }
--- 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 @@ 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--;
--- 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 @@ i_free(uidlist); } -static int maildir_uid_cmp(const void *p1, const void *p2) +static int maildir_uid_cmp(struct maildir_uidlist_rec *const *rec1, + struct maildir_uidlist_rec *const *rec2) { - const struct maildir_uidlist_rec *const *rec1 = p1, *const *rec2 = p2; - return (*rec1)->uid < (*rec2)->uid ? -1 : (*rec1)->uid > (*rec2)->uid ? 1 : 0; } @@ -400,12 +399,12 @@ struct maildir_uidlist_rec *rec) { struct maildir_uidlist_rec *const *recs, *const *pos; - unsigned int idx, count; + unsigned int idx; - recs = array_get(&uidlist->records, &count); - pos = bsearch(&rec, recs, count, sizeof(*recs), maildir_uid_cmp); + 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;
--- 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 @@ -664,11 +664,6 @@ 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) @@ -1092,10 +1087,9 @@ 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 @@ 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, + acl_rights_cmp, &i)) changed = vfile_object_add_right(aclobj, i, update); else changed = vfile_object_modify_right(aclobj, i, update);
--- a/src/plugins/fts/fts-storage.c Fri Jul 10 13:59:24 2009 -0400 +++ b/src/plugins/fts/fts-storage.c Fri Jul 10 13:59:32 2009 -0400 @@ -848,11 +848,8 @@ fmail->module_ctx.super.expunge(_mail); } -static int fts_score_cmp(const void *key, const void *data) +static int fts_score_cmp(const uint32_t *uid, const struct fts_score_map *score) { - const uint32_t *uid = key; - const struct fts_score_map *score = data; - return *uid < score->uid ? -1 : (*uid > score->uid ? 1 : 0); } @@ -864,15 +861,13 @@ struct fts_mail *fmail = FTS_MAIL_CONTEXT(mail); struct fts_transaction_context *ft = FTS_CONTEXT(_mail->transaction); const struct fts_score_map *scores; - unsigned int count; if (field != MAIL_FETCH_SEARCH_SCORE || ft->score_map == NULL || !array_is_created(ft->score_map)) scores = NULL; else { - scores = array_get(ft->score_map, &count); - scores = bsearch(&_mail->uid, scores, count, sizeof(*scores), - fts_score_cmp); + scores = array_bsearch(ft->score_map, &_mail->uid, + fts_score_cmp); } if (scores != NULL) { i_assert(scores->uid == _mail->uid);
--- a/src/plugins/virtual/virtual-storage.c Fri Jul 10 13:59:24 2009 -0400 +++ b/src/plugins/virtual/virtual-storage.c Fri Jul 10 13:59:32 2009 -0400 @@ -453,11 +453,9 @@ return ret; } -static int virtual_backend_uidmap_cmp(const void *key, const void *data) +static int virtual_backend_uidmap_cmp(const uint32_t *uid, + const struct virtual_backend_uidmap *map) { - const uint32_t *uid = key; - const struct virtual_backend_uidmap *map = data; - return *uid < map->real_uid ? -1 : *uid > map->real_uid ? 1 : 0; } @@ -471,7 +469,6 @@ struct virtual_backend_box *bbox; struct mailbox_status status; const struct virtual_backend_uidmap *uids; - unsigned int count; if (strcmp(mbox->vseq_lookup_prev_mailbox, backend_mailbox) == 0) bbox = mbox->vseq_lookup_prev_bbox; @@ -489,9 +486,8 @@ if (status.uidvalidity != backend_uidvalidity) return FALSE; - uids = array_get(&bbox->uids, &count); - uids = bsearch(&backend_uid, uids, count, sizeof(*uids), - virtual_backend_uidmap_cmp); + uids = array_bsearch(&bbox->uids, &backend_uid, + virtual_backend_uidmap_cmp); if (uids == NULL) return FALSE;
--- a/src/plugins/virtual/virtual-sync.c Fri Jul 10 13:59:24 2009 -0400 +++ b/src/plugins/virtual/virtual-sync.c Fri Jul 10 13:59:32 2009 -0400 @@ -473,11 +473,10 @@ return ret; } -static int virtual_backend_uidmap_bsearch_cmp(const void *key, const void *data) +static int +virtual_backend_uidmap_bsearch_cmp(const uint32_t *uidp, + const struct virtual_backend_uidmap *uidmap) { - const uint32_t *uidp = key; - const struct virtual_backend_uidmap *uidmap = data; - return *uidp < uidmap->real_uid ? -1 : (*uidp > uidmap->real_uid ? 1 : 0); } @@ -501,9 +500,8 @@ i_assert(rec_count >= uid_count); /* find the first uidmap record to be removed */ - if (!bsearch_insert_pos(&uids[0].seq1, uidmap, rec_count, - sizeof(*uidmap), - virtual_backend_uidmap_bsearch_cmp, &src)) + if (!array_bsearch_insert_pos(&bbox->uids, &uids[0].seq1, + virtual_backend_uidmap_bsearch_cmp, &src)) i_unreached(); /* remove the unwanted messages */ @@ -550,10 +548,9 @@ added_uids[0].seq1 > uidmap[rec_count-1].real_uid) { /* fast path: usually messages are appended */ dest = rec_count; - } else if (bsearch_insert_pos(&added_uids[0].seq1, uidmap, rec_count, - sizeof(*uidmap), - virtual_backend_uidmap_bsearch_cmp, - &dest)) + } else if (array_bsearch_insert_pos(&bbox->uids, &added_uids[0].seq1, + virtual_backend_uidmap_bsearch_cmp, + &dest)) i_unreached(); /* make space for all added UIDs. */ @@ -724,9 +721,10 @@ if (!seq_range_array_iter_nth(&iter, n++, &add_uid)) return; + (void)array_bsearch_insert_pos(&bbox->uids, &add_uid, + virtual_backend_uidmap_bsearch_cmp, &i); + uidmap = array_get_modifiable(&bbox->uids, &count); - (void)bsearch_insert_pos(&add_uid, uidmap, count, sizeof(*uidmap), - virtual_backend_uidmap_bsearch_cmp, &i); if (i == count) return; @@ -760,11 +758,11 @@ if (!seq_range_array_iter_nth(&iter, n++, &remove_uid)) return; - uidmap = array_get_modifiable(&bbox->uids, &count); - (void)bsearch_insert_pos(&remove_uid, uidmap, count, sizeof(*uidmap), - virtual_backend_uidmap_bsearch_cmp, &i); + (void)array_bsearch_insert_pos(&bbox->uids, &remove_uid, + virtual_backend_uidmap_bsearch_cmp, &i); t_array_init(&drop_uids, array_count(removed_uids)); iter_done = FALSE; + uidmap = array_get_modifiable(&bbox->uids, &count); for (; i < count; ) { if (uidmap[i].real_uid < remove_uid) { i++; @@ -862,9 +860,11 @@ mail_index_lookup_uid(ibox->view, sync_rec->seq1, &uid1); mail_index_lookup_uid(ibox->view, sync_rec->seq2, &uid2); + (void)array_bsearch_insert_pos(&bbox->uids, &uid1, + virtual_backend_uidmap_bsearch_cmp, + &idx); + uidmap = array_get_modifiable(&bbox->uids, &count); - (void)bsearch_insert_pos(&uid1, uidmap, count, sizeof(*uidmap), - virtual_backend_uidmap_bsearch_cmp, &idx); if (idx == count || uidmap[idx].real_uid > uid2) return FALSE; @@ -888,9 +888,10 @@ /* remember only the expunges for messages that already exist for this mailbox */ + (void)array_bsearch_insert_pos(&bbox->uids, &uid1, + virtual_backend_uidmap_bsearch_cmp, + &idx1); uidmap = array_get_modifiable(&bbox->uids, &count); - (void)bsearch_insert_pos(&uid1, uidmap, count, sizeof(*uidmap), - virtual_backend_uidmap_bsearch_cmp, &idx1); for (i = idx1; i < count; i++) { if (uidmap[i].real_uid > uid2) break; @@ -1196,7 +1197,7 @@ struct virtual_backend_uidmap *uidmap; const struct mail_index_header *hdr; const struct virtual_mail_index_record *vrec; - unsigned int i, count, idx, uid_count; + unsigned int i, count, idx; uint32_t vseq, first_uid, next_uid; hdr = mail_index_get_header(ctx->sync_view); @@ -1239,14 +1240,13 @@ vrec->mailbox_id); } - uidmap = array_get_modifiable(&bbox->uids, &uid_count); - if (!bsearch_insert_pos(&vrec->real_uid, uidmap, uid_count, - sizeof(*uidmap), - virtual_backend_uidmap_bsearch_cmp, - &idx)) + if (!array_bsearch_insert_pos(&bbox->uids, &vrec->real_uid, + virtual_backend_uidmap_bsearch_cmp, + &idx)) i_unreached(); - i_assert(uidmap[idx].virtual_uid == 0); - uidmap[idx].virtual_uid = first_uid + i; + uidmap = array_idx_modifiable(&bbox->uids, idx); + i_assert(uidmap->virtual_uid == 0); + uidmap->virtual_uid = first_uid + i; } ctx->mbox->sync_virtual_next_uid = first_uid + i; }