1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-10 10:46:08 +02:00

use REALLOC_ARRAY for changing the allocation size of arrays

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2014-09-16 20:56:57 +02:00 committed by Junio C Hamano
parent 3ac22f82ed
commit 2756ca4347
26 changed files with 40 additions and 70 deletions

3
attr.c
View File

@ -97,8 +97,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
a->attr_nr = attr_nr++; a->attr_nr = attr_nr++;
git_attr_hash[pos] = a; git_attr_hash[pos] = a;
check_all_attr = xrealloc(check_all_attr, REALLOC_ARRAY(check_all_attr, attr_nr);
sizeof(*check_all_attr) * attr_nr);
check_all_attr[a->attr_nr].attr = a; check_all_attr[a->attr_nr].attr = a;
check_all_attr[a->attr_nr].value = ATTR__UNKNOWN; check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
return a; return a;

View File

@ -2626,7 +2626,7 @@ static void update_image(struct image *img,
* NOTE: this knows that we never call remove_first_line() * NOTE: this knows that we never call remove_first_line()
* on anything other than pre/post image. * on anything other than pre/post image.
*/ */
img->line = xrealloc(img->line, nr * sizeof(*img->line)); REALLOC_ARRAY(img->line, nr);
img->line_allocated = img->line; img->line_allocated = img->line;
} }
if (preimage_limit != postimage->nr) if (preimage_limit != postimage->nr)

View File

@ -138,10 +138,8 @@ static int parse_atom(const char *atom, const char *ep)
/* Add it in, including the deref prefix */ /* Add it in, including the deref prefix */
at = used_atom_cnt; at = used_atom_cnt;
used_atom_cnt++; used_atom_cnt++;
used_atom = xrealloc(used_atom, REALLOC_ARRAY(used_atom, used_atom_cnt);
(sizeof *used_atom) * used_atom_cnt); REALLOC_ARRAY(used_atom_type, used_atom_cnt);
used_atom_type = xrealloc(used_atom_type,
(sizeof(*used_atom_type) * used_atom_cnt));
used_atom[at] = xmemdupz(atom, ep - atom); used_atom[at] = xmemdupz(atom, ep - atom);
used_atom_type[at] = valid_atom[i].cmp_type; used_atom_type[at] = valid_atom[i].cmp_type;
if (*atom == '*') if (*atom == '*')
@ -870,8 +868,7 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
ref->flag = flag; ref->flag = flag;
cnt = cb->grab_cnt; cnt = cb->grab_cnt;
cb->grab_array = xrealloc(cb->grab_array, REALLOC_ARRAY(cb->grab_array, cnt + 1);
sizeof(*cb->grab_array) * (cnt + 1));
cb->grab_array[cnt++] = ref; cb->grab_array[cnt++] = ref;
cb->grab_cnt = cnt; cb->grab_cnt = cnt;
return 0; return 0;

View File

@ -1140,9 +1140,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
int nr_objects_initial = nr_objects; int nr_objects_initial = nr_objects;
if (nr_unresolved <= 0) if (nr_unresolved <= 0)
die(_("confusion beyond insanity")); die(_("confusion beyond insanity"));
objects = xrealloc(objects, REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
(nr_objects + nr_unresolved + 1)
* sizeof(*objects));
memset(objects + nr_objects + 1, 0, memset(objects + nr_objects + 1, 0,
nr_unresolved * sizeof(*objects)); nr_unresolved * sizeof(*objects));
f = sha1fd(output_fd, curr_pack); f = sha1fd(output_fd, curr_pack);

View File

@ -1440,7 +1440,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
continue; continue;
nr++; nr++;
list = xrealloc(list, nr * sizeof(list[0])); REALLOC_ARRAY(list, nr);
list[nr - 1] = commit; list[nr - 1] = commit;
} }
if (nr == 0) if (nr == 0)

View File

@ -556,7 +556,7 @@ static void parse_branch_merge_options(char *bmo)
if (argc < 0) if (argc < 0)
die(_("Bad branch.%s.mergeoptions string: %s"), branch, die(_("Bad branch.%s.mergeoptions string: %s"), branch,
split_cmdline_strerror(argc)); split_cmdline_strerror(argc));
argv = xrealloc(argv, sizeof(*argv) * (argc + 2)); REALLOC_ARRAY(argv, argc + 2);
memmove(argv + 1, argv, sizeof(*argv) * (argc + 1)); memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
argc++; argc++;
argv[0] = "branch.*.mergeoptions"; argv[0] = "branch.*.mergeoptions";

