Skip to content

Commit 92ccd7b

Browse files
committed
Merge branch 'rs/calloc-array'
CALLOC_ARRAY() macro replaces many uses of xcalloc(). * rs/calloc-array: cocci: allow xcalloc(1, size) use CALLOC_ARRAY git-compat-util.h: drop trailing semicolon from macro definition
2 parents a8a0ac3 + 1c57cc7 commit 92ccd7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+191
-189
lines changed

add-interactive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ static void collect_changes_cb(struct diff_queue_struct *q,
476476

477477
add_file_item(s->files, name);
478478

479-
entry = xcalloc(sizeof(*entry), 1);
479+
CALLOC_ARRAY(entry, 1);
480480
hashmap_entry_init(&entry->ent, hash);
481481
entry->name = s->files->items[s->files->nr - 1].string;
482482
entry->item = s->files->items[s->files->nr - 1].util;

apply.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,7 @@ static int parse_single_patch(struct apply_state *state,
17811781
struct fragment *fragment;
17821782
int len;
17831783

1784-
fragment = xcalloc(1, sizeof(*fragment));
1784+
CALLOC_ARRAY(fragment, 1);
17851785
fragment->linenr = state->linenr;
17861786
len = parse_fragment(state, line, size, patch, fragment);
17871787
if (len <= 0) {
@@ -1959,7 +1959,7 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
19591959
size -= llen;
19601960
}
19611961

1962-
frag = xcalloc(1, sizeof(*frag));
1962+
CALLOC_ARRAY(frag, 1);
19631963
frag->patch = inflate_it(data, hunk_size, origlen);
19641964
frag->free_patch = 1;
19651965
if (!frag->patch)
@@ -4681,7 +4681,7 @@ static int apply_patch(struct apply_state *state,
46814681
struct patch *patch;
46824682
int nr;
46834683

4684-
patch = xcalloc(1, sizeof(*patch));
4684+
CALLOC_ARRAY(patch, 1);
46854685
patch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF);
46864686
patch->recount = !!(options & APPLY_OPT_RECOUNT);
46874687
nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);

archive-tar.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static int tar_filter_config(const char *var, const char *value, void *data)
371371

372372
ar = find_tar_filter(name, namelen);
373373
if (!ar) {
374-
ar = xcalloc(1, sizeof(*ar));
374+
CALLOC_ARRAY(ar, 1);
375375
ar->name = xmemdupz(name, namelen);
376376
ar->write_archive = write_tar_filter_archive;
377377
ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |

attr.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ struct attr_check *attr_check_initl(const char *one, ...)
569569
check = attr_check_alloc();
570570
check->nr = cnt;
571571
check->alloc = cnt;
572-
check->items = xcalloc(cnt, sizeof(struct attr_check_item));
572+
CALLOC_ARRAY(check->items, cnt);
573573

574574
check->items[0].attr = git_attr(one);
575575
va_start(params, one);
@@ -670,7 +670,7 @@ static struct attr_stack *read_attr_from_array(const char **list)
670670
const char *line;
671671
int lineno = 0;
672672

673-
res = xcalloc(1, sizeof(*res));
673+
CALLOC_ARRAY(res, 1);
674674
while ((line = *(list++)) != NULL)
675675
handle_attr_line(res, line, "[builtin]", ++lineno, 1);
676676
return res;
@@ -707,7 +707,7 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
707707

708708
if (!fp)
709709
return NULL;
710-
res = xcalloc(1, sizeof(*res));
710+
CALLOC_ARRAY(res, 1);
711711
while (fgets(buf, sizeof(buf), fp)) {
712712
char *bufp = buf;
713713
if (!lineno)
@@ -733,7 +733,7 @@ static struct attr_stack *read_attr_from_index(const struct index_state *istate,
733733
if (!buf)
734734
return NULL;
735735

736-
res = xcalloc(1, sizeof(*res));
736+
CALLOC_ARRAY(res, 1);
737737
for (sp = buf; *sp; ) {
738738
char *ep;
739739
int more;
@@ -774,7 +774,7 @@ static struct attr_stack *read_attr(const struct index_state *istate,
774774
}
775775

776776
if (!res)
777-
res = xcalloc(1, sizeof(*res));
777+
CALLOC_ARRAY(res, 1);
778778
return res;
779779
}
780780

@@ -874,7 +874,7 @@ static void bootstrap_attr_stack(const struct index_state *istate,
874874
else
875875
e = NULL;
876876
if (!e)
877-
e = xcalloc(1, sizeof(struct attr_stack));
877+
CALLOC_ARRAY(e, 1);
878878
push_stack(stack, e, NULL, 0);
879879
}
880880

bisect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ void find_bisection(struct commit_list **commit_list, int *reaches,
423423
show_list("bisection 2 sorted", 0, nr, list);
424424

425425
*all = nr;
426-
weights = xcalloc(on_list, sizeof(*weights));
426+
CALLOC_ARRAY(weights, on_list);
427427

428428
/* Do the real work of finding bisection commit. */
429429
best = do_find_bisection(list, nr, weights, bisect_flags);

blame.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -951,13 +951,13 @@ static int *fuzzy_find_matching_lines(struct blame_origin *parent,
951951
max_search_distance_b = ((2 * max_search_distance_a + 1) * length_b
952952
- 1) / length_a;
953953

954-
result = xcalloc(sizeof(int), length_b);
955-
second_best_result = xcalloc(sizeof(int), length_b);
956-
certainties = xcalloc(sizeof(int), length_b);
954+
CALLOC_ARRAY(result, length_b);
955+
CALLOC_ARRAY(second_best_result, length_b);
956+
CALLOC_ARRAY(certainties, length_b);
957957

958958
/* See get_similarity() for details of similarities. */
959959
similarity_count = length_b * (max_search_distance_a * 2 + 1);
960-
similarities = xcalloc(sizeof(int), similarity_count);
960+
CALLOC_ARRAY(similarities, similarity_count);
961961

962962
for (i = 0; i < length_b; ++i) {
963963
result[i] = -1;
@@ -995,7 +995,7 @@ static void fill_origin_fingerprints(struct blame_origin *o)
995995
return;
996996
o->num_lines = find_line_starts(&line_starts, o->file.ptr,
997997
o->file.size);
998-
o->fingerprints = xcalloc(sizeof(struct fingerprint), o->num_lines);
998+
CALLOC_ARRAY(o->fingerprints, o->num_lines);
999999
get_line_fingerprints(o->fingerprints, o->file.ptr, line_starts,
10001000
0, o->num_lines);
10011001
free(line_starts);
@@ -1853,8 +1853,7 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
18531853
diffp = NULL;
18541854

18551855
if (ignore_diffs && same - tlno > 0) {
1856-
line_blames = xcalloc(sizeof(struct blame_line_tracker),
1857-
same - tlno);
1856+
CALLOC_ARRAY(line_blames, same - tlno);
18581857
guess_line_blames(parent, target, tlno, offset, same,
18591858
parent_len, line_blames);
18601859
}
@@ -2216,7 +2215,7 @@ static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
22162215
for (e = unblamed, num_ents = 0; e; e = e->next)
22172216
num_ents++;
22182217
if (num_ents) {
2219-
blame_list = xcalloc(num_ents, sizeof(struct blame_list));
2218+
CALLOC_ARRAY(blame_list, num_ents);
22202219
for (e = unblamed, i = 0; e; e = e->next)
22212220
blame_list[i++].ent = e;
22222221
}
@@ -2428,7 +2427,7 @@ static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin,
24282427
else if (num_sg < ARRAY_SIZE(sg_buf))
24292428
memset(sg_buf, 0, sizeof(sg_buf));
24302429
else
2431-
sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
2430+
CALLOC_ARRAY(sg_origin, num_sg);
24322431

24332432
/*
24342433
* The first pass looks for unrenamed path to optimize for

bloom.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
277277
*computed |= BLOOM_TRUNC_EMPTY;
278278
filter->len = 1;
279279
}
280-
filter->data = xcalloc(filter->len, sizeof(unsigned char));
280+
CALLOC_ARRAY(filter->data, filter->len);
281281

282282
hashmap_for_each_entry(&pathmap, &iter, e, entry) {
283283
struct bloom_key key;

builtin/clean.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
623623
nr += chosen[i];
624624
}
625625

626-
result = xcalloc(st_add(nr, 1), sizeof(int));
626+
CALLOC_ARRAY(result, st_add(nr, 1));
627627
for (i = 0; i < stuff->nr && j < nr; i++) {
628628
if (chosen[i])
629629
result[j++] = i;

builtin/fast-import.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3322,7 +3322,7 @@ static void option_rewrite_submodules(const char *arg, struct string_list *list)
33223322
die(_("Expected format name:filename for submodule rewrite option"));
33233323
*f = '\0';
33243324
f++;
3325-
ms = xcalloc(1, sizeof(*ms));
3325+
CALLOC_ARRAY(ms, 1);
33263326

33273327
fp = fopen(f, "r");
33283328
if (!fp)
@@ -3519,9 +3519,9 @@ int cmd_fast_import(int argc, const char **argv, const char *prefix)
35193519

35203520
alloc_objects(object_entry_alloc);
35213521
strbuf_init(&command_buf, 0);
3522-
atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
3523-
branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
3524-
avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
3522+
CALLOC_ARRAY(atom_table, atom_table_sz);
3523+
CALLOC_ARRAY(branch_table, branch_table_sz);
3524+
CALLOC_ARRAY(avail_tree_table, avail_tree_table_sz);
35253525
marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
35263526

35273527
hashmap_init(&object_table, object_entry_hashcmp, NULL, 0);

builtin/grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static void start_threads(struct grep_opt *opt)
211211
strbuf_init(&todo[i].out, 0);
212212
}
213213

214-
threads = xcalloc(num_threads, sizeof(*threads));
214+
CALLOC_ARRAY(threads, num_threads);
215215
for (i = 0; i < num_threads; i++) {
216216
int err;
217217
struct grep_opt *o = grep_opt_dup(opt);

0 commit comments

Comments
 (0)