Merge branch 'rs/dup-array'
Code cleaning. * rs/dup-array: use DUP_ARRAY add DUP_ARRAY do full type check in BARF_UNLESS_COPYABLE factor out BARF_UNLESS_COPYABLE mingw: make argv2 in try_shell_exec() non-constmaint
						commit
						60ce816cb6
					
				
							
								
								
									
										3
									
								
								attr.c
								
								
								
								
							
							
						
						
									
										3
									
								
								attr.c
								
								
								
								
							|  | @ -603,8 +603,7 @@ struct attr_check *attr_check_dup(const struct attr_check *check) | ||||||
|  |  | ||||||
| 	ret->nr = check->nr; | 	ret->nr = check->nr; | ||||||
| 	ret->alloc = check->alloc; | 	ret->alloc = check->alloc; | ||||||
| 	ALLOC_ARRAY(ret->items, ret->nr); | 	DUP_ARRAY(ret->items, check->items, ret->nr); | ||||||
| 	COPY_ARRAY(ret->items, check->items, ret->nr); |  | ||||||
|  |  | ||||||
| 	return ret; | 	return ret; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1492,8 +1492,7 @@ static int run_apply(const struct am_state *state, const char *index_file) | ||||||
| 	 * apply_opts.v keeps referencing the allocated strings for | 	 * apply_opts.v keeps referencing the allocated strings for | ||||||
| 	 * strvec_clear() to release. | 	 * strvec_clear() to release. | ||||||
| 	 */ | 	 */ | ||||||
| 	ALLOC_ARRAY(apply_argv, apply_opts.nr); | 	DUP_ARRAY(apply_argv, apply_opts.v, apply_opts.nr); | ||||||
| 	COPY_ARRAY(apply_argv, apply_opts.v, apply_opts.nr); |  | ||||||
|  |  | ||||||
| 	opts_left = apply_parse_options(apply_opts.nr, apply_argv, | 	opts_left = apply_parse_options(apply_opts.nr, apply_argv, | ||||||
| 					&apply_state, &force_apply, &options, | 					&apply_state, &force_apply, &options, | ||||||
|  |  | ||||||
|  | @ -1594,8 +1594,7 @@ static void compute_bloom_filters(struct write_commit_graph_context *ctx) | ||||||
| 			_("Computing commit changed paths Bloom filters"), | 			_("Computing commit changed paths Bloom filters"), | ||||||
| 			ctx->commits.nr); | 			ctx->commits.nr); | ||||||
|  |  | ||||||
| 	ALLOC_ARRAY(sorted_commits, ctx->commits.nr); | 	DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr); | ||||||
| 	COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr); |  | ||||||
|  |  | ||||||
| 	if (ctx->order_by_pack) | 	if (ctx->order_by_pack) | ||||||
| 		QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp); | 		QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp); | ||||||
|  |  | ||||||
|  | @ -245,8 +245,7 @@ static int remove_redundant_with_gen(struct repository *r, | ||||||
| 	 * min_gen_pos points to the current position within 'array' | 	 * min_gen_pos points to the current position within 'array' | ||||||
| 	 * that is not yet known to be STALE. | 	 * that is not yet known to be STALE. | ||||||
| 	 */ | 	 */ | ||||||
| 	ALLOC_ARRAY(sorted, cnt); | 	DUP_ARRAY(sorted, array, cnt); | ||||||
| 	COPY_ARRAY(sorted, array, cnt); |  | ||||||
| 	QSORT(sorted, cnt, compare_commits_by_gen); | 	QSORT(sorted, cnt, compare_commits_by_gen); | ||||||
| 	min_generation = commit_graph_generation(sorted[0]); | 	min_generation = commit_graph_generation(sorted[0]); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @ -1396,8 +1396,7 @@ static wchar_t *make_environment_block(char **deltaenv) | ||||||
| 			p += s; | 			p += s; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		ALLOC_ARRAY(result, size); | 		DUP_ARRAY(result, wenv, size); | ||||||
| 		COPY_ARRAY(result, wenv, size); |  | ||||||
| 		FreeEnvironmentStringsW(wenv); | 		FreeEnvironmentStringsW(wenv); | ||||||
| 		return result; | 		return result; | ||||||
| 	} | 	} | ||||||
|  | @ -1839,16 +1838,13 @@ static int try_shell_exec(const char *cmd, char *const *argv) | ||||||
| 	if (prog) { | 	if (prog) { | ||||||
| 		int exec_id; | 		int exec_id; | ||||||
| 		int argc = 0; | 		int argc = 0; | ||||||
| #ifndef _MSC_VER |  | ||||||
| 		const |  | ||||||
| #endif |  | ||||||
| 		char **argv2; | 		char **argv2; | ||||||
| 		while (argv[argc]) argc++; | 		while (argv[argc]) argc++; | ||||||
| 		ALLOC_ARRAY(argv2, argc + 1); | 		ALLOC_ARRAY(argv2, argc + 1); | ||||||
| 		argv2[0] = (char *)cmd;	/* full path to the script file */ | 		argv2[0] = (char *)cmd;	/* full path to the script file */ | ||||||
| 		COPY_ARRAY(&argv2[1], &argv[1], argc); | 		COPY_ARRAY(&argv2[1], &argv[1], argc); | ||||||
| 		exec_id = trace2_exec(prog, argv2); | 		exec_id = trace2_exec(prog, (const char **)argv2); | ||||||
| 		pid = mingw_spawnv(prog, argv2, 1); | 		pid = mingw_spawnv(prog, (const char **)argv2, 1); | ||||||
| 		if (pid >= 0) { | 		if (pid >= 0) { | ||||||
| 			int status; | 			int status; | ||||||
| 			if (waitpid(pid, &status, 0) < 0) | 			if (waitpid(pid, &status, 0) < 0) | ||||||
|  |  | ||||||
|  | @ -94,3 +94,10 @@ expression n != 1; | ||||||
| @@ | @@ | ||||||
| - ptr = xcalloc(n, \( sizeof(*ptr) \| sizeof(T) \) ) | - ptr = xcalloc(n, \( sizeof(*ptr) \| sizeof(T) \) ) | ||||||
| + CALLOC_ARRAY(ptr, n) | + CALLOC_ARRAY(ptr, n) | ||||||
|  |  | ||||||
|  | @@ | ||||||
|  | expression dst, src, n; | ||||||
|  | @@ | ||||||
|  | -ALLOC_ARRAY(dst, n); | ||||||
|  | -COPY_ARRAY(dst, src, n); | ||||||
|  | +DUP_ARRAY(dst, src, n); | ||||||
|  |  | ||||||
|  | @ -97,8 +97,14 @@ struct strbuf; | ||||||
| # define BARF_UNLESS_AN_ARRAY(arr)						\ | # define BARF_UNLESS_AN_ARRAY(arr)						\ | ||||||
| 	BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(__typeof__(arr), \ | 	BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(__typeof__(arr), \ | ||||||
| 							   __typeof__(&(arr)[0]))) | 							   __typeof__(&(arr)[0]))) | ||||||
|  | # define BARF_UNLESS_COPYABLE(dst, src) \ | ||||||
|  | 	BUILD_ASSERT_OR_ZERO(__builtin_types_compatible_p(__typeof__(*(dst)), \ | ||||||
|  | 							  __typeof__(*(src)))) | ||||||
| #else | #else | ||||||
| # define BARF_UNLESS_AN_ARRAY(arr) 0 | # define BARF_UNLESS_AN_ARRAY(arr) 0 | ||||||
|  | # define BARF_UNLESS_COPYABLE(dst, src) \ | ||||||
|  | 	BUILD_ASSERT_OR_ZERO(0 ? ((*(dst) = *(src)), 0) : \ | ||||||
|  | 				 sizeof(*(dst)) == sizeof(*(src))) | ||||||
| #endif | #endif | ||||||
| /* | /* | ||||||
|  * ARRAY_SIZE - get the number of elements in a visible array |  * ARRAY_SIZE - get the number of elements in a visible array | ||||||
|  | @ -1102,7 +1108,7 @@ int xstrncmpz(const char *s, const char *t, size_t len); | ||||||
| #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc))) | #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc))) | ||||||
|  |  | ||||||
| #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \ | #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \ | ||||||
| 	BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) | 	BARF_UNLESS_COPYABLE((dst), (src))) | ||||||
| static inline void copy_array(void *dst, const void *src, size_t n, size_t size) | static inline void copy_array(void *dst, const void *src, size_t n, size_t size) | ||||||
| { | { | ||||||
| 	if (n) | 	if (n) | ||||||
|  | @ -1110,13 +1116,18 @@ static inline void copy_array(void *dst, const void *src, size_t n, size_t size) | ||||||
| } | } | ||||||
|  |  | ||||||
| #define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \ | #define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \ | ||||||
| 	BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) | 	BARF_UNLESS_COPYABLE((dst), (src))) | ||||||
| static inline void move_array(void *dst, const void *src, size_t n, size_t size) | static inline void move_array(void *dst, const void *src, size_t n, size_t size) | ||||||
| { | { | ||||||
| 	if (n) | 	if (n) | ||||||
| 		memmove(dst, src, st_mult(size, n)); | 		memmove(dst, src, st_mult(size, n)); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | #define DUP_ARRAY(dst, src, n) do { \ | ||||||
|  | 	size_t dup_array_n_ = (n); \ | ||||||
|  | 	COPY_ARRAY(ALLOC_ARRAY((dst), dup_array_n_), (src), dup_array_n_); \ | ||||||
|  | } while (0) | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * These functions help you allocate structs with flex arrays, and copy |  * These functions help you allocate structs with flex arrays, and copy | ||||||
|  * the data directly into the array. For example, if you had: |  * the data directly into the array. For example, if you had: | ||||||
|  |  | ||||||
|  | @ -702,8 +702,7 @@ static struct option *preprocess_options(struct parse_opt_ctx_t *ctx, | ||||||
| 	if (!nr_aliases) | 	if (!nr_aliases) | ||||||
| 		return NULL; | 		return NULL; | ||||||
|  |  | ||||||
| 	ALLOC_ARRAY(newopt, nr + 1); | 	DUP_ARRAY(newopt, options, nr + 1); | ||||||
| 	COPY_ARRAY(newopt, options, nr + 1); |  | ||||||
|  |  | ||||||
| 	/* each alias has two string pointers and NULL */ | 	/* each alias has two string pointers and NULL */ | ||||||
| 	CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1)); | 	CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1)); | ||||||
|  |  | ||||||
|  | @ -681,8 +681,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src) | ||||||
| 	int i, j; | 	int i, j; | ||||||
|  |  | ||||||
| 	*dst = *src; | 	*dst = *src; | ||||||
| 	ALLOC_ARRAY(dst->items, dst->nr); | 	DUP_ARRAY(dst->items, src->items, dst->nr); | ||||||
| 	COPY_ARRAY(dst->items, src->items, dst->nr); |  | ||||||
|  |  | ||||||
| 	for (i = 0; i < dst->nr; i++) { | 	for (i = 0; i < dst->nr; i++) { | ||||||
| 		struct pathspec_item *d = &dst->items[i]; | 		struct pathspec_item *d = &dst->items[i]; | ||||||
|  | @ -691,8 +690,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src) | ||||||
| 		d->match = xstrdup(s->match); | 		d->match = xstrdup(s->match); | ||||||
| 		d->original = xstrdup(s->original); | 		d->original = xstrdup(s->original); | ||||||
|  |  | ||||||
| 		ALLOC_ARRAY(d->attr_match, d->attr_match_nr); | 		DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr); | ||||||
| 		COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr); |  | ||||||
| 		for (j = 0; j < d->attr_match_nr; j++) { | 		for (j = 0; j < d->attr_match_nr; j++) { | ||||||
| 			const char *value = s->attr_match[j].value; | 			const char *value = s->attr_match[j].value; | ||||||
| 			d->attr_match[j].value = xstrdup_or_null(value); | 			d->attr_match[j].value = xstrdup_or_null(value); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Junio C Hamano
						Junio C Hamano