X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdlib%2Fqsort.c;h=3e349fb683179a2adbe9553c24a447256d1dede5;hb=ffa73a4a2673fece7cd63c471476db6bc4aef9e6;hp=d12776993988278b304d625a8f0b604f0b4c988d;hpb=b08f4b52b1cd1f7a9553c0f357a7c90859fa3e73;p=pdclib diff --git a/functions/stdlib/qsort.c b/functions/stdlib/qsort.c index d127769..3e349fb 100644 --- a/functions/stdlib/qsort.c +++ b/functions/stdlib/qsort.c @@ -19,7 +19,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 ); } @@ -34,21 +34,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;