Skip to content

Commit fe9122a

Browse files
committed
Merge branch 'dd/use-alloc-grow'
Replace open-coded reallocation with ALLOC_GROW() macro. * dd/use-alloc-grow: sha1_file.c: use ALLOC_GROW() in pretend_sha1_file() read-cache.c: use ALLOC_GROW() in add_index_entry() builtin/mktree.c: use ALLOC_GROW() in append_to_tree() attr.c: use ALLOC_GROW() in handle_attr_line() dir.c: use ALLOC_GROW() in create_simplify() reflog-walk.c: use ALLOC_GROW() replace_object.c: use ALLOC_GROW() in register_replace_object() patch-ids.c: use ALLOC_GROW() in add_commit() diffcore-rename.c: use ALLOC_GROW() diff.c: use ALLOC_GROW() commit.c: use ALLOC_GROW() in register_commit_graft() cache-tree.c: use ALLOC_GROW() in find_subtree() bundle.c: use ALLOC_GROW() in add_to_ref_list() builtin/pack-objects.c: use ALLOC_GROW() in check_pbase_path()
2 parents a8e1d71 + c735396 commit fe9122a

File tree

14 files changed

+21
-87
lines changed

14 files changed

+21
-87
lines changed

attr.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,7 @@ static void handle_attr_line(struct attr_stack *res,
338338
a = parse_attr_line(line, src, lineno, macro_ok);
339339
if (!a)
340340
return;
341-
if (res->alloc <= res->num_matches) {
342-
res->alloc = alloc_nr(res->num_matches);
343-
res->attrs = xrealloc(res->attrs,
344-
sizeof(struct match_attr *) *
345-
res->alloc);
346-
}
341+
ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
347342
res->attrs[res->num_matches++] = a;
348343
}
349344

builtin/mktree.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
2323
if (strchr(path, '/'))
2424
die("path %s contains slash", path);
2525

26-
if (alloc <= used) {
27-
alloc = alloc_nr(used);
28-
entries = xrealloc(entries, sizeof(*entries) * alloc);
29-
}
26+
ALLOC_GROW(entries, used + 1, alloc);
3027
ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
3128
ent->mode = mode;
3229
ent->len = len;

