Browse Source
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>maint
René Scharfe
8 years ago
committed by
Junio C Hamano
2 changed files with 27 additions and 0 deletions
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
@@ |
||||
expression base, nmemb, compar; |
||||
@@ |
||||
- qsort(base, nmemb, sizeof(*base), compar); |
||||
+ QSORT(base, nmemb, compar); |
||||
|
||||
@@ |
||||
expression base, nmemb, compar; |
||||
@@ |
||||
- qsort(base, nmemb, sizeof(base[0]), compar); |
||||
+ QSORT(base, nmemb, compar); |
||||
|
||||
@@ |
||||
type T; |
||||
T *base; |
||||
expression nmemb, compar; |
||||
@@ |
||||
- qsort(base, nmemb, sizeof(T), compar); |
||||
+ QSORT(base, nmemb, compar); |
Loading…
Reference in new issue