Skip to content

Commit d3ee883

Browse files
committed
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>
1 parent 600fe74 commit d3ee883

14 files changed

Lines changed: 83 additions & 68 deletions

builtin/describe.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ static void lazy_queue_put(struct lazy_queue *queue, void *thing)
278278

279279
static bool lazy_queue_empty(const struct lazy_queue *queue)
280280
{
281-
return queue->queue.nr == (queue->get_pending ? 1 : 0);
281+
return prio_queue_size(&queue->queue) == (queue->get_pending ? 1 : 0);
282282
}
283283

284284
static void lazy_queue_clear(struct lazy_queue *queue)
@@ -292,9 +292,14 @@ static unsigned long finish_depth_computation(struct lazy_queue *queue,
292292
{
293293
unsigned long seen_commits = 0;
294294
struct oidset unflagged = OIDSET_INIT;
295+
struct commit *commit;
296+
int skip = queue->get_pending ? 1 : 0;
295297

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+
}
298303
if (!(commit->object.flags & best->flag_within))
299304
oidset_insert(&unflagged, &commit->object.oid);
300305
}

builtin/last-modified.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ static void process_parent(struct last_modified *lm,
344344
static int last_modified_run(struct last_modified *lm)
345345
{
346346
int max_count, queue_popped = 0;
347+
struct commit *c, *n;
347348
struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
348349
struct prio_queue not_queue = { compare_commits_by_gen_then_commit_date };
349350
struct commit_list *list;
@@ -389,10 +390,9 @@ static int last_modified_run(struct last_modified *lm)
389390
}
390391
}
391392

