prio-queue: rename .nr to .nr_ and add accessor helpers
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>
Kristofer Karlsson committed
Jun 8, 2026 at 19:10 UTC
3c5783698854b56376b775af5053ae6fbe825cc0
14 files changed
+85
-70
builtin/describe.c
+8
-3
@@ -278,7 +278,7 @@ static void lazy_queue_put(struct lazy_queue *queue, void *thing)
278
279
static bool lazy_queue_empty(const struct lazy_queue *queue)
280
{
281
- return queue->queue.nr == (queue->get_pending ? 1 : 0);
281
+ return prio_queue_size(&queue->queue) == (queue->get_pending ? 1 : 0);
282
}
283
284
static void lazy_queue_clear(struct lazy_queue *queue)
@@ -292,9 +292,14 @@ static unsigned long finish_depth_computation(struct lazy_queue *queue,
292
{
293
unsigned long seen_commits = 0;
294
struct oidset unflagged = OIDSET_INIT;
295
+ struct commit *commit;
296
+ int skip = queue->get_pending ? 1 : 0;
297
296
- for (size_t i = queue->get_pending ? 1 : 0; i < queue->queue.nr; i++) {
297
- struct commit *commit = queue->queue.array[i].data;
298
+ prio_queue_for_each(&queue->queue, commit) {
299
+ if (skip) {
300
+ skip = 0;
301
+ continue;
302
+ }
303
if (!(commit->object.flags & best->flag_within))
304
oidset_insert(&unflagged, &commit->object.oid);
305
}
builtin/last-modified.c
+3
-4
@@ -344,6 +344,7 @@ static void process_parent(struct last_modified *lm,
344
static int last_modified_run(struct last_modified *lm)
345
{
346
int max_count, queue_popped = 0;
347
+ struct commit *c, *n;
348
struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
349
struct prio_queue not_queue = { compare_commits_by_gen_then_commit_date };
350
struct commit_list *list;
@@ -389,10 +390,9 @@ static int last_modified_run(struct last_modified *lm)
390
}
391
}
392
392
- while (queue.nr) {
393
+ while ((c = prio_queue_get(&queue))) {
394
int parent_i;
395
struct commit_list *p;
395
- struct commit *c = prio_queue_get(&queue);
396
struct bitmap *active_c = active_paths_for(lm, c);
397
398
if ((0 <= max_count && max_count < ++queue_popped) ||
@@ -416,9 +416,8 @@ static int last_modified_run(struct last_modified *lm)
416
*/
417
repo_parse_commit(lm->rev.repo, c);
418
419
- while (not_queue.nr) {
419
+ while ((n = prio_queue_get(¬_queue))) {
420
struct commit_list *np;
421
- struct commit *n = prio_queue_get(¬_queue);
421
422
repo_parse_commit(lm->rev.repo, n);
423
builtin/show-branch.c
+6
-7
@@ -62,11 +62,10 @@ static const char *get_color_reset_code(void)
62
63
static struct commit *interesting(struct prio_queue *queue)
64
{
65
- for (size_t i = 0; i < queue->nr; i++) {
66
- struct commit *commit = queue->array[i].data;
67
- if (commit->object.flags & UNINTERESTING)
68
- continue;
69
- return commit;
65
+ struct commit *commit;
66
+ prio_queue_for_each(queue, commit) {
67
+ if (!(commit->object.flags & UNINTERESTING))
68
+ return commit;
69
}
70
return NULL;
71
}
@@ -228,11 +227,11 @@ static void join_revs(struct prio_queue *queue,
227
{
228
int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
229
int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
230
+ struct commit *commit;
231
232
- while (queue->nr) {
232
+ while ((commit = prio_queue_peek(queue))) {
233
struct commit_list *parents;
234
int still_interesting = !!interesting(queue);
235
- struct commit *commit = prio_queue_peek(queue);
235
bool get_pending = true;
236
int flags = commit->object.flags & all_mask;
237
commit-reach.c
+7
-7
@@ -41,8 +41,8 @@ static int compare_commits_by_gen(const void *_a, const void *_b)
41
42
static int queue_has_nonstale(struct prio_queue *queue)
43
{
44
- for (size_t i = 0; i < queue->nr; i++) {
45
- struct commit *commit = queue->array[i].data;
44
+ struct commit *commit;
45
+ prio_queue_for_each(queue, commit) {
46
if (!(commit->object.flags & STALE))
47
return 1;
48
}
@@ -1070,6 +1070,7 @@ void ahead_behind(struct repository *r,
1070
struct ahead_behind_count *counts, size_t counts_nr)
1071
{
1072
struct prio_queue queue = { .compare = compare_commits_by_gen_then_commit_date };
1073
+ void *entry;
1074
size_t width = DIV_ROUND_UP(commits_nr, BITS_IN_EWORD);
1075
1076
if (!commits_nr || !counts_nr)
@@ -1135,8 +1136,8 @@ void ahead_behind(struct repository *r,
1136
1137
/* STALE is used here, PARENT2 is used by insert_no_dup(). */
1138
repo_clear_commit_marks(r, PARENT2 | STALE);
1138
- for (size_t i = 0; i < queue.nr; i++)
1139
- free_bit_array(queue.array[i].data);
1139
+ prio_queue_for_each(&queue, entry)
1140
+ free_bit_array(entry);
1141
clear_bit_arrays(&bit_arrays);
1142
clear_prio_queue(&queue);
1143
}
@@ -1269,7 +1270,7 @@ int get_branch_base_for_tip(struct repository *r,
1270
size_t bases_nr)
1271
{
1272
int best_index = -1;
1272
- struct commit *branch_point = NULL;
1273
+ struct commit *c, *branch_point = NULL;
1274
struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
1275
int found_missing_gen = 0;
1276
@@ -1322,8 +1323,7 @@ int get_branch_base_for_tip(struct repository *r,
1323
prio_queue_put(&queue, c);
1324
}
1325
1325
- while (queue.nr) {
1326
- struct commit *c = prio_queue_get(&queue);
1326
+ while ((c = prio_queue_get(&queue))) {
1327
int best_for_c = get_best(c);
1328
int best_for_p, positive;
1329
struct commit *parent;
fetch-pack.c
+2
-2
@@ -662,8 +662,8 @@ static int mark_complete_oid(const struct reference *ref, void *cb_data UNUSED)
662
static void mark_recent_complete_commits(struct fetch_pack_args *args,
663
timestamp_t cutoff)
664
{
665
- while (complete.nr) {
666
- struct commit *item = prio_queue_peek(&complete);
665
+ struct commit *item;
666
+ while ((item = prio_queue_peek(&complete))) {
667
if (item->date < cutoff)
668
break;
669
print_verbose(args, _("Marking %s as complete"),
negotiator/default.c
+3
-1
@@ -113,10 +113,12 @@ static const struct object_id *get_rev(struct negotiation_state *ns)
113
unsigned int mark;
114
struct commit_list *parents;
115
116
- if (ns->rev_list.nr == 0 || ns->non_common_revs == 0)
116
+ if (ns->non_common_revs == 0)
117
return NULL;
118
119
commit = prio_queue_get(&ns->rev_list);
120
+ if (!commit)
121
+ return NULL;
122
repo_parse_commit(the_repository, commit);
123
parents = commit->parents;
124
negotiator/skipping.c
+7
-5
@@ -143,8 +143,7 @@ static int push_parent(struct data *data, struct entry *entry,
143
/*
144
* Find the existing entry and use it.
145
*/
146
- for (size_t i = 0; i < data->rev_list.nr; i++) {
147
- parent_entry = data->rev_list.array[i].data;
146
+ prio_queue_for_each(&data->rev_list, parent_entry) {
147
if (parent_entry->commit == to_push)
148
goto parent_found;
149
}
@@ -181,10 +180,12 @@ static const struct object_id *get_rev(struct data *data)
180
struct commit_list *p;
181
int parent_pushed = 0;
182
184
- if (data->rev_list.nr == 0 || data->non_common_revs == 0)
183
+ if (data->non_common_revs == 0)
184
return NULL;
185
186
entry = prio_queue_get(&data->rev_list);
187
+ if (!entry)
188
+ return NULL;
189
commit = entry->commit;
190
commit->object.flags |= POPPED;
191
if (!(commit->object.flags & COMMON))
@@ -253,8 +254,9 @@ static void have_sent(struct fetch_negotiator *n, struct commit *c)
254
static void release(struct fetch_negotiator *n)
255
{
256
struct data *data = n->data;
256
- for (size_t i = 0; i < data->rev_list.nr; i++)
257
- free(data->rev_list.array[i].data);
257
+ void *entry;
258
+ prio_queue_for_each(&data->rev_list, entry)
259
+ free(entry);
260
clear_prio_queue(&data->rev_list);
261
FREE_AND_NULL(data);
262
}
object-name.c
+1
-1
@@ -1208,7 +1208,7 @@ static int get_oid_oneline(struct repository *r,
1208
l->item->object.flags |= ONELINE_SEEN;
1209
prio_queue_put(©, l->item);
1210
}
1211
- while (copy.nr) {
1211
+ while (prio_queue_size(©)) {
1212
const char *p, *buf;
1213
struct commit *commit;
1214
int matches;
pack-bitmap-write.c
+5
-5
@@ -513,6 +513,8 @@ static int fill_bitmap_commit(struct bitmap_writer *writer,
513
struct bitmap_index *old_bitmap,
514
const uint32_t *mapping)
515
{
516
+ struct commit *c;
517
+ struct tree *tree;
518
int found;
519
uint32_t pos;
520
if (!ent->bitmap)
@@ -520,9 +522,8 @@ static int fill_bitmap_commit(struct bitmap_writer *writer,
522
523
prio_queue_put(queue, commit);
524
523
- while (queue->nr) {
525
+ while ((c = prio_queue_get(queue))) {
526
struct commit_list *p;
525
- struct commit *c = prio_queue_get(queue);
527
528
if (old_bitmap && mapping) {
529
struct ewah_bitmap *old;
@@ -574,9 +575,8 @@ static int fill_bitmap_commit(struct bitmap_writer *writer,
575
}
576
}
577
577
- while (tree_queue->nr) {
578
- if (fill_bitmap_tree(writer, ent->bitmap,
579
- prio_queue_get(tree_queue)) < 0)
578
+ while ((tree = prio_queue_get(tree_queue))) {
579
+ if (fill_bitmap_tree(writer, ent->bitmap, tree) < 0)
580
return -1;
581
}
582
return 0;
path-walk.c
+4
-4
@@ -699,6 +699,7 @@ int walk_objects_by_path(struct path_walk_info *info)
699
int ret;
700
size_t commits_nr = 0, paths_nr = 0;
701
struct commit *c;
702
+ char *path;
703
struct type_and_oid_list *root_tree_list;
704
struct type_and_oid_list *commit_list;
705
struct path_walk_context ctx = {
@@ -808,8 +809,7 @@ int walk_objects_by_path(struct path_walk_info *info)
809
free(commit_list);
810
811
trace2_region_enter("path-walk", "path-walk", info->revs->repo);
811
- while (!ret && ctx.path_stack.nr) {
812
- char *path = prio_queue_get(&ctx.path_stack);
812
+ while (!ret && (path = prio_queue_get(&ctx.path_stack))) {
813
paths_nr++;
814
815
ret = walk_path(&ctx, path);
@@ -821,12 +821,12 @@ int walk_objects_by_path(struct path_walk_info *info)
821
if (!strmap_empty(&ctx.paths_to_lists)) {
822
struct hashmap_iter iter;
823
struct strmap_entry *entry;
824
+ char *path;
825
826
strmap_for_each_entry(&ctx.paths_to_lists, &iter, entry)
827
push_to_stack(&ctx, entry->key);
828
828
- while (!ret && ctx.path_stack.nr) {
829
- char *path = prio_queue_get(&ctx.path_stack);
829
+ while (!ret && (path = prio_queue_get(&ctx.path_stack))) {
830
paths_nr++;
831
832
ret = walk_path(&ctx, path);
prio-queue.c
+19
-19
@@ -22,16 +22,16 @@ void prio_queue_reverse(struct prio_queue *queue)
22
23
if (queue->compare)
24
BUG("prio_queue_reverse() on non-LIFO queue");
25
- if (!queue->nr)
25
+ if (!queue->nr_)
26
return;
27
- for (i = 0; i < (j = (queue->nr - 1) - i); i++)
27
+ for (i = 0; i < (j = (queue->nr_ - 1) - i); i++)
28
swap(queue, i, j);
29
}
30
31
void clear_prio_queue(struct prio_queue *queue)
32
{
33
FREE_AND_NULL(queue->array);
34
- queue->nr = 0;
34
+ queue->nr_ = 0;
35
queue->alloc = 0;
36
queue->insertion_ctr = 0;
37
}
@@ -41,15 +41,15 @@ void prio_queue_put(struct prio_queue *queue, void *thing)
41
size_t ix, parent;
42
43
/* Append at the end */
44
- ALLOC_GROW(queue->array, queue->nr + 1, queue->alloc);
45
- queue->array[queue->nr].ctr = queue->insertion_ctr++;
46
- queue->array[queue->nr].data = thing;
47
- queue->nr++;
44
+ ALLOC_GROW(queue->array, queue->nr_ + 1, queue->alloc);
45
+ queue->array[queue->nr_].ctr = queue->insertion_ctr++;
46
+ queue->array[queue->nr_].data = thing;
47
+ queue->nr_++;
48
if (!queue->compare)
49
return; /* LIFO */
50
51
/* Bubble up the new one */
52
- for (ix = queue->nr - 1; ix; ix = parent) {
52
+ for (ix = queue->nr_ - 1; ix; ix = parent) {
53
parent = (ix - 1) / 2;
54
if (compare(queue, parent, ix) <= 0)
55
break;
@@ -63,9 +63,9 @@ static void sift_down_root(struct prio_queue *queue)
63
size_t ix, child;
64
65
/* Push down the one at the root */
66
- for (ix = 0; ix * 2 + 1 < queue->nr; ix = child) {
66
+ for (ix = 0; ix * 2 + 1 < queue->nr_; ix = child) {
67
child = ix * 2 + 1; /* left */
68
- if (child + 1 < queue->nr &&
68
+ if (child + 1 < queue->nr_ &&
69
compare(queue, child, child + 1) >= 0)
70
child++; /* use right child */
71
@@ -80,36 +80,36 @@ void *prio_queue_get(struct prio_queue *queue)
80
{
81
void *result;
82
83
- if (!queue->nr)
83
+ if (!queue->nr_)
84
return NULL;
85
if (!queue->compare)
86
- return queue->array[--queue->nr].data; /* LIFO */
86
+ return queue->array[--queue->nr_].data; /* LIFO */
87
88
result = queue->array[0].data;
89
- if (!--queue->nr)
89
+ if (!--queue->nr_)
90
return result;
91
92
- queue->array[0] = queue->array[queue->nr];
92
+ queue->array[0] = queue->array[queue->nr_];
93
sift_down_root(queue);
94
return result;
95
}
96
97
void *prio_queue_peek(struct prio_queue *queue)
98
{
99
- if (!queue->nr)
99
+ if (!queue->nr_)
100
return NULL;
101
if (!queue->compare)
102
- return queue->array[queue->nr - 1].data;
102
+ return queue->array[queue->nr_ - 1].data;
103
return queue->array[0].data;
104
}
105
106
void prio_queue_replace(struct prio_queue *queue, void *thing)
107
{
108
- if (!queue->nr) {
108
+ if (!queue->nr_) {
109
prio_queue_put(queue, thing);
110
} else if (!queue->compare) {
111
- queue->array[queue->nr - 1].ctr = queue->insertion_ctr++;
112
- queue->array[queue->nr - 1].data = thing;
111
+ queue->array[queue->nr_ - 1].ctr = queue->insertion_ctr++;
112
+ queue->array[queue->nr_ - 1].data = thing;
113
} else {
114
queue->array[0].ctr = queue->insertion_ctr++;
115
queue->array[0].data = thing;
prio-queue.h
+11
-1
@@ -30,7 +30,7 @@ struct prio_queue {
30
prio_queue_compare_fn compare;
31
size_t insertion_ctr;
32
void *cb_data;
33
- size_t alloc, nr;
33
+ size_t alloc, nr_;
34
struct prio_queue_entry *array;
35
};
36
@@ -52,6 +52,16 @@ void *prio_queue_get(struct prio_queue *);
52
*/
53
void *prio_queue_peek(struct prio_queue *);
54
55
+static inline size_t prio_queue_size(const struct prio_queue *queue)
56
+{
57
+ return queue->nr_;
58
+}
59
+
60
+#define prio_queue_for_each(queue, it) \
61
+ for (size_t pq_ix_ = 0; \
62
+ pq_ix_ < (queue)->nr_ && ((it) = (queue)->array[pq_ix_].data, 1); \
63
+ pq_ix_++)
64
+
65
/*
66
* Replace the "thing" that compares the smallest with a new "thing",
67
* like prio_queue_get()+prio_queue_put() would do, but in a more
revision.c
+7
-9
@@ -476,16 +476,15 @@ static struct commit *handle_commit(struct rev_info *revs,
476
static int everybody_uninteresting(struct prio_queue *orig,
477
struct commit **interesting_cache)
478
{
479
- size_t i;
479
+ struct commit *commit;
480
481
if (*interesting_cache) {
482
- struct commit *commit = *interesting_cache;
482
+ commit = *interesting_cache;
483
if (!(commit->object.flags & UNINTERESTING))
484
return 0;
485
}
486
487
- for (i = 0; i < orig->nr; i++) {
488
- struct commit *commit = orig->array[i].data;
487
+ prio_queue_for_each(orig, commit) {
488
if (commit->object.flags & UNINTERESTING)
489
continue;
490
@@ -1446,7 +1445,7 @@ static int limit_list(struct rev_info *revs)
1445
struct commit_list *original_list = revs->commits;
1446
struct commit_list *newlist = NULL;
1447
struct commit_list **p = &newlist;
1449
- struct commit *interesting_cache = NULL;
1448
+ struct commit *commit, *interesting_cache = NULL;
1449
struct prio_queue queue = { .compare = compare_commits_by_commit_date };
1450
1451
if (revs->ancestry_path_implicit_bottoms) {
@@ -1461,8 +1460,7 @@ static int limit_list(struct rev_info *revs)
1460
prio_queue_put(&queue, commit);
1461
}
1462
1464
- while (queue.nr) {
1465
- struct commit *commit = prio_queue_get(&queue);
1463
+ while ((commit = prio_queue_get(&queue))) {
1464
struct object *obj = &commit->object;
1465
1466
if (commit == interesting_cache)
@@ -4028,8 +4026,8 @@ static enum rewrite_result rewrite_one_1(struct rev_info *revs,
4026
4027
static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
4028
{
4031
- while (q->nr) {
4032
- struct commit *item = prio_queue_peek(q);
4029
+ struct commit *item;
4030
+ while ((item = prio_queue_peek(q))) {
4031
struct commit_list *p = *list;
4032
4033
if (p && p->item->date >= item->date)
walker.c
+2
-2
@@ -84,12 +84,12 @@ static struct prio_queue complete = { compare_commits_by_commit_date };
84
static int process_commit(struct walker *walker, struct commit *commit)
85
{
86
struct commit_list *parents;
87
+ struct commit *item;
88
89
if (repo_parse_commit(the_repository, commit))
90
return -1;
91
91
- while (complete.nr) {
92
- struct commit *item = prio_queue_peek(&complete);
92
+ while ((item = prio_queue_peek(&complete))) {
93
if (item->date < commit->date)
94
break;
95
pop_most_recent_commit(&complete, COMPLETE);