Skip to content

Commit dbc540c

Browse files
rscharfegitster
authored andcommitted
add QSORT
Add the macro QSORT, a convenient wrapper for qsort(3) that infers the size of the array elements and supports the convention of initializing empty arrays with a NULL pointer, which we use in some places. Calling qsort(3) directly with a NULL pointer is undefined -- even with an element count of zero -- and allows the compiler to optimize away any following NULL checks. Using the macro avoids such surprises. Add a semantic patch as well to demonstrate the macro's usage and to automate the transformation of trivial cases. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 21f862b commit dbc540c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

contrib/coccinelle/qsort.cocci

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@@
2+
expression base, nmemb, compar;
3+
@@
4+
- qsort(base, nmemb, sizeof(*base), compar);
5+
+ QSORT(base, nmemb, compar);
6+
7+
@@
8+
expression base, nmemb, compar;
9+
@@
10+
- qsort(base, nmemb, sizeof(base[0]), compar);
11+
+ QSORT(base, nmemb, compar);
12+
13+
@@
14+
type T;
15+
T *base;
16+
expression nmemb, compar;
17+
@@
18+
- qsort(base, nmemb, sizeof(T), compar);
19+
+ QSORT(base, nmemb, compar);

git-compat-util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,14 @@ void git_qsort(void *base, size_t nmemb, size_t size,
977977
#define qsort git_qsort
978978
#endif
979979

980+
#define QSORT(base, n, compar) sane_qsort((base), (n), sizeof(*(base)), compar)
981+
static inline void sane_qsort(void *base, size_t nmemb, size_t size,
982+
int(*compar)(const void *, const void *))
983+
{
984+
if (nmemb > 1)
985+
qsort(base, nmemb, size, compar);
986+
}
987+
980988
#ifndef REG_STARTEND
981989
#error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
982990
#endif

0 commit comments

Comments
 (0)