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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
21f862b498
commit
dbc540c7a5
@ -977,6 +977,14 @@ void git_qsort(void *base, size_t nmemb, size_t size,
|
||||
#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
|
||||
|
Reference in New Issue
Block a user