Skip to content

Commit ca56dad

Browse files
rscharfegitster
authored andcommitted
use CALLOC_ARRAY
Add and apply a semantic patch for converting code that open-codes CALLOC_ARRAY to use it instead. It shortens the code and infers the element size automatically. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f112149 commit ca56dad

Some content is hidden

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

88 files changed

+190
-188
lines changed

Diff for: add-interactive.c

+1-1
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;

Diff for: apply.c

+3-3
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);

Diff for: archive-tar.c

+1-1
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 |

Diff for: attr.c

+6-6
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

Diff for: bisect.c

+1-1
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);

Diff for: blame.c

+8-9
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

Diff for: bloom.c

+1-1
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;

Diff for: builtin/clean.c

+1-1
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;

Diff for: builtin/fast-import.c

+4-4
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);

Diff for: builtin/grep.c

+1-1
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);

Diff for: builtin/index-pack.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static void init_thread(void)
185185
if (show_stat)
186186
pthread_mutex_init(&deepest_delta_mutex, NULL);
187187
pthread_key_create(&key, NULL);
188-
thread_data = xcalloc(nr_threads, sizeof(*thread_data));
188+
CALLOC_ARRAY(thread_data, nr_threads);
189189
for (i = 0; i < nr_threads; i++) {
190190
thread_data[i].pack_fd = open(curr_pack, O_RDONLY);
191191
if (thread_data[i].pack_fd == -1)
@@ -1674,7 +1674,7 @@ static void show_pack_info(int stat_only)
16741674
unsigned long *chain_histogram = NULL;
16751675

16761676
if (deepest_delta)
1677-
chain_histogram = xcalloc(deepest_delta, sizeof(unsigned long));
1677+
CALLOC_ARRAY(chain_histogram, deepest_delta);
16781678

16791679
for (i = 0; i < nr_objects; i++) {
16801680
struct object_entry *obj = &objects[i];
@@ -1912,10 +1912,10 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
19121912

19131913
curr_pack = open_pack_file(pack_name);
19141914
parse_pack_header();
1915-
objects = xcalloc(st_add(nr_objects, 1), sizeof(struct object_entry));
1915+
CALLOC_ARRAY(objects, st_add(nr_objects, 1));
19161916
if (show_stat)
1917-
obj_stat = xcalloc(st_add(nr_objects, 1), sizeof(struct object_stat));
1918-
ofs_deltas = xcalloc(nr_objects, sizeof(struct ofs_delta_entry));
1917+
CALLOC_ARRAY(obj_stat, st_add(nr_objects, 1));
1918+
CALLOC_ARRAY(ofs_deltas, nr_objects);
19191919
parse_pack_objects(pack_hash);
19201920
if (report_end_of_input)
19211921
write_in_full(2, "\0", 1);

Diff for: builtin/ls-remote.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
8888

8989
if (argc > 1) {
9090
int i;
91-
pattern = xcalloc(argc, sizeof(const char *));
91+
CALLOC_ARRAY(pattern, argc);
9292
for (i = 1; i < argc; i++) {
9393
pattern[i - 1] = xstrfmt("*/%s", argv[i]);
9494
}

Diff for: builtin/merge.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static struct strategy *get_strategy(const char *name)
210210
exit(1);
211211
}
212212

213-
ret = xcalloc(1, sizeof(struct strategy));
213+
CALLOC_ARRAY(ret, 1);
214214
ret->name = xstrdup(name);
215215
ret->attr = NO_TRIVIAL;
216216
return ret;

Diff for: builtin/notes.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ static int merge_commit(struct notes_merge_options *o)
730730
else
731731
oidclr(&parent_oid);
732732

733-
t = xcalloc(1, sizeof(struct notes_tree));
733+
CALLOC_ARRAY(t, 1);
734734
init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
735735

736736
o->local_ref = local_ref_to_free =

Diff for: builtin/pack-objects.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ static void add_preferred_base(struct object_id *oid)
16351635
}
16361636
}
16371637

1638-
it = xcalloc(1, sizeof(*it));
1638+
CALLOC_ARRAY(it, 1);
16391639
it->next = pbase_tree;
16401640
pbase_tree = it;
16411641

