X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdlib%2Fqsort.c;h=b654c309ccae1493aaf87efc0aaecd4a231555cd;hb=b1fc26afebd4d557ff89a44bc21767a8704c3809;hp=8941e6db7a187e73aee6fcc03668396bcd63f8fc;hpb=744bbe0fcec2f59f755dd56fa59d8979f4253dfd;p=pdclib diff --git a/functions/stdlib/qsort.c b/functions/stdlib/qsort.c index 8941e6d..b654c30 100644 --- a/functions/stdlib/qsort.c +++ b/functions/stdlib/qsort.c @@ -1,7 +1,3 @@ -/* $Id$ */ - -/* Release $Name$ */ - /* qsort( void *, size_t, size_t, int(*)( const void *, const void * ) ) This file is part of the Public Domain C Library (PDCLib). @@ -21,7 +17,7 @@ */ /* Wrapper for _PDCLIB_memswp protects against multiple argument evaluation. */ -static inline void memswp( char * i, char * j, unsigned int size ) +static inline void memswp( char * i, char * j, size_t size ) { _PDCLIB_memswp( i, j, size ); } @@ -36,21 +32,24 @@ static inline void memswp( char * i, char * j, unsigned int size ) #define PREPARE_STACK char * stack[STACKSIZE]; char * * stackptr = stack #define PUSH( base, limit ) stackptr[0] = base; stackptr[1] = limit; stackptr += 2 #define POP( base, limit ) stackptr -= 2; base = stackptr[0]; limit = stackptr[1] -/* TODO: This is platform-dependent */ -#define STACKSIZE 40 +/* TODO: Stack usage is log2( nmemb ) (minus what T shaves off the worst case). + Worst-case nmemb is platform dependent and should probably be + configured through _PDCLIB_config.h. +*/ +#define STACKSIZE 64 void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) ) { char * i; char * j; - size_t thresh = T * size; - char * base_ = (char *)base; - char * limit = base_ + nmemb * size; + _PDCLIB_size_t thresh = T * size; + char * base_ = (char *)base; + char * limit = base_ + nmemb * size; PREPARE_STACK; for ( ;; ) { - if ( limit - base_ > thresh ) /* QSort for more than T elements. */ + if ( (size_t)( limit - base_ ) > thresh ) /* QSort for more than T elements. */ { /* We work from second to last - first will be pivot element. */ i = base_ + size; @@ -133,18 +132,17 @@ void qsort( void * base, size_t nmemb, size_t size, int (*compar)( const void *, #include #include -int compare( const void * left, const void * right ) +static int compare( const void * left, const void * right ) { return *( (unsigned char *)left ) - *( (unsigned char *)right ); } -int main() +int main( void ) { char presort[] = { "shreicnyjqpvozxmbt" }; char sorted1[] = { "bcehijmnopqrstvxyz" }; char sorted2[] = { "bticjqnyozpvreshxm" }; char s[19]; - BEGIN_TESTS; strcpy( s, presort ); qsort( s, 18, 1, compare ); TESTCASE( strcmp( s, sorted1 ) == 0 ); @@ -154,8 +152,12 @@ int main() strcpy( s, presort ); qsort( s, 1, 1, compare ); TESTCASE( strcmp( s, presort ) == 0 ); +#if __BSD_VISIBLE + puts( "qsort.c: Skipping test #4 for BSD as it goes into endless loop here." ); +#else qsort( s, 100, 0, compare ); TESTCASE( strcmp( s, presort ) == 0 ); +#endif return TEST_RESULTS; }