]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
global: Use array_lsearch_ptr[_idx]() where possible
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 3 Sep 2024 10:04:00 +0000 (13:04 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:14 +0000 (12:34 +0200)
20 files changed:
src/auth/passdb.c
src/auth/userdb.c
src/lib-dict/dict.c
src/lib-http/http-client-connection.c
src/lib-http/http-client-peer.c
src/lib-http/http-client-queue.c
src/lib-http/http-client.c
src/lib-http/http-server-resource.c
src/lib-index/mail-index-map.c
src/lib-sql/driver-cassandra.c
src/lib-sql/sql-api.c
src/lib-storage/index/imapc/imapc-mail-fetch.c
src/lib-storage/mail-storage-hooks.c
src/lib-storage/mail-storage.c
src/lib-storage/mailbox-search-result.c
src/lib/ioloop.c
src/lib/lib-event.c
src/plugins/quota/quota.c
src/submission/submission-backend.c
src/submission/submission-recipient.c

index cf54b0a447c6b88a12a51b803e3fd44aebc70008..187b659cc050bea90e92e15f02e622ab61ad37c1 100644 (file)
@@ -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);
index 610b9cc4f46af4296988b0e23504e6452cbdbc62..f822cdda1fd9cd640244bf4713ba341cdc0427de 100644 (file)
@@ -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);
index c23644d57ab6cf71bc77f55215b1edf709009928..38a6ecd23f0eb4a553081a2412b7f58b6f0a5eb7 100644 (file)
@@ -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)
index b3f15c8868aef0fa690386fb424c40eb9435916b..95247c7c126f100de970378714151d21832184d0 100644 (file)
@@ -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);
 
index 9182f55b255444bed4d97cb8908d9fbb5026abe1..2238cbda291d0e364c53f884c72e63a9ed03e76a 100644 (file)
@@ -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 *
index c1b27a1ffbbc3bebc24a92959d9814de6b08c64d..773e06cd1f0a252c9368dd79f7735a3330932276 100644 (file)
@@ -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 */
index 42f126dbc5d9c2cf9a0f62cb709ef60d2cb6a0d0..a6146c0d0e89e6b94c49f76f5d8c06352fd0af44 100644 (file)
@@ -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);
 }
 
 /*
index 943914deffaabcf6d86676a4c7c62223282876d1..a773acdbb6c7ea514d02c50404886be921c7b333 100644 (file)
@@ -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);
 }
 
 /*
index dcfc93fbe78883d4793e02960cb08c19ccadecd4..b3aeb07ffa0a61e15e84c6d64dd84966e1226e2c 100644 (file)
@@ -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;
index 04a8230349e6df89834ec409f92d27d83d20a466..f4019e01349785ec70a133789a9e4f9d5c849305 100644 (file)
@@ -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)
index a3d7d519cd5b6c282af3ce5594b8ba0a6f16df96..1af955445892566ebd00559452cca785d54d9257 100644 (file)
@@ -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,
index 3c4ae4ec69167930bb82083bcb33ac59e887bde7..71d636f5187cb0e349e9d246050434dd2879f5fe 100644 (file)
@@ -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);
index 3506aaf3a9550ce71287f839b773f72a1992df6d..e9aaaa913d5c0e35cf0504e4ed716b80839ddac7 100644 (file)
@@ -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);
 }
 
index 0c4583c4caf26c61f83d06a1b40d91e5a8c06bc1..582e1e718ea7db12166b86249275220eaec512c4 100644 (file)
@@ -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,
index d239c7227860dad699d1acc3db3e3dfb86e356a3..d49cb9a24376184cbae8adae4092f91a42aa2fe4 100644 (file)
@@ -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);
index 4a2757abd3614c81486c9cb9dbc3811d55a3a024..2485eec28ceadefbb92155ef8cb54496200fd433 100644 (file)
@@ -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)
index fa9b7055c2b526c976234729f8f1460fe3514709..2785df803870a61a64d854e7016feca1d149f7b0 100644 (file)
@@ -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 *
index 917bd0ff9864476649ead95135e677dd3de10cb9..5997f5f84bd29c4fe5c70975a62cd891336ca2fa 100644 (file)
@@ -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(&quota->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);
        }
 }
 
index b9a22203d881eac68f5a0b2212d4c790123eb046..a413e6625eab44cb36e34071213da19bfc83c1c4 100644 (file)
@@ -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);
 }
index a49b038bee73536556a1544994410e385bf0e7ee..7bb2c0d04b835bb294979885ca1e48c576fc1a50 100644 (file)
@@ -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);
 }