# [PATCH] Use ALLOC_GROW() instead of inline code

42 messages from 2014-02-27 to 2014-03-03. Participants: Dmitry S. Dolzhenko, Michael Haggerty, Junio C Hamano, Duy Nguyen, Jeff King, Eric Sunshine, He Sun.
Thread: https://gitlist.dev/t/35982

## Dmitry S. Dolzhenko, 2014-02-27 20:45

Subject: [PATCH] Use ALLOC_GROW() instead of inline code
Message-ID: <530FA3E7.8020200@yandex.ru>
URL: https://gitlist.dev/e/530FA3E7.8020200%40yandex.ru

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@gmail.com>
---
 attr.c                 |  7 +------
 builtin/pack-objects.c |  7 +------
 bundle.c               |  6 +-----
 cache-tree.c           |  6 +-----
 commit.c               |  8 ++------
 diff.c                 | 12 ++----------
 diffcore-rename.c      | 12 ++----------
 dir.c                  |  5 +----
 patch-ids.c            |  5 +----
 read-cache.c           |  9 ++-------
 reflog-walk.c          | 13 +++----------
 replace_object.c       |  8 ++------
 12 files changed, 19 insertions(+), 79 deletions(-)

diff --git a/attr.c b/attr.c
index 8d13d70..734222d 100644
--- a/attr.c
+++ b/attr.c
@@ -338,12 +338,7 @@ static void handle_attr_line(struct attr_stack *res,
 	a = parse_attr_line(line, src, lineno, macro_ok);
 	if (!a)
 		return;
-	if (res->alloc <= res->num_matches) {
-		res->alloc = alloc_nr(res->num_matches);
-		res->attrs = xrealloc(res->attrs,
-				      sizeof(struct match_attr *) *
-				      res->alloc);
-	}
+	ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
 	res->attrs[res->num_matches++] = a;
 }
 
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 541667f..92cbce8 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1156,12 +1156,7 @@ static int check_pbase_path(unsigned hash)
 	if (0 <= pos)
 		return 1;
 	pos = -pos - 1;
