]> git.ipfire.org Git - thirdparty/git.git/commitdiff
prio-queue: rename .nr to .nr_ and add accessor helpers
authorKristofer Karlsson <krka@spotify.com>
Mon, 8 Jun 2026 19:10:50 +0000 (19:10 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 9 Jun 2026 18:11:46 +0000 (11:11 -0700)
Rename the .nr member to .nr_ so that callers outside prio-queue.c
that directly reference .nr get a compilation error.  This catches
both existing misuse and future in-flight topics.

Add prio_queue_size() for callers that need to know the element count
and prio_queue_for_each() for callers that need to walk all elements.

Convert all external .nr users:
 - Loop conditions: use prio_queue_size(), prio_queue_get(), or
   prio_queue_peek() as the loop condition
 - Array iterations: use prio_queue_for_each()

Signed-off-by: Kristofer Karlsson <krka@spotify.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
14 files changed:
builtin/describe.c
builtin/last-modified.c
builtin/show-branch.c
commit-reach.c
fetch-pack.c
negotiator/default.c
negotiator/skipping.c
object-name.c
pack-bitmap-write.c
path-walk.c
prio-queue.c
prio-queue.h
revision.c
walker.c

index 1c47d7c0b7c38d23032179fb5b0800d7170c4aeb..8e88bdeea6cf1c6a320b397bf8e61a0d7bac82c2 100644 (file)
@@ -278,7 +278,7 @@ static void lazy_queue_put(struct lazy_queue *queue, void *thing)
 
 static bool lazy_queue_empty(const struct lazy_queue *queue)
 {
-       return queue->queue.nr == (queue->get_pending ? 1 : 0);
+       return prio_queue_size(&queue->queue) == (queue->get_pending ? 1 : 0);
 }
 
 static void lazy_queue_clear(struct lazy_queue *queue)
@@ -292,9 +292,14 @@ static unsigned long finish_depth_computation(struct lazy_queue *queue,
 {
        unsigned long seen_commits = 0;
        struct oidset unflagged = OIDSET_INIT;
+       struct commit *commit;
+       int skip = queue->get_pending ? 1 : 0;
 
-       for (size_t i = queue->get_pending ? 1 : 0; i < queue->queue.nr; i++) {
-               struct commit *commit = queue->queue.array[i].data;
+       prio_queue_for_each(&queue->queue, commit) {
+               if (skip) {
+                       skip = 0;
+                       continue;
+               }
                if (!(commit->object.flags & best->flag_within))
                        oidset_insert(&unflagged, &commit->object.oid);
        }
index 8900ceece1cdf339ca7beee266814e6acfff472b..5478182f2e95c235357dbed778c10d4b656a1d62 100644 (file)
@@ -344,6 +344,7 @@ static void process_parent(struct last_modified *lm,
 static int last_modified_run(struct last_modified *lm)
 {
        int max_count, queue_popped = 0;
+       struct commit *c, *n;
        struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
        struct prio_queue not_queue = { compare_commits_by_gen_then_commit_date };
        struct commit_list *list;
@@ -389,10 +390,9 @@ static int last_modified_run(struct last_modified *lm)
                }
        }
 
-       while (queue.nr) {
+       while ((c = prio_queue_get(&queue))) {
                int parent_i;
                struct commit_list *p;
-               struct commit *c = prio_queue_get(&queue);
                struct bitmap *active_c = active_paths_for(lm, c);
 
                if ((0 <= max_count && max_count < ++queue_popped) ||
@@ -416,9 +416,8 @@ static int last_modified_run(struct last_modified *lm)
                 */
                repo_parse_commit(lm->rev.repo, c);
 
-               while (not_queue.nr) {
+               while ((n = prio_queue_get(&not_queue))) {
                        struct commit_list *np;
-                       struct commit *n = prio_queue_get(&not_queue);
 
                        repo_parse_commit(lm->rev.repo, n);
 
index f02831b08500c445b0d0610390e21c08ad6b0fc3..8846f2376fc2ff98e9cd776f585c01f36be21aea 100644 (file)
@@ -62,11 +62,10 @@ static const char *get_color_reset_code(void)
 
 static struct commit *interesting(struct prio_queue *queue)
 {
-       for (size_t i = 0; i < queue->nr; i++) {
-               struct commit *commit = queue->array[i].data;
-               if (commit->object.flags & UNINTERESTING)
-                       continue;
-               return commit;
+       struct commit *commit;
+       prio_queue_for_each(queue, commit) {
+               if (!(commit->object.flags & UNINTERESTING))
+                       return commit;
        }
        return NULL;
 }
@@ -228,11 +227,11 @@ static void join_revs(struct prio_queue *queue,
 {
        int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
        int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
+       struct commit *commit;
 
-       while (queue->nr) {
+       while ((commit = prio_queue_peek(queue))) {
                struct commit_list *parents;
                int still_interesting = !!interesting(queue);
-               struct commit *commit = prio_queue_peek(queue);
                bool get_pending = true;
                int flags = commit->object.flags & all_mask;
 
index 9b3ea46d6f2824ccab3e2067c09ef34f08e1685c..a849de653ef54415aca477ac157da27161268a5f 100644 (file)
@@ -41,8 +41,8 @@ static int compare_commits_by_gen(const void *_a, const void *_b)
 
 static int queue_has_nonstale(struct prio_queue *queue)
 {
-       for (size_t i = 0; i < queue->nr; i++) {
-               struct commit *commit = queue->array[i].data;
+       struct commit *commit;
+       prio_queue_for_each(queue, commit) {
                if (!(commit->object.flags & STALE))
                        return 1;
        }
@@ -1070,6 +1070,7 @@ void ahead_behind(struct repository *r,
                  struct ahead_behind_count *counts, size_t counts_nr)
 {
        struct prio_queue queue = { .compare = compare_commits_by_gen_then_commit_date };
+       void *entry;
        size_t width = DIV_ROUND_UP(commits_nr, BITS_IN_EWORD);
 
        if (!commits_nr || !counts_nr)
@@ -1135,8 +1136,8 @@ void ahead_behind(struct repository *r,
 
        /* STALE is used here, PARENT2 is used by insert_no_dup(). */
        repo_clear_commit_marks(r, PARENT2 | STALE);
-       for (size_t i = 0; i < queue.nr; i++)
-               free_bit_array(queue.array[i].data);
+       prio_queue_for_each(&queue, entry)
+               free_bit_array(entry);
        clear_bit_arrays(&bit_arrays);
        clear_prio_queue(&queue);
 }
@@ -1269,7 +1270,7 @@ int get_branch_base_for_tip(struct repository *r,
                            size_t bases_nr)
 {
        int best_index = -1;
-       struct commit *branch_point = NULL;
+       struct commit *c, *branch_point = NULL;
        struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
        int found_missing_gen = 0;
 
@@ -1322,8 +1323,7 @@ int get_branch_base_for_tip(struct repository *r,
                prio_queue_put(&queue, c);
        }
 
-       while (queue.nr) {
-               struct commit *c = prio_queue_get(&queue);
+       while ((c = prio_queue_get(&queue))) {
                int best_for_c = get_best(c);
                int best_for_p, positive;
                struct commit *parent;
index 120e01f3cf26740ffc896c12eb4a9d233603e89f..29c41132ee04957d582c899146cc7afbfe626ee9 100644 (file)
@@ -662,8 +662,8 @@ static int mark_complete_oid(const struct reference *ref, void *cb_data UNUSED)
 static void mark_recent_complete_commits(struct fetch_pack_args *args,
                                         timestamp_t cutoff)
 {
-       while (complete.nr) {
-               struct commit *item = prio_queue_peek(&complete);
+       struct commit *item;
+       while ((item = prio_queue_peek(&complete))) {
                if (item->date < cutoff)
                        break;
                print_verbose(args, _("Marking %s as complete"),
index 78d58d57cebbfb6cb32d4fc67c0851c893d002fd..19cdf3808cf26634fbb57f395713f910c9ddb48d 100644 (file)
@@ -113,10 +113,12 @@ static const struct object_id *get_rev(struct negotiation_state *ns)
                unsigned int mark;
                struct commit_list *parents;
 
-               if (ns->rev_list.nr == 0 || ns->non_common_revs == 0)
+               if (ns->non_common_revs == 0)
                        return NULL;
 
                commit = prio_queue_get(&ns->rev_list);
+               if (!commit)
+                       return NULL;
                repo_parse_commit(the_repository, commit);
                parents = commit->parents;
 
index 68c9b3b997dc8051c0c677c08c7cb731cc8b39ed..db90fa77b5325b7bfa29aca8ffda1371cf899774 100644 (file)
@@ -143,8 +143,7 @@ static int push_parent(struct data *data, struct entry *entry,
                /*
                 * Find the existing entry and use it.
                 */
-               for (size_t i = 0; i < data->rev_list.nr; i++) {
-                       parent_entry = data->rev_list.array[i].data;
+               prio_queue_for_each(&data->rev_list, parent_entry) {
                        if (parent_entry->commit == to_push)
                                goto parent_found;
                }
@@ -181,10 +180,12 @@ static const struct object_id *get_rev(struct data *data)
                struct commit_list *p;
                int parent_pushed = 0;
 
-               if (data->rev_list.nr == 0 || data->non_common_revs == 0)
+               if (data->non_common_revs == 0)
                        return NULL;
 
                entry = prio_queue_get(&data->rev_list);
+               if (!entry)
+                       return NULL;
                commit = entry->commit;
                commit->object.flags |= POPPED;
                if (!(commit->object.flags & COMMON))
@@ -253,8 +254,9 @@ static void have_sent(struct fetch_negotiator *n, struct commit *c)
 static void release(struct fetch_negotiator *n)
 {
        struct data *data = n->data;
-       for (size_t i = 0; i < data->rev_list.nr; i++)
-               free(data->rev_list.array[i].data);
+       void *entry;
+       prio_queue_for_each(&data->rev_list, entry)
+               free(entry);
        clear_prio_queue(&data->rev_list);
        FREE_AND_NULL(data);
 }
index 9ac86f19c77bbdf9873872b00cf72cdc13220022..2fedfe1761016e8978400fe31f68f162683b5f9f 100644 (file)
@@ -1208,7 +1208,7 @@ static int get_oid_oneline(struct repository *r,
                l->item->object.flags |= ONELINE_SEEN;
                prio_queue_put(&copy, l->item);
        }
-       while (copy.nr) {
+       while (prio_queue_size(&copy)) {
                const char *p, *buf;
                struct commit *commit;
                int matches;
index 1c8070f99c03ca9ce832a4ca47b5d9a66d526a29..ed9714b135cf52de7e90a9abccceb17ef6f333da 100644 (file)
@@ -513,6 +513,8 @@ static int fill_bitmap_commit(struct bitmap_writer *writer,
                              struct bitmap_index *old_bitmap,
                              const uint32_t *mapping)
 {
+       struct commit *c;
+       struct tree *tree;
        int found;
        uint32_t pos;
        if (!ent->bitmap)
@@ -520,9 +522,8 @@ static int fill_bitmap_commit(struct bitmap_writer *writer,
 
        prio_queue_put(queue, commit);
 
-       while (queue->nr) {
+       while ((c = prio_queue_get(queue))) {
                struct commit_list *p;
-               struct commit *c = prio_queue_get(queue);
 
                if (old_bitmap && mapping) {
                        struct ewah_bitmap *old;
@@ -574,9 +575,8 @@ static int fill_bitmap_commit(struct bitmap_writer *writer,
                }
        }
 
-       while (tree_queue->nr) {
-               if (fill_bitmap_tree(writer, ent->bitmap,
-                                    prio_queue_get(tree_queue)) < 0)
+       while ((tree = prio_queue_get(tree_queue))) {
+               if (fill_bitmap_tree(writer, ent->bitmap, tree) < 0)
                        return -1;
        }
        return 0;
index 94ff90bd1566b629b4660f5c3e5cdacf2a7ab24e..cf3b2d0765961b58107133ea3ede4d202107aa44 100644 (file)
@@ -699,6 +699,7 @@ int walk_objects_by_path(struct path_walk_info *info)
        int ret;
        size_t commits_nr = 0, paths_nr = 0;
        struct commit *c;
+       char *path;
        struct type_and_oid_list *root_tree_list;
        struct type_and_oid_list *commit_list;
        struct path_walk_context ctx = {
@@ -808,8 +809,7 @@ int walk_objects_by_path(struct path_walk_info *info)
        free(commit_list);
 
        trace2_region_enter("path-walk", "path-walk", info->revs->repo);
-       while (!ret && ctx.path_stack.nr) {
-               char *path = prio_queue_get(&ctx.path_stack);
+       while (!ret && (path = prio_queue_get(&ctx.path_stack))) {
                paths_nr++;
 
                ret = walk_path(&ctx, path);
@@ -821,12 +821,12 @@ int walk_objects_by_path(struct path_walk_info *info)
        if (!strmap_empty(&ctx.paths_to_lists)) {
                struct hashmap_iter iter;
                struct strmap_entry *entry;
+               char *path;
 
                strmap_for_each_entry(&ctx.paths_to_lists, &iter, entry)
                        push_to_stack(&ctx, entry->key);
 
-               while (!ret && ctx.path_stack.nr) {
-                       char *path = prio_queue_get(&ctx.path_stack);
+               while (!ret && (path = prio_queue_get(&ctx.path_stack))) {
                        paths_nr++;
 
                        ret = walk_path(&ctx, path);
index 9748528ce6ecd6f3d1e707884bc62693fbda0bf4..ead4faf4bbda4a9f0d54a00f536798037943c7b8 100644 (file)
@@ -22,16 +22,16 @@ void prio_queue_reverse(struct prio_queue *queue)
 
        if (queue->compare)
                BUG("prio_queue_reverse() on non-LIFO queue");
-       if (!queue->nr)
+       if (!queue->nr_)
                return;
-       for (i = 0; i < (j = (queue->nr - 1) - i); i++)
+       for (i = 0; i < (j = (queue->nr_ - 1) - i); i++)
                swap(queue, i, j);
 }
 
 void clear_prio_queue(struct prio_queue *queue)
 {
        FREE_AND_NULL(queue->array);
-       queue->nr = 0;
+       queue->nr_ = 0;
        queue->alloc = 0;
        queue->insertion_ctr = 0;
 }
@@ -41,15 +41,15 @@ void prio_queue_put(struct prio_queue *queue, void *thing)
        size_t ix, parent;
 
        /* Append at the end */
-       ALLOC_GROW(queue->array, queue->nr + 1, queue->alloc);
-       queue->array[queue->nr].ctr = queue->insertion_ctr++;
-       queue->array[queue->nr].data = thing;
-       queue->nr++;
+       ALLOC_GROW(queue->array, queue->nr_ + 1, queue->alloc);
+       queue->array[queue->nr_].ctr = queue->insertion_ctr++;
+       queue->array[queue->nr_].data = thing;
+       queue->nr_++;
        if (!queue->compare)
                return; /* LIFO */
 
        /* Bubble up the new one */
-       for (ix = queue->nr - 1; ix; ix = parent) {
+       for (ix = queue->nr_ - 1; ix; ix = parent) {
                parent = (ix - 1) / 2;
                if (compare(queue, parent, ix) <= 0)
                        break;
@@ -63,9 +63,9 @@ static void sift_down_root(struct prio_queue *queue)
        size_t ix, child;
 
        /* Push down the one at the root */
-       for (ix = 0; ix * 2 + 1 < queue->nr; ix = child) {
+       for (ix = 0; ix * 2 + 1 < queue->nr_; ix = child) {
                child = ix * 2 + 1; /* left */
-               if (child + 1 < queue->nr &&
+               if (child + 1 < queue->nr_ &&
                    compare(queue, child, child + 1) >= 0)
                        child++; /* use right child */
 
@@ -80,36 +80,36 @@ void *prio_queue_get(struct prio_queue *queue)
 {
        void *result;
 
-       if (!queue->nr)
+       if (!queue->nr_)
                return NULL;
        if (!queue->compare)
-               return queue->array[--queue->nr].data; /* LIFO */
+               return queue->array[--queue->nr_].data; /* LIFO */
 
        result = queue->array[0].data;
-       if (!--queue->nr)
+       if (!--queue->nr_)
                return result;
 
-       queue->array[0] = queue->array[queue->nr];
+       queue->array[0] = queue->array[queue->nr_];
        sift_down_root(queue);
        return result;
 }
 
 void *prio_queue_peek(struct prio_queue *queue)
 {
-       if (!queue->nr)
+       if (!queue->nr_)
                return NULL;
        if (!queue->compare)
-               return queue->array[queue->nr - 1].data;
+               return queue->array[queue->nr_ - 1].data;
        return queue->array[0].data;
 }
 
 void prio_queue_replace(struct prio_queue *queue, void *thing)
 {
-       if (!queue->nr) {
+       if (!queue->nr_) {
                prio_queue_put(queue, thing);
        } else if (!queue->compare) {
-               queue->array[queue->nr - 1].ctr = queue->insertion_ctr++;
-               queue->array[queue->nr - 1].data = thing;
+               queue->array[queue->nr_ - 1].ctr = queue->insertion_ctr++;
+               queue->array[queue->nr_ - 1].data = thing;
        } else {
                queue->array[0].ctr = queue->insertion_ctr++;
                queue->array[0].data = thing;
index da7fad2f1f408ff0e00fd1e0ee85f4e268465185..7f2aa986b1c7dbbceb59311680dfe763218574c4 100644 (file)
@@ -30,7 +30,7 @@ struct prio_queue {
        prio_queue_compare_fn compare;
        size_t insertion_ctr;
        void *cb_data;
-       size_t alloc, nr;
+       size_t alloc, nr_;
        struct prio_queue_entry *array;
 };
 
@@ -52,6 +52,16 @@ void *prio_queue_get(struct prio_queue *);
  */
 void *prio_queue_peek(struct prio_queue *);
 
+static inline size_t prio_queue_size(const struct prio_queue *queue)
+{
+       return queue->nr_;
+}
+
+#define prio_queue_for_each(queue, it) \
+       for (size_t pq_ix_ = 0; \
+            pq_ix_ < (queue)->nr_ && ((it) = (queue)->array[pq_ix_].data, 1); \
+            pq_ix_++)
+
 /*
  * Replace the "thing" that compares the smallest with a new "thing",
  * like prio_queue_get()+prio_queue_put() would do, but in a more
index 5693618be4ec81b8a3dc82d55319d9bc4de64673..34e2d146f4e09d55fd63c581981c27728c40bc69 100644 (file)
@@ -476,16 +476,15 @@ static struct commit *handle_commit(struct rev_info *revs,
 static int everybody_uninteresting(struct prio_queue *orig,
                                   struct commit **interesting_cache)
 {
-       size_t i;
+       struct commit *commit;
 
        if (*interesting_cache) {
-               struct commit *commit = *interesting_cache;
+               commit = *interesting_cache;
                if (!(commit->object.flags & UNINTERESTING))
                        return 0;
        }
 
-       for (i = 0; i < orig->nr; i++) {
-               struct commit *commit = orig->array[i].data;
+       prio_queue_for_each(orig, commit) {
                if (commit->object.flags & UNINTERESTING)
                        continue;
 
@@ -1446,7 +1445,7 @@ static int limit_list(struct rev_info *revs)
        struct commit_list *original_list = revs->commits;
        struct commit_list *newlist = NULL;
        struct commit_list **p = &newlist;
-       struct commit *interesting_cache = NULL;
+       struct commit *commit, *interesting_cache = NULL;
        struct prio_queue queue = { .compare = compare_commits_by_commit_date };
 
        if (revs->ancestry_path_implicit_bottoms) {
@@ -1461,8 +1460,7 @@ static int limit_list(struct rev_info *revs)
                prio_queue_put(&queue, commit);
        }
 
-       while (queue.nr) {
-               struct commit *commit = prio_queue_get(&queue);
+       while ((commit = prio_queue_get(&queue))) {
                struct object *obj = &commit->object;
 
                if (commit == interesting_cache)
@@ -4028,8 +4026,8 @@ static enum rewrite_result rewrite_one_1(struct rev_info *revs,
 
 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
 {
-       while (q->nr) {
-               struct commit *item = prio_queue_peek(q);
+       struct commit *item;
+       while ((item = prio_queue_peek(q))) {
                struct commit_list *p = *list;
 
                if (p && p->item->date >= item->date)
index e98eb6da53692e6e8d56c0f32ad642d31bcad9d0..e3de77f0925082ed813ba29e6464bd8356322ba0 100644 (file)
--- a/walker.c
+++ b/walker.c
@@ -84,12 +84,12 @@ static struct prio_queue complete = { compare_commits_by_commit_date };
 static int process_commit(struct walker *walker, struct commit *commit)
 {
        struct commit_list *parents;
+       struct commit *item;
 
        if (repo_parse_commit(the_repository, commit))
                return -1;
 
-       while (complete.nr) {
-               struct commit *item = prio_queue_peek(&complete);
+       while ((item = prio_queue_peek(&complete))) {
                if (item->date < commit->date)
                        break;
                pop_most_recent_commit(&complete, COMPLETE);