Browse Source

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 <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
pull/985/head
René Scharfe 1 year ago committed by Junio C Hamano
parent
commit
ca56dadb4b
  1. 2
      add-interactive.c
  2. 6
      apply.c
  3. 2
      archive-tar.c
  4. 12
      attr.c
  5. 2
      bisect.c
  6. 17
      blame.c
  7. 2
      bloom.c
  8. 2
      builtin/clean.c
  9. 8
      builtin/fast-import.c
  10. 2
      builtin/grep.c
  11. 10
      builtin/index-pack.c
  12. 2
      builtin/ls-remote.c
  13. 2
      builtin/merge.c
  14. 2
      builtin/notes.c
  15. 8
      builtin/pack-objects.c
  16. 2
      builtin/pack-redundant.c
  17. 4
      builtin/receive-pack.c
  18. 4
      builtin/unpack-objects.c
  19. 2
      bulk-checkin.c
  20. 2
      cache-tree.c
  21. 20
      combine-diff.c
  22. 4
      commit-graph.c
  23. 6
      commit-reach.c
  24. 4
      commit.c
  25. 2
      config.c
  26. 8
      contrib/coccinelle/array.cocci
  27. 2
      convert.c
  28. 4
      daemon.c
  29. 2
      decorate.c
  30. 10
      diff.c
  31. 3
      diffcore-rename.c
  32. 6
      dir.c
  33. 2
      ewah/bitmap.c
  34. 2
      fetch-pack.c
  35. 2
      fmt-merge-msg.c
  36. 8
      grep.c
  37. 2
      hashmap.c
  38. 2
      http-backend.c
  39. 4
      http-push.c
  40. 4
      http.c
  41. 4
      imap-send.c
  42. 2
      line-log.c
  43. 2
      line-range.c
  44. 6
      list-objects-filter.c
  45. 2
      ll-merge.c
  46. 2
      mailmap.c
  47. 9
      merge-ort.c
  48. 9
      merge-recursive.c
  49. 8
      midx.c
  50. 8
      name-hash.c
  51. 2
      negotiator/default.c
  52. 4
      negotiator/skipping.c
  53. 2
      notes-merge.c
  54. 2
      notes-utils.c
  55. 2
      notes.c
  56. 2
      object-file.c
  57. 4
      object.c
  58. 4
      pack-bitmap.c
  59. 2
      pack-objects.c
  60. 2
      packfile.c
  61. 2
      patch-ids.c
  62. 4
      pathspec.c
  63. 2
      pretty.c
  64. 2
      progress.c
  65. 2
      promisor-remote.c
  66. 2
      range-diff.c
  67. 8
      read-cache.c
  68. 6
      ref-filter.c
  69. 4
      reflog-walk.c
  70. 4
      refs.c
  71. 10
      refs/files-backend.c
  72. 2
      refs/iterator.c
  73. 4
      refs/packed-backend.c
  74. 2
      refs/ref-cache.c
  75. 8
      remote.c
  76. 4
      repository.c
  77. 4
      resolve-undo.c
  78. 6
      revision.c
  79. 4
      run-command.c
  80. 4
      send-pack.c
  81. 2
      sequencer.c
  82. 2
      server-info.c
  83. 4
      split-index.c
  84. 2
      trailer.c
  85. 4
      transport-helper.c
  86. 2
      transport.c
  87. 4
      worktree.c
  88. 6
      wt-status.c

2
add-interactive.c

