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>
René Scharfe committed
Sep 16, 2014 at 20:56 UTC
2756ca4347cbda05b16954cd7f445c216b935e76
26 files changed
+40
-70
attr.c
+1
-2
@@ -97,8 +97,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
97
a->attr_nr = attr_nr++;
98
git_attr_hash[pos] = a;
99
100
- check_all_attr = xrealloc(check_all_attr,
101
- sizeof(*check_all_attr) * attr_nr);
100
+ REALLOC_ARRAY(check_all_attr, attr_nr);
101
check_all_attr[a->attr_nr].attr = a;
102
check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
103
return a;
builtin/apply.c
+1
-1
@@ -2626,7 +2626,7 @@ static void update_image(struct image *img,
2626
* NOTE: this knows that we never call remove_first_line()
2627
* on anything other than pre/post image.
2628
*/
2629
- img->line = xrealloc(img->line, nr * sizeof(*img->line));
2629
+ REALLOC_ARRAY(img->line, nr);
2630
img->line_allocated = img->line;
2631
}
2632
if (preimage_limit != postimage->nr)
builtin/for-each-ref.c
+3
-6
@@ -138,10 +138,8 @@ static int parse_atom(const char *atom, const char *ep)
138
/* Add it in, including the deref prefix */
139
at = used_atom_cnt;
140
used_atom_cnt++;
141
- used_atom = xrealloc(used_atom,
142
- (sizeof *used_atom) * used_atom_cnt);
143
- used_atom_type = xrealloc(used_atom_type,
144
- (sizeof(*used_atom_type) * used_atom_cnt));
141
+ REALLOC_ARRAY(used_atom, used_atom_cnt);
142
+ REALLOC_ARRAY(used_atom_type, used_atom_cnt);
143
used_atom[at] = xmemdupz(atom, ep - atom);
144
used_atom_type[at] = valid_atom[i].cmp_type;
145
if (*atom == '*')
@@ -870,8 +868,7 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
868
ref->flag = flag;
869
870
cnt = cb->grab_cnt;
873
- cb->grab_array = xrealloc(cb->grab_array,
874
- sizeof(*cb->grab_array) * (cnt + 1));
871
+ REALLOC_ARRAY(cb->grab_array, cnt + 1);
872
cb->grab_array[cnt++] = ref;
873
cb->grab_cnt = cnt;
874
return 0;
builtin/index-pack.c
+1
-3
@@ -1140,9 +1140,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
1140
int nr_objects_initial = nr_objects;
1141
if (nr_unresolved <= 0)
1142
die(_("confusion beyond insanity"));
1143
- objects = xrealloc(objects,
1144
- (nr_objects + nr_unresolved + 1)
1145
- * sizeof(*objects));
1143
+ REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
1144
memset(objects + nr_objects + 1, 0,
1145
nr_unresolved * sizeof(*objects));
1146
f = sha1fd(output_fd, curr_pack);
builtin/log.c
+1
-1
@@ -1440,7 +1440,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1440
continue;
1441
1442
nr++;
1443
- list = xrealloc(list, nr * sizeof(list[0]));
1443
+ REALLOC_ARRAY(list, nr);
1444
list[nr - 1] = commit;
1445
}
1446
if (nr == 0)
builtin/merge.c
+1
-1
@@ -556,7 +556,7 @@ static void parse_branch_merge_options(char *bmo)
556
if (argc < 0)
557
die(_("Bad branch.%s.mergeoptions string: %s"), branch,
558
split_cmdline_strerror(argc));
559
- argv = xrealloc(argv, sizeof(*argv) * (argc + 2));
559
+ REALLOC_ARRAY(argv, argc + 2);
560
memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
561
argc++;
562
argv[0] = "branch.*.mergeoptions";
builtin/mv.c
+4
-4
@@ -184,10 +184,10 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
184
185
modes[i] = WORKING_DIRECTORY;
186
n = argc + last - first;
187
- source = xrealloc(source, n * sizeof(char *));
188
- destination = xrealloc(destination, n * sizeof(char *));
189
- modes = xrealloc(modes, n * sizeof(enum update_mode));
190
- submodule_gitfile = xrealloc(submodule_gitfile, n * sizeof(char *));
187
+ REALLOC_ARRAY(source, n);
188
+ REALLOC_ARRAY(destination, n);
189
+ REALLOC_ARRAY(modes, n);
190
+ REALLOC_ARRAY(submodule_gitfile, n);
191
192
dst = add_slash(dst);
193
dst_len = strlen(dst);
builtin/pack-objects.c
+1
-2
@@ -89,8 +89,7 @@ static void index_commit_for_bitmap(struct commit *commit)
89
{
90
if (indexed_commits_nr >= indexed_commits_alloc) {
91
indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
92
- indexed_commits = xrealloc(indexed_commits,
93
- indexed_commits_alloc * sizeof(struct commit *));
92
+ REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
93
}
94
95
indexed_commits[indexed_commits_nr++] = commit;
builtin/show-branch.c
+1
-1
@@ -563,7 +563,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
563
default_arg[default_num++] = "show-branch";
564
} else if (default_alloc <= default_num + 1) {
565
default_alloc = default_alloc * 3 / 2 + 20;
566
- default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc);
566
+ REALLOC_ARRAY(default_arg, default_alloc);
567
}
568
default_arg[default_num++] = xstrdup(value);
569
default_arg[default_num] = NULL;
cache.h
+1
-1
@@ -482,7 +482,7 @@ extern int daemonize(void);
482
alloc = (nr); \
483
else \
484
alloc = alloc_nr(alloc); \
485
- x = xrealloc((x), alloc * sizeof(*(x))); \
485
+ REALLOC_ARRAY(x, alloc); \
486
} \
487
} while (0)
488
column.c
+2
-4
@@ -81,8 +81,7 @@ static void compute_column_width(struct column_data *data)
81
*/
82
static void shrink_columns(struct column_data *data)
83
{
84
- data->width = xrealloc(data->width,
85
- sizeof(*data->width) * data->cols);
84
+ REALLOC_ARRAY(data->width, data->cols);
85
while (data->rows > 1) {
86
int x, total_width, cols, rows;
87
rows = data->rows;
@@ -91,8 +90,7 @@ static void shrink_columns(struct column_data *data)
90
data->rows--;
91
data->cols = DIV_ROUND_UP(data->list->nr, data->rows);
92
if (data->cols != cols)
94
- data->width = xrealloc(data->width,
95
- sizeof(*data->width) * data->cols);
93
+ REALLOC_ARRAY(data->width, data->cols);
94
compute_column_width(data);
95
96
total_width = strlen(data->opts.indent);
commit-slab.h
+1
-2
@@ -90,8 +90,7 @@ static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
90
\
91
if (s->slab_count <= nth_slab) { \
92
int i; \
93
- s->slab = xrealloc(s->slab, \
94
- (nth_slab + 1) * sizeof(*s->slab)); \
93
+ REALLOC_ARRAY(s->slab, nth_slab + 1); \
94
stat_ ##slabname## realloc++; \
95
for (i = s->slab_count; i <= nth_slab; i++) \
96
s->slab[i] = NULL; \
fast-import.c
+1
-1
@@ -878,7 +878,7 @@ static void start_packfile(void)
878
pack_size = sizeof(hdr);
879
object_count = 0;
880
881
- all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
881
+ REALLOC_ARRAY(all_packs, pack_id + 1);
882
all_packs[pack_id] = p;
883
}
884
git.c
+1
-2
@@ -282,8 +282,7 @@ static int handle_alias(int *argcp, const char ***argv)
282
"trace: alias expansion: %s =>",
283
alias_command);
284
285
- new_argv = xrealloc(new_argv, sizeof(char *) *
286
- (count + *argcp));
285
+ REALLOC_ARRAY(new_argv, count + *argcp);
286
/* insert after command name */
287
memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
288
graph.c
+4
-10
@@ -267,16 +267,10 @@ static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
267
graph->column_capacity *= 2;
268
} while (graph->column_capacity < num_columns);
269
270
- graph->columns = xrealloc(graph->columns,
271
- sizeof(struct column) *
272
- graph->column_capacity);
273
- graph->new_columns = xrealloc(graph->new_columns,
274
- sizeof(struct column) *
275
- graph->column_capacity);
276
- graph->mapping = xrealloc(graph->mapping,
277
- sizeof(int) * 2 * graph->column_capacity);
278
- graph->new_mapping = xrealloc(graph->new_mapping,
279
- sizeof(int) * 2 * graph->column_capacity);
270
+ REALLOC_ARRAY(graph->columns, graph->column_capacity);
271
+ REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
272
+ REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
273
+ REALLOC_ARRAY(graph->new_mapping, graph->column_capacity * 2);
274
}
275
276
/*
khash.h
+4
-8
@@ -121,13 +121,9 @@ static const double __ac_HASH_UPPER = 0.77;
121
if (!new_flags) return -1; \
122
memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
123
if (h->n_buckets < new_n_buckets) { /* expand */ \
124
- khkey_t *new_keys = (khkey_t*)xrealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
125
- if (!new_keys) return -1; \
126
- h->keys = new_keys; \
124
+ REALLOC_ARRAY(h->keys, new_n_buckets); \
125
if (kh_is_map) { \
128
- khval_t *new_vals = (khval_t*)xrealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
129
- if (!new_vals) return -1; \
130
- h->vals = new_vals; \
126
+ REALLOC_ARRAY(h->vals, new_n_buckets); \
127
} \
128
} /* otherwise shrink */ \
129
} \
@@ -160,8 +156,8 @@ static const double __ac_HASH_UPPER = 0.77;
156
} \
157
} \
158
if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
163
- h->keys = (khkey_t*)xrealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
164
- if (kh_is_map) h->vals = (khval_t*)xrealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
159
+ REALLOC_ARRAY(h->keys, new_n_buckets); \
160
+ if (kh_is_map) REALLOC_ARRAY(h->vals, new_n_buckets); \
161
} \
162
free(h->flags); /* free the working space */ \
163
h->flags = new_flags; \
line-log.c
+1
-1
@@ -533,7 +533,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,
533
}
534
535
/* shrink the array to fit the elements */
536
- ends = xrealloc(ends, cur * sizeof(*ends));
536
+ REALLOC_ARRAY(ends, cur);
537
*lines = cur-1;
538
*line_ends = ends;
539
}
object.c
+1
-1
@@ -312,7 +312,7 @@ static void add_object_array_with_mode_context(struct object *obj, const char *n
312
313
if (nr >= alloc) {
314
alloc = (alloc + 32) * 2;
315
- objects = xrealloc(objects, alloc * sizeof(*objects));
315
+ REALLOC_ARRAY(objects, alloc);
316
array->alloc = alloc;
317
array->objects = objects;
318
}
pack-bitmap-write.c
+1
-2
@@ -111,8 +111,7 @@ static inline void push_bitmapped_commit(struct commit *commit, struct ewah_bitm
111
{
112
if (writer.selected_nr >= writer.selected_alloc) {
113
writer.selected_alloc = (writer.selected_alloc + 32) * 2;
114
- writer.selected = xrealloc(writer.selected,
115
- writer.selected_alloc * sizeof(struct bitmapped_commit));
114
+ REALLOC_ARRAY(writer.selected, writer.selected_alloc);
115
}
116
117
writer.selected[writer.selected_nr].commit = commit;
pack-bitmap.c
+2
-4
@@ -400,10 +400,8 @@ static int ext_index_add_object(struct object *object, const char *name)
400
if (hash_ret > 0) {
401
if (eindex->count >= eindex->alloc) {
402
eindex->alloc = (eindex->alloc + 16) * 3 / 2;
403
- eindex->objects = xrealloc(eindex->objects,
404
- eindex->alloc * sizeof(struct object *));
405
- eindex->hashes = xrealloc(eindex->hashes,
406
- eindex->alloc * sizeof(uint32_t));
403
+ REALLOC_ARRAY(eindex->objects, eindex->alloc);
404
+ REALLOC_ARRAY(eindex->hashes, eindex->alloc);
405
}
406
407
bitmap_pos = eindex->count;
pack-objects.c
+1
-2
@@ -92,8 +92,7 @@ struct object_entry *packlist_alloc(struct packing_data *pdata,
92
93
if (pdata->nr_objects >= pdata->nr_alloc) {
94
pdata->nr_alloc = (pdata->nr_alloc + 1024) * 3 / 2;
95
- pdata->objects = xrealloc(pdata->objects,
96
- pdata->nr_alloc * sizeof(*new_entry));
95
+ REALLOC_ARRAY(pdata->objects, pdata->nr_alloc);
96
}
97
98
new_entry = pdata->objects + pdata->nr_objects++;
revision.c
+1
-1
@@ -1397,7 +1397,7 @@ static void prepare_show_merge(struct rev_info *revs)
1397
continue;
1398
if (ce_path_match(ce, &revs->prune_data, NULL)) {
1399
prune_num++;
1400
- prune = xrealloc(prune, sizeof(*prune) * prune_num);
1400
+ REALLOC_ARRAY(prune, prune_num);
1401
prune[prune_num-2] = ce->name;
1402
prune[prune_num-1] = NULL;
1403
}
sh-i18n--envsubst.c
+1
-4
@@ -208,11 +208,8 @@ string_list_append (string_list_ty *slp, const char *s)
208
/* Grow the list. */
209
if (slp->nitems >= slp->nitems_max)
210
{
211
- size_t nbytes;
212
-
211
slp->nitems_max = slp->nitems_max * 2 + 4;
214
- nbytes = slp->nitems_max * sizeof (slp->item[0]);
215
- slp->item = (const char **) xrealloc (slp->item, nbytes);
212
+ REALLOC_ARRAY(slp->item, slp->nitems_max);
213
}
214
215
/* Add the string to the end of the list. */
shallow.c
+1
-2
@@ -392,8 +392,7 @@ static uint32_t *paint_alloc(struct paint_info *info)
392
void *p;
393
if (!info->slab_count || info->free + size > info->end) {
394
info->slab_count++;
395
- info->slab = xrealloc(info->slab,
396
- info->slab_count * sizeof(*info->slab));
395
+ REALLOC_ARRAY(info->slab, info->slab_count);
396
info->free = xmalloc(COMMIT_SLAB_SIZE);
397
info->slab[info->slab_count - 1] = info->free;
398
info->end = info->free + COMMIT_SLAB_SIZE;
string-list.c
+1
-2
@@ -43,8 +43,7 @@ static int add_entry(int insert_at, struct string_list *list, const char *string
43
44
if (list->nr + 1 >= list->alloc) {
45
list->alloc += 32;
46
- list->items = xrealloc(list->items, list->alloc
47
- * sizeof(struct string_list_item));
46
+ REALLOC_ARRAY(list->items, list->alloc);
47
}
48
if (index < list->nr)
49
memmove(list->items + index + 1, list->items + index,
walker.c
+2
-2
@@ -228,8 +228,8 @@ int walker_targets_stdin(char ***target, const char ***write_ref)
228
229
if (targets >= targets_alloc) {
230
targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
231
- *target = xrealloc(*target, targets_alloc * sizeof(**target));
232
- *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
231
+ REALLOC_ARRAY(*target, targets_alloc);
232
+ REALLOC_ARRAY(*write_ref, targets_alloc);
233
}
234
(*target)[targets] = xstrdup(tg_one);
235
(*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;