builtin/pack-objects.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,12 +1213,9 @@ static int check_pbase_path(unsigned hash)
12131213
if (0 <= pos)
12141214
return 1;
12151215
pos = -pos - 1;
1216-
if (done_pbase_paths_alloc <= done_pbase_paths_num) {
1217-
done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
1218-
done_pbase_paths = xrealloc(done_pbase_paths,
1219-
done_pbase_paths_alloc *
1220-
sizeof(unsigned));
1221-
}
1216+
ALLOC_GROW(done_pbase_paths,
1217+
done_pbase_paths_num + 1,
1218+
done_pbase_paths_alloc);
12221219
done_pbase_paths_num++;
12231220
if (pos < done_pbase_paths_num)
12241221
memmove(done_pbase_paths + pos + 1,

bundle.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ static const char bundle_signature[] = "# v2 git bundle\n";
1414
static void add_to_ref_list(const unsigned char *sha1, const char *name,
1515
struct ref_list *list)
1616
{
17-
if (list->nr + 1 >= list->alloc) {
18-
list->alloc = alloc_nr(list->nr + 1);
19-
list->list = xrealloc(list->list,
20-
list->alloc * sizeof(list->list[0]));
21-
}
17+
ALLOC_GROW(list->list, list->nr + 1, list->alloc);
2218
memcpy(list->list[list->nr].sha1, sha1, 20);
2319
list->list[list->nr].name = xstrdup(name);
2420
list->nr++;

cache-tree.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
7575
return NULL;
7676

7777
pos = -pos-1;
78-
if (it->subtree_alloc <= it->subtree_nr) {
79-
it->subtree_alloc = alloc_nr(it->subtree_alloc);
80-
it->down = xrealloc(it->down, it->subtree_alloc *
81-
sizeof(*it->down));
82-
}
78+
ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
8379
it->subtree_nr++;
8480

8581
down = xmalloc(sizeof(*down) + pathlen + 1);

commit.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,8 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
141141
return 1;
142142
}
143143
pos = -pos - 1;
144-
if (commit_graft_alloc <= ++commit_graft_nr) {
145-
commit_graft_alloc = alloc_nr(commit_graft_alloc);
146-
commit_graft = xrealloc(commit_graft,
147-
sizeof(*commit_graft) *
148-
commit_graft_alloc);
149-
}
144+
ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
145+
commit_graft_nr++;
150146
if (pos < commit_graft_nr)
151147
memmove(commit_graft + pos + 1,
152148
commit_graft + pos,

diff.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,11 +1361,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
13611361
{
13621362
struct diffstat_file *x;
13631363
x = xcalloc(sizeof (*x), 1);
1364-
if (diffstat->nr == diffstat->alloc) {
1365-
diffstat->alloc = alloc_nr(diffstat->alloc);
1366-
diffstat->files = xrealloc(diffstat->files,
1367-
diffstat->alloc * sizeof(x));
1368-
}
1364+
ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
13691365
diffstat->files[diffstat->nr++] = x;
13701366
if (name_b) {
13711367
x->from_name = xstrdup(name_a);
@@ -3958,11 +3954,7 @@ struct diff_queue_struct diff_queued_diff;
39583954

39593955
void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
39603956
{
3961-
if (queue->alloc <= queue->nr) {
3962-
queue->alloc = alloc_nr(queue->alloc);
3963-
queue->queue = xrealloc(queue->queue,
3964-
sizeof(dp) * queue->alloc);
3965-
}
3957+
ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
39663958
queue->queue[queue->nr++] = dp;
39673959
}
39683960

diffcore-rename.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
3838
if (!insert_ok)
3939
return NULL;
4040
/* insert to make it at "first" */
41-
if (rename_dst_alloc <= rename_dst_nr) {
42-
rename_dst_alloc = alloc_nr(rename_dst_alloc);
43-
rename_dst = xrealloc(rename_dst,
44-
rename_dst_alloc * sizeof(*rename_dst));
45-
}
41+
ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
4642
rename_dst_nr++;
4743
if (first < rename_dst_nr)
4844
memmove(rename_dst + first + 1, rename_dst + first,
@@ -82,11 +78,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
8278
}
8379

8480
/* insert to make it at "first" */
85-
if (rename_src_alloc <= rename_src_nr) {
86-
rename_src_alloc = alloc_nr(rename_src_alloc);
87-
rename_src = xrealloc(rename_src,
88-
rename_src_alloc * sizeof(*rename_src));
89-
}
81+
ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
9082
rename_src_nr++;
9183
if (first < rename_src_nr)
9284
memmove(rename_src + first + 1, rename_src + first,

dir.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,10 +1364,7 @@ static struct path_simplify *create_simplify(const char **pathspec)
13641364

13651365
for (nr = 0 ; ; nr++) {
13661366
const char *match;
1367-
if (nr >= alloc) {
1368-
alloc = alloc_nr(alloc);
1369-
simplify = xrealloc(simplify, alloc * sizeof(*simplify));
1370-
}
1367+
ALLOC_GROW(simplify, nr + 1, alloc);
13711368
match = *pathspec++;
13721369
if (!match)
13731370
break;

patch-ids.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ static struct patch_id *add_commit(struct commit *commit,
8383
ent = &bucket->bucket[bucket->nr++];
8484
hashcpy(ent->patch_id, sha1);
8585

86-
if (ids->alloc <= ids->nr) {
87-
ids->alloc = alloc_nr(ids->nr);
88-
ids->table = xrealloc(ids->table, sizeof(ent) * ids->alloc);
89-
}
86+
ALLOC_GROW(ids->table, ids->nr + 1, ids->alloc);
9087
if (pos < ids->nr)
9188
memmove(ids->table + pos + 1, ids->table + pos,
9289
sizeof(ent) * (ids->nr - pos));

0 commit comments

Comments
 (0)