-	if (done_pbase_paths_alloc <= done_pbase_paths_num) {
-		done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
-		done_pbase_paths = xrealloc(done_pbase_paths,
-					    done_pbase_paths_alloc *
-					    sizeof(unsigned));
-	}
+	ALLOC_GROW(done_pbase_paths, done_pbase_paths_num + 1, done_pbase_paths_alloc);
 	done_pbase_paths_num++;
 	if (pos < done_pbase_paths_num)
 		memmove(done_pbase_paths + pos + 1,
diff --git a/bundle.c b/bundle.c
index e99065c..1388a3e 100644
--- a/bundle.c
+++ b/bundle.c
@@ -14,11 +14,7 @@ static const char bundle_signature[] = "# v2 git bundle\n";
 static void add_to_ref_list(const unsigned char *sha1, const char *name,
 		struct ref_list *list)
 {
-	if (list->nr + 1 >= list->alloc) {
-		list->alloc = alloc_nr(list->nr + 1);
-		list->list = xrealloc(list->list,
-				list->alloc * sizeof(list->list[0]));
-	}
+	ALLOC_GROW(list->list, list->nr + 1, list->alloc);
 	memcpy(list->list[list->nr].sha1, sha1, 20);
 	list->list[list->nr].name = xstrdup(name);
 	list->nr++;
diff --git a/cache-tree.c b/cache-tree.c
index 0bbec43..30149d1 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -75,11 +75,7 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
 		return NULL;
 
 	pos = -pos-1;
-	if (it->subtree_alloc <= it->subtree_nr) {
-		it->subtree_alloc = alloc_nr(it->subtree_alloc);
-		it->down = xrealloc(it->down, it->subtree_alloc *
-				    sizeof(*it->down));
-	}
+	ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
 	it->subtree_nr++;
 
 	down = xmalloc(sizeof(*down) + pathlen + 1);
diff --git a/commit.c b/commit.c
index 6bf4fe0..e004314 100644
--- a/commit.c
+++ b/commit.c
@@ -147,12 +147,8 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
 		return 1;
 	}
 	pos = -pos - 1;
-	if (commit_graft_alloc <= ++commit_graft_nr) {
-		commit_graft_alloc = alloc_nr(commit_graft_alloc);
-		commit_graft = xrealloc(commit_graft,
-					sizeof(*commit_graft) *
-					commit_graft_alloc);
-	}
+	ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
+	commit_graft_nr++;
 	if (pos < commit_graft_nr)
 		memmove(commit_graft + pos + 1,
 			commit_graft + pos,
diff --git a/diff.c b/diff.c
index 8e4a6a9..f5f0fd1 100644
--- a/diff.c
+++ b/diff.c
@@ -1361,11 +1361,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
 {
 	struct diffstat_file *x;
 	x = xcalloc(sizeof (*x), 1);
-	if (diffstat->nr == diffstat->alloc) {
-		diffstat->alloc = alloc_nr(diffstat->alloc);
-		diffstat->files = xrealloc(diffstat->files,
-				diffstat->alloc * sizeof(x));
-	}
+	ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
 	diffstat->files[diffstat->nr++] = x;
 	if (name_b) {
 		x->from_name = xstrdup(name_a);
@@ -3965,11 +3961,7 @@ struct diff_queue_struct diff_queued_diff;
 
 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
 {
-	if (queue->alloc <= queue->nr) {
-		queue->alloc = alloc_nr(queue->alloc);
-		queue->queue = xrealloc(queue->queue,
-					sizeof(dp) * queue->alloc);
-	}
+	ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
 	queue->queue[queue->nr++] = dp;
 }
 
diff --git a/diffcore-rename.c b/diffcore-rename.c
index 6c7a72f..f54d5bf 100644
--- a/diffcore-rename.c
+++ b/diffcore-rename.c
@@ -38,11 +38,7 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
 	if (!insert_ok)
 		return NULL;
 	/* insert to make it at "first" */
-	if (rename_dst_alloc <= rename_dst_nr) {
-		rename_dst_alloc = alloc_nr(rename_dst_alloc);
-		rename_dst = xrealloc(rename_dst,
-				      rename_dst_alloc * sizeof(*rename_dst));
-	}
+	ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
 	rename_dst_nr++;
 	if (first < rename_dst_nr)
 		memmove(rename_dst + first + 1, rename_dst + first,
@@ -82,11 +78,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
 	}
 
 	/* insert to make it at "first" */
-	if (rename_src_alloc <= rename_src_nr) {
-		rename_src_alloc = alloc_nr(rename_src_alloc);
-		rename_src = xrealloc(rename_src,
-				      rename_src_alloc * sizeof(*rename_src));
-	}
+	ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
 	rename_src_nr++;
 	if (first < rename_src_nr)
 		memmove(rename_src + first + 1, rename_src + first,
diff --git a/dir.c b/dir.c
index b35b633..72f6e2a 100644
--- a/dir.c
+++ b/dir.c
@@ -1329,13 +1329,10 @@ static struct path_simplify *create_simplify(const char **pathspec)
 
 	for (nr = 0 ; ; nr++) {
 		const char *match;
-		if (nr >= alloc) {
-			alloc = alloc_nr(alloc);
-			simplify = xrealloc(simplify, alloc * sizeof(*simplify));
-		}
 		match = *pathspec++;
 		if (!match)
 			break;
+		ALLOC_GROW(simplify, nr + 1, alloc);
 		simplify[nr].path = match;
 		simplify[nr].len = simple_length(match);
 	}
diff --git a/patch-ids.c b/patch-ids.c
index bc8a28f..bf81b92 100644
--- a/patch-ids.c
+++ b/patch-ids.c
@@ -83,10 +83,7 @@ static struct patch_id *add_commit(struct commit *commit,
 	ent = &bucket->bucket[bucket->nr++];
 	hashcpy(ent->patch_id, sha1);
 
-	if (ids->alloc <= ids->nr) {
-		ids->alloc = alloc_nr(ids->nr);
-		ids->table = xrealloc(ids->table, sizeof(ent) * ids->alloc);
-	}
+	ALLOC_GROW(ids->table, ids->nr + 1, ids->alloc);
 	if (pos < ids->nr)
 		memmove(ids->table + pos + 1, ids->table + pos,
 			sizeof(ent) * (ids->nr - pos));
diff --git a/read-cache.c b/read-cache.c
index 33dd676..e585541 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -993,11 +993,7 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti
 	}
 
 	/* Make sure the array is big enough .. */
-	if (istate->cache_nr == istate->cache_alloc) {
-		istate->cache_alloc = alloc_nr(istate->cache_alloc);
-		istate->cache = xrealloc(istate->cache,
-					istate->cache_alloc * sizeof(*istate->cache));
-	}
+	ALLOC_GROW(istate->cache, istate->cache_nr + 1, istate->cache_alloc);
 
 	/* Add it in.. */
 	istate->cache_nr++;
@@ -1466,8 +1462,7 @@ int read_index_from(struct index_state *istate, const char *path)
 
 	istate->version = ntohl(hdr->hdr_version);
 	istate->cache_nr = ntohl(hdr->hdr_entries);
-	istate->cache_alloc = alloc_nr(istate->cache_nr);
-	istate->cache = xcalloc(istate->cache_alloc, sizeof(*istate->cache));
+	ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
 	istate->initialized = 1;
 
 	if (istate->version == 4)
diff --git a/reflog-walk.c b/reflog-walk.c
index b2fbdb2..879d2ed 100644
--- a/reflog-walk.c
+++ b/reflog-walk.c
@@ -26,11 +26,7 @@ static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,
 	struct complete_reflogs *array = cb_data;
 	struct reflog_info *item;
 
-	if (array->nr >= array->alloc) {
-		array->alloc = alloc_nr(array->nr + 1);
-		array->items = xrealloc(array->items, array->alloc *
-			sizeof(struct reflog_info));
-	}
+	ALLOC_GROW(array->items, array->nr + 1, array->alloc);
 	item = array->items + array->nr;
 	memcpy(item->osha1, osha1, 20);
 	memcpy(item->nsha1, nsha1, 20);
@@ -114,11 +110,8 @@ static void add_commit_info(struct commit *commit, void *util,
 		struct commit_info_lifo *lifo)
 {
 	struct commit_info *info;
-	if (lifo->nr >= lifo->alloc) {
-		lifo->alloc = alloc_nr(lifo->nr + 1);
-		lifo->items = xrealloc(lifo->items,
-			lifo->alloc * sizeof(struct commit_info));
-	}
+
+	ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
 	info = lifo->items + lifo->nr;
 	info->commit = commit;
 	info->util = util;
diff --git a/replace_object.c b/replace_object.c
index cdcaf8c..843deef 100644
--- a/replace_object.c
+++ b/replace_object.c
@@ -36,12 +36,8 @@ static int register_replace_object(struct replace_object *replace,
 		return 1;
 	}
 	pos = -pos - 1;
-	if (replace_object_alloc <= ++replace_object_nr) {
-		replace_object_alloc = alloc_nr(replace_object_alloc);
-		replace_object = xrealloc(replace_object,
-					  sizeof(*replace_object) *
-					  replace_object_alloc);
-	}
+	ALLOC_GROW(replace_object, replace_object_nr + 1, replace_object_alloc);
+	replace_object_nr++;
 	if (pos < replace_object_nr)
 		memmove(replace_object + pos + 1,
 			replace_object + pos,
-- 
1.8.3.2

```

## Michael Haggerty, 2014-02-27 22:11

Subject: Re: [PATCH] Use ALLOC_GROW() instead of inline code
Message-ID: <530FB82C.70005@alum.mit.edu>
URL: https://gitlist.dev/e/530FB82C.70005%40alum.mit.edu
In-Reply-To: <530FA3E7.8020200@yandex.ru>

```
Dmitry,

That's cool; I never imagined there would be so many sites that could be
cleaned up in this way.

In my opinion, it would be preferable for this patch to be broken into
multiple commits, one for each site (or each file, if a file has
multiple sites that are logically related).  That would make it easier
to review the patches and easier to bisect if we later find a problem.
Please make sure that you note if there are any sites where the
rewritten code doesn't have exactly the same semantics as the original
(I don't know if there are sites like this, but if there are...).  That
helps reviewers focus on the changes that might be "controversial".

[Please leave the other microprojects for other students (I just wrote
an email on this topic).]

Thanks,
Michael

On 02/27/2014 09:45 PM, Dmitry S. Dolzhenko wrote:
> Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@gmail.com>
> ---
>  attr.c                 |  7 +------
>  builtin/pack-objects.c |  7 +------
>  bundle.c               |  6 +-----
>  cache-tree.c           |  6 +-----
>  commit.c               |  8 ++------
>  diff.c                 | 12 ++----------
>  diffcore-rename.c      | 12 ++----------
>  dir.c                  |  5 +----
>  patch-ids.c            |  5 +----
>  read-cache.c           |  9 ++-------
>  reflog-walk.c          | 13 +++----------
>  replace_object.c       |  8 ++------
>  12 files changed, 19 insertions(+), 79 deletions(-)
> 
> diff --git a/attr.c b/attr.c
> index 8d13d70..734222d 100644
> --- a/attr.c
> +++ b/attr.c
> @@ -338,12 +338,7 @@ static void handle_attr_line(struct attr_stack *res,
>  	a = parse_attr_line(line, src, lineno, macro_ok);
>  	if (!a)
>  		return;
> -	if (res->alloc <= res->num_matches) {
> -		res->alloc = alloc_nr(res->num_matches);
> -		res->attrs = xrealloc(res->attrs,
> -				      sizeof(struct match_attr *) *
> -				      res->alloc);
> -	}
> +	ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
>  	res->attrs[res->num_matches++] = a;
>  }
>  
> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
> index 541667f..92cbce8 100644
> --- a/builtin/pack-objects.c
> +++ b/builtin/pack-objects.c
> @@ -1156,12 +1156,7 @@ static int check_pbase_path(unsigned hash)
>  	if (0 <= pos)
>  		return 1;
>  	pos = -pos - 1;
> -	if (done_pbase_paths_alloc <= done_pbase_paths_num) {
> -		done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
> -		done_pbase_paths = xrealloc(done_pbase_paths,
> -					    done_pbase_paths_alloc *
> -					    sizeof(unsigned));
> -	}
> +	ALLOC_GROW(done_pbase_paths, done_pbase_paths_num + 1, done_pbase_paths_alloc);
>  	done_pbase_paths_num++;
>  	if (pos < done_pbase_paths_num)
>  		memmove(done_pbase_paths + pos + 1,
> diff --git a/bundle.c b/bundle.c
> index e99065c..1388a3e 100644
> --- a/bundle.c
> +++ b/bundle.c
> @@ -14,11 +14,7 @@ static const char bundle_signature[] = "# v2 git bundle\n";
>  static void add_to_ref_list(const unsigned char *sha1, const char *name,
>  		struct ref_list *list)
>  {
> -	if (list->nr + 1 >= list->alloc) {
> -		list->alloc = alloc_nr(list->nr + 1);
> -		list->list = xrealloc(list->list,
> -				list->alloc * sizeof(list->list[0]));
> -	}
> +	ALLOC_GROW(list->list, list->nr + 1, list->alloc);
>  	memcpy(list->list[list->nr].sha1, sha1, 20);
>  	list->list[list->nr].name = xstrdup(name);
>  	list->nr++;
> diff --git a/cache-tree.c b/cache-tree.c
> index 0bbec43..30149d1 100644
> --- a/cache-tree.c
> +++ b/cache-tree.c
> @@ -75,11 +75,7 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
>  		return NULL;
>  
>  	pos = -pos-1;
> -	if (it->subtree_alloc <= it->subtree_nr) {
> -		it->subtree_alloc = alloc_nr(it->subtree_alloc);
> -		it->down = xrealloc(it->down, it->subtree_alloc *
> -				    sizeof(*it->down));
> -	}
> +	ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
>  	it->subtree_nr++;
>  
>  	down = xmalloc(sizeof(*down) + pathlen + 1);
> diff --git a/commit.c b/commit.c
> index 6bf4fe0..e004314 100644
> --- a/commit.c
> +++ b/commit.c
> @@ -147,12 +147,8 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
>  		return 1;
>  	}
>  	pos = -pos - 1;
> -	if (commit_graft_alloc <= ++commit_graft_nr) {
> -		commit_graft_alloc = alloc_nr(commit_graft_alloc);
> -		commit_graft = xrealloc(commit_graft,
> -					sizeof(*commit_graft) *
> -					commit_graft_alloc);
> -	}
> +	ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
> +	commit_graft_nr++;
>  	if (pos < commit_graft_nr)
>  		memmove(commit_graft + pos + 1,
>  			commit_graft + pos,
> diff --git a/diff.c b/diff.c
> index 8e4a6a9..f5f0fd1 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -1361,11 +1361,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
>  {
>  	struct diffstat_file *x;
>  	x = xcalloc(sizeof (*x), 1);
> -	if (diffstat->nr == diffstat->alloc) {
> -		diffstat->alloc = alloc_nr(diffstat->alloc);
> -		diffstat->files = xrealloc(diffstat->files,
> -				diffstat->alloc * sizeof(x));
> -	}
> +	ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
>  	diffstat->files[diffstat->nr++] = x;
>  	if (name_b) {
>  		x->from_name = xstrdup(name_a);
> @@ -3965,11 +3961,7 @@ struct diff_queue_struct diff_queued_diff;
>  
>  void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
>  {
> -	if (queue->alloc <= queue->nr) {
> -		queue->alloc = alloc_nr(queue->alloc);
> -		queue->queue = xrealloc(queue->queue,
> -					sizeof(dp) * queue->alloc);
> -	}
> +	ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
>  	queue->queue[queue->nr++] = dp;
>  }
>  
> diff --git a/diffcore-rename.c b/diffcore-rename.c
> index 6c7a72f..f54d5bf 100644
> --- a/diffcore-rename.c
> +++ b/diffcore-rename.c
> @@ -38,11 +38,7 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
>  	if (!insert_ok)
>  		return NULL;
>  	/* insert to make it at "first" */
> -	if (rename_dst_alloc <= rename_dst_nr) {
> -		rename_dst_alloc = alloc_nr(rename_dst_alloc);
> -		rename_dst = xrealloc(rename_dst,
> -				      rename_dst_alloc * sizeof(*rename_dst));
> -	}
> +	ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
>  	rename_dst_nr++;
>  	if (first < rename_dst_nr)
>  		memmove(rename_dst + first + 1, rename_dst + first,
> @@ -82,11 +78,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
>  	}
>  
>  	/* insert to make it at "first" */
> -	if (rename_src_alloc <= rename_src_nr) {
> -		rename_src_alloc = alloc_nr(rename_src_alloc);
> -		rename_src = xrealloc(rename_src,
> -				      rename_src_alloc * sizeof(*rename_src));
> -	}
> +	ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
>  	rename_src_nr++;
>  	if (first < rename_src_nr)
>  		memmove(rename_src + first + 1, rename_src + first,
> diff --git a/dir.c b/dir.c
> index b35b633..72f6e2a 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -1329,13 +1329,10 @@ static struct path_simplify *create_simplify(const char **pathspec)
>  
>  	for (nr = 0 ; ; nr++) {
>  		const char *match;
> -		if (nr >= alloc) {
> -			alloc = alloc_nr(alloc);
> -			simplify = xrealloc(simplify, alloc * sizeof(*simplify));
> -		}
>  		match = *pathspec++;
>  		if (!match)
>  			break;
> +		ALLOC_GROW(simplify, nr + 1, alloc);
>  		simplify[nr].path = match;
>  		simplify[nr].len = simple_length(match);
>  	}
> diff --git a/patch-ids.c b/patch-ids.c
> index bc8a28f..bf81b92 100644
> --- a/patch-ids.c
> +++ b/patch-ids.c
> @@ -83,10 +83,7 @@ static struct patch_id *add_commit(struct commit *commit,
>  	ent = &bucket->bucket[bucket->nr++];
>  	hashcpy(ent->patch_id, sha1);
>  
> -	if (ids->alloc <= ids->nr) {
> -		ids->alloc = alloc_nr(ids->nr);
> -		ids->table = xrealloc(ids->table, sizeof(ent) * ids->alloc);
> -	}
> +	ALLOC_GROW(ids->table, ids->nr + 1, ids->alloc);
>  	if (pos < ids->nr)
>  		memmove(ids->table + pos + 1, ids->table + pos,
>  			sizeof(ent) * (ids->nr - pos));
> diff --git a/read-cache.c b/read-cache.c
> index 33dd676..e585541 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -993,11 +993,7 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti
>  	}
>  
>  	/* Make sure the array is big enough .. */
> -	if (istate->cache_nr == istate->cache_alloc) {
> -		istate->cache_alloc = alloc_nr(istate->cache_alloc);
> -		istate->cache = xrealloc(istate->cache,
> -					istate->cache_alloc * sizeof(*istate->cache));
> -	}
> +	ALLOC_GROW(istate->cache, istate->cache_nr + 1, istate->cache_alloc);
>  
>  	/* Add it in.. */
>  	istate->cache_nr++;
> @@ -1466,8 +1462,7 @@ int read_index_from(struct index_state *istate, const char *path)
>  
>  	istate->version = ntohl(hdr->hdr_version);
>  	istate->cache_nr = ntohl(hdr->hdr_entries);
> -	istate->cache_alloc = alloc_nr(istate->cache_nr);
> -	istate->cache = xcalloc(istate->cache_alloc, sizeof(*istate->cache));
> +	ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
>  	istate->initialized = 1;
>  
>  	if (istate->version == 4)
> diff --git a/reflog-walk.c b/reflog-walk.c
> index b2fbdb2..879d2ed 100644
> --- a/reflog-walk.c
> +++ b/reflog-walk.c
> @@ -26,11 +26,7 @@ static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,
>  	struct complete_reflogs *array = cb_data;
>  	struct reflog_info *item;
>  
> -	if (array->nr >= array->alloc) {
> -		array->alloc = alloc_nr(array->nr + 1);
> -		array->items = xrealloc(array->items, array->alloc *
> -			sizeof(struct reflog_info));
> -	}
> +	ALLOC_GROW(array->items, array->nr + 1, array->alloc);
>  	item = array->items + array->nr;
>  	memcpy(item->osha1, osha1, 20);
>  	memcpy(item->nsha1, nsha1, 20);
> @@ -114,11 +110,8 @@ static void add_commit_info(struct commit *commit, void *util,
>  		struct commit_info_lifo *lifo)
>  {
>  	struct commit_info *info;
> -	if (lifo->nr >= lifo->alloc) {
> -		lifo->alloc = alloc_nr(lifo->nr + 1);
> -		lifo->items = xrealloc(lifo->items,
> -			lifo->alloc * sizeof(struct commit_info));
> -	}
> +
> +	ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
>  	info = lifo->items + lifo->nr;
>  	info->commit = commit;
>  	info->util = util;
> diff --git a/replace_object.c b/replace_object.c
> index cdcaf8c..843deef 100644
> --- a/replace_object.c
> +++ b/replace_object.c
> @@ -36,12 +36,8 @@ static int register_replace_object(struct replace_object *replace,
>  		return 1;
>  	}
>  	pos = -pos - 1;
> -	if (replace_object_alloc <= ++replace_object_nr) {
> -		replace_object_alloc = alloc_nr(replace_object_alloc);
> -		replace_object = xrealloc(replace_object,
> -					  sizeof(*replace_object) *
> -					  replace_object_alloc);
> -	}
> +	ALLOC_GROW(replace_object, replace_object_nr + 1, replace_object_alloc);
> +	replace_object_nr++;
>  	if (pos < replace_object_nr)
>  		memmove(replace_object + pos + 1,
>  			replace_object + pos,
> 


-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/

```

## Junio C Hamano, 2014-02-27 22:44

Subject: Re: [PATCH] Use ALLOC_GROW() instead of inline code
Message-ID: <xmqqr46ow4zs.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqr46ow4zs.fsf%40gitster.dls.corp.google.com
In-Reply-To: <530FA3E7.8020200@yandex.ru>

```
"Dmitry S. Dolzhenko" <dmitrys.dolzhenko@yandex.ru> writes:

> diff --git a/dir.c b/dir.c
> index b35b633..72f6e2a 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -1329,13 +1329,10 @@ static struct path_simplify *create_simplify(const char **pathspec)
>  
>  	for (nr = 0 ; ; nr++) {
>  		const char *match;
> -		if (nr >= alloc) {
> -			alloc = alloc_nr(alloc);
> -			simplify = xrealloc(simplify, alloc * sizeof(*simplify));
> -		}
>  		match = *pathspec++;
>  		if (!match)
>  			break;
> +		ALLOC_GROW(simplify, nr + 1, alloc);
>  		simplify[nr].path = match;
>  		simplify[nr].len = simple_length(match);
>  	}

What follows the post-context of this hunk is a NULL termination of
the array:

	simplify[nr].path = NULL;
        simplify[nr].len = 0;

If the first element in pathspec[] were NULL, we set nr to 0, break
the loop without calling ALLOC_GROW() even once, and try to NULL
terminate simplify[] array after the loop.

Don't we try to store to an unallocated piece of memory with this
change?

> diff --git a/read-cache.c b/read-cache.c
> index 33dd676..e585541 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -1466,8 +1462,7 @@ int read_index_from(struct index_state *istate, const char *path)
>  
>  	istate->version = ntohl(hdr->hdr_version);
>  	istate->cache_nr = ntohl(hdr->hdr_entries);
> -	istate->cache_alloc = alloc_nr(istate->cache_nr);
> -	istate->cache = xcalloc(istate->cache_alloc, sizeof(*istate->cache));
> +	ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);

This being the initial allocation, not growing reallocation, use of
ALLOC_GROW() looks somewhat strange.  I know that an realloc from
NULL ends up being the same as calloc(), but still.

```

## Dmitry S. Dolzhenko, 2014-02-28 09:29

Subject: [PATCH v2 00/11] Use ALLOC_GROW() instead of inline code
Message-ID: <531056E7.3010305@yandex.ru>
URL: https://gitlist.dev/e/531056E7.3010305%40yandex.ru
In-Reply-To: <530FA3E7.8020200@yandex.ru>

```
Thank you for your remarks. In this patch I tried to take them into account.

Dmitry S. Dolzhenko (11):
  builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
  bundle.c: change add_to_ref_list() to use ALLOC_GROW()
  cache-tree.c: change find_subtree() to use ALLOC_GROW()
  commit.c: change register_commit_graft() to use ALLOC_GROW()
  diff.c: use ALLOC_GROW() instead of inline code
  diffcore-rename.c: use ALLOC_GROW() instead of inline code
  patch-ids.c: change add_commit() to use ALLOC_GROW()
  replace_object.c: change register_replace_object() to use ALLOC_GROW()
  reflog-walk.c: use ALLOC_GROW() instead of inline code
  dir.c: change create_simplify() to use ALLOC_GROW()
  attr.c: change handle_attr_line() to use ALLOC_GROW()

 attr.c                 |  7 +------
 builtin/pack-objects.c |  7 +------
 bundle.c               |  6 +-----
 cache-tree.c           |  6 +-----
 commit.c               |  8 ++------
 diff.c                 | 12 ++----------
 diffcore-rename.c      | 12 ++----------
 dir.c                  |  5 +----
 patch-ids.c            |  5 +----
 reflog-walk.c          | 13 +++----------
 replace_object.c       |  8 ++------
 11 files changed, 17 insertions(+), 72 deletions(-)

-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:40

Subject: [PATCH v2 01/11] builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
Message-ID: <53105995.3010001@yandex.ru>
URL: https://gitlist.dev/e/53105995.3010001%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 builtin/pack-objects.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index c733379..56a6fc8 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1213,12 +1213,7 @@ static int check_pbase_path(unsigned hash)
 	if (0 <= pos)
 		return 1;
 	pos = -pos - 1;
-	if (done_pbase_paths_alloc <= done_pbase_paths_num) {
-		done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
-		done_pbase_paths = xrealloc(done_pbase_paths,
-					    done_pbase_paths_alloc *
-					    sizeof(unsigned));
-	}
+	ALLOC_GROW(done_pbase_paths, done_pbase_paths_num + 1, done_pbase_paths_alloc);
 	done_pbase_paths_num++;
 	if (pos < done_pbase_paths_num)
 		memmove(done_pbase_paths + pos + 1,
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:41

Subject: [PATCH v2 02/11] bundle.c: change add_to_ref_list() to use ALLOC_GROW()
Message-ID: <531059CA.4020700@yandex.ru>
URL: https://gitlist.dev/e/531059CA.4020700%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 bundle.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/bundle.c b/bundle.c
index e99065c..1388a3e 100644
--- a/bundle.c
+++ b/bundle.c
@@ -14,11 +14,7 @@ static const char bundle_signature[] = "# v2 git bundle\n";
 static void add_to_ref_list(const unsigned char *sha1, const char *name,
 		struct ref_list *list)
 {
-	if (list->nr + 1 >= list->alloc) {
-		list->alloc = alloc_nr(list->nr + 1);
-		list->list = xrealloc(list->list,
-				list->alloc * sizeof(list->list[0]));
-	}
+	ALLOC_GROW(list->list, list->nr + 1, list->alloc);
 	memcpy(list->list[list->nr].sha1, sha1, 20);
 	list->list[list->nr].name = xstrdup(name);
 	list->nr++;
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:41

Subject: [PATCH v2 03/11] cache-tree.c: change find_subtree() to use ALLOC_GROW()
Message-ID: <531059E7.4030406@yandex.ru>
URL: https://gitlist.dev/e/531059E7.4030406%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 cache-tree.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/cache-tree.c b/cache-tree.c
index 0bbec43..30149d1 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -75,11 +75,7 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
 		return NULL;
 
 	pos = -pos-1;
-	if (it->subtree_alloc <= it->subtree_nr) {
-		it->subtree_alloc = alloc_nr(it->subtree_alloc);
-		it->down = xrealloc(it->down, it->subtree_alloc *
-				    sizeof(*it->down));
-	}
+	ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
 	it->subtree_nr++;
 
 	down = xmalloc(sizeof(*down) + pathlen + 1);
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:42

Subject: [PATCH v2 04/11] commit.c: change register_commit_graft() to use ALLOC_GROW()
Message-ID: <53105A13.9070704@yandex.ru>
URL: https://gitlist.dev/e/53105A13.9070704%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 commit.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/commit.c b/commit.c
index 6bf4fe0..e004314 100644
--- a/commit.c
+++ b/commit.c
@@ -147,12 +147,8 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
 		return 1;
 	}
 	pos = -pos - 1;
-	if (commit_graft_alloc <= ++commit_graft_nr) {
-		commit_graft_alloc = alloc_nr(commit_graft_alloc);
-		commit_graft = xrealloc(commit_graft,
-					sizeof(*commit_graft) *
-					commit_graft_alloc);
-	}
+	ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
+	commit_graft_nr++;
 	if (pos < commit_graft_nr)
 		memmove(commit_graft + pos + 1,
 			commit_graft + pos,
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:43

Subject: [PATCH v2 05/11] diff.c: use ALLOC_GROW() instead of inline code
Message-ID: <53105A37.5060603@yandex.ru>
URL: https://gitlist.dev/e/53105A37.5060603%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 diff.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/diff.c b/diff.c
index e800666..aebdfda 100644
--- a/diff.c
+++ b/diff.c
@@ -1361,11 +1361,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
 {
 	struct diffstat_file *x;
 	x = xcalloc(sizeof (*x), 1);
-	if (diffstat->nr == diffstat->alloc) {
-		diffstat->alloc = alloc_nr(diffstat->alloc);
-		diffstat->files = xrealloc(diffstat->files,
-				diffstat->alloc * sizeof(x));
-	}
+	ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
 	diffstat->files[diffstat->nr++] = x;
 	if (name_b) {
 		x->from_name = xstrdup(name_a);
@@ -3965,11 +3961,7 @@ struct diff_queue_struct diff_queued_diff;
 
 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
 {
-	if (queue->alloc <= queue->nr) {
-		queue->alloc = alloc_nr(queue->alloc);
-		queue->queue = xrealloc(queue->queue,
-					sizeof(dp) * queue->alloc);
-	}
+	ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
 	queue->queue[queue->nr++] = dp;
 }
 
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:44

Subject: [PATCH v2 06/11] diffcore-rename.c: use ALLOC_GROW() instead of inline code
Message-ID: <53105A6A.30200@yandex.ru>
URL: https://gitlist.dev/e/53105A6A.30200%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Affected functions: locate_rename_dst(), register_rename_src()

Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 diffcore-rename.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/diffcore-rename.c b/diffcore-rename.c
index 9b4f068..fbf3272 100644
--- a/diffcore-rename.c
+++ b/diffcore-rename.c
@@ -38,11 +38,7 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
 	if (!insert_ok)
 		return NULL;
 	/* insert to make it at "first" */
-	if (rename_dst_alloc <= rename_dst_nr) {
-		rename_dst_alloc = alloc_nr(rename_dst_alloc);
-		rename_dst = xrealloc(rename_dst,
-				      rename_dst_alloc * sizeof(*rename_dst));
-	}
+	ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
 	rename_dst_nr++;
 	if (first < rename_dst_nr)
 		memmove(rename_dst + first + 1, rename_dst + first,
@@ -82,11 +78,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
 	}
 
 	/* insert to make it at "first" */
-	if (rename_src_alloc <= rename_src_nr) {
-		rename_src_alloc = alloc_nr(rename_src_alloc);
-		rename_src = xrealloc(rename_src,
-				      rename_src_alloc * sizeof(*rename_src));
-	}
+	ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
 	rename_src_nr++;
 	if (first < rename_src_nr)
 		memmove(rename_src + first + 1, rename_src + first,
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:45

Subject: [PATCH v2 07/11] patch-ids.c: change add_commit() to use ALLOC_GROW()
Message-ID: <53105A9D.9040400@yandex.ru>
URL: https://gitlist.dev/e/53105A9D.9040400%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 patch-ids.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/patch-ids.c b/patch-ids.c
index bc8a28f..bf81b92 100644
--- a/patch-ids.c
+++ b/patch-ids.c
@@ -83,10 +83,7 @@ static struct patch_id *add_commit(struct commit *commit,
 	ent = &bucket->bucket[bucket->nr++];
 	hashcpy(ent->patch_id, sha1);
 
-	if (ids->alloc <= ids->nr) {
-		ids->alloc = alloc_nr(ids->nr);
-		ids->table = xrealloc(ids->table, sizeof(ent) * ids->alloc);
-	}
+	ALLOC_GROW(ids->table, ids->nr + 1, ids->alloc);
 	if (pos < ids->nr)
 		memmove(ids->table + pos + 1, ids->table + pos,
 			sizeof(ent) * (ids->nr - pos));
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:45

Subject: [PATCH v2 08/11] replace_object.c: change register_replace_object() to use ALLOC_GROW()
Message-ID: <53105AC5.8000607@yandex.ru>
URL: https://gitlist.dev/e/53105AC5.8000607%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 replace_object.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/replace_object.c b/replace_object.c
index cdcaf8c..843deef 100644
--- a/replace_object.c
+++ b/replace_object.c
@@ -36,12 +36,8 @@ static int register_replace_object(struct replace_object *replace,
 		return 1;
 	}
 	pos = -pos - 1;
-	if (replace_object_alloc <= ++replace_object_nr) {
-		replace_object_alloc = alloc_nr(replace_object_alloc);
-		replace_object = xrealloc(replace_object,
-					  sizeof(*replace_object) *
-					  replace_object_alloc);
-	}
+	ALLOC_GROW(replace_object, replace_object_nr + 1, replace_object_alloc);
+	replace_object_nr++;
 	if (pos < replace_object_nr)
 		memmove(replace_object + pos + 1,
 			replace_object + pos,
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:46

Subject: [PATCH v2 09/11] reflog-walk.c: use ALLOC_GROW() instead of inline code
Message-ID: <53105AF2.3070301@yandex.ru>
URL: https://gitlist.dev/e/53105AF2.3070301%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Affected functions: read_one_reflog(), add_commit_info()

Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 reflog-walk.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/reflog-walk.c b/reflog-walk.c
index b2fbdb2..879d2ed 100644
--- a/reflog-walk.c
+++ b/reflog-walk.c
@@ -26,11 +26,7 @@ static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,
 	struct complete_reflogs *array = cb_data;
 	struct reflog_info *item;
 
-	if (array->nr >= array->alloc) {
-		array->alloc = alloc_nr(array->nr + 1);
-		array->items = xrealloc(array->items, array->alloc *
-			sizeof(struct reflog_info));
-	}
+	ALLOC_GROW(array->items, array->nr + 1, array->alloc);
 	item = array->items + array->nr;
 	memcpy(item->osha1, osha1, 20);
 	memcpy(item->nsha1, nsha1, 20);
@@ -114,11 +110,8 @@ static void add_commit_info(struct commit *commit, void *util,
 		struct commit_info_lifo *lifo)
 {
 	struct commit_info *info;
-	if (lifo->nr >= lifo->alloc) {
-		lifo->alloc = alloc_nr(lifo->nr + 1);
-		lifo->items = xrealloc(lifo->items,
-			lifo->alloc * sizeof(struct commit_info));
-	}
+
+	ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
 	info = lifo->items + lifo->nr;
 	info->commit = commit;
 	info->util = util;
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:46

Subject: [PATCH v2 10/11] dir.c: change create_simplify() to use ALLOC_GROW()
Message-ID: <53105B0D.2090407@yandex.ru>
URL: https://gitlist.dev/e/53105B0D.2090407%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 dir.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/dir.c b/dir.c
index 98bb50f..4ae38e4 100644
--- a/dir.c
+++ b/dir.c
@@ -1341,10 +1341,7 @@ static struct path_simplify *create_simplify(const char **pathspec)
 
 	for (nr = 0 ; ; nr++) {
 		const char *match;
-		if (nr >= alloc) {
-			alloc = alloc_nr(alloc);
-			simplify = xrealloc(simplify, alloc * sizeof(*simplify));
-		}
+		ALLOC_GROW(simplify, nr + 1, alloc);
 		match = *pathspec++;
 		if (!match)
 			break;
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-02-28 09:47

Subject: [PATCH v2 11/11] attr.c: change handle_attr_line() to use ALLOC_GROW()
Message-ID: <53105B30.7010608@yandex.ru>
URL: https://gitlist.dev/e/53105B30.7010608%40yandex.ru
In-Reply-To: <531056E7.3010305@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 attr.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/attr.c b/attr.c
index 8d13d70..734222d 100644
--- a/attr.c
+++ b/attr.c
@@ -338,12 +338,7 @@ static void handle_attr_line(struct attr_stack *res,
 	a = parse_attr_line(line, src, lineno, macro_ok);
 	if (!a)
 		return;
-	if (res->alloc <= res->num_matches) {
-		res->alloc = alloc_nr(res->num_matches);
-		res->attrs = xrealloc(res->attrs,
-				      sizeof(struct match_attr *) *
-				      res->alloc);
-	}
+	ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
 	res->attrs[res->num_matches++] = a;
 }
 
-- 
1.8.5.3

```

## Duy Nguyen, 2014-02-28 12:32

Subject: Re: [PATCH v2 01/11] builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
Message-ID: <CACsJy8C2h13JFqh=CKvR=3TByHkxWCNR-XhK-WxA+DOE3GmvAQ@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8C2h13JFqh%3DCKvR%3D3TByHkxWCNR-XhK-WxA%2BDOE3GmvAQ%40mail.gmail.com
In-Reply-To: <53105995.3010001@yandex.ru>

```
On Fri, Feb 28, 2014 at 4:40 PM, Dmitry S. Dolzhenko
<dmitrys.dolzhenko@yandex.ru> wrote:
> Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
> ---
>  builtin/pack-objects.c | 7 +------
>  1 file changed, 1 insertion(+), 6 deletions(-)
>
> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
> index c733379..56a6fc8 100644
> --- a/builtin/pack-objects.c
> +++ b/builtin/pack-objects.c
> @@ -1213,12 +1213,7 @@ static int check_pbase_path(unsigned hash)
>         if (0 <= pos)
>                 return 1;
>         pos = -pos - 1;
> -       if (done_pbase_paths_alloc <= done_pbase_paths_num) {
> -               done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
> -               done_pbase_paths = xrealloc(done_pbase_paths,
> -                                           done_pbase_paths_alloc *
> -                                           sizeof(unsigned));
> -       }
> +       ALLOC_GROW(done_pbase_paths, done_pbase_paths_num + 1, done_pbase_paths_alloc);

Not strictly a rule, but I usually try to keep it within 80 columns,
unless the surrounding code already breaks it.

>         done_pbase_paths_num++;

If you move this up one line, then you don't have to "+ 1" in ALLOC_GROW

>         if (pos < done_pbase_paths_num)
>                 memmove(done_pbase_paths + pos + 1,
> --
> 1.8.5.3
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html



-- 
Duy

```

## Duy Nguyen, 2014-02-28 12:39

Subject: Re: [PATCH v2 09/11] reflog-walk.c: use ALLOC_GROW() instead of inline code
Message-ID: <CACsJy8DKXYJc2k6Zsqd0ruF_Jex=FH8rWzmhax2Co8z_oa+7zw@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8DKXYJc2k6Zsqd0ruF_Jex%3DFH8rWzmhax2Co8z_oa%2B7zw%40mail.gmail.com
In-Reply-To: <53105AF2.3070301@yandex.ru>

```
On Fri, Feb 28, 2014 at 4:46 PM, Dmitry S. Dolzhenko
<dmitrys.dolzhenko@yandex.ru> wrote:
> Affected functions: read_one_reflog(), add_commit_info()

We can usually see this from @@ line so it's not really needed to
describe. Same comment for a few other patches.
-- 
Duy

```

## Duy Nguyen, 2014-02-28 12:40

Subject: Re: [PATCH v2 01/11] builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
Message-ID: <CACsJy8AmQeVb-i6Sn0BH-_ikEXPHTdtKnQRyzChX1WfD9Lj4Jw@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8AmQeVb-i6Sn0BH-_ikEXPHTdtKnQRyzChX1WfD9Lj4Jw%40mail.gmail.com
In-Reply-To: <CACsJy8C2h13JFqh=CKvR=3TByHkxWCNR-XhK-WxA+DOE3GmvAQ@mail.gmail.com>

```
On Fri, Feb 28, 2014 at 7:32 PM, Duy Nguyen <pclouds@gmail.com> wrote:
>>         done_pbase_paths_num++;
>
> If you move this up one line, then you don't have to "+ 1" in ALLOC_GROW
>

same comment to a few other patches. The rest of your series looks good.
-- 
Duy

```

## Michael Haggerty, 2014-02-28 14:20

Subject: Re: [PATCH v2 01/11] builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
Message-ID: <53109B19.8070103@alum.mit.edu>
URL: https://gitlist.dev/e/53109B19.8070103%40alum.mit.edu
In-Reply-To: <CACsJy8AmQeVb-i6Sn0BH-_ikEXPHTdtKnQRyzChX1WfD9Lj4Jw@mail.gmail.com>

```
On 02/28/2014 01:40 PM, Duy Nguyen wrote:
> On Fri, Feb 28, 2014 at 7:32 PM, Duy Nguyen <pclouds@gmail.com> wrote:
>>>         done_pbase_paths_num++;
>>
>> If you move this up one line, then you don't have to "+ 1" in ALLOC_GROW
>>
> 
> same comment to a few other patches. The rest of your series looks good.

Duy,

The example in Documentation/technical/api-allocation-growing.txt does
it the same way as Dmitry:

    ALLOC_GROW(item, nr + 1, alloc);
    item[nr++] = value you like;

The alternative,

    nr++;
    ALLOC_GROW(item, nr, alloc);
    item[nr] = value you like;

is an extra line, which is at least a small argument for the variant
shown in the docs.  (Since ALLOC_GROW is a macro, it is not OK to use
"++nr" as its second argument.)  Personally, I also prefer the shorter
version.  The line

    item[nr++] = value

is an easy-to-recognize idiom, and

    ALLOC_GROW(item, nr + 1, alloc);

somehow makes it more transparent by how much more space will be needed.

So my vote is that the patches are OK the way Dmitry wrote them (mind, I
have only read through 05/11 so far).

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/

```

## Duy Nguyen, 2014-02-28 14:36

Subject: Re: [PATCH v2 01/11] builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
Message-ID: <CACsJy8C1Jv-7Fz=qTZ94vZCVD6s39iju2BRZdhnkSR25VoJ=Ow@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8C1Jv-7Fz%3DqTZ94vZCVD6s39iju2BRZdhnkSR25VoJ%3DOw%40mail.gmail.com
In-Reply-To: <53109B19.8070103@alum.mit.edu>

```
On Fri, Feb 28, 2014 at 9:20 PM, Michael Haggerty <mhagger@alum.mit.edu> wrote:
> Duy,
>
> The example in Documentation/technical/api-allocation-growing.txt does
> it the same way as Dmitry:
>
>     ALLOC_GROW(item, nr + 1, alloc);
>     item[nr++] = value you like;
>
> The alternative,
>
>     nr++;
>     ALLOC_GROW(item, nr, alloc);
>     item[nr] = value you like;
>
> is an extra line, which is at least a small argument for the variant
> shown in the docs.  (Since ALLOC_GROW is a macro, it is not OK to use
> "++nr" as its second argument.)  Personally, I also prefer the shorter
> version.  The line
>
>     item[nr++] = value
>
> is an easy-to-recognize idiom, and
>
>     ALLOC_GROW(item, nr + 1, alloc);
>
> somehow makes it more transparent by how much more space will be needed.
>
> So my vote is that the patches are OK the way Dmitry wrote them (mind, I
> have only read through 05/11 so far).

I'm not saying all patches should do

nr++;
ALLOC_GROW(item, nr, alloc);

only those that do

if (..) realloc...;
nr++;
....

should be reordered. Those changes that do item[nr++] = yyy should be
kept. Anyway it's just an observation, not something that should block
these patches.
-- 
Duy

```

## Michael Haggerty, 2014-02-28 14:38

Subject: Re: [PATCH v2 00/11] Use ALLOC_GROW() instead of inline code
Message-ID: <53109F78.2060203@alum.mit.edu>
URL: https://gitlist.dev/e/53109F78.2060203%40alum.mit.edu
In-Reply-To: <531056E7.3010305@yandex.ru>

```
On 02/28/2014 10:29 AM, Dmitry S. Dolzhenko wrote:
> Thank you for your remarks. In this patch I tried to take them into account.
> 
> Dmitry S. Dolzhenko (11):
>   builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
>   bundle.c: change add_to_ref_list() to use ALLOC_GROW()
>   cache-tree.c: change find_subtree() to use ALLOC_GROW()
>   commit.c: change register_commit_graft() to use ALLOC_GROW()
>   diff.c: use ALLOC_GROW() instead of inline code
>   diffcore-rename.c: use ALLOC_GROW() instead of inline code
>   patch-ids.c: change add_commit() to use ALLOC_GROW()
>   replace_object.c: change register_replace_object() to use ALLOC_GROW()
>   reflog-walk.c: use ALLOC_GROW() instead of inline code
>   dir.c: change create_simplify() to use ALLOC_GROW()
>   attr.c: change handle_attr_line() to use ALLOC_GROW()
> 
>  attr.c                 |  7 +------
>  builtin/pack-objects.c |  7 +------
>  bundle.c               |  6 +-----
>  cache-tree.c           |  6 +-----
>  commit.c               |  8 ++------
>  diff.c                 | 12 ++----------
>  diffcore-rename.c      | 12 ++----------
>  dir.c                  |  5 +----
>  patch-ids.c            |  5 +----
>  reflog-walk.c          | 13 +++----------
>  replace_object.c       |  8 ++------
>  11 files changed, 17 insertions(+), 72 deletions(-)

Everything looks fine to me.  Assuming the test suite ran 100%,

Acked-by: Michael Haggerty <mhagger@alum.mit.edu>

Thanks!
Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/

```

## Junio C Hamano, 2014-02-28 19:03

Subject: Re: [PATCH v2 01/11] builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
Message-ID: <xmqqfvn3ukjs.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqfvn3ukjs.fsf%40gitster.dls.corp.google.com
In-Reply-To: <53109B19.8070103@alum.mit.edu>

```
Michael Haggerty <mhagger@alum.mit.edu> writes:

> So my vote is that the patches are OK the way Dmitry wrote them (mind, I
> have only read through 05/11 so far).

Seconded ;-)

By the way, I do not like these long subjects.  "change" is a
redundant word when one sends a patch---as all patches are about
changing something.

	Subject: builtin/pack-objects.c: use ALLOC_GROW() in check_pbase_path()

would be a lot more appropriate for "git shortlog" consumption.

```

## Junio C Hamano, 2014-02-28 19:06

Subject: Re: [PATCH v2 09/11] reflog-walk.c: use ALLOC_GROW() instead of inline code
Message-ID: <xmqqbnxrukdw.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqbnxrukdw.fsf%40gitster.dls.corp.google.com
In-Reply-To: <CACsJy8DKXYJc2k6Zsqd0ruF_Jex=FH8rWzmhax2Co8z_oa+7zw@mail.gmail.com>

```
Duy Nguyen <pclouds@gmail.com> writes:

> On Fri, Feb 28, 2014 at 4:46 PM, Dmitry S. Dolzhenko
> <dmitrys.dolzhenko@yandex.ru> wrote:
>> Affected functions: read_one_reflog(), add_commit_info()
>
> We can usually see this from @@ line so it's not really needed to
> describe. Same comment for a few other patches.

Not everybody always reads "git log" with "-p".  It is good to see
what are changed mentioned somewhere.

I prefer to see full sentences, though ;-)

	Subject: reflog-walk.c: use ALLOC_GROW()

	read_one_reflog() and add_commit_info() open-codes reallocation;
        use ALLOC_GROW() instead.

or something.  But that is minor.

```

## Dmitry S. Dolzhenko, 2014-03-01 06:57

Subject: Re: [PATCH v2 00/11] Use ALLOC_GROW() instead of inline code
Message-ID: <531184E0.3000808@yandex.ru>
URL: https://gitlist.dev/e/531184E0.3000808%40yandex.ru
In-Reply-To: <53109F78.2060203@alum.mit.edu>

```
Michael,

On 28.02.2014 18:38, Michael Haggerty wrote:
> Everything looks fine to me.  Assuming the test suite ran 100%,
> 
> Acked-by: Michael Haggerty <mhagger@alum.mit.edu>

All tests passed successfully for this patch, at least on my machine.
Can I do something else to improve this patch?

```

## Jeff King, 2014-03-01 07:07

Subject: Re: [PATCH v2 01/11] builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
Message-ID: <20140301070758.GE20397@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20140301070758.GE20397%40sigill.intra.peff.net
In-Reply-To: <xmqqfvn3ukjs.fsf@gitster.dls.corp.google.com>

```
On Fri, Feb 28, 2014 at 11:03:19AM -0800, Junio C Hamano wrote:

> Michael Haggerty <mhagger@alum.mit.edu> writes:
> 
> > So my vote is that the patches are OK the way Dmitry wrote them (mind, I
> > have only read through 05/11 so far).
> 
> Seconded ;-)
> 
> By the way, I do not like these long subjects.  "change" is a
> redundant word when one sends a patch---as all patches are about
> changing something.
> 
> 	Subject: builtin/pack-objects.c: use ALLOC_GROW() in check_pbase_path()
> 
> would be a lot more appropriate for "git shortlog" consumption.

I would actually go one step further and drop or shorten the filename in
the subject. It is very long, it is already easy to see which file was
changed from the diffstat, and it doesn't give any useful context for
other parts of the subject.

I really like the "foo:" convention for starting a subject line, because
it immediately makes clear what area you are working in without having
to waste space on English conjunctions or prepositions. But it does not
have to be a filename. It can be a subsystem, a command, a function, an
area of the project, or anything that gives context to the rest of the
line.

So I would suggest one of:

  Subject: use ALLOC_GROW() in check_pbase_path()

    Talking about the filename is redundant; there's only one
    check_pbase_path.

  Subject: check_pbase_path: use ALLOW_GROW

    Even shorter.

  Subject: builtin/pack-objects.c: use ALLOC_GROW

    This one implies to me that the point of the commit is to convert
    the whole file to use ALLOC_GROW where appropriate, not just that
    function (even if that function may be the only spot changed).

I'd probably not use:

  Subject: pack-objects: use ALLOC_GROW

as the scope is not about the command, but about the C file.

I realize that I just bikeshedded on subject lines for half a page, and
part of me wants to go kill myself in shame. But I feel like I see the
technique misapplied often enough that maybe some guidance is merited.
Feel free to ignore. :)

-Peff

```

## Dmitry S. Dolzhenko, 2014-03-03 07:18

Subject: [PATCH v3 00/11] Use ALLOC_GROW() instead of inline code
Message-ID: <53142CB9.80100@yandex.ru>
URL: https://gitlist.dev/e/53142CB9.80100%40yandex.ru
In-Reply-To: <530FA3E7.8020200@yandex.ru>

```
Dmitry S. Dolzhenko (11):
  builtin/pack-objects.c: use ALLOC_GROW() in check_pbase_path()
  bundle.c: use ALLOC_GROW() in add_to_ref_list()
  cache-tree.c: use ALLOC_GROW() in find_subtree()
  commit.c: use ALLOC_GROW() in register_commit_graft()
  diff.c: use ALLOC_GROW()
  diffcore-rename.c: use ALLOC_GROW()
  patch-ids.c: use ALLOC_GROW() in add_commit()
  replace_object.c: use ALLOC_GROW() in register_replace_object()
  reflog-walk.c: use ALLOC_GROW()
  dir.c: use ALLOC_GROW() in create_simplify()
  attr.c: use ALLOC_GROW() in handle_attr_line()

 attr.c                 |  7 +------
 builtin/pack-objects.c |  9 +++------
 bundle.c               |  6 +-----
 cache-tree.c           |  6 +-----
 commit.c               |  8 ++------
 diff.c                 | 12 ++----------
 diffcore-rename.c      | 12 ++----------
 dir.c                  |  5 +----
 patch-ids.c            |  5 +----
 reflog-walk.c          | 12 ++----------
 replace_object.c       |  8 ++------
 11 files changed, 18 insertions(+), 72 deletions(-)

-- 
1.8.5.3

This version differs from previous only minor changes:
  - update commit messages
  - keep code lines within 80 columns

```

## Dmitry S. Dolzhenko, 2014-03-03 07:19

Subject: [PATCH v3 01/11] builtin/pack-objects.c: use ALLOC_GROW() in check_pbase_path()
Message-ID: <53142D08.4060108@yandex.ru>
URL: https://gitlist.dev/e/53142D08.4060108%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 builtin/pack-objects.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index c733379..0ffad6f 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1213,12 +1213,9 @@ static int check_pbase_path(unsigned hash)
 	if (0 <= pos)
 		return 1;
 	pos = -pos - 1;
-	if (done_pbase_paths_alloc <= done_pbase_paths_num) {
-		done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
-		done_pbase_paths = xrealloc(done_pbase_paths,
-					    done_pbase_paths_alloc *
-					    sizeof(unsigned));
-	}
+	ALLOC_GROW(done_pbase_paths,
+		   done_pbase_paths_num + 1,
+		   done_pbase_paths_alloc);
 	done_pbase_paths_num++;
 	if (pos < done_pbase_paths_num)
 		memmove(done_pbase_paths + pos + 1,
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-03-03 07:20

Subject: [PATCH v3 02/11] bundle.c: use ALLOC_GROW() in add_to_ref_list()
Message-ID: <53142D23.6000505@yandex.ru>
URL: https://gitlist.dev/e/53142D23.6000505%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 bundle.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/bundle.c b/bundle.c
index e99065c..1388a3e 100644
--- a/bundle.c
+++ b/bundle.c
@@ -14,11 +14,7 @@ static const char bundle_signature[] = "# v2 git bundle\n";
 static void add_to_ref_list(const unsigned char *sha1, const char *name,
 		struct ref_list *list)
 {
-	if (list->nr + 1 >= list->alloc) {
-		list->alloc = alloc_nr(list->nr + 1);
-		list->list = xrealloc(list->list,
-				list->alloc * sizeof(list->list[0]));
-	}
+	ALLOC_GROW(list->list, list->nr + 1, list->alloc);
 	memcpy(list->list[list->nr].sha1, sha1, 20);
 	list->list[list->nr].name = xstrdup(name);
 	list->nr++;
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-03-03 07:20

Subject: [PATCH v3 03/11] cache-tree.c: use ALLOC_GROW() in find_subtree()
Message-ID: <53142D45.6050601@yandex.ru>
URL: https://gitlist.dev/e/53142D45.6050601%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 cache-tree.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/cache-tree.c b/cache-tree.c
index 0bbec43..30149d1 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -75,11 +75,7 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
 		return NULL;
 
 	pos = -pos-1;
-	if (it->subtree_alloc <= it->subtree_nr) {
-		it->subtree_alloc = alloc_nr(it->subtree_alloc);
-		it->down = xrealloc(it->down, it->subtree_alloc *
-				    sizeof(*it->down));
-	}
+	ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
 	it->subtree_nr++;
 
 	down = xmalloc(sizeof(*down) + pathlen + 1);
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-03-03 07:21

Subject: [PATCH v3 04/11] commit.c: use ALLOC_GROW() in register_commit_graft()
Message-ID: <53142D70.30704@yandex.ru>
URL: https://gitlist.dev/e/53142D70.30704%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 commit.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/commit.c b/commit.c
index 6bf4fe0..e004314 100644
--- a/commit.c
+++ b/commit.c
@@ -147,12 +147,8 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
 		return 1;
 	}
 	pos = -pos - 1;
-	if (commit_graft_alloc <= ++commit_graft_nr) {
-		commit_graft_alloc = alloc_nr(commit_graft_alloc);
-		commit_graft = xrealloc(commit_graft,
-					sizeof(*commit_graft) *
-					commit_graft_alloc);
-	}
+	ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
+	commit_graft_nr++;
 	if (pos < commit_graft_nr)
 		memmove(commit_graft + pos + 1,
 			commit_graft + pos,
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-03-03 07:22

Subject: [PATCH v3 05/11] diff.c: use ALLOC_GROW()
Message-ID: <53142D9A.40707@yandex.ru>
URL: https://gitlist.dev/e/53142D9A.40707%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Use ALLOC_GROW() instead inline code in
diffstat_add() and diff_q()

Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 diff.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/diff.c b/diff.c
index e800666..aebdfda 100644
--- a/diff.c
+++ b/diff.c
@@ -1361,11 +1361,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
 {
 	struct diffstat_file *x;
 	x = xcalloc(sizeof (*x), 1);
-	if (diffstat->nr == diffstat->alloc) {
-		diffstat->alloc = alloc_nr(diffstat->alloc);
-		diffstat->files = xrealloc(diffstat->files,
-				diffstat->alloc * sizeof(x));
-	}
+	ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
 	diffstat->files[diffstat->nr++] = x;
 	if (name_b) {
 		x->from_name = xstrdup(name_a);
@@ -3965,11 +3961,7 @@ struct diff_queue_struct diff_queued_diff;
 
 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
 {
-	if (queue->alloc <= queue->nr) {
-		queue->alloc = alloc_nr(queue->alloc);
-		queue->queue = xrealloc(queue->queue,
-					sizeof(dp) * queue->alloc);
-	}
+	ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
 	queue->queue[queue->nr++] = dp;
 }
 
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-03-03 07:22

Subject: [PATCH v3 06/11] diffcore-rename.c: use ALLOC_GROW()
Message-ID: <53142DC0.1080403@yandex.ru>
URL: https://gitlist.dev/e/53142DC0.1080403%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Use ALLOC_GROW() instead inline code in
locate_rename_dst() and register_rename_src()

Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 diffcore-rename.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/diffcore-rename.c b/diffcore-rename.c
index 9b4f068..fbf3272 100644
--- a/diffcore-rename.c
+++ b/diffcore-rename.c
@@ -38,11 +38,7 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
 	if (!insert_ok)
 		return NULL;
 	/* insert to make it at "first" */
-	if (rename_dst_alloc <= rename_dst_nr) {
-		rename_dst_alloc = alloc_nr(rename_dst_alloc);
-		rename_dst = xrealloc(rename_dst,
-				      rename_dst_alloc * sizeof(*rename_dst));
-	}
+	ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
 	rename_dst_nr++;
 	if (first < rename_dst_nr)
 		memmove(rename_dst + first + 1, rename_dst + first,
@@ -82,11 +78,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
 	}
 
 	/* insert to make it at "first" */
-	if (rename_src_alloc <= rename_src_nr) {
-		rename_src_alloc = alloc_nr(rename_src_alloc);
-		rename_src = xrealloc(rename_src,
-				      rename_src_alloc * sizeof(*rename_src));
-	}
+	ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
 	rename_src_nr++;
 	if (first < rename_src_nr)
 		memmove(rename_src + first + 1, rename_src + first,
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-03-03 07:23

Subject: [PATCH v3 07/11] patch-ids.c: use ALLOC_GROW() in add_commit()
Message-ID: <53142DDB.40600@yandex.ru>
URL: https://gitlist.dev/e/53142DDB.40600%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 patch-ids.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/patch-ids.c b/patch-ids.c
index bc8a28f..bf81b92 100644
--- a/patch-ids.c
+++ b/patch-ids.c
@@ -83,10 +83,7 @@ static struct patch_id *add_commit(struct commit *commit,
 	ent = &bucket->bucket[bucket->nr++];
 	hashcpy(ent->patch_id, sha1);
 
-	if (ids->alloc <= ids->nr) {
-		ids->alloc = alloc_nr(ids->nr);
-		ids->table = xrealloc(ids->table, sizeof(ent) * ids->alloc);
-	}
+	ALLOC_GROW(ids->table, ids->nr + 1, ids->alloc);
 	if (pos < ids->nr)
 		memmove(ids->table + pos + 1, ids->table + pos,
 			sizeof(ent) * (ids->nr - pos));
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-03-03 07:23

Subject: [PATCH v3 08/11] replace_object.c: use ALLOC_GROW() in register_replace_object()
Message-ID: <53142E07.9000409@yandex.ru>
URL: https://gitlist.dev/e/53142E07.9000409%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 replace_object.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/replace_object.c b/replace_object.c
index cdcaf8c..843deef 100644
--- a/replace_object.c
+++ b/replace_object.c
@@ -36,12 +36,8 @@ static int register_replace_object(struct replace_object *replace,
 		return 1;
 	}
 	pos = -pos - 1;
-	if (replace_object_alloc <= ++replace_object_nr) {
-		replace_object_alloc = alloc_nr(replace_object_alloc);
-		replace_object = xrealloc(replace_object,
-					  sizeof(*replace_object) *
-					  replace_object_alloc);
-	}
+	ALLOC_GROW(replace_object, replace_object_nr + 1, replace_object_alloc);
+	replace_object_nr++;
 	if (pos < replace_object_nr)
 		memmove(replace_object + pos + 1,
 			replace_object + pos,
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-03-03 07:24

Subject: [PATCH v3 09/11] reflog-walk.c: use ALLOC_GROW()
Message-ID: <53142E33.6030303@yandex.ru>
URL: https://gitlist.dev/e/53142E33.6030303%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Use ALLOC_GROW() instead inline code in
add_commit_info() and read_one_reflog()

Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 reflog-walk.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/reflog-walk.c b/reflog-walk.c
index b2fbdb2..2899729 100644
--- a/reflog-walk.c
+++ b/reflog-walk.c
@@ -26,11 +26,7 @@ static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,
 	struct complete_reflogs *array = cb_data;
 	struct reflog_info *item;
 
-	if (array->nr >= array->alloc) {
-		array->alloc = alloc_nr(array->nr + 1);
-		array->items = xrealloc(array->items, array->alloc *
-			sizeof(struct reflog_info));
-	}
+	ALLOC_GROW(array->items, array->nr + 1, array->alloc);
 	item = array->items + array->nr;
 	memcpy(item->osha1, osha1, 20);
 	memcpy(item->nsha1, nsha1, 20);
@@ -114,11 +110,7 @@ static void add_commit_info(struct commit *commit, void *util,
 		struct commit_info_lifo *lifo)
 {
 	struct commit_info *info;
-	if (lifo->nr >= lifo->alloc) {
-		lifo->alloc = alloc_nr(lifo->nr + 1);
-		lifo->items = xrealloc(lifo->items,
-			lifo->alloc * sizeof(struct commit_info));
-	}
+	ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
 	info = lifo->items + lifo->nr;
 	info->commit = commit;
 	info->util = util;
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-03-03 07:25

Subject: [PATCH v3 10/11] dir.c: use ALLOC_GROW() in create_simplify()
Message-ID: <53142E6B.6030602@yandex.ru>
URL: https://gitlist.dev/e/53142E6B.6030602%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 dir.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/dir.c b/dir.c
index 98bb50f..4ae38e4 100644
--- a/dir.c
+++ b/dir.c
@@ -1341,10 +1341,7 @@ static struct path_simplify *create_simplify(const char **pathspec)
 
 	for (nr = 0 ; ; nr++) {
 		const char *match;
-		if (nr >= alloc) {
-			alloc = alloc_nr(alloc);
-			simplify = xrealloc(simplify, alloc * sizeof(*simplify));
-		}
+		ALLOC_GROW(simplify, nr + 1, alloc);
 		match = *pathspec++;
 		if (!match)
 			break;
-- 
1.8.5.3

```

## Dmitry S. Dolzhenko, 2014-03-03 07:25

Subject: [PATCH v3 11/11] attr.c: use ALLOC_GROW() in handle_attr_line()
Message-ID: <53142E82.5080806@yandex.ru>
URL: https://gitlist.dev/e/53142E82.5080806%40yandex.ru
In-Reply-To: <53142CB9.80100@yandex.ru>

```
Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>
---
 attr.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/attr.c b/attr.c
index 8d13d70..734222d 100644
--- a/attr.c
+++ b/attr.c
@@ -338,12 +338,7 @@ static void handle_attr_line(struct attr_stack *res,
 	a = parse_attr_line(line, src, lineno, macro_ok);
 	if (!a)
 		return;
-	if (res->alloc <= res->num_matches) {
-		res->alloc = alloc_nr(res->num_matches);
-		res->attrs = xrealloc(res->attrs,
-				      sizeof(struct match_attr *) *
-				      res->alloc);
-	}
+	ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
 	res->attrs[res->num_matches++] = a;
 }
 
-- 
1.8.5.3

```

## Eric Sunshine, 2014-03-03 08:23

Subject: Re: [PATCH v3 00/11] Use ALLOC_GROW() instead of inline code
Message-ID: <CAPig+cQ-6h3JJr5uUu0J1_m+ENfFq2X4CusetPssQuJNB61zCA@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQ-6h3JJr5uUu0J1_m%2BENfFq2X4CusetPssQuJNB61zCA%40mail.gmail.com
In-Reply-To: <53142CB9.80100@yandex.ru>

```
On Mon, Mar 3, 2014 at 2:18 AM, Dmitry S. Dolzhenko
<dmitrys.dolzhenko@yandex.ru> wrote:
> Dmitry S. Dolzhenko (11):
>   builtin/pack-objects.c: use ALLOC_GROW() in check_pbase_path()
>   bundle.c: use ALLOC_GROW() in add_to_ref_list()
>   cache-tree.c: use ALLOC_GROW() in find_subtree()
>   commit.c: use ALLOC_GROW() in register_commit_graft()
>   diff.c: use ALLOC_GROW()
>   diffcore-rename.c: use ALLOC_GROW()
>   patch-ids.c: use ALLOC_GROW() in add_commit()
>   replace_object.c: use ALLOC_GROW() in register_replace_object()
>   reflog-walk.c: use ALLOC_GROW()
>   dir.c: use ALLOC_GROW() in create_simplify()
>   attr.c: use ALLOC_GROW() in handle_attr_line()
>
>  attr.c                 |  7 +------
>  builtin/pack-objects.c |  9 +++------
>  bundle.c               |  6 +-----
>  cache-tree.c           |  6 +-----
>  commit.c               |  8 ++------
>  diff.c                 | 12 ++----------
>  diffcore-rename.c      | 12 ++----------
>  dir.c                  |  5 +----
>  patch-ids.c            |  5 +----
>  reflog-walk.c          | 12 ++----------
>  replace_object.c       |  8 ++------
>  11 files changed, 18 insertions(+), 72 deletions(-)
>
> --
> 1.8.5.3
>
> This version differs from previous only minor changes:
>   - update commit messages
>   - keep code lines within 80 columns

Place this commentary at the top of the cover letter since that's
where people look for it.

You want to ease the reviewer's job as much as possible, so it helps
to link to the previous submission, like this [1].

Likewise, you can help the reviewer by being more specific about how
you updated the commit messages (and perhaps by linking to the
relevant discussion points, like this [2][3]).

[1]: http://thread.gmane.org/gmane.comp.version-control.git/242857
[2]: http://article.gmane.org/gmane.comp.version-control.git/243004
[3]: http://article.gmane.org/gmane.comp.version-control.git/243049

```

## He Sun, 2014-03-03 14:20

Subject: Re: [PATCH] Use ALLOC_GROW() instead of inline code
Message-ID: <CAJr59C0Fh5eqAqMwidB7EFq9u_ZLQ1=ZUyEEifhEb3ALUpr33w@mail.gmail.com>
URL: https://gitlist.dev/e/CAJr59C0Fh5eqAqMwidB7EFq9u_ZLQ1%3DZUyEEifhEb3ALUpr33w%40mail.gmail.com
In-Reply-To: <530FA3E7.8020200@yandex.ru>

```
2014-02-28 4:45 GMT+08:00 Dmitry S. Dolzhenko <dmitrys.dolzhenko@yandex.ru>:
> Signed-off-by: Dmitry S. Dolzhenko <dmitrys.dolzhenko@gmail.com>
> ---
>  attr.c                 |  7 +------
>  builtin/pack-objects.c |  7 +------
>  bundle.c               |  6 +-----
>  cache-tree.c           |  6 +-----
>  commit.c               |  8 ++------
>  diff.c                 | 12 ++----------
>  diffcore-rename.c      | 12 ++----------
>  dir.c                  |  5 +----
>  patch-ids.c            |  5 +----
>  read-cache.c           |  9 ++-------
>  reflog-walk.c          | 13 +++----------
>  replace_object.c       |  8 ++------
>  12 files changed, 19 insertions(+), 79 deletions(-)
>

I find another two files can be fixed this way.
- builtin/mktree.c
- sha1_file.c

I find it by searching the key word "alloc_nr" throughout the source code.
May be there are other places of this kind of code and you can find it
by other ways.

> diff --git a/attr.c b/attr.c
> index 8d13d70..734222d 100644
> --- a/attr.c
> +++ b/attr.c
> @@ -338,12 +338,7 @@ static void handle_attr_line(struct attr_stack *res,
>         a = parse_attr_line(line, src, lineno, macro_ok);
>         if (!a)
>                 return;
> -       if (res->alloc <= res->num_matches) {
> -               res->alloc = alloc_nr(res->num_matches);
> -               res->attrs = xrealloc(res->attrs,
> -                                     sizeof(struct match_attr *) *
> -                                     res->alloc);
> -       }
> +       ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
>         res->attrs[res->num_matches++] = a;
>  }
>
> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
> index 541667f..92cbce8 100644
> --- a/builtin/pack-objects.c
> +++ b/builtin/pack-objects.c
> @@ -1156,12 +1156,7 @@ static int check_pbase_path(unsigned hash)
>         if (0 <= pos)
>                 return 1;
>         pos = -pos - 1;
> -       if (done_pbase_paths_alloc <= done_pbase_paths_num) {
> -               done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
> -               done_pbase_paths = xrealloc(done_pbase_paths,
> -                                           done_pbase_paths_alloc *
> -                                           sizeof(unsigned));
> -       }
> +       ALLOC_GROW(done_pbase_paths, done_pbase_paths_num + 1, done_pbase_paths_alloc);
>         done_pbase_paths_num++;
>         if (pos < done_pbase_paths_num)
>                 memmove(done_pbase_paths + pos + 1,
> diff --git a/bundle.c b/bundle.c
> index e99065c..1388a3e 100644
> --- a/bundle.c
> +++ b/bundle.c
> @@ -14,11 +14,7 @@ static const char bundle_signature[] = "# v2 git bundle\n";
>  static void add_to_ref_list(const unsigned char *sha1, const char *name,
>                 struct ref_list *list)
>  {
> -       if (list->nr + 1 >= list->alloc) {
> -               list->alloc = alloc_nr(list->nr + 1);
> -               list->list = xrealloc(list->list,
> -                               list->alloc * sizeof(list->list[0]));
> -       }
> +       ALLOC_GROW(list->list, list->nr + 1, list->alloc);
>         memcpy(list->list[list->nr].sha1, sha1, 20);
>         list->list[list->nr].name = xstrdup(name);
>         list->nr++;
> diff --git a/cache-tree.c b/cache-tree.c
> index 0bbec43..30149d1 100644
> --- a/cache-tree.c
> +++ b/cache-tree.c
> @@ -75,11 +75,7 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,
>                 return NULL;
>
>         pos = -pos-1;
> -       if (it->subtree_alloc <= it->subtree_nr) {
> -               it->subtree_alloc = alloc_nr(it->subtree_alloc);
> -               it->down = xrealloc(it->down, it->subtree_alloc *
> -                                   sizeof(*it->down));
> -       }
> +       ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
>         it->subtree_nr++;
>
>         down = xmalloc(sizeof(*down) + pathlen + 1);
> diff --git a/commit.c b/commit.c
> index 6bf4fe0..e004314 100644
> --- a/commit.c
> +++ b/commit.c
> @@ -147,12 +147,8 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
>                 return 1;
>         }
>         pos = -pos - 1;
> -       if (commit_graft_alloc <= ++commit_graft_nr) {
> -               commit_graft_alloc = alloc_nr(commit_graft_alloc);
> -               commit_graft = xrealloc(commit_graft,
> -                                       sizeof(*commit_graft) *
> -                                       commit_graft_alloc);
> -       }
> +       ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
> +       commit_graft_nr++;
>         if (pos < commit_graft_nr)
>                 memmove(commit_graft + pos + 1,
>                         commit_graft + pos,
> diff --git a/diff.c b/diff.c
> index 8e4a6a9..f5f0fd1 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -1361,11 +1361,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
>  {
>         struct diffstat_file *x;
>         x = xcalloc(sizeof (*x), 1);
> -       if (diffstat->nr == diffstat->alloc) {
> -               diffstat->alloc = alloc_nr(diffstat->alloc);
> -               diffstat->files = xrealloc(diffstat->files,
> -                               diffstat->alloc * sizeof(x));
> -       }
> +       ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
>         diffstat->files[diffstat->nr++] = x;
>         if (name_b) {
>                 x->from_name = xstrdup(name_a);
> @@ -3965,11 +3961,7 @@ struct diff_queue_struct diff_queued_diff;
>
>  void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
>  {
> -       if (queue->alloc <= queue->nr) {
> -               queue->alloc = alloc_nr(queue->alloc);
> -               queue->queue = xrealloc(queue->queue,
> -                                       sizeof(dp) * queue->alloc);
> -       }
> +       ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
>         queue->queue[queue->nr++] = dp;
>  }
>
> diff --git a/diffcore-rename.c b/diffcore-rename.c
> index 6c7a72f..f54d5bf 100644
> --- a/diffcore-rename.c
> +++ b/diffcore-rename.c
> @@ -38,11 +38,7 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
>         if (!insert_ok)
>                 return NULL;
>         /* insert to make it at "first" */
> -       if (rename_dst_alloc <= rename_dst_nr) {
> -               rename_dst_alloc = alloc_nr(rename_dst_alloc);
> -               rename_dst = xrealloc(rename_dst,
> -                                     rename_dst_alloc * sizeof(*rename_dst));
> -       }
> +       ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
>         rename_dst_nr++;
>         if (first < rename_dst_nr)
>                 memmove(rename_dst + first + 1, rename_dst + first,
> @@ -82,11 +78,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
>         }
>
>         /* insert to make it at "first" */
> -       if (rename_src_alloc <= rename_src_nr) {
> -               rename_src_alloc = alloc_nr(rename_src_alloc);
> -               rename_src = xrealloc(rename_src,
> -                                     rename_src_alloc * sizeof(*rename_src));
> -       }
> +       ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
>         rename_src_nr++;
>         if (first < rename_src_nr)
>                 memmove(rename_src + first + 1, rename_src + first,
> diff --git a/dir.c b/dir.c
> index b35b633..72f6e2a 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -1329,13 +1329,10 @@ static struct path_simplify *create_simplify(const char **pathspec)
>
>         for (nr = 0 ; ; nr++) {
>                 const char *match;
> -               if (nr >= alloc) {
> -                       alloc = alloc_nr(alloc);
> -                       simplify = xrealloc(simplify, alloc * sizeof(*simplify));
> -               }
>                 match = *pathspec++;
>                 if (!match)
>                         break;
> +               ALLOC_GROW(simplify, nr + 1, alloc);
>                 simplify[nr].path = match;
>                 simplify[nr].len = simple_length(match);
>         }
> diff --git a/patch-ids.c b/patch-ids.c
> index bc8a28f..bf81b92 100644
> --- a/patch-ids.c
> +++ b/patch-ids.c
> @@ -83,10 +83,7 @@ static struct patch_id *add_commit(struct commit *commit,
>         ent = &bucket->bucket[bucket->nr++];
>         hashcpy(ent->patch_id, sha1);
>
> -       if (ids->alloc <= ids->nr) {
> -               ids->alloc = alloc_nr(ids->nr);
> -               ids->table = xrealloc(ids->table, sizeof(ent) * ids->alloc);
> -       }
> +       ALLOC_GROW(ids->table, ids->nr + 1, ids->alloc);
>         if (pos < ids->nr)
>                 memmove(ids->table + pos + 1, ids->table + pos,
>                         sizeof(ent) * (ids->nr - pos));
> diff --git a/read-cache.c b/read-cache.c
> index 33dd676..e585541 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -993,11 +993,7 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti
>         }
>
>         /* Make sure the array is big enough .. */
> -       if (istate->cache_nr == istate->cache_alloc) {
> -               istate->cache_alloc = alloc_nr(istate->cache_alloc);
> -               istate->cache = xrealloc(istate->cache,
> -                                       istate->cache_alloc * sizeof(*istate->cache));
> -       }
> +       ALLOC_GROW(istate->cache, istate->cache_nr + 1, istate->cache_alloc);
>
>         /* Add it in.. */
>         istate->cache_nr++;
> @@ -1466,8 +1462,7 @@ int read_index_from(struct index_state *istate, const char *path)
>
>         istate->version = ntohl(hdr->hdr_version);
>         istate->cache_nr = ntohl(hdr->hdr_entries);
> -       istate->cache_alloc = alloc_nr(istate->cache_nr);
> -       istate->cache = xcalloc(istate->cache_alloc, sizeof(*istate->cache));
> +       ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
>         istate->initialized = 1;
>
>         if (istate->version == 4)
> diff --git a/reflog-walk.c b/reflog-walk.c
> index b2fbdb2..879d2ed 100644
> --- a/reflog-walk.c
> +++ b/reflog-walk.c
> @@ -26,11 +26,7 @@ static int read_one_reflog(unsigned char *osha1, unsigned char *nsha1,
>         struct complete_reflogs *array = cb_data;
>         struct reflog_info *item;
>
> -       if (array->nr >= array->alloc) {
> -               array->alloc = alloc_nr(array->nr + 1);
> -               array->items = xrealloc(array->items, array->alloc *
> -                       sizeof(struct reflog_info));
> -       }
> +       ALLOC_GROW(array->items, array->nr + 1, array->alloc);
>         item = array->items + array->nr;
>         memcpy(item->osha1, osha1, 20);
>         memcpy(item->nsha1, nsha1, 20);
> @@ -114,11 +110,8 @@ static void add_commit_info(struct commit *commit, void *util,
>                 struct commit_info_lifo *lifo)
>  {
>         struct commit_info *info;
> -       if (lifo->nr >= lifo->alloc) {
> -               lifo->alloc = alloc_nr(lifo->nr + 1);
> -               lifo->items = xrealloc(lifo->items,
> -                       lifo->alloc * sizeof(struct commit_info));
> -       }
> +
> +       ALLOC_GROW(lifo->items, lifo->nr + 1, lifo->alloc);
>         info = lifo->items + lifo->nr;
>         info->commit = commit;
>         info->util = util;
> diff --git a/replace_object.c b/replace_object.c
> index cdcaf8c..843deef 100644
> --- a/replace_object.c
> +++ b/replace_object.c
> @@ -36,12 +36,8 @@ static int register_replace_object(struct replace_object *replace,
>                 return 1;
>         }
>         pos = -pos - 1;
> -       if (replace_object_alloc <= ++replace_object_nr) {
> -               replace_object_alloc = alloc_nr(replace_object_alloc);
> -               replace_object = xrealloc(replace_object,
> -                                         sizeof(*replace_object) *
> -                                         replace_object_alloc);
> -       }
> +       ALLOC_GROW(replace_object, replace_object_nr + 1, replace_object_alloc);
> +       replace_object_nr++;
>         if (pos < replace_object_nr)
>                 memmove(replace_object + pos + 1,
>                         replace_object + pos,
> --
> 1.8.3.2
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

Cheers,
He Sun

```

## Junio C Hamano, 2014-03-03 18:20

Subject: Re: [PATCH v2 00/11] Use ALLOC_GROW() instead of inline code
Message-ID: <xmqqa9d7rvna.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqa9d7rvna.fsf%40gitster.dls.corp.google.com
In-Reply-To: <53109F78.2060203@alum.mit.edu>

```
Michael Haggerty <mhagger@alum.mit.edu> writes:

> On 02/28/2014 10:29 AM, Dmitry S. Dolzhenko wrote:
>> Thank you for your remarks. In this patch I tried to take them into account.
>> 
>> Dmitry S. Dolzhenko (11):
>>   builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
>>   bundle.c: change add_to_ref_list() to use ALLOC_GROW()
>>   cache-tree.c: change find_subtree() to use ALLOC_GROW()
>>   commit.c: change register_commit_graft() to use ALLOC_GROW()
>>   diff.c: use ALLOC_GROW() instead of inline code
>>   diffcore-rename.c: use ALLOC_GROW() instead of inline code
>>   patch-ids.c: change add_commit() to use ALLOC_GROW()
>>   replace_object.c: change register_replace_object() to use ALLOC_GROW()
>>   reflog-walk.c: use ALLOC_GROW() instead of inline code
>>   dir.c: change create_simplify() to use ALLOC_GROW()
>>   attr.c: change handle_attr_line() to use ALLOC_GROW()
>> 
>>  attr.c                 |  7 +------
>>  builtin/pack-objects.c |  7 +------
>>  bundle.c               |  6 +-----
>>  cache-tree.c           |  6 +-----
>>  commit.c               |  8 ++------
>>  diff.c                 | 12 ++----------
>>  diffcore-rename.c      | 12 ++----------
>>  dir.c                  |  5 +----
>>  patch-ids.c            |  5 +----
>>  reflog-walk.c          | 13 +++----------
>>  replace_object.c       |  8 ++------
>>  11 files changed, 17 insertions(+), 72 deletions(-)
>
> Everything looks fine to me.  Assuming the test suite ran 100%,
>
> Acked-by: Michael Haggerty <mhagger@alum.mit.edu>

Looked good (modulo titles, which I think we already discussed),
and queued on 'pu'.

Thanks.

```

## Junio C Hamano, 2014-03-03 18:23

Subject: Re: [PATCH v2 01/11] builtin/pack-objects.c: change check_pbase_path() to use ALLOC_GROW()
Message-ID: <xmqq61nvrvir.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqq61nvrvir.fsf%40gitster.dls.corp.google.com
In-Reply-To: <20140301070758.GE20397@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> I realize that I just bikeshedded on subject lines for half a page, and
> part of me wants to go kill myself in shame. But I feel like I see the
> technique misapplied often enough that maybe some guidance is merited.

Thanks.  What I queued read like these:

$ git shortlog ..dd/use-alloc-grow

Dmitry S. Dolzhenko (11):
      builtin/pack-objects.c: use ALLOC_GROW() in check_pbase_path()
      bundle.c: use ALLOC_GROW() in add_to_ref_list()
      cache-tree.c: use ALLOC_GROW() in find_subtree()
      commit.c: use ALLOC_GROW() in register_commit_graft()
      diff.c: use ALLOC_GROW() instead of inline code
      diffcore-rename.c: use ALLOC_GROW() instead of inline code
      patch-ids.c: use ALLOC_GROW() in add_commit()
      replace_object.c: use ALLOC_GROW() in register_replace_object()
      reflog-walk.c: use ALLOC_GROW() instead of inline code
      dir.c: use ALLOC_GROW() in create_simplify()
      attr.c: use ALLOC_GROW() in handle_attr_line()

but I tend to agree with you that we can just stop at "use ALLOC_GROW"
after the filename.

```

## Junio C Hamano, 2014-03-03 19:07

Subject: Re: [PATCH v3 00/11] Use ALLOC_GROW() instead of inline code
Message-ID: <xmqqr46jp0db.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqr46jp0db.fsf%40gitster.dls.corp.google.com
In-Reply-To: <CAPig+cQ-6h3JJr5uUu0J1_m+ENfFq2X4CusetPssQuJNB61zCA@mail.gmail.com>

```
Eric Sunshine <sunshine@sunshineco.com> writes:

> On Mon, Mar 3, 2014 at 2:18 AM, Dmitry S. Dolzhenko
> <dmitrys.dolzhenko@yandex.ru> wrote:
>> Dmitry S. Dolzhenko (11):
>>   builtin/pack-objects.c: use ALLOC_GROW() in check_pbase_path()
>>   bundle.c: use ALLOC_GROW() in add_to_ref_list()
>>   cache-tree.c: use ALLOC_GROW() in find_subtree()
>>   commit.c: use ALLOC_GROW() in register_commit_graft()
>>   diff.c: use ALLOC_GROW()
>>   diffcore-rename.c: use ALLOC_GROW()
>>   patch-ids.c: use ALLOC_GROW() in add_commit()
>>   replace_object.c: use ALLOC_GROW() in register_replace_object()
>>   reflog-walk.c: use ALLOC_GROW()
>>   dir.c: use ALLOC_GROW() in create_simplify()
>>   attr.c: use ALLOC_GROW() in handle_attr_line()
>>
>>  attr.c                 |  7 +------
>>  builtin/pack-objects.c |  9 +++------
>>  bundle.c               |  6 +-----
>>  cache-tree.c           |  6 +-----
>>  commit.c               |  8 ++------
>>  diff.c                 | 12 ++----------
>>  diffcore-rename.c      | 12 ++----------
>>  dir.c                  |  5 +----
>>  patch-ids.c            |  5 +----
>>  reflog-walk.c          | 12 ++----------
>>  replace_object.c       |  8 ++------
>>  11 files changed, 18 insertions(+), 72 deletions(-)
>>
>> --
>> 1.8.5.3
>>
>> This version differs from previous only minor changes:
>>   - update commit messages
>>   - keep code lines within 80 columns
>
> Place this commentary at the top of the cover letter since that's
> where people look for it.
>
> You want to ease the reviewer's job as much as possible, so it helps
> to link to the previous submission, like this [1].
>
> Likewise, you can help the reviewer by being more specific about how
> you updated the commit messages (and perhaps by linking to the
> relevant discussion points, like this [2][3]).
>
> [1]: http://thread.gmane.org/gmane.comp.version-control.git/242857
> [2]: http://article.gmane.org/gmane.comp.version-control.git/243004
> [3]: http://article.gmane.org/gmane.comp.version-control.git/243049

It would be helpful for people to also pay attention to what is
pushed out on 'pu' ;-)

```