View File

@ -184,10 +184,10 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
modes[i] = WORKING_DIRECTORY; modes[i] = WORKING_DIRECTORY;
n = argc + last - first; n = argc + last - first;
source = xrealloc(source, n * sizeof(char *)); REALLOC_ARRAY(source, n);
destination = xrealloc(destination, n * sizeof(char *)); REALLOC_ARRAY(destination, n);
modes = xrealloc(modes, n * sizeof(enum update_mode)); REALLOC_ARRAY(modes, n);
submodule_gitfile = xrealloc(submodule_gitfile, n * sizeof(char *)); REALLOC_ARRAY(submodule_gitfile, n);
dst = add_slash(dst); dst = add_slash(dst);
dst_len = strlen(dst); dst_len = strlen(dst);

View File

@ -89,8 +89,7 @@ static void index_commit_for_bitmap(struct commit *commit)
{ {
if (indexed_commits_nr >= indexed_commits_alloc) { if (indexed_commits_nr >= indexed_commits_alloc) {
indexed_commits_alloc = (indexed_commits_alloc + 32) * 2; indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
indexed_commits = xrealloc(indexed_commits, REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
indexed_commits_alloc * sizeof(struct commit *));
} }
indexed_commits[indexed_commits_nr++] = commit; indexed_commits[indexed_commits_nr++] = commit;

View File

@ -563,7 +563,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
default_arg[default_num++] = "show-branch"; default_arg[default_num++] = "show-branch";
} else if (default_alloc <= default_num + 1) { } else if (default_alloc <= default_num + 1) {
default_alloc = default_alloc * 3 / 2 + 20; default_alloc = default_alloc * 3 / 2 + 20;
default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc); REALLOC_ARRAY(default_arg, default_alloc);
} }
default_arg[default_num++] = xstrdup(value); default_arg[default_num++] = xstrdup(value);
default_arg[default_num] = NULL; default_arg[default_num] = NULL;

View File

@ -482,7 +482,7 @@ extern int daemonize(void);
alloc = (nr); \ alloc = (nr); \
else \ else \
alloc = alloc_nr(alloc); \ alloc = alloc_nr(alloc); \
x = xrealloc((x), alloc * sizeof(*(x))); \ REALLOC_ARRAY(x, alloc); \
} \ } \
} while (0) } while (0)

View File

@ -81,8 +81,7 @@ static void compute_column_width(struct column_data *data)
*/ */
static void shrink_columns(struct column_data *data) static void shrink_columns(struct column_data *data)
{ {
data->width = xrealloc(data->width, REALLOC_ARRAY(data->width, data->cols);
sizeof(*data->width) * data->cols);
while (data->rows > 1) { while (data->rows > 1) {
int x, total_width, cols, rows; int x, total_width, cols, rows;
rows = data->rows; rows = data->rows;
@ -91,8 +90,7 @@ static void shrink_columns(struct column_data *data)
data->rows--; data->rows--;
data->cols = DIV_ROUND_UP(data->list->nr, data->rows); data->cols = DIV_ROUND_UP(data->list->nr, data->rows);
if (data->cols != cols) if (data->cols != cols)
data->width = xrealloc(data->width, REALLOC_ARRAY(data->width, data->cols);
sizeof(*data->width) * data->cols);
compute_column_width(data); compute_column_width(data);
total_width = strlen(data->opts.indent); total_width = strlen(data->opts.indent);

View File

@ -90,8 +90,7 @@ static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
\ \
if (s->slab_count <= nth_slab) { \ if (s->slab_count <= nth_slab) { \
int i; \ int i; \
s->slab = xrealloc(s->slab, \ REALLOC_ARRAY(s->slab, nth_slab + 1); \
(nth_slab + 1) * sizeof(*s->slab)); \
stat_ ##slabname## realloc++; \ stat_ ##slabname## realloc++; \
for (i = s->slab_count; i <= nth_slab; i++) \ for (i = s->slab_count; i <= nth_slab; i++) \
s->slab[i] = NULL; \ s->slab[i] = NULL; \

View File

@ -878,7 +878,7 @@ static void start_packfile(void)
pack_size = sizeof(hdr); pack_size = sizeof(hdr);
object_count = 0; object_count = 0;
all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1)); REALLOC_ARRAY(all_packs, pack_id + 1);
all_packs[pack_id] = p; all_packs[pack_id] = p;
} }

3
git.c
View File

@ -282,8 +282,7 @@ static int handle_alias(int *argcp, const char ***argv)
"trace: alias expansion: %s =>", "trace: alias expansion: %s =>",
alias_command); alias_command);
new_argv = xrealloc(new_argv, sizeof(char *) * REALLOC_ARRAY(new_argv, count + *argcp);
(count + *argcp));
/* insert after command name */ /* insert after command name */
memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp); memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);

