The Wayback Machine - https://web.archive.org/web/20220623175051/https://github.com/git-for-windows/git/commit/ca56dadb4b65ccaeab809d80db80a312dc00941a
Skip to content
Permalink
Browse files
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>
  • Loading branch information
rscharfe authored and gitster committed Mar 14, 2021
1 parent f112149 commit ca56dadb4b65ccaeab809d80db80a312dc00941a
Showing 88 changed files with 190 additions and 188 deletions.
@@ -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;
@@ -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);
@@ -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);
}

@@ -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
@@ -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;
@@ -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;
@@ -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);
@@ -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);
@@ -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);
@@ -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]);
}
@@ -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;
@@ -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 =
@@ -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++) {
@@ -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;

@@ -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;
@@ -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);
@@ -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;

@@ -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;

0 comments on commit ca56dad

Please sign in to comment.