From: Junio C Hamano Date: Mon, 10 Oct 2016 21:03:46 +0000 (-0700) Subject: Merge branch 'rs/qsort' X-Git-Tag: v2.11.0-rc0~73 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/b8688adb12d086b161aa7c369126bdd56843a01b?hp=-c Merge branch 'rs/qsort' We call "qsort(array, nelem, sizeof(array[0]), fn)", and most of the time third parameter is redundant. A new QSORT() macro lets us omit it. * rs/qsort: show-branch: use QSORT use QSORT, part 2 coccicheck: use --all-includes by default remove unnecessary check before QSORT use QSORT add QSORT --- b8688adb12d086b161aa7c369126bdd56843a01b diff --combined diff.c index be11e4ef2b,c2f09fb8dd..1d304e0550 --- a/diff.c +++ b/diff.c @@@ -2019,7 -2019,7 +2019,7 @@@ found_damage return; /* Show all directories with more than x% of the changes */ - qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare); + QSORT(dir.files, dir.nr, dirstat_compare); gather_dirstat(options, &dir, changed, "", 0); } @@@ -2063,7 -2063,7 +2063,7 @@@ static void show_dirstat_by_line(struc return; /* Show all directories with more than x% of the changes */ - qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare); + QSORT(dir.files, dir.nr, dirstat_compare); gather_dirstat(options, &dir, changed, "", 0); } @@@ -3109,7 -3109,7 +3109,7 @@@ static void fill_metainfo(struct strbu } strbuf_addf(msg, "%s%sindex %s..", line_prefix, set, find_unique_abbrev(one->oid.hash, abbrev)); - strbuf_addstr(msg, find_unique_abbrev(two->oid.hash, abbrev)); + strbuf_add_unique_abbrev(msg, two->oid.hash, abbrev); if (one->mode == two->mode) strbuf_addf(msg, " %06o", one->mode); strbuf_addf(msg, "%s\n", reset); @@@ -4923,7 -4923,7 +4923,7 @@@ static int diffnamecmp(const void *a_, void diffcore_fix_diff_index(struct diff_options *options) { struct diff_queue_struct *q = &diff_queued_diff; - qsort(q->queue, q->nr, sizeof(q->queue[0]), diffnamecmp); + QSORT(q->queue, q->nr, diffnamecmp); } void diffcore_std(struct diff_options *options) diff --combined fetch-pack.c index 413937e740,8a38d30707..300763fae1 --- a/fetch-pack.c +++ b/fetch-pack.c @@@ -428,17 -428,10 +428,17 @@@ static int find_common(struct fetch_pac const char *hex = sha1_to_hex(result_sha1); packet_buf_write(&req_buf, "have %s\n", hex); state_len = req_buf.len; - } + /* + * Reset in_vain because an ack + * for this commit has not been + * seen. + */ + in_vain = 0; + } else if (!args->stateless_rpc + || ack != ACK_common) + in_vain = 0; mark_common(commit, 0, 1); retval = 0; - in_vain = 0; got_continue = 1; if (ack == ACK_ready) { clear_prio_queue(&rev_list); @@@ -819,7 -812,7 +819,7 @@@ static struct ref *do_fetch_pack(struc int agent_len; sort_ref_list(&ref, ref_compare_name); - qsort(sought, nr_sought, sizeof(*sought), cmp_ref_by_name); + QSORT(sought, nr_sought, cmp_ref_by_name); if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow")) die("Server does not support shallow clients"); diff --combined git-compat-util.h index 0ce2cdfb98,544db2ae65..43718dabae --- a/git-compat-util.h +++ b/git-compat-util.h @@@ -801,14 -801,6 +801,14 @@@ extern FILE *fopen_for_writing(const ch #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(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)) + \ + BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) +static inline void copy_array(void *dst, const void *src, size_t n, size_t size) +{ + if (n) + memcpy(dst, src, st_mult(size, n)); +} + /* * These functions help you allocate structs with flex arrays, and copy * the data directly into the array. For example, if you had: @@@ -985,6 -977,14 +985,14 @@@ void git_qsort(void *base, size_t nmemb #define qsort git_qsort #endif + #define QSORT(base, n, compar) sane_qsort((base), (n), sizeof(*(base)), compar) + static inline void sane_qsort(void *base, size_t nmemb, size_t size, + int(*compar)(const void *, const void *)) + { + if (nmemb > 1) + qsort(base, nmemb, size, compar); + } + #ifndef REG_STARTEND #error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd" #endif diff --combined pack-check.c index c5c7763323,72440a8fce..27f70d345f --- a/pack-check.c +++ b/pack-check.c @@@ -57,8 -57,11 +57,8 @@@ static int verify_packfile(struct packe int err = 0; struct idx_entry *entries; - /* Note that the pack header checks are actually performed by - * use_pack when it first opens the pack file. If anything - * goes wrong during those checks then the call will die out - * immediately. - */ + if (!is_pack_valid(p)) + return error("packfile %s cannot be accessed", p->pack_name); git_SHA1_Init(&ctx); do { @@@ -96,7 -99,7 +96,7 @@@ entries[i].offset = nth_packed_object_offset(p, i); entries[i].nr = i; } - qsort(entries, nr_objects, sizeof(*entries), compare_entries); + QSORT(entries, nr_objects, compare_entries); for (i = 0; i < nr_objects; i++) { void *data; diff --combined pathspec.c index 49a53607bb,eda13b54c7..86f2b449b1 --- a/pathspec.c +++ b/pathspec.c @@@ -446,8 -446,7 +446,7 @@@ void parse_pathspec(struct pathspec *pa if (pathspec->magic & PATHSPEC_MAXDEPTH) { if (flags & PATHSPEC_KEEP_ORDER) die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible"); - qsort(pathspec->items, pathspec->nr, - sizeof(struct pathspec_item), pathspec_item_cmp); + QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp); } } @@@ -485,7 -484,8 +484,7 @@@ void copy_pathspec(struct pathspec *dst { *dst = *src; ALLOC_ARRAY(dst->items, dst->nr); - memcpy(dst->items, src->items, - sizeof(struct pathspec_item) * dst->nr); + COPY_ARRAY(dst->items, src->items, dst->nr); } void clear_pathspec(struct pathspec *pathspec) diff --combined ref-filter.c index 9a8f55e45a,44029b0e34..d4c2931f3a --- a/ref-filter.c +++ b/ref-filter.c @@@ -235,7 -235,7 +235,7 @@@ int parse_ref_filter_atom(const char *a { const char *sp; const char *arg; - int i, at; + int i, at, atom_len; sp = atom; if (*sp == '*' && sp < ep) @@@ -250,19 -250,19 +250,19 @@@ return i; } + /* + * If the atom name has a colon, strip it and everything after + * it off - it specifies the format for this entry, and + * shouldn't be used for checking against the valid_atom + * table. + */ + arg = memchr(sp, ':', ep - sp); + atom_len = (arg ? arg : ep) - sp; + /* Is the atom a valid one? */ for (i = 0; i < ARRAY_SIZE(valid_atom); i++) { int len = strlen(valid_atom[i].name); - - /* - * If the atom name has a colon, strip it and everything after - * it off - it specifies the format for this entry, and - * shouldn't be used for checking against the valid_atom - * table. - */ - arg = memchr(sp, ':', ep - sp); - if (len == (arg ? arg : ep) - sp && - !memcmp(valid_atom[i].name, sp, len)) + if (len == atom_len && !memcmp(valid_atom[i].name, sp, len)) break; } @@@ -1573,7 -1573,7 +1573,7 @@@ static int compare_refs(const void *a_ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) { ref_sorting = sorting; - qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs); + QSORT(array->items, array->nr, compare_refs); } static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state) diff --combined sha1-array.c index af1d7d560d,21188deb7a..c1cc25cd95 --- a/sha1-array.c +++ b/sha1-array.c @@@ -16,7 -16,7 +16,7 @@@ static int void_hashcmp(const void *a, static void sha1_array_sort(struct sha1_array *array) { - qsort(array->sha1, array->nr, sizeof(*array->sha1), void_hashcmp); + QSORT(array->sha1, array->nr, void_hashcmp); array->sorted = 1; } @@@ -42,7 -42,7 +42,7 @@@ void sha1_array_clear(struct sha1_arra array->sorted = 0; } -void sha1_array_for_each_unique(struct sha1_array *array, +int sha1_array_for_each_unique(struct sha1_array *array, for_each_sha1_fn fn, void *data) { @@@ -52,12 -52,8 +52,12 @@@ sha1_array_sort(array); for (i = 0; i < array->nr; i++) { + int ret; if (i > 0 && !hashcmp(array->sha1[i], array->sha1[i-1])) continue; - fn(array->sha1[i], data); + ret = fn(array->sha1[i], data); + if (ret) + return ret; } + return 0; }