@@ -2096,7 +2096,7 @@ static void get_object_details(void)
20962096
progress_state = start_progress(_("Counting objects"),
20972097
to_pack.nr_objects);
20982098

2099-
sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));
2099+
CALLOC_ARRAY(sorted_by_offset, to_pack.nr_objects);
21002100
for (i = 0; i < to_pack.nr_objects; i++)
21012101
sorted_by_offset[i] = to_pack.objects + i;
21022102
QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
@@ -2428,7 +2428,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
24282428
struct unpacked *array;
24292429
unsigned long mem_usage = 0;
24302430

2431-
array = xcalloc(window, sizeof(struct unpacked));
2431+
CALLOC_ARRAY(array, window);
24322432

24332433
for (;;) {
24342434
struct object_entry *entry;
@@ -2665,7 +2665,7 @@ static void ll_find_deltas(struct object_entry **list, unsigned list_size,
26652665
if (progress > pack_to_stdout)
26662666
fprintf_ln(stderr, _("Delta compression using up to %d threads"),
26672667
delta_search_threads);
2668-
p = xcalloc(delta_search_threads, sizeof(*p));
2668+
CALLOC_ARRAY(p, delta_search_threads);
26692669

26702670
/* Partition the work amongst work threads. */
26712671
for (i = 0; i < delta_search_threads; i++) {

Diff for: builtin/pack-redundant.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ static void sort_pack_list(struct pack_list **pl)
373373
return;
374374

375375
/* prepare an array of packed_list for easier sorting */
376-
ary = xcalloc(n, sizeof(struct pack_list *));
376+
CALLOC_ARRAY(ary, n);
377377
for (n = 0, p = *pl; p; p = p->next)
378378
ary[n++] = p;
379379

Diff for: builtin/receive-pack.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ static void proc_receive_ref_append(const char *prefix)
358358
char *p;
359359
int len;
360360

361-
ref_pattern = xcalloc(1, sizeof(struct proc_receive_ref));
361+
CALLOC_ARRAY(ref_pattern, 1);
362362
p = strchr(prefix, ':');
363363
if (p) {
364364
while (prefix < p) {
@@ -1024,7 +1024,7 @@ static int read_proc_receive_report(struct packet_reader *reader,
10241024
}
10251025
if (new_report) {
10261026
if (!hint->report) {
1027-
hint->report = xcalloc(1, sizeof(struct ref_push_report));
1027+
CALLOC_ARRAY(hint->report, 1);
10281028
report = hint->report;
10291029
} else {
10301030
report = hint->report;

Diff for: builtin/unpack-objects.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static struct obj_buffer *lookup_object_buffer(struct object *base)
4646
static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
4747
{
4848
struct obj_buffer *obj;
49-
obj = xcalloc(1, sizeof(struct obj_buffer));
49+
CALLOC_ARRAY(obj, 1);
5050
obj->buffer = buffer;
5151
obj->size = size;
5252
if (add_decoration(&obj_decorate, object, obj))
@@ -500,7 +500,7 @@ static void unpack_all(void)
500500

501501
if (!quiet)
502502
progress = start_progress(_("Unpacking objects"), nr_objects);
503-
obj_list = xcalloc(nr_objects, sizeof(*obj_list));
503+
CALLOC_ARRAY(obj_list, nr_objects);
504504
for (i = 0; i < nr_objects; i++) {
505505
unpack_one(i);
506506
display_progress(progress, i + 1);

Diff for: bulk-checkin.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static int deflate_to_pack(struct bulk_checkin_state *state,
211211

212212
/* Note: idx is non-NULL when we are writing */
213213
if ((flags & HASH_WRITE_OBJECT) != 0)
214-
idx = xcalloc(1, sizeof(*idx));
214+
CALLOC_ARRAY(idx, 1);
215215

216216
already_hashed_to = 0;
217217

Diff for: cache-tree.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
564564
* hence +2.
565565
*/
566566
it->subtree_alloc = subtree_nr + 2;
567-
it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
567+
CALLOC_ARRAY(it->down, it->subtree_alloc);
568568
for (i = 0; i < subtree_nr; i++) {
569569
/* read each subtree */
570570
struct cache_tree *sub;

0 commit comments

Comments
 (0)