diff options
author | Philip Guenther <guenther@cvs.openbsd.org> | 2015-12-06 23:22:52 +0000 |
---|---|---|
committer | Philip Guenther <guenther@cvs.openbsd.org> | 2015-12-06 23:22:52 +0000 |
commit | 9ac61005742c439141221cf577db80ba25069147 (patch) | |
tree | ad3d4d19d8b54f26f38145052921b17fdb4754b5 /usr.bin/gprof | |
parent | 803c3666945ff9615726973c8f6639118d991f48 (diff) |
Fix qsort() comparison functions:
- correct the function types to eliminate warnings
- avoid substractions that may overflow
- rework tests to be consistent: cmp(A,A)==0 && cmp(A,B)=-cmp(B,A)
worked out with Serguey Parkhomovsky (sergueyparkhomovsky (at) gmail.com)
Diffstat (limited to 'usr.bin/gprof')
-rw-r--r-- | usr.bin/gprof/arcs.c | 11 | ||||
-rw-r--r-- | usr.bin/gprof/gprof.h | 8 | ||||
-rw-r--r-- | usr.bin/gprof/printgprof.c | 64 |
3 files changed, 50 insertions, 33 deletions
diff --git a/usr.bin/gprof/arcs.c b/usr.bin/gprof/arcs.c index 8ba102e3cae..4f9da03268a 100644 --- a/usr.bin/gprof/arcs.c +++ b/usr.bin/gprof/arcs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: arcs.c,v 1.13 2015/08/20 22:32:41 deraadt Exp $ */ +/* $OpenBSD: arcs.c,v 1.14 2015/12/06 23:22:51 guenther Exp $ */ /* $NetBSD: arcs.c,v 1.6 1995/04/19 07:15:52 cgd Exp $ */ /* @@ -95,9 +95,14 @@ addarc(nltype *parentp, nltype *childp, long count) nltype **topsortnlp; int -topcmp(nltype **npp1, nltype **npp2) +topcmp(const void *v1, const void *v2) { - return (*npp1) -> toporder - (*npp2) -> toporder; + const nltype * const *npp1 = v1; + const nltype * const *npp2 = v2; + + if ((*npp1) -> toporder < (*npp2) -> toporder) + return -1; + return (*npp1) -> toporder > (*npp2) -> toporder; } nltype ** diff --git a/usr.bin/gprof/gprof.h b/usr.bin/gprof/gprof.h index 6ae47c822e0..39a2334abaa 100644 --- a/usr.bin/gprof/gprof.h +++ b/usr.bin/gprof/gprof.h @@ -1,4 +1,4 @@ -/* $OpenBSD: gprof.h,v 1.15 2015/11/16 17:43:17 pascal Exp $ */ +/* $OpenBSD: gprof.h,v 1.16 2015/12/06 23:22:51 guenther Exp $ */ /* $NetBSD: gprof.h,v 1.13 1996/04/01 21:54:06 mark Exp $ */ /* @@ -282,10 +282,10 @@ void sortchildren(nltype *); void sortmembers(nltype *); void sortparents(nltype *); void tally(struct rawarc *); -int timecmp(nltype **, nltype **); +int timecmp(const void *, const void *); void timepropagate(nltype *); -int topcmp(nltype **, nltype **); -int totalcmp(nltype **, nltype **); +int topcmp(const void *, const void *); +int totalcmp(const void *, const void *); #define LESSTHAN -1 #define EQUALTO 0 diff --git a/usr.bin/gprof/printgprof.c b/usr.bin/gprof/printgprof.c index e53a8f0e00c..9dabd36c12e 100644 --- a/usr.bin/gprof/printgprof.c +++ b/usr.bin/gprof/printgprof.c @@ -1,4 +1,4 @@ -/* $OpenBSD: printgprof.c,v 1.13 2015/08/20 22:32:41 deraadt Exp $ */ +/* $OpenBSD: printgprof.c,v 1.14 2015/12/06 23:22:51 guenther Exp $ */ /* $NetBSD: printgprof.c,v 1.5 1995/04/19 07:16:21 cgd Exp $ */ /* @@ -35,7 +35,7 @@ #include "gprof.h" #include "pathnames.h" -int namecmp(nltype **, nltype **); +int namecmp(const void *, const void *); void printprof() @@ -66,21 +66,19 @@ printprof() } int -timecmp(nltype **npp1, nltype **npp2) +timecmp(const void *v1, const void *v2) { - double timediff; - long calldiff; + const nltype * const *npp1 = v1; + const nltype * const *npp2 = v2; - timediff = (*npp2) -> time - (*npp1) -> time; - if ( timediff > 0.0 ) + if ((*npp2) -> time < (*npp1) -> time) + return -1; + if ((*npp2) -> time > (*npp1) -> time) return 1 ; - if ( timediff < 0.0 ) + if ((*npp2) -> ncall < (*npp1) -> ncall) return -1; - calldiff = (*npp2) -> ncall - (*npp1) -> ncall; - if ( calldiff > 0 ) + if ((*npp2) -> ncall > (*npp1) -> ncall) return 1; - if ( calldiff < 0 ) - return -1; return( strcmp( (*npp1) -> name , (*npp2) -> name ) ); } @@ -233,26 +231,37 @@ printgprof(nltype **timesortnlp) * all else being equal, sort by names. */ int -totalcmp(nltype **npp1, nltype **npp2) +totalcmp(const void *v1, const void *v2) { - nltype *np1 = *npp1; - nltype *np2 = *npp2; - double diff; - - diff = ( np1 -> propself + np1 -> propchild ) - - ( np2 -> propself + np2 -> propchild ); - if ( diff < 0.0 ) + const nltype *np1 = *(const nltype **)v1; + const nltype *np2 = *(const nltype **)v2; + double t1, t2; + int np1noname, np2noname, np1cyclehdr, np2cyclehdr; + + t1 = np1 -> propself + np1 -> propchild; + t2 = np2 -> propself + np2 -> propchild; + if ( t2 > t1 ) return 1; - if ( diff > 0.0 ) + if ( t2 < t1 ) return -1; - if ( np1 -> name == 0 && np1 -> cycleno != 0 ) + + np1noname = ( np1 -> name == 0 ); + np2noname = ( np2 -> name == 0 ); + np1cyclehdr = ( np1noname && np1 -> cycleno != 0 ); + np2cyclehdr = ( np2noname && np2 -> cycleno != 0 ); + + if ( np1cyclehdr && !np2cyclehdr ) return -1; - if ( np2 -> name == 0 && np2 -> cycleno != 0 ) + else if ( !np1cyclehdr && np2cyclehdr ) return 1; - if ( np1 -> name == 0 ) + + if ( np1noname && !np2noname ) return -1; - if ( np2 -> name == 0 ) + else if ( !np1noname && np2noname ) return 1; + else if ( np1noname && np2noname ) + return 0; + if ( *(np1 -> name) != '_' && *(np2 -> name) == '_' ) return -1; if ( *(np1 -> name) == '_' && *(np2 -> name) != '_' ) @@ -642,8 +651,11 @@ printblurb(const char *blurbname) } int -namecmp(nltype **npp1, nltype **npp2) +namecmp(const void *v1, const void *v2) { + const nltype * const *npp1 = v1; + const nltype * const *npp2 = v2; + return( strcmp( (*npp1) -> name , (*npp2) -> name ) ); } |