From 09fa59987c90f3039e8b96233af431eade5607cd Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Tue, 3 Sep 2024 13:04:00 +0300 Subject: [PATCH] global: Use array_lsearch_ptr[_idx]() where possible --- src/auth/passdb.c | 27 ++---- src/auth/userdb.c | 27 ++---- src/lib-dict/dict.c | 12 +-- src/lib-http/http-client-connection.c | 90 +++++-------------- src/lib-http/http-client-peer.c | 29 ++---- src/lib-http/http-client-queue.c | 51 +++-------- src/lib-http/http-client.c | 16 ++-- src/lib-http/http-server-resource.c | 15 ++-- src/lib-index/mail-index-map.c | 14 +-- src/lib-sql/driver-cassandra.c | 25 ++---- src/lib-sql/sql-api.c | 13 +-- .../index/imapc/imapc-mail-fetch.c | 14 +-- src/lib-storage/mail-storage-hooks.c | 13 +-- src/lib-storage/mail-storage.c | 30 +++---- src/lib-storage/mailbox-search-result.c | 14 +-- src/lib/ioloop.c | 37 +++----- src/lib/lib-event.c | 36 ++------ src/plugins/quota/quota.c | 12 +-- src/submission/submission-backend.c | 7 +- src/submission/submission-recipient.c | 10 +-- 20 files changed, 135 insertions(+), 357 deletions(-) diff --git a/src/auth/passdb.c b/src/auth/passdb.c index cf54b0a447..187b659cc0 100644 --- a/src/auth/passdb.c +++ b/src/auth/passdb.c @@ -41,17 +41,11 @@ void passdb_register_module(struct passdb_module_interface *iface) void passdb_unregister_module(struct passdb_module_interface *iface) { - struct passdb_module_interface *const *ifaces; unsigned int idx; - array_foreach(&passdb_interfaces, ifaces) { - if (*ifaces == iface) { - idx = array_foreach_idx(&passdb_interfaces, ifaces); - array_delete(&passdb_interfaces, idx, 1); - return; - } - } - i_panic("passdb_unregister_module(%s): Not registered", iface->name); + if (!array_lsearch_ptr_idx(&passdb_interfaces, iface, &idx)) + i_panic("passdb_unregister_module(%s): Not registered", iface->name); + array_delete(&passdb_interfaces, idx, 1); } bool passdb_get_credentials(struct auth_request *auth_request, @@ -232,17 +226,10 @@ void passdb_deinit(struct passdb_module *passdb) if (--passdb->init_refcount > 0) return; - struct passdb_module *const *passdbs; - unsigned int i, count; - - passdbs = array_get(&passdb_modules, &count); - for (i = 0; i < count; i++) { - if (passdbs[i] == passdb) { - array_delete(&passdb_modules, i, 1); - break; - } - } - i_assert(i < count); + unsigned int i; + if (!array_lsearch_ptr_idx(&passdb_modules, passdb, &i)) + i_unreached(); + array_delete(&passdb_modules, i, 1); if (passdb->iface.deinit != NULL) passdb->iface.deinit(passdb); diff --git a/src/auth/userdb.c b/src/auth/userdb.c index 610b9cc4f4..f822cdda1f 100644 --- a/src/auth/userdb.c +++ b/src/auth/userdb.c @@ -41,17 +41,11 @@ void userdb_register_module(struct userdb_module_interface *iface) void userdb_unregister_module(struct userdb_module_interface *iface) { - struct userdb_module_interface *const *ifaces; unsigned int idx; - array_foreach(&userdb_interfaces, ifaces) { - if (*ifaces == iface) { - idx = array_foreach_idx(&userdb_interfaces, ifaces); - array_delete(&userdb_interfaces, idx, 1); - return; - } - } - i_panic("userdb_unregister_module(%s): Not registered", iface->name); + if (!array_lsearch_ptr_idx(&userdb_interfaces, iface, &idx)) + i_panic("userdb_unregister_module(%s): Not registered", iface->name); + array_delete(&userdb_interfaces, idx, 1); } uid_t userdb_parse_uid(struct auth_request *request, const char *str) @@ -164,17 +158,10 @@ void userdb_deinit(struct userdb_module *userdb) if (--userdb->init_refcount > 0) return; - struct userdb_module *const *userdbs; - unsigned int i, count; - - userdbs = array_get(&userdb_modules, &count); - for (i = 0; i < count; i++) { - if (userdbs[i] == userdb) { - array_delete(&userdb_modules, i, 1); - break; - } - } - i_assert(i < count); + unsigned int i; + if (!array_lsearch_ptr_idx(&userdb_modules, userdb, &i)) + i_unreached(); + array_delete(&userdb_modules, i, 1); if (userdb->iface->deinit != NULL) userdb->iface->deinit(userdb); diff --git a/src/lib-dict/dict.c b/src/lib-dict/dict.c index c23644d57a..38a6ecd23f 100644 --- a/src/lib-dict/dict.c +++ b/src/lib-dict/dict.c @@ -103,16 +103,10 @@ void dict_driver_register(struct dict *driver) void dict_driver_unregister(struct dict *driver) { - struct dict *const *dicts; - unsigned int idx = UINT_MAX; + unsigned int idx; - array_foreach(&dict_drivers, dicts) { - if (*dicts == driver) { - idx = array_foreach_idx(&dict_drivers, dicts); - break; - } - } - i_assert(idx != UINT_MAX); + if (!array_lsearch_ptr_idx(&dict_drivers, driver, &idx)) + i_unreached(); array_delete(&dict_drivers, idx, 1); if (array_count(&dict_drivers) == 0) diff --git a/src/lib-http/http-client-connection.c b/src/lib-http/http-client-connection.c index b3f15c8868..95247c7c12 100644 --- a/src/lib-http/http-client-connection.c +++ b/src/lib-http/http-client-connection.c @@ -59,31 +59,20 @@ http_client_connection_unlist_pending(struct http_client_connection *conn) { struct http_client_peer *peer = conn->peer; struct http_client_peer_pool *ppool = conn->ppool; - ARRAY_TYPE(http_client_connection) *conn_arr; - struct http_client_connection *const *conn_idx; + unsigned int idx; /* Remove from pending lists */ - conn_arr = &ppool->pending_conns; - array_foreach(conn_arr, conn_idx) { - if (*conn_idx == conn) { - array_delete(conn_arr, - array_foreach_idx(conn_arr, conn_idx), 1); - break; - } - } + if (!array_lsearch_ptr_idx(&ppool->pending_conns, conn, &idx)) + i_unreached(); + array_delete(&ppool->pending_conns, idx, 1); if (peer == NULL) return; - conn_arr = &peer->pending_conns; - array_foreach(conn_arr, conn_idx) { - if (*conn_idx == conn) { - array_delete(conn_arr, - array_foreach_idx(conn_arr, conn_idx), 1); - break; - } - } + if (!array_lsearch_ptr_idx(&peer->pending_conns, conn, &idx)) + i_unreached(); + array_delete(&peer->pending_conns, idx, 1); } static inline void @@ -379,33 +368,18 @@ static void http_client_connection_detach_peer(struct http_client_connection *conn) { struct http_client_peer *peer = conn->peer; - struct http_client_connection *const *conn_idx; - ARRAY_TYPE(http_client_connection) *conn_arr; - bool found = FALSE; + unsigned int idx; if (peer == NULL) return; http_client_peer_ref(peer); - conn_arr = &peer->conns; - array_foreach(conn_arr, conn_idx) { - if (*conn_idx == conn) { - array_delete(conn_arr, - array_foreach_idx(conn_arr, conn_idx), 1); - found = TRUE; - break; - } - } - i_assert(found); + if (!array_lsearch_ptr_idx(&peer->conns, conn, &idx)) + i_unreached(); + array_delete(&peer->conns, idx, 1); - conn_arr = &peer->pending_conns; - array_foreach(conn_arr, conn_idx) { - if (*conn_idx == conn) { - array_delete(conn_arr, - array_foreach_idx(conn_arr, conn_idx), 1); - break; - } - } + if (array_lsearch_ptr_idx(&peer->pending_conns, conn, &idx)) + array_delete(&peer->pending_conns, idx, 1); conn->peer = NULL; e_debug(conn->event, "Detached peer"); @@ -542,20 +516,13 @@ void http_client_connection_check_idle(struct http_client_connection *conn) static void http_client_connection_stop_idle(struct http_client_connection *conn) { - struct http_client_connection *const *conn_idx; - ARRAY_TYPE(http_client_connection) *conn_arr; + unsigned int idx; timeout_remove(&conn->to_idle); conn->idle = FALSE; - conn_arr = &conn->ppool->idle_conns; - array_foreach(conn_arr, conn_idx) { - if (*conn_idx == conn) { - array_delete(conn_arr, - array_foreach_idx(conn_arr, conn_idx), 1); - break; - } - } + if (array_lsearch_ptr_idx(&conn->ppool->idle_conns, conn, &idx)) + array_delete(&conn->ppool->idle_conns, idx, 1); } void http_client_connection_claim_idle(struct http_client_connection *conn, @@ -1804,8 +1771,7 @@ static void http_client_connection_disconnect(struct http_client_connection *conn) { struct http_client_peer_pool *ppool = conn->ppool; - ARRAY_TYPE(http_client_connection) *conn_arr; - struct http_client_connection *const *conn_idx; + unsigned int idx; if (conn->disconnected) return; @@ -1837,22 +1803,12 @@ http_client_connection_disconnect(struct http_client_connection *conn) timeout_remove(&conn->to_response); /* Remove this connection from the lists */ - conn_arr = &ppool->conns; - array_foreach(conn_arr, conn_idx) { - if (*conn_idx == conn) { - array_delete(conn_arr, - array_foreach_idx(conn_arr, conn_idx), 1); - break; - } - } - conn_arr = &ppool->pending_conns; - array_foreach(conn_arr, conn_idx) { - if (*conn_idx == conn) { - array_delete(conn_arr, - array_foreach_idx(conn_arr, conn_idx), 1); - break; - } - } + if (!array_lsearch_ptr_idx(&ppool->conns, conn, &idx)) + i_unreached(); + array_delete(&ppool->conns, idx, 1); + + if (array_lsearch_ptr_idx(&ppool->pending_conns, conn, &idx)) + array_delete(&ppool->pending_conns, idx, 1); http_client_connection_detach_peer(conn); diff --git a/src/lib-http/http-client-peer.c b/src/lib-http/http-client-peer.c index 9182f55b25..2238cbda29 100644 --- a/src/lib-http/http-client-peer.c +++ b/src/lib-http/http-client-peer.c @@ -1184,13 +1184,7 @@ void http_client_peer_trigger_request_handler(struct http_client_peer *peer) bool http_client_peer_have_queue(struct http_client_peer *peer, struct http_client_queue *queue) { - struct http_client_queue *const *queue_idx; - - array_foreach(&peer->queues, queue_idx) { - if (*queue_idx == queue) - return TRUE; - } - return FALSE; + return array_lsearch_ptr(&peer->queues, queue) != NULL; } void http_client_peer_link_queue(struct http_client_peer *peer, @@ -1207,22 +1201,17 @@ void http_client_peer_link_queue(struct http_client_peer *peer, void http_client_peer_unlink_queue(struct http_client_peer *peer, struct http_client_queue *queue) { - struct http_client_queue *const *queue_idx; + unsigned int idx; - array_foreach(&peer->queues, queue_idx) { - if (*queue_idx == queue) { - array_delete(&peer->queues, - array_foreach_idx(&peer->queues, queue_idx), 1); + if (array_lsearch_ptr_idx(&peer->queues, queue, &idx)) + array_delete(&peer->queues, idx, 1); - e_debug(peer->event, - "Unlinked queue %s (%d queues linked)", - queue->name, array_count(&peer->queues)); + e_debug(peer->event, + "Unlinked queue %s (%d queues linked)", + queue->name, array_count(&peer->queues)); - if (array_count(&peer->queues) == 0) - http_client_peer_check_idle(peer); - return; - } - } + if (array_count(&peer->queues) == 0) + http_client_peer_check_idle(peer); } struct http_client_request * diff --git a/src/lib-http/http-client-queue.c b/src/lib-http/http-client-queue.c index c1b27a1ffb..773e06cd1f 100644 --- a/src/lib-http/http-client-queue.c +++ b/src/lib-http/http-client-queue.c @@ -511,7 +511,6 @@ void http_client_queue_connection_failure(struct http_client_queue *queue, const char *https_name = http_client_peer_addr_get_https_name(addr); struct http_client_host *host = queue->host; unsigned int ips_count = http_client_host_get_ips_count(host); - struct http_client_peer *const *peer_idx; unsigned int num_requests = array_count(&queue->queued_requests) + array_count(&queue->queued_urgent_requests); @@ -530,24 +529,16 @@ void http_client_queue_connection_failure(struct http_client_queue *queue, i_assert(queue->cur_peer == NULL || queue->cur_peer == peer); queue->cur_peer = NULL; } else { - bool found = FALSE; + unsigned int idx; i_assert(queue->cur_peer == NULL); /* We're still doing the initial connections to this hport. if we're also doing parallel connections with soft timeouts (pending_peer_count>1), wait for them to finish first. */ - array_foreach(&queue->pending_peers, peer_idx) { - if (*peer_idx == peer) { - array_delete(&queue->pending_peers, - array_foreach_idx( - &queue->pending_peers, - peer_idx), 1); - found = TRUE; - break; - } - } - i_assert(found); + if (!array_lsearch_ptr_idx(&queue->pending_peers, peer, &idx)) + i_unreached(); + array_delete(&queue->pending_peers, idx, 1); if (array_count(&queue->pending_peers) > 0) { e_debug(queue->event, "Waiting for remaining pending peers."); @@ -613,21 +604,16 @@ void http_client_queue_connection_failure(struct http_client_queue *queue, void http_client_queue_peer_disconnected(struct http_client_queue *queue, struct http_client_peer *peer) { - struct http_client_peer *const *peer_idx; + unsigned int idx; if (queue->cur_peer == peer) { queue->cur_peer = NULL; return; } - array_foreach(&queue->pending_peers, peer_idx) { - if (*peer_idx == peer) { - array_delete(&queue->pending_peers, - array_foreach_idx(&queue->pending_peers, - peer_idx), 1); - break; - } - } + if (!array_lsearch_ptr_idx(&queue->pending_peers, peer, &idx)) + i_unreached(); + array_delete(&queue->pending_peers, idx, 1); } /* @@ -645,23 +631,12 @@ void http_client_queue_drop_request(struct http_client_queue *queue, /* Drop from queue */ if (req->urgent) { - reqs = array_get_modifiable(&queue->queued_urgent_requests, - &count); - for (i = 0; i < count; i++) { - if (reqs[i] == req) { - array_delete(&queue->queued_urgent_requests, - i, 1); - break; - } - } + if (array_lsearch_ptr_idx(&queue->queued_urgent_requests, + req, &i)) + array_delete(&queue->queued_urgent_requests, i, 1); } else { - reqs = array_get_modifiable(&queue->queued_requests, &count); - for (i = 0; i < count; i++) { - if (reqs[i] == req) { - array_delete(&queue->queued_requests, i, 1); - break; - } - } + if (array_lsearch_ptr_idx(&queue->queued_requests, req, &i)) + array_delete(&queue->queued_requests, i, 1); } /* Drop from delay queue */ diff --git a/src/lib-http/http-client.c b/src/lib-http/http-client.c index 42f126dbc5..a6146c0d0e 100644 --- a/src/lib-http/http-client.c +++ b/src/lib-http/http-client.c @@ -408,16 +408,12 @@ void http_client_delay_request_error(struct http_client *client, void http_client_remove_request_error(struct http_client *client, struct http_client_request *req) { - struct http_client_request *const *reqs; - unsigned int i, count; - - reqs = array_get(&client->delayed_failing_requests, &count); - for (i = 0; i < count; i++) { - if (reqs[i] == req) { - array_delete(&client->delayed_failing_requests, i, 1); - return; - } - } + unsigned int i; + + if (!array_lsearch_ptr_idx(&client->delayed_failing_requests, + req, &i)) + i_unreached(); + array_delete(&client->delayed_failing_requests, i, 1); } /* diff --git a/src/lib-http/http-server-resource.c b/src/lib-http/http-server-resource.c index 943914deff..a773acdbb6 100644 --- a/src/lib-http/http-server-resource.c +++ b/src/lib-http/http-server-resource.c @@ -90,16 +90,11 @@ static void http_server_location_remove(struct http_server *server, struct http_server_location *loc) { - struct http_server_location *const *locp; - - array_foreach(&server->locations, locp) { - if (*locp == loc) { - array_delete( - &server->locations, - array_foreach_idx(&server->locations, locp), 1); - return; - } - } + unsigned int idx; + + if (!array_lsearch_ptr_idx(&server->locations, loc, &idx)) + i_unreached(); + array_delete(&server->locations, idx, 1); } /* diff --git a/src/lib-index/mail-index-map.c b/src/lib-index/mail-index-map.c index dcfc93fbe7..b3aeb07ffa 100644 --- a/src/lib-index/mail-index-map.c +++ b/src/lib-index/mail-index-map.c @@ -284,18 +284,12 @@ static void mail_index_record_map_free(struct mail_index_map *map, static void mail_index_record_map_unlink(struct mail_index_map *map) { - struct mail_index_map *const *maps; - unsigned int idx = UINT_MAX; - - array_foreach(&map->rec_map->maps, maps) { - if (*maps == map) { - idx = array_foreach_idx(&map->rec_map->maps, maps); - break; - } - } - i_assert(idx != UINT_MAX); + unsigned int idx; + if (!array_lsearch_ptr_idx(&map->rec_map->maps, map, &idx)) + i_unreached(); array_delete(&map->rec_map->maps, idx, 1); + if (array_count(&map->rec_map->maps) == 0) { mail_index_record_map_free(map, map->rec_map); map->rec_map = NULL; diff --git a/src/lib-sql/driver-cassandra.c b/src/lib-sql/driver-cassandra.c index 04a8230349..f4019e0134 100644 --- a/src/lib-sql/driver-cassandra.c +++ b/src/lib-sql/driver-cassandra.c @@ -1471,17 +1471,11 @@ static void driver_cassandra_deinit_v(struct sql_db *_db) static void driver_cassandra_result_unlink(struct cassandra_db *db, struct cassandra_result *result) { - struct cassandra_result *const *results; - unsigned int i, count; + unsigned int i; - results = array_get(&db->results, &count); - for (i = 0; i < count; i++) { - if (results[i] == result) { - array_delete(&db->results, i, 1); - return; - } - } - i_unreached(); + if (!array_lsearch_ptr_idx(&db->results, result, &i)) + i_unreached(); + array_delete(&db->results, i, 1); } static void driver_cassandra_log_result(struct cassandra_result *result, @@ -2388,18 +2382,13 @@ static void cassandra_prepared_statement_remove_pending(struct cassandra_sql_statement *stmt) { struct cassandra_sql_prepared_statement *prep = stmt->prep; - struct cassandra_sql_statement *const *iter_stmt; + unsigned int idx; if (prep == NULL) return; - array_foreach(&prep->pending_statements, iter_stmt) { - if (stmt == *iter_stmt) { - array_delete(&prep->pending_statements, - array_foreach_idx(&prep->pending_statements, iter_stmt), 1); - break; - } - } + if (array_lsearch_ptr_idx(&prep->pending_statements, stmt, &idx)) + array_delete(&prep->pending_statements, idx, 1); } static void cassandra_sql_statement_free(struct cassandra_sql_statement *stmt) diff --git a/src/lib-sql/sql-api.c b/src/lib-sql/sql-api.c index a3d7d519cd..1af9554458 100644 --- a/src/lib-sql/sql-api.c +++ b/src/lib-sql/sql-api.c @@ -86,16 +86,11 @@ void sql_driver_register(const struct sql_db *driver) void sql_driver_unregister(const struct sql_db *driver) { - const struct sql_db *const *drivers; - unsigned int i, count; + unsigned int i; - drivers = array_get(&sql_drivers, &count); - for (i = 0; i < count; i++) { - if (drivers[i] == driver) { - array_delete(&sql_drivers, i, 1); - break; - } - } + if (!array_lsearch_ptr_idx(&sql_drivers, driver, &i)) + i_unreached(); + array_delete(&sql_drivers, i, 1); } int sql_init_auto(struct event *event, struct sql_db **db_r, diff --git a/src/lib-storage/index/imapc/imapc-mail-fetch.c b/src/lib-storage/index/imapc/imapc-mail-fetch.c index 3c4ae4ec69..71d636f518 100644 --- a/src/lib-storage/index/imapc/imapc-mail-fetch.c +++ b/src/lib-storage/index/imapc/imapc-mail-fetch.c @@ -56,10 +56,9 @@ imapc_mail_fetch_callback(const struct imapc_command_reply *reply, void *context) { struct imapc_fetch_request *request = context; - struct imapc_fetch_request *const *requests; struct imapc_mail *mail; struct imapc_mailbox *mbox = NULL; - unsigned int i, count; + unsigned int i; array_foreach_elem(&request->mails, mail) { i_assert(mail->fetch_count > 0); @@ -70,14 +69,9 @@ imapc_mail_fetch_callback(const struct imapc_command_reply *reply, } i_assert(mbox != NULL); - requests = array_get(&mbox->fetch_requests, &count); - for (i = 0; i < count; i++) { - if (requests[i] == request) { - array_delete(&mbox->fetch_requests, i, 1); - break; - } - } - i_assert(i < count); + if (!array_lsearch_ptr_idx(&mbox->fetch_requests, request, &i)) + i_unreached(); + array_delete(&mbox->fetch_requests, i, 1); array_free(&request->mails); i_free(request); diff --git a/src/lib-storage/mail-storage-hooks.c b/src/lib-storage/mail-storage-hooks.c index 3506aaf3a9..e9aaaa913d 100644 --- a/src/lib-storage/mail-storage-hooks.c +++ b/src/lib-storage/mail-storage-hooks.c @@ -91,17 +91,10 @@ void mail_storage_hooks_add_internal(const struct mail_storage_hooks *hooks) void mail_storage_hooks_remove_internal(const struct mail_storage_hooks *hooks) { - const struct mail_storage_hooks *const *old_hooks; - unsigned int idx = UINT_MAX; - - array_foreach(&internal_hooks, old_hooks) { - if (*old_hooks == hooks) { - idx = array_foreach_idx(&internal_hooks, old_hooks); - break; - } - } - i_assert(idx != UINT_MAX); + unsigned int idx; + if (!array_lsearch_ptr_idx(&internal_hooks, hooks, &idx)) + i_unreached(); array_delete(&internal_hooks, idx, 1); } diff --git a/src/lib-storage/mail-storage.c b/src/lib-storage/mail-storage.c index 0c4583c4ca..582e1e718e 100644 --- a/src/lib-storage/mail-storage.c +++ b/src/lib-storage/mail-storage.c @@ -111,16 +111,11 @@ void mail_storage_class_register(struct mail_storage *storage_class) void mail_storage_class_unregister(struct mail_storage *storage_class) { - struct mail_storage *const *classes; - unsigned int i, count; + unsigned int i; - classes = array_get(&mail_storage_classes, &count); - for (i = 0; i < count; i++) { - if (classes[i] == storage_class) { - array_delete(&mail_storage_classes, i, 1); - break; - } - } + if (!array_lsearch_ptr_idx(&mail_storage_classes, storage_class, &i)) + i_unreached(); + array_delete(&mail_storage_classes, i, 1); } struct mail_storage *mail_storage_find_class(const char *name) @@ -2693,17 +2688,12 @@ void mailbox_search_mail_detach(struct mail_search_context *ctx, { struct mail_private *pmail = container_of(mail, struct mail_private, mail); - struct mail *const *mailp; - - array_foreach(&ctx->mails, mailp) { - if (*mailp == mail) { - pmail->search_mail = FALSE; - array_delete(&ctx->mails, - array_foreach_idx(&ctx->mails, mailp), 1); - return; - } - } - i_unreached(); + unsigned int idx; + + if (!array_lsearch_ptr_idx(&ctx->mails, mail, &idx)) + i_unreached(); + pmail->search_mail = FALSE; + array_delete(&ctx->mails, idx, 1); } int mailbox_search_result_build(struct mailbox_transaction_context *t, diff --git a/src/lib-storage/mailbox-search-result.c b/src/lib-storage/mailbox-search-result.c index d239c72278..d49cb9a243 100644 --- a/src/lib-storage/mailbox-search-result.c +++ b/src/lib-storage/mailbox-search-result.c @@ -57,19 +57,13 @@ mailbox_search_result_alloc(struct mailbox *box, struct mail_search_args *args, void mailbox_search_result_free(struct mail_search_result **_result) { struct mail_search_result *result = *_result; - struct mail_search_result *const *results; - unsigned int i, count; + unsigned int i; *_result = NULL; - results = array_get(&result->box->search_results, &count); - for (i = 0; i < count; i++) { - if (results[i] == result) { - array_delete(&result->box->search_results, i, 1); - break; - } - } - i_assert(i != count); + if (!array_lsearch_ptr_idx(&result->box->search_results, result, &i)) + i_unreached(); + array_delete(&result->box->search_results, i, 1); if (result->search_args != NULL) mail_search_args_unref(&result->search_args); diff --git a/src/lib/ioloop.c b/src/lib/ioloop.c index 4a2757abd3..2485eec28c 100644 --- a/src/lib/ioloop.c +++ b/src/lib/ioloop.c @@ -413,14 +413,11 @@ void timeout_remove(struct timeout **_timeout) if (timeout->item.idx != UINT_MAX) priorityq_remove(timeout->ioloop->timeouts, &timeout->item); else if (!timeout->one_shot && timeout->msecs > 0) { - struct timeout *const *to_idx; - array_foreach(&ioloop->timeouts_new, to_idx) { - if (*to_idx == timeout) { - array_delete(&ioloop->timeouts_new, - array_foreach_idx(&ioloop->timeouts_new, to_idx), 1); - break; - } - } + unsigned int idx; + + if (!array_lsearch_ptr_idx(&ioloop->timeouts_new, timeout, &idx)) + i_unreached(); + array_delete(&ioloop->timeouts_new, idx, 1); } timeout_free(timeout); } @@ -1013,17 +1010,11 @@ void io_loop_add_switch_callback(io_switch_callback_t *callback) void io_loop_remove_switch_callback(io_switch_callback_t *callback) { - io_switch_callback_t *const *callbackp; unsigned int idx; - array_foreach(&io_switch_callbacks, callbackp) { - if (*callbackp == callback) { - idx = array_foreach_idx(&io_switch_callbacks, callbackp); - array_delete(&io_switch_callbacks, idx, 1); - return; - } - } - i_unreached(); + if (!array_lsearch_ptr_idx(&io_switch_callbacks, callback, &idx)) + i_unreached(); + array_delete(&io_switch_callbacks, idx, 1); } void io_loop_add_destroy_callback(io_destroy_callback_t *callback) @@ -1037,17 +1028,11 @@ void io_loop_add_destroy_callback(io_destroy_callback_t *callback) void io_loop_remove_destroy_callback(io_destroy_callback_t *callback) { - io_destroy_callback_t *const *callbackp; unsigned int idx; - array_foreach(&io_destroy_callbacks, callbackp) { - if (*callbackp == callback) { - idx = array_foreach_idx(&io_destroy_callbacks, callbackp); - array_delete(&io_destroy_callbacks, idx, 1); - return; - } - } - i_unreached(); + if (!array_lsearch_ptr_idx(&io_destroy_callbacks, callback, &idx)) + i_unreached(); + array_delete(&io_destroy_callbacks, idx, 1); } struct ioloop_context *io_loop_context_new(struct ioloop *ioloop) diff --git a/src/lib/lib-event.c b/src/lib/lib-event.c index fa9b7055c2..2785df8038 100644 --- a/src/lib/lib-event.c +++ b/src/lib/lib-event.c @@ -907,16 +907,11 @@ event_find_category(const struct event *event, const struct event_category *category) { struct event_internal_category *internal = category->internal; - struct event_category *cat; /* make sure we're always looking for a representative */ i_assert(category == &internal->representative); - array_foreach_elem(&event->categories, cat) { - if (cat == category) - return TRUE; - } - return FALSE; + return array_lsearch_ptr(&event->categories, category) != NULL; } struct event * @@ -1691,17 +1686,11 @@ void event_register_callback(event_callback_t *callback) void event_unregister_callback(event_callback_t *callback) { - event_callback_t *const *callbackp; + unsigned int idx; - array_foreach(&event_handlers, callbackp) { - if (*callbackp == callback) { - unsigned int idx = - array_foreach_idx(&event_handlers, callbackp); - array_delete(&event_handlers, idx, 1); - return; - } - } - i_unreached(); + if (!array_lsearch_ptr_idx(&event_handlers, callback, &idx)) + i_unreached(); + array_delete(&event_handlers, idx, 1); } void event_category_register_callback(event_category_callback_t *callback) @@ -1711,18 +1700,11 @@ void event_category_register_callback(event_category_callback_t *callback) void event_category_unregister_callback(event_category_callback_t *callback) { - event_category_callback_t *const *callbackp; + unsigned int idx; - array_foreach(&event_category_callbacks, callbackp) { - if (*callbackp == callback) { - unsigned int idx = - array_foreach_idx(&event_category_callbacks, - callbackp); - array_delete(&event_category_callbacks, idx, 1); - return; - } - } - i_unreached(); + if (!array_lsearch_ptr_idx(&event_category_callbacks, callback, &idx)) + i_unreached(); + array_delete(&event_category_callbacks, idx, 1); } static struct event_passthrough * diff --git a/src/plugins/quota/quota.c b/src/plugins/quota/quota.c index 917bd0ff98..5997f5f84b 100644 --- a/src/plugins/quota/quota.c +++ b/src/plugins/quota/quota.c @@ -464,8 +464,7 @@ void quota_remove_user_namespace(struct mail_namespace *ns) { struct quota *quota; struct quota_root *root; - struct mail_namespace *const *namespaces; - unsigned int i, count; + unsigned int i; quota = ns->owner != NULL ? quota_get_mail_user_quota(ns->owner) : @@ -476,13 +475,8 @@ void quota_remove_user_namespace(struct mail_namespace *ns) } array_foreach_elem("a->all_roots, root) { - namespaces = array_get(&root->namespaces, &count); - for (i = 0; i < count; i++) { - if (namespaces[i] == ns) { - array_delete(&root->namespaces, i, 1); - break; - } - } + if (array_lsearch_ptr_idx(&root->namespaces, ns, &i)) + array_delete(&root->namespaces, i, 1); } } diff --git a/src/submission/submission-backend.c b/src/submission/submission-backend.c index b9a22203d8..a413e6625e 100644 --- a/src/submission/submission-backend.c +++ b/src/submission/submission-backend.c @@ -303,12 +303,9 @@ static void submission_backend_add_pending(struct submission_backend *backend) { struct client *client = backend->client; - struct submission_backend *pending_backend; - array_foreach_elem(&client->pending_backends, pending_backend) { - if (backend == pending_backend) - return; - } + if (array_lsearch_ptr(&client->pending_backends, backend) != NULL) + return; array_push_back(&client->pending_backends, &backend); } diff --git a/src/submission/submission-recipient.c b/src/submission/submission-recipient.c index a49b038bee..7bb2c0d04b 100644 --- a/src/submission/submission-recipient.c +++ b/src/submission/submission-recipient.c @@ -39,17 +39,9 @@ submission_recipient_approved(struct smtp_server_recipient *rcpt ATTR_UNUSED, { struct submission_backend *backend = srcpt->backend; struct client *client = backend->client; - struct submission_backend *rcpt_backend; - bool backend_found = FALSE; array_push_back(&client->rcpt_to, &srcpt); - array_foreach_elem(&client->rcpt_backends, rcpt_backend) { - if (rcpt_backend == backend) { - backend_found = TRUE; - break; - } - } - if (!backend_found) + if (array_lsearch_ptr(&client->rcpt_backends, backend) == NULL) array_push_back(&client->rcpt_backends, &backend); } -- 2.47.3