14
graph.c
View File

@ -267,16 +267,10 @@ static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
graph->column_capacity *= 2; graph->column_capacity *= 2;
} while (graph->column_capacity < num_columns); } while (graph->column_capacity < num_columns);
graph->columns = xrealloc(graph->columns, REALLOC_ARRAY(graph->columns, graph->column_capacity);
sizeof(struct column) * REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
graph->column_capacity); REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
graph->new_columns = xrealloc(graph->new_columns, REALLOC_ARRAY(graph->new_mapping, graph->column_capacity * 2);
sizeof(struct column) *
graph->column_capacity);
graph->mapping = xrealloc(graph->mapping,
sizeof(int) * 2 * graph->column_capacity);
graph->new_mapping = xrealloc(graph->new_mapping,
sizeof(int) * 2 * graph->column_capacity);
} }
/* /*

12
khash.h
View File

@ -121,13 +121,9 @@ static const double __ac_HASH_UPPER = 0.77;
if (!new_flags) return -1; \ if (!new_flags) return -1; \
memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \ memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
if (h->n_buckets < new_n_buckets) { /* expand */ \ if (h->n_buckets < new_n_buckets) { /* expand */ \
khkey_t *new_keys = (khkey_t*)xrealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \ REALLOC_ARRAY(h->keys, new_n_buckets); \
if (!new_keys) return -1; \
h->keys = new_keys; \
if (kh_is_map) { \ if (kh_is_map) { \
khval_t *new_vals = (khval_t*)xrealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \ REALLOC_ARRAY(h->vals, new_n_buckets); \
if (!new_vals) return -1; \
h->vals = new_vals; \
} \ } \
} /* otherwise shrink */ \ } /* otherwise shrink */ \
} \ } \
@ -160,8 +156,8 @@ static const double __ac_HASH_UPPER = 0.77;
} \ } \
} \ } \
if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \ if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
h->keys = (khkey_t*)xrealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \ REALLOC_ARRAY(h->keys, new_n_buckets); \
if (kh_is_map) h->vals = (khval_t*)xrealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \ if (kh_is_map) REALLOC_ARRAY(h->vals, new_n_buckets); \
} \ } \
free(h->flags); /* free the working space */ \ free(h->flags); /* free the working space */ \
h->flags = new_flags; \ h->flags = new_flags; \

View File

@ -533,7 +533,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,
} }
/* shrink the array to fit the elements */ /* shrink the array to fit the elements */
ends = xrealloc(ends, cur * sizeof(*ends)); REALLOC_ARRAY(ends, cur);
*lines = cur-1; *lines = cur-1;
*line_ends = ends; *line_ends = ends;
} }

View File