@ -476,7 +476,7 @@ static void collect_changes_cb(struct diff_queue_struct *q,
add_file_item(s->files, name);
entry = xcalloc(sizeof(*entry), 1);
CALLOC_ARRAY(entry, 1);
hashmap_entry_init(&entry->ent, hash);
entry->name = s->files->items[s->files->nr - 1].string;
entry->item = s->files->items[s->files->nr - 1].util;

6
apply.c

@ -1781,7 +1781,7 @@ static int parse_single_patch(struct apply_state *state,
struct fragment *fragment;
int len;
fragment = xcalloc(1, sizeof(*fragment));
CALLOC_ARRAY(fragment, 1);
fragment->linenr = state->linenr;
len = parse_fragment(state, line, size, patch, fragment);
if (len <= 0) {
@ -1959,7 +1959,7 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
size -= llen;
}
frag = xcalloc(1, sizeof(*frag));
CALLOC_ARRAY(frag, 1);
frag->patch = inflate_it(data, hunk_size, origlen);
frag->free_patch = 1;
if (!frag->patch)
@ -4681,7 +4681,7 @@ static int apply_patch(struct apply_state *state,
struct patch *patch;
int nr;
patch = xcalloc(1, sizeof(*patch));
CALLOC_ARRAY(patch, 1);
patch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF);
patch->recount = !!(options & APPLY_OPT_RECOUNT);
nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);

2
archive-tar.c

@ -371,7 +371,7 @@ static int tar_filter_config(const char *var, const char *value, void *data)
ar = find_tar_filter(name, namelen);
if (!ar) {
ar = xcalloc(1, sizeof(*ar));
CALLOC_ARRAY(ar, 1);
ar->name = xmemdupz(name, namelen);
ar->write_archive = write_tar_filter_archive;
ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |

12
attr.c

@ -569,7 +569,7 @@ struct attr_check *attr_check_initl(const char *one, ...)
check = attr_check_alloc();
check->nr = cnt;
check->alloc = cnt;
check->items = xcalloc(cnt, sizeof(struct attr_check_item));
CALLOC_ARRAY(check->items, cnt);
check->items[0].attr = git_attr(one);
va_start(params, one);
@ -670,7 +670,7 @@ static struct attr_stack *read_attr_from_array(const char **list)
const char *line;
int lineno = 0;
res = xcalloc(1, sizeof(*res));
CALLOC_ARRAY(res, 1);
while ((line = *(list++)) != NULL)
handle_attr_line(res, line, "[builtin]", ++lineno, 1);
return res;
@ -707,7 +707,7 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
if (!fp)
return NULL;
res = xcalloc(1, sizeof(*res));
CALLOC_ARRAY(res, 1);
while (fgets(buf, sizeof(buf), fp)) {
char *bufp = buf;
if (!lineno)
@ -733,7 +733,7 @@ static struct attr_stack *read_attr_from_index(const struct index_state *istate,
if (!buf)
return NULL;
res = xcalloc(1, sizeof(*res));
CALLOC_ARRAY(res, 1);
for (sp = buf; *sp; ) {
char *ep;
int more;
@ -774,7 +774,7 @@ static struct attr_stack *read_attr(const struct index_state *istate,
}
if (!res)
res = xcalloc(1, sizeof(*res));
CALLOC_ARRAY(res, 1);
return res;
}
@ -874,7 +874,7 @@ static void bootstrap_attr_stack(const struct index_state *istate,
else
e = NULL;
if (!e)
e = xcalloc(1, sizeof(struct attr_stack));
CALLOC_ARRAY(e, 1);
push_stack(stack, e, NULL, 0);
}

2
bisect.c

@ -423,7 +423,7 @@ void find_bisection(struct commit_list **commit_list, int *reaches,
show_list("bisection 2 sorted", 0, nr, list);
*all = nr;
weights = xcalloc(on_list, sizeof(*weights));
CALLOC_ARRAY(weights, on_list);
/* Do the real work of finding bisection commit. */
best = do_find_bisection(list, nr, weights, bisect_flags);

17
blame.c

@ -951,13 +951,13 @@ static int *fuzzy_find_matching_lines(struct blame_origin *parent,
max_search_distance_b = ((2 * max_search_distance_a + 1) * length_b
- 1) / length_a;
result = xcalloc(sizeof(int), length_b);
second_best_result = xcalloc(sizeof(int), length_b);
certainties = xcalloc(sizeof(int), length_b);
CALLOC_ARRAY(result, length_b);
CALLOC_ARRAY(second_best_result, length_b);
CALLOC_ARRAY(certainties, length_b);
/* See get_similarity() for details of similarities. */
similarity_count = length_b * (max_search_distance_a * 2 + 1);
similarities = xcalloc(sizeof(int), similarity_count);
CALLOC_ARRAY(similarities, similarity_count);
for (i = 0; i < length_b; ++i) {
result[i] = -1;
@ -995,7 +995,7 @@ static void fill_origin_fingerprints(struct blame_origin *o)
return;
o->num_lines = find_line_starts(&line_starts, o->file.ptr,
o->file.size);
o->fingerprints = xcalloc(sizeof(struct fingerprint), o->num_lines);
CALLOC_ARRAY(o->fingerprints, o->num_lines);
get_line_fingerprints(o->fingerprints, o->file.ptr, line_starts,
0, o->num_lines);
free(line_starts);
@ -1853,8 +1853,7 @@ static void blame_chunk(struct blame_entry ***dstq, struct blame_entry ***srcq,
diffp = NULL;
if (ignore_diffs && same - tlno > 0) {
line_blames = xcalloc(sizeof(struct blame_line_tracker),
same - tlno);
CALLOC_ARRAY(line_blames, same - tlno);
guess_line_blames(parent, target, tlno, offset, same,
parent_len, line_blames);
}
@ -2216,7 +2215,7 @@ static struct blame_list *setup_blame_list(struct blame_entry *unblamed,
for (e = unblamed, num_ents = 0; e; e = e->next)
num_ents++;
if (num_ents) {
blame_list = xcalloc(num_ents, sizeof(struct blame_list));
CALLOC_ARRAY(blame_list, num_ents);
for (e = unblamed, i = 0; e; e = e->next)
blame_list[i++].ent = e;
}
@ -2428,7 +2427,7 @@ static void pass_blame(struct blame_scoreboard *sb, struct blame_origin *origin,
else if (num_sg < ARRAY_SIZE(sg_buf))
memset(sg_buf, 0, sizeof(sg_buf));
else
sg_origin = xcalloc(num_sg, sizeof(*sg_origin));
CALLOC_ARRAY(sg_origin, num_sg);
/*
* The first pass looks for unrenamed path to optimize for

2
bloom.c

@ -277,7 +277,7 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
*computed |= BLOOM_TRUNC_EMPTY;
filter->len = 1;
}
filter->data = xcalloc(filter->len, sizeof(unsigned char));
CALLOC_ARRAY(filter->data, filter->len);
hashmap_for_each_entry(&pathmap, &iter, e, entry) {
struct bloom_key key;

2
builtin/clean.c

@ -623,7 +623,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
nr += chosen[i];
}
result = xcalloc(st_add(nr, 1), sizeof(int));
CALLOC_ARRAY(result, st_add(nr, 1));
for (i = 0; i < stuff->nr && j < nr; i++) {
if (chosen[i])
result[j++] = i;

8
builtin/fast-import.c

@ -3322,7 +3322,7 @@ static void option_rewrite_submodules(const char *arg, struct string_list *list)
die(_("Expected format name:filename for submodule rewrite option"));
*f = '\0';
f++;
ms = xcalloc(1, sizeof(*ms));
CALLOC_ARRAY(ms, 1);
fp = fopen(f, "r");
if (!fp)
@ -3519,9 +3519,9 @@ int cmd_fast_import(int argc, const char **argv, const char *prefix)
alloc_objects(object_entry_alloc);
strbuf_init(&command_buf, 0);
atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
CALLOC_ARRAY(atom_table, atom_table_sz);
CALLOC_ARRAY(branch_table, branch_table_sz);
CALLOC_ARRAY(avail_tree_table, avail_tree_table_sz);
marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
hashmap_init(&object_table, object_entry_hashcmp, NULL, 0);

2
builtin/grep.c

@ -211,7 +211,7 @@ static void start_threads(struct grep_opt *opt)
strbuf_init(&todo[i].out, 0);
}
threads = xcalloc(num_threads, sizeof(*threads));
CALLOC_ARRAY(threads, num_threads);
for (i = 0; i < num_threads; i++) {
int err;
struct grep_opt *o = grep_opt_dup(opt);

10
builtin/index-pack.c

@ -185,7 +185,7 @@ static void init_thread(void)
if (show_stat)
pthread_mutex_init(&deepest_delta_mutex, NULL);
pthread_key_create(&key, NULL);
thread_data = xcalloc(nr_threads, sizeof(*thread_data));
CALLOC_ARRAY(thread_data, nr_threads);
for (i = 0; i < nr_threads; i++) {
thread_data[i].pack_fd = open(curr_pack, O_RDONLY);
if (thread_data[i].pack_fd == -1)
@ -1674,7 +1674,7 @@ static void show_pack_info(int stat_only)
unsigned long *chain_histogram = NULL;
if (deepest_delta)
chain_histogram = xcalloc(deepest_delta, sizeof(unsigned long));
CALLOC_ARRAY(chain_histogram, deepest_delta);
for (i = 0; i < nr_objects; i++) {
struct object_entry *obj = &objects[i];
@ -1912,10 +1912,10 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
curr_pack = open_pack_file(pack_name);
parse_pack_header();
objects = xcalloc(st_add(nr_objects, 1), sizeof(struct object_entry));
CALLOC_ARRAY(objects, st_add(nr_objects, 1));
if (show_stat)
obj_stat = xcalloc(st_add(nr_objects, 1), sizeof(struct object_stat));
ofs_deltas = xcalloc(nr_objects, sizeof(struct ofs_delta_entry));
CALLOC_ARRAY(obj_stat, st_add(nr_objects, 1));
CALLOC_ARRAY(ofs_deltas, nr_objects);
parse_pack_objects(pack_hash);
if (report_end_of_input)
write_in_full(2, "\0", 1);

2
builtin/ls-remote.c

@ -88,7 +88,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
if (argc > 1) {
int i;
pattern = xcalloc(argc, sizeof(const char *));
CALLOC_ARRAY(pattern, argc);
for (i = 1; i < argc; i++) {
pattern[i - 1] = xstrfmt("*/%s", argv[i]);
}

2
builtin/merge.c

@ -210,7 +210,7 @@ static struct strategy *get_strategy(const char *name)
exit(1);
}
ret = xcalloc(1, sizeof(struct strategy));
CALLOC_ARRAY(ret, 1);
ret->name = xstrdup(name);
ret->attr = NO_TRIVIAL;
return ret;

2
builtin/notes.c

@ -730,7 +730,7 @@ static int merge_commit(struct notes_merge_options *o)
else
oidclr(&parent_oid);
t = xcalloc(1, sizeof(struct notes_tree));
CALLOC_ARRAY(t, 1);
init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0);
o->local_ref = local_ref_to_free =

8
builtin/pack-objects.c

@ -1635,7 +1635,7 @@ static void add_preferred_base(struct object_id *oid)
}
}
it = xcalloc(1, sizeof(*it));
CALLOC_ARRAY(it, 1);
it->next = pbase_tree;
pbase_tree = it;
@ -2096,7 +2096,7 @@ static void get_object_details(void)
progress_state = start_progress(_("Counting objects"),
to_pack.nr_objects);
sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));
CALLOC_ARRAY(sorted_by_offset, to_pack.nr_objects);
for (i = 0; i < to_pack.nr_objects; i++)
sorted_by_offset[i] = to_pack.objects + i;
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,
struct unpacked *array;
unsigned long mem_usage = 0;
array = xcalloc(window, sizeof(struct unpacked));
CALLOC_ARRAY(array, window);
for (;;) {
struct object_entry *entry;
@ -2665,7 +2665,7 @@ static void ll_find_deltas(struct object_entry **list, unsigned list_size,
if (progress > pack_to_stdout)
fprintf_ln(stderr, _("Delta compression using up to %d threads"),
delta_search_threads);
p = xcalloc(delta_search_threads, sizeof(*p));
CALLOC_ARRAY(p, delta_search_threads);
/* Partition the work amongst work threads. */
for (i = 0; i < delta_search_threads; i++) {

2
builtin/pack-redundant.c

@ -373,7 +373,7 @@ static void sort_pack_list(struct pack_list **pl)
return;
/* prepare an array of packed_list for easier sorting */
ary = xcalloc(n, sizeof(struct pack_list *));
CALLOC_ARRAY(ary, n);
for (n = 0, p = *pl; p; p = p->next)
ary[n++] = p;

4
builtin/receive-pack.c

@ -358,7 +358,7 @@ static void proc_receive_ref_append(const char *prefix)
char *p;
int len;
ref_pattern = xcalloc(1, sizeof(struct proc_receive_ref));
CALLOC_ARRAY(ref_pattern, 1);
p = strchr(prefix, ':');
if (p) {
while (prefix < p) {
@ -1024,7 +1024,7 @@ static int read_proc_receive_report(struct packet_reader *reader,
}
if (new_report) {
if (!hint->report) {
hint->report = xcalloc(1, sizeof(struct ref_push_report));
CALLOC_ARRAY(hint->report, 1);
report = hint->report;
} else {
report = hint->report;

4
builtin/unpack-objects.c

@ -46,7 +46,7 @@ static struct obj_buffer *lookup_object_buffer(struct object *base)
static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
{
struct obj_buffer *obj;
obj = xcalloc(1, sizeof(struct obj_buffer));
CALLOC_ARRAY(obj, 1);
obj->buffer = buffer;
obj->size = size;
if (add_decoration(&obj_decorate, object, obj))
@ -500,7 +500,7 @@ static void unpack_all(void)
if (!quiet)
progress = start_progress(_("Unpacking objects"), nr_objects);
obj_list = xcalloc(nr_objects, sizeof(*obj_list));
CALLOC_ARRAY(obj_list, nr_objects);
for (i = 0; i < nr_objects; i++) {
unpack_one(i);
display_progress(progress, i + 1);

2
bulk-checkin.c

@ -211,7 +211,7 @@ static int deflate_to_pack(struct bulk_checkin_state *state,
/* Note: idx is non-NULL when we are writing */
if ((flags & HASH_WRITE_OBJECT) != 0)
idx = xcalloc(1, sizeof(*idx));
CALLOC_ARRAY(idx, 1);
already_hashed_to = 0;

2
cache-tree.c

@ -564,7 +564,7 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
* hence +2.
*/
it->subtree_alloc = subtree_nr + 2;
it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
CALLOC_ARRAY(it->down, it->subtree_alloc);
for (i = 0; i < subtree_nr; i++) {
/* read each subtree */
struct cache_tree *sub;

20
combine-diff.c

@ -214,11 +214,11 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase,
* - Else if we have NEW, insert newend lline into base and
* consume newend
*/
lcs = xcalloc(st_add(origbaselen, 1), sizeof(int*));
directions = xcalloc(st_add(origbaselen, 1), sizeof(enum coalesce_direction*));
CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
CALLOC_ARRAY(directions, st_add(origbaselen, 1));
for (i = 0; i < origbaselen + 1; i++) {
lcs[i] = xcalloc(st_add(lennew, 1), sizeof(int));
directions[i] = xcalloc(st_add(lennew, 1), sizeof(enum coalesce_direction));
CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
CALLOC_ARRAY(directions[i], st_add(lennew, 1));
directions[i][0] = BASE;
}
for (j = 1; j < lennew + 1; j++)
@ -398,8 +398,8 @@ static void consume_hunk(void *state_,
state->lost_bucket = &state->sline[state->nb-1];
}
if (!state->sline[state->nb-1].p_lno)
state->sline[state->nb-1].p_lno =
xcalloc(state->num_parent, sizeof(unsigned long));
CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
state->num_parent);
state->sline[state->nb-1].p_lno[state->n] = state->ob;
}
@ -1159,7 +1159,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
if (result_size && result[result_size-1] != '\n')
cnt++; /* incomplete line */
sline = xcalloc(st_add(cnt, 2), sizeof(*sline));
CALLOC_ARRAY(sline, st_add(cnt, 2));
sline[0].bol = result;
for (lno = 0, cp = result; cp < result + result_size; cp++) {
if (*cp == '\n') {
@ -1178,7 +1178,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
/* Even p_lno[cnt+1] is valid -- that is for the end line number
* for deletion hunk at the end.
*/
sline[0].p_lno = xcalloc(st_mult(st_add(cnt, 2), num_parent), sizeof(unsigned long));
CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
for (lno = 0; lno <= cnt; lno++)
sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
@ -1319,7 +1319,7 @@ static struct diff_filepair *combined_pair(struct combine_diff_path *p,
struct diff_filespec *pool;
pair = xmalloc(sizeof(*pair));
pool = xcalloc(st_add(num_parent, 1), sizeof(struct diff_filespec));
CALLOC_ARRAY(pool, st_add(num_parent, 1));
pair->one = pool + 1;
pair->two = pool;
@ -1348,7 +1348,7 @@ static void handle_combined_callback(struct diff_options *opt,
struct diff_queue_struct q;
int i;
q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
CALLOC_ARRAY(q.queue, num_paths);
q.alloc = num_paths;
q.nr = num_paths;
for (i = 0, p = paths; p; p = p->next)

4
commit-graph.c

@ -516,7 +516,7 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r,
return NULL;
count = st.st_size / (the_hash_algo->hexsz + 1);
oids = xcalloc(count, sizeof(struct object_id));
CALLOC_ARRAY(oids, count);
prepare_alt_odb(r);
@ -2238,7 +2238,7 @@ int write_commit_graph(struct object_directory *odb,
if (!commit_graph_compatible(the_repository))
return 0;
ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
CALLOC_ARRAY(ctx, 1);
ctx->r = the_repository;
ctx->odb = odb;
ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;

6
commit-reach.c

@ -183,7 +183,7 @@ static int remove_redundant_no_gen(struct repository *r,
int *filled_index;
int i, j, filled;
work = xcalloc(cnt, sizeof(*work));
CALLOC_ARRAY(work, cnt);
redundant = xcalloc(cnt, 1);
ALLOC_ARRAY(filled_index, cnt - 1);
@ -399,7 +399,7 @@ static struct commit_list *get_merge_bases_many_0(struct repository *r,
/* There are more than one */
cnt = commit_list_count(result);
rslt = xcalloc(cnt, sizeof(*rslt));
CALLOC_ARRAY(rslt, cnt);
for (list = result, i = 0; list; list = list->next)
rslt[i++] = list->item;
free_commit_list(result);
@ -541,7 +541,7 @@ struct commit_list *reduce_heads(struct commit_list *heads)
p->item->object.flags |= STALE;
num_head++;
}
array = xcalloc(num_head, sizeof(*array));
CALLOC_ARRAY(array, num_head);
for (p = heads, i = 0; p; p = p->next) {
if (p->item->object.flags & STALE) {
array[i++] = p->item;

4
commit.c

@ -1171,7 +1171,7 @@ static void handle_signed_tag(struct commit *parent, struct commit_extra_header
* if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
* warn("warning: signed tag unverified.");
*/
mergetag = xcalloc(1, sizeof(*mergetag));
CALLOC_ARRAY(mergetag, 1);
mergetag->key = xstrdup("mergetag");
mergetag->value = buf;
mergetag->len = size;
@ -1336,7 +1336,7 @@ static struct commit_extra_header *read_commit_extra_header_lines(
excluded_header_field(line, eof - line, exclude))
continue;
it = xcalloc(1, sizeof(*it));
CALLOC_ARRAY(it, 1);
it->key = xmemdupz(line, eof-line);
*tail = it;
tail = &it->next;

2
config.c

@ -2269,7 +2269,7 @@ static void repo_read_config(struct repository *repo)
opts.git_dir = repo->gitdir;
if (!repo->config)
repo->config = xcalloc(1, sizeof(struct config_set));
CALLOC_ARRAY(repo->config, 1);
else
git_configset_clear(repo->config);

8
contrib/coccinelle/array.cocci

@ -88,3 +88,11 @@ expression n;
@@
- ptr = xmalloc((n) * sizeof(T));
+ ALLOC_ARRAY(ptr, n);
@@
type T;
T *ptr;
expression n;
@@
- ptr = xcalloc(n, \( sizeof(*ptr) \| sizeof(T) \) )
+ CALLOC_ARRAY(ptr, n)

2
convert.c

@ -1028,7 +1028,7 @@ static int read_convert_config(const char *var, const char *value, void *cb)
if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
break;
if (!drv) {
drv = xcalloc(1, sizeof(struct convert_driver));
CALLOC_ARRAY(drv, 1);
drv->name = xmemdupz(name, namelen);
*user_convert_tail = drv;
user_convert_tail = &(drv->next);

4
daemon.c

@ -840,7 +840,7 @@ static void add_child(struct child_process *cld, struct sockaddr *addr, socklen_
{
struct child *newborn, **cradle;
newborn = xcalloc(1, sizeof(*newborn));
CALLOC_ARRAY(newborn, 1);
live_children++;
memcpy(&newborn->cld, cld, sizeof(*cld));
memcpy(&newborn->address, addr, addrlen);
@ -1148,7 +1148,7 @@ static int service_loop(struct socketlist *socklist)
struct pollfd *pfd;
int i;
pfd = xcalloc(socklist->nr, sizeof(struct pollfd));
CALLOC_ARRAY(pfd, socklist->nr);
for (i = 0; i < socklist->nr; i++) {
pfd[i].fd = socklist->list[i];

2
decorate.c

@ -39,7 +39,7 @@ static void grow_decoration(struct decoration *n)
struct decoration_entry *old_entries = n->entries;
n->size = (old_size + 1000) * 3 / 2;
n->entries = xcalloc(n->size, sizeof(struct decoration_entry));
CALLOC_ARRAY(n->entries, n->size);
n->nr = 0;
for (i = 0; i < old_size; i++) {

10
diff.c

@ -2233,14 +2233,12 @@ static void init_diff_words_data(struct emit_callback *ecbdata,
struct diff_options *o = xmalloc(sizeof(struct diff_options));
memcpy(o, orig_opts, sizeof(struct diff_options));
ecbdata->diff_words =
xcalloc(1, sizeof(struct diff_words_data));
CALLOC_ARRAY(ecbdata->diff_words, 1);
ecbdata->diff_words->type = o->word_diff;
ecbdata->diff_words->opt = o;
if (orig_opts->emitted_symbols)
o->emitted_symbols =
xcalloc(1, sizeof(struct emitted_diff_symbols));
CALLOC_ARRAY(o->emitted_symbols, 1);
if (!o->word_regex)
o->word_regex = userdiff_word_regex(one, o->repo->index);
@ -2509,7 +2507,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
const char *name_b)
{
struct diffstat_file *x;
x = xcalloc(1, sizeof(*x));
CALLOC_ARRAY(x, 1);
ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
diffstat->files[diffstat->nr++] = x;
if (name_b) {
@ -4918,7 +4916,7 @@ static int diff_opt_find_object(const struct option *option,
return error(_("unable to resolve '%s'"), arg);
if (!opt->objfind)
opt->objfind = xcalloc(1, sizeof(*opt->objfind));
CALLOC_ARRAY(opt->objfind, 1);
opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
opt->flags.recursive = 1;

3
diffcore-rename.c

@ -787,8 +787,7 @@ void diffcore_rename(struct diff_options *options)
(uint64_t)num_destinations * (uint64_t)num_sources);
}
mx = xcalloc(st_mult(NUM_CANDIDATE_PER_DST, num_destinations),
sizeof(*mx));
CALLOC_ARRAY(mx, st_mult(NUM_CANDIDATE_PER_DST, num_destinations));
for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
struct diff_filespec *two = rename_dst[i].p->two;
struct diff_score *m;

6
dir.c

@ -1488,7 +1488,7 @@ static void prep_exclude(struct dir_struct *dir,
const char *cp;
struct oid_stat oid_stat;
stk = xcalloc(1, sizeof(*stk));
CALLOC_ARRAY(stk, 1);
if (current < 0) {
cp = base;
current = 0;
@ -3162,7 +3162,7 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra
int varint_len;
const unsigned hashsz = the_hash_algo->rawsz;
ouc = xcalloc(1, sizeof(*ouc));
CALLOC_ARRAY(ouc, 1);
stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);
stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);
ouc->dir_flags = htonl(untracked->dir_flags);
@ -3373,7 +3373,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long
if (next + exclude_per_dir_offset + 1 > end)
return NULL;
uc = xcalloc(1, sizeof(*uc));
CALLOC_ARRAY(uc, 1);
strbuf_init(&uc->ident, ident_len);
strbuf_add(&uc->ident, ident, ident_len);
load_oid_stat(&uc->ss_info_exclude,

2
ewah/bitmap.c

@ -25,7 +25,7 @@
struct bitmap *bitmap_word_alloc(size_t word_alloc)
{
struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
bitmap->words = xcalloc(word_alloc, sizeof(eword_t));
CALLOC_ARRAY(bitmap->words, word_alloc);
bitmap->word_alloc = word_alloc;
return bitmap;
}

2
fetch-pack.c

@ -1916,7 +1916,7 @@ static void update_shallow(struct fetch_pack_args *args,
* remote is also shallow, check what ref is safe to update
* without updating .git/shallow
*/
status = xcalloc(nr_sought, sizeof(*status));
CALLOC_ARRAY(status, nr_sought);
assign_shallow_commits_to_refs(si, NULL, status);
if (si->nr_ours || si->nr_theirs) {
for (i = 0; i < nr_sought; i++)

2
fmt-merge-msg.c

@ -130,7 +130,7 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
if (!find_merge_parent(merge_parents, &oid, NULL))
return 0; /* subsumed by other parents */
origin_data = xcalloc(1, sizeof(struct origin_data));
CALLOC_ARRAY(origin_data, 1);
oidcpy(&origin_data->oid, &oid);
if (line[len - 1] == '\n')

8
grep.c

@ -621,7 +621,7 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
case GREP_PATTERN: /* atom */
case GREP_PATTERN_HEAD:
case GREP_PATTERN_BODY:
x = xcalloc(1, sizeof (struct grep_expr));
CALLOC_ARRAY(x, 1);
x->node = GREP_NODE_ATOM;
x->u.atom = p;
*list = p->next;
@ -651,7 +651,7 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
if (!p->next)
die("--not not followed by pattern expression");
*list = p->next;
x = xcalloc(1, sizeof (struct grep_expr));
CALLOC_ARRAY(x, 1);
x->node = GREP_NODE_NOT;
x->u.unary = compile_pattern_not(list);
if (!x->u.unary)
@ -676,7 +676,7 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list)
y = compile_pattern_and(list);
if (!y)
die("--and not followed by pattern expression");
z = xcalloc(1, sizeof (struct grep_expr));
CALLOC_ARRAY(z, 1);
z->node = GREP_NODE_AND;
z->u.binary.left = x;
z->u.binary.right = y;
@ -696,7 +696,7 @@ static struct grep_expr *compile_pattern_or(struct grep_pat **list)
y = compile_pattern_or(list);
if (!y)
die("not a pattern expression %s", p->pattern);
z = xcalloc(1, sizeof (struct grep_expr));
CALLOC_ARRAY(z, 1);
z->node = GREP_NODE_OR;
z->u.binary.left = x;
z->u.binary.right = y;

2
hashmap.c

@ -76,7 +76,7 @@ unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len)
static void alloc_table(struct hashmap *map, unsigned int size)
{
map->tablesize = size;
map->table = xcalloc(size, sizeof(struct hashmap_entry *));
CALLOC_ARRAY(map->table, size);
/* calculate resize thresholds for new size */
map->grow_at = (unsigned int) ((uint64_t) size * HASHMAP_LOAD_FACTOR / 100);

2
http-backend.c

@ -39,7 +39,7 @@ static struct string_list *get_parameters(void)
if (!query_params) {
const char *query = getenv("QUERY_STRING");
query_params = xcalloc(1, sizeof(*query_params));
CALLOC_ARRAY(query_params, 1);
while (query && *query) {
char *name = url_decode_parameter_name(&query);
char *value = url_decode_parameter_value(&query);

4
http-push.c

@ -896,7 +896,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
lock = xcalloc(1, sizeof(*lock));
CALLOC_ARRAY(lock, 1);
lock->timeout = -1;
if (start_active_slot(slot)) {
@ -1713,7 +1713,7 @@ int cmd_main(int argc, const char **argv)
int new_refs;
struct ref *ref, *local_refs;
repo = xcalloc(1, sizeof(*repo));
CALLOC_ARRAY(repo, 1);
argv++;
for (i = 1; i < argc; i++, argv++) {

4
http.c

@ -2324,7 +2324,7 @@ struct http_pack_request *new_direct_http_pack_request(
off_t prev_posn = 0;
struct http_pack_request *preq;
preq = xcalloc(1, sizeof(*preq));
CALLOC_ARRAY(preq, 1);
strbuf_init(&preq->tmpfile, 0);
preq->url = url;
@ -2419,7 +2419,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
off_t prev_posn = 0;
struct http_object_request *freq;
freq = xcalloc(1, sizeof(*freq));
CALLOC_ARRAY(freq, 1);
strbuf_init(&freq->tmpfile, 0);
oidcpy(&freq->oid, oid);
freq->localfile = -1;

4
imap-send.c

@ -963,9 +963,9 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const c
char *arg, *rsp;
int s = -1, preauth;
ctx = xcalloc(1, sizeof(*ctx));
CALLOC_ARRAY(ctx, 1);
ctx->imap = imap = xcalloc(1, sizeof(*imap));
ctx->imap = CALLOC_ARRAY(imap, 1);
imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1;
imap->in_progress_append = &imap->in_progress;

2
line-log.c

@ -296,7 +296,7 @@ static void line_log_data_insert(struct line_log_data **list,
return;
}
p = xcalloc(1, sizeof(struct line_log_data));
CALLOC_ARRAY(p, 1);
p->path = path;
range_set_append(&p->ranges, begin, end);
if (ip) {

2
line-range.c

@ -202,7 +202,7 @@ static const char *parse_range_funcname(
drv = userdiff_find_by_path(istate, path);
if (drv && drv->funcname.pattern) {
const struct userdiff_funcname *pe = &drv->funcname;
xecfg = xcalloc(1, sizeof(*xecfg));
CALLOC_ARRAY(xecfg, 1);
xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
}

6
list-objects-filter.c

@ -186,7 +186,7 @@ static enum list_objects_filter_result filter_trees_depth(
seen_info = oidmap_get(
&filter_data->seen_at_depth, &obj->oid);
if (!seen_info) {
seen_info = xcalloc(1, sizeof(*seen_info));
CALLOC_ARRAY(seen_info, 1);
oidcpy(&seen_info->base.oid, &obj->oid);
seen_info->depth = filter_data->current_depth;
oidmap_put(&filter_data->seen_at_depth, seen_info);
@ -626,7 +626,7 @@ static void filter_combine__init(
size_t sub;
d->nr = filter_options->sub_nr;
d->sub = xcalloc(d->nr, sizeof(*d->sub));
CALLOC_ARRAY(d->sub, d->nr);
for (sub = 0; sub < d->nr; sub++)
d->sub[sub].filter = list_objects_filter__init(
filter->omits ? &d->sub[sub].omits : NULL,
@ -674,7 +674,7 @@ struct filter *list_objects_filter__init(
if (!init_fn)
return NULL;
filter = xcalloc(1, sizeof(*filter));
CALLOC_ARRAY(filter, 1);
filter->omits = omitted;
init_fn(filter_options, filter);
return filter;

2
ll-merge.c

@ -268,7 +268,7 @@ static int read_merge_config(const char *var, const char *value, void *cb)
if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
break;
if (!fn) {
fn = xcalloc(1, sizeof(struct ll_merge_driver));
CALLOC_ARRAY(fn, 1);
fn->name = xmemdupz(name, namelen);
fn->fn = ll_ext_merge;
*ll_user_merge_tail = fn;

2
mailmap.c

@ -83,7 +83,7 @@ static void add_mapping(struct string_list *map,
if (item->util) {
me = (struct mailmap_entry *)item->util;
} else {
me = xcalloc(1, sizeof(struct mailmap_entry));
CALLOC_ARRAY(me, 1);
me->namemap.strdup_strings = 1;
me->namemap.cmp = namemap_cmp;
item->util = me;

9
merge-ort.c

@ -1576,8 +1576,7 @@ static void compute_collisions(struct strmap *collisions,
if (collision_info) {
free(new_path);
} else {
collision_info = xcalloc(1,
sizeof(struct collision_info));
CALLOC_ARRAY(collision_info, 1);
string_list_init(&collision_info->source_files, 0);
strmap_put(collisions, new_path, collision_info);
}
@ -1718,7 +1717,7 @@ static void apply_directory_rename_modifications(struct merge_options *opt,
struct conflict_info *dir_ci;
char *cur_dir = dirs_to_insert.items[i].string;
dir_ci = xcalloc(1, sizeof(*dir_ci));
CALLOC_ARRAY(dir_ci, 1);
dir_ci->merged.directory_name = parent_name;
len = strlen(parent_name);
@ -2703,7 +2702,7 @@ static void process_entry(struct merge_options *opt,
* the directory to remain here, so we need to move this
* path to some new location.
*/
new_ci = xcalloc(1, sizeof(*new_ci));
CALLOC_ARRAY(new_ci, 1);
/* We don't really want new_ci->merged.result copied, but it'll
* be overwritten below so it doesn't matter. We also don't
* want any directory mode/oid values copied, but we'll zero
@ -3083,7 +3082,7 @@ static int checkout(struct merge_options *opt,
unpack_opts.verbose_update = (opt->verbosity > 2);
unpack_opts.fn = twoway_merge;
if (1/* FIXME: opts->overwrite_ignore*/) {
unpack_opts.dir = xcalloc(1, sizeof(*unpack_opts.dir));
CALLOC_ARRAY(unpack_opts.dir, 1);
unpack_opts.dir->flags |= DIR_SHOW_IGNORED;
setup_standard_excludes(unpack_opts.dir);
}

9
merge-recursive.c

@ -303,7 +303,7 @@ static inline void setup_rename_conflict_info(enum rename_type rename_type,
return;
}
ci = xcalloc(1, sizeof(struct rename_conflict_info));
CALLOC_ARRAY(ci, 1);
ci->rename_type = rename_type;
ci->ren1 = ren1;
ci->ren2 = ren2;
@ -2389,8 +2389,7 @@ static void compute_collisions(struct hashmap *collisions,
continue;
collision_ent = collision_find_entry(collisions, new_path);
if (!collision_ent) {
collision_ent = xcalloc(1,
sizeof(struct collision_entry));
CALLOC_ARRAY(collision_ent, 1);
hashmap_entry_init(&collision_ent->ent,
strhash(new_path));
hashmap_put(collisions, &collision_ent->ent);
@ -2594,7 +2593,7 @@ static struct string_list *get_renames(struct merge_options *opt,
struct string_list *renames;
compute_collisions(&collisions, dir_renames, pairs);
renames = xcalloc(1, sizeof(struct string_list));
CALLOC_ARRAY(renames, 1);
for (i = 0; i < pairs->nr; ++i) {
struct string_list_item *item;
@ -3664,7 +3663,7 @@ static int merge_start(struct merge_options *opt, struct tree *head)
return -1;
}
opt->priv = xcalloc(1, sizeof(*opt->priv));
CALLOC_ARRAY(opt->priv, 1);
string_list_init(&opt->priv->df_conflict_file_set, 1);
return 0;
}

8
midx.c

@ -145,8 +145,8 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local
m->num_objects = ntohl(m->chunk_oid_fanout[255]);
m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names));