392-
while (queue.nr) {
393+
while ((c = prio_queue_get(&queue))) {
393394
int parent_i;
394395
struct commit_list *p;
395-
struct commit *c = prio_queue_get(&queue);
396396
struct bitmap *active_c = active_paths_for(lm, c);
397397

398398
if ((0 <= max_count && max_count < ++queue_popped) ||
@@ -416,9 +416,8 @@ static int last_modified_run(struct last_modified *lm)
416416
*/
417417
repo_parse_commit(lm->rev.repo, c);
418418

419-
while (not_queue.nr) {
419+
while ((n = prio_queue_get(&not_queue))) {
420420
struct commit_list *np;
421-
struct commit *n = prio_queue_get(&not_queue);
422421

423422
repo_parse_commit(lm->rev.repo, n);
424423

builtin/show-branch.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ static const char *get_color_reset_code(void)
6262

6363
static struct commit *interesting(struct prio_queue *queue)
6464
{
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;
7069
}
7170
return NULL;
7271
}
@@ -228,11 +227,11 @@ static void join_revs(struct prio_queue *queue,
228227
{
229228
int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
230229
int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
230+
struct commit *commit;
231231

232-
while (queue->nr) {
232+
while ((commit = prio_queue_peek(queue))) {
233233
struct commit_list *parents;
234234
int still_interesting = !!interesting(queue);
235-
struct commit *commit = prio_queue_peek(queue);
236235
bool get_pending = true;
237236
int flags = commit->object.flags & all_mask;
238237

commit-reach.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,7 @@ void ahead_behind(struct repository *r,
11211121
struct nonstale_queue queue = {
11221122
{ .compare = compare_commits_by_gen_then_commit_date }
11231123
};
1124+
void *entry;
11241125
size_t width = DIV_ROUND_UP(commits_nr, BITS_IN_EWORD);
11251126

11261127
if (!commits_nr || !counts_nr)
@@ -1186,8 +1187,8 @@ void ahead_behind(struct repository *r,
11861187

11871188
/* STALE is used here, PARENT2 is used by insert_no_dup(). */
11881189
repo_clear_commit_marks(r, PARENT2 | STALE);
1189-
for (size_t i = 0; i < queue.pq.nr; i++)
1190-
free_bit_array(queue.pq.array[i].data);
1190+
prio_queue_for_each(&queue.pq, entry)
1191+
free_bit_array(entry);
11911192
clear_bit_arrays(&bit_arrays);
11921193
clear_nonstale_queue(&queue);
11931194
}
@@ -1320,7 +1321,7 @@ int get_branch_base_for_tip(struct repository *r,
13201321
size_t bases_nr)
13211322
{
13221323
int best_index = -1;
1323-
struct commit *branch_point = NULL;
1324+
struct commit *c, *branch_point = NULL;
13241325
struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
13251326
int found_missing_gen = 0;
13261327

@@ -1373,8 +1374,7 @@ int get_branch_base_for_tip(struct repository *r,
13731374
prio_queue_put(&queue, c);
13741375
}
13751376

1376-
while (queue.nr) {
1377-
struct commit *c = prio_queue_get(&queue);
1377+
while ((c = prio_queue_get(&queue))) {
13781378
int best_for_c = get_best(c);
13791379
int best_for_p, positive;
13801380
struct commit *parent;

fetch-pack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ static int mark_complete_oid(const struct reference *ref, void *cb_data UNUSED)
662662
static void mark_recent_complete_commits(struct fetch_pack_args *args,
663663
timestamp_t cutoff)
664664
{
665-
while (complete.nr) {
666-
struct commit *item = prio_queue_peek(&complete);
665+
struct commit *item;
666+
while ((item = prio_queue_peek(&complete))) {
667667
if (item->date < cutoff)
668668
break;
669669
print_verbose(args, _("Marking %s as complete"),

negotiator/default.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ static const struct object_id *get_rev(struct negotiation_state *ns)
113113
unsigned int mark;
114114
struct commit_list *parents;
115115

116-
if (ns->rev_list.nr == 0 || ns->non_common_revs == 0)
116+
if (ns->non_common_revs == 0)
117117
return NULL;
118118

119119
commit = prio_queue_get(&ns->rev_list);
120+
if (!commit)
121+
return NULL;
120122
repo_parse_commit(the_repository, commit);
121123
parents = commit->parents;
122124

negotiator/skipping.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ static int push_parent(struct data *data, struct entry *entry,
143143
/*
144144
* Find the existing entry and use it.
145145
*/
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) {
148147
if (parent_entry->commit == to_push)
149148
goto parent_found;
150149
}
@@ -181,10 +180,12 @@ static const struct object_id *get_rev(struct data *data)
181180
struct commit_list *p;
182181
int parent_pushed = 0;
183182

184-
if (data->rev_list.nr == 0 || data->non_common_revs == 0)
183+
if (data->non_common_revs == 0)
185184
return NULL;
186185

187186
entry = prio_queue_get(&data->rev_list);
187+
if (!entry)
188+
return NULL;
188189
commit = entry->commit;
189190
commit->object.flags |= POPPED;
190191
if (!(commit->object.flags & COMMON))
@@ -253,8 +254,9 @@ static void have_sent(struct fetch_negotiator *n, struct commit *c)
253254
static void release(struct fetch_negotiator *n)
254255
{
255256
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);
258260
clear_prio_queue(&data->rev_list);
259261
FREE_AND_NULL(data);
260262
}

object-name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ static int get_oid_oneline(struct repository *r,
12081208
l->item->object.flags |= ONELINE_SEEN;
12091209
prio_queue_put(&copy, l->item);
12101210
}
1211-
while (copy.nr) {
1211+
while (prio_queue_size(&copy)) {
12121212
const char *p, *buf;
12131213
struct commit *commit;
12141214
int matches;

pack-bitmap-write.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -513,16 +513,17 @@ static int fill_bitmap_commit(struct bitmap_writer *writer,
513513
struct bitmap_index *old_bitmap,
514514
const uint32_t *mapping)
515515
{
516+
struct commit *c;
517+
struct tree *tree;
516518
int found;
517519
uint32_t pos;
518520
if (!ent->bitmap)
519521
ent->bitmap = bitmap_new();
520522

521523
prio_queue_put(queue, commit);
522524

523-
while (queue->nr) {
525+
while ((c = prio_queue_get(queue))) {
524526
struct commit_list *p;
525-
struct commit *c = prio_queue_get(queue);
526527

527528
if (old_bitmap && mapping) {
528529
struct ewah_bitmap *old;
@@ -574,9 +575,8 @@ static int fill_bitmap_commit(struct bitmap_writer *writer,
574575
}
575576
}
576577

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)
580580
return -1;
581581
}
582582
return 0;

path-walk.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ int walk_objects_by_path(struct path_walk_info *info)
699699
int ret;
700700
size_t commits_nr = 0, paths_nr = 0;
701701
struct commit *c;
702+
char *path;
702703
struct type_and_oid_list *root_tree_list;
703704
struct type_and_oid_list *commit_list;
704705
struct path_walk_context ctx = {
@@ -808,8 +809,7 @@ int walk_objects_by_path(struct path_walk_info *info)
808809
free(commit_list);
809810

810811
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))) {
813813
paths_nr++;
814814

815815
ret = walk_path(&ctx, path);
@@ -821,12 +821,12 @@ int walk_objects_by_path(struct path_walk_info *info)
821821
if (!strmap_empty(&ctx.paths_to_lists)) {
822822
struct hashmap_iter iter;
823823
struct strmap_entry *entry;
824+
char *path;
824825

825826
strmap_for_each_entry(&ctx.paths_to_lists, &iter, entry)
826827
push_to_stack(&ctx, entry->key);
827828

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))) {
830830
paths_nr++;
831831

832832
ret = walk_path(&ctx, path);

0 commit comments

Comments
 (0)