@ -312,7 +312,7 @@ static void add_object_array_with_mode_context(struct object *obj, const char *n
if (nr >= alloc) { if (nr >= alloc) {
alloc = (alloc + 32) * 2; alloc = (alloc + 32) * 2;
objects = xrealloc(objects, alloc * sizeof(*objects)); REALLOC_ARRAY(objects, alloc);
array->alloc = alloc; array->alloc = alloc;
array->objects = objects; array->objects = objects;
} }

View File

@ -111,8 +111,7 @@ static inline void push_bitmapped_commit(struct commit *commit, struct ewah_bitm
{ {
if (writer.selected_nr >= writer.selected_alloc) { if (writer.selected_nr >= writer.selected_alloc) {
writer.selected_alloc = (writer.selected_alloc + 32) * 2; writer.selected_alloc = (writer.selected_alloc + 32) * 2;
writer.selected = xrealloc(writer.selected, REALLOC_ARRAY(writer.selected, writer.selected_alloc);
writer.selected_alloc * sizeof(struct bitmapped_commit));
} }
writer.selected[writer.selected_nr].commit = commit; writer.selected[writer.selected_nr].commit = commit;

View File

@ -400,10 +400,8 @@ static int ext_index_add_object(struct object *object, const char *name)
if (hash_ret > 0) { if (hash_ret > 0) {
if (eindex->count >= eindex->alloc) { if (eindex->count >= eindex->alloc) {
eindex->alloc = (eindex->alloc + 16) * 3 / 2; eindex->alloc = (eindex->alloc + 16) * 3 / 2;
eindex->objects = xrealloc(eindex->objects, REALLOC_ARRAY(eindex->objects, eindex->alloc);
eindex->alloc * sizeof(struct object *)); REALLOC_ARRAY(eindex->hashes, eindex->alloc);
eindex->hashes = xrealloc(eindex->hashes,
eindex->alloc * sizeof(uint32_t));
} }
bitmap_pos = eindex->count; bitmap_pos = eindex->count;

View File

@ -92,8 +92,7 @@ struct object_entry *packlist_alloc(struct packing_data *pdata,
if (pdata->nr_objects >= pdata->nr_alloc) { if (pdata->nr_objects >= pdata->nr_alloc) {
pdata->nr_alloc = (pdata->nr_alloc + 1024) * 3 / 2; pdata->nr_alloc = (pdata->nr_alloc + 1024) * 3 / 2;
pdata->objects = xrealloc(pdata->objects, REALLOC_ARRAY(pdata->objects, pdata->nr_alloc);
pdata->nr_alloc * sizeof(*new_entry));
} }
new_entry = pdata->objects + pdata->nr_objects++; new_entry = pdata->objects + pdata->nr_objects++;

View File

@ -1397,7 +1397,7 @@ static void prepare_show_merge(struct rev_info *revs)
continue; continue;
if (ce_path_match(ce, &revs->prune_data, NULL)) { if (ce_path_match(ce, &revs->prune_data, NULL)) {
prune_num++; prune_num++;
prune = xrealloc(prune, sizeof(*prune) * prune_num); REALLOC_ARRAY(prune, prune_num);
prune[prune_num-2] = ce->name; prune[prune_num-2] = ce->name;
prune[prune_num-1] = NULL; prune[prune_num-1] = NULL;
} }

View File

@ -208,11 +208,8 @@ string_list_append (string_list_ty *slp, const char *s)
/* Grow the list. */ /* Grow the list. */
if (slp->nitems >= slp->nitems_max) if (slp->nitems >= slp->nitems_max)
{ {
size_t nbytes;
slp->nitems_max = slp->nitems_max * 2 + 4; slp->nitems_max = slp->nitems_max * 2 + 4;
nbytes = slp->nitems_max * sizeof (slp->item[0]); REALLOC_ARRAY(slp->item, slp->nitems_max);
slp->item = (const char **) xrealloc (slp->item, nbytes);
} }
/* Add the string to the end of the list. */ /* Add the string to the end of the list. */

View File

@ -392,8 +392,7 @@ static uint32_t *paint_alloc(struct paint_info *info)
void *p; void *p;
if (!info->slab_count || info->free + size > info->end) { if (!info->slab_count || info->free + size > info->end) {
info->slab_count++; info->slab_count++;
info->slab = xrealloc(info->slab, REALLOC_ARRAY(info->slab, info->slab_count);
info->slab_count * sizeof(*info->slab));
info->free = xmalloc(COMMIT_SLAB_SIZE); info->free = xmalloc(COMMIT_SLAB_SIZE);
info->slab[info->slab_count - 1] = info->free; info->slab[info->slab_count - 1] = info->free;
info->end = info->free + COMMIT_SLAB_SIZE; info->end = info->free + COMMIT_SLAB_SIZE;

View File

@ -43,8 +43,7 @@ static int add_entry(int insert_at, struct string_list *list, const char *string
if (list->nr + 1 >= list->alloc) { if (list->nr + 1 >= list->alloc) {
list->alloc += 32; list->alloc += 32;
list->items = xrealloc(list->items, list->alloc REALLOC_ARRAY(list->items, list->alloc);
* sizeof(struct string_list_item));
} }
if (index < list->nr) if (index < list->nr)
memmove(list->items + index + 1, list->items + index, memmove(list->items + index + 1, list->items + index,

View File

@ -228,8 +228,8 @@ int walker_targets_stdin(char ***target, const char ***write_ref)
if (targets >= targets_alloc) { if (targets >= targets_alloc) {
targets_alloc = targets_alloc ? targets_alloc * 2 : 64; targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
*target = xrealloc(*target, targets_alloc * sizeof(**target)); REALLOC_ARRAY(*target, targets_alloc);
*write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref)); REALLOC_ARRAY(*write_ref, targets_alloc);
} }
(*target)[targets] = xstrdup(tg_one); (*target)[targets] = xstrdup(tg_one);
(*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL; (*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;