Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Implementation of qsort_s() - bounds-checking qsort().
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include "saferlib.h"
  34. #undef __INLINE_FUNCTIONS__
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include "extfunc.h"
  38.  
  39. typedef int qcomp( const void *, const void *, void * );
  40. #if defined(_M_IX86)
  41.     #pragma aux (__outside_CLIB) qcomp;
  42. #endif
  43.  
  44.  
  45. /* Function to find the median value */
  46. static char *med3( char *a, char *b, char *c, qcomp cmp, void *context )
  47. {
  48.     if( cmp( a, b, context ) > 0 ) {
  49.         if( cmp( a, c, context ) > 0 ) {
  50.             if( cmp( b, c, context ) > 0 ) {
  51.                 return( b );
  52.             } else {
  53.                 return( c );
  54.             }
  55.         } else {
  56.             return( a );
  57.         }
  58.     } else {
  59.         if( cmp( a, c, context ) >= 0 ) {
  60.             return( a );
  61.         } else {
  62.             if( cmp( b, c, context ) > 0 ) {
  63.                 return( c );
  64.             } else {
  65.                 return( b );
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71.  
  72. #ifdef __AXP__
  73.  
  74.     #define FUNCTION_LINKAGE    static
  75.     #define FUNCTION_NAME       aligned_qsort
  76.     #define PTRATTR
  77.     #define MED3                med3
  78.     #define BYTESWAP            aligned_byteswap
  79.     #include "qsortr_s.c"
  80.  
  81.     #undef FUNCTION_NAME
  82.     #undef PTRATTR
  83.     #undef MED3
  84.     #undef BYTESWAP
  85.     #define FUNCTION_NAME       unaligned_qsort
  86.     #define PTRATTR             __unaligned
  87.     #define BYTESWAP            unaligned_byteswap
  88.     #define MED3(a,b,c,f,x)     med3( (char*)(a), (char*)(b), (char*)(c), (f), (x) )
  89.     #include "qsortr_s.c"
  90.  
  91.     _WCRTLINK errno_t qsort_s( void *in_base, rsize_t n, rsize_t size,
  92.                    int (*compar)( const void *, const void *, void * ),
  93.                                void *context )
  94.     /*****************************************************************/
  95.     {
  96.         /*
  97.          * If size%4 is 0 and the input base pointer is on a word boundary,
  98.          * call the aligned version of qsort.  Otherwise, call the version
  99.          * that knows its pointers will be unaligned, so all the fixups can
  100.          * be done by qsort rather than by expensive OS exceptions.
  101.          */
  102.         if( (size&3) == 0  &&  (((unsigned)in_base)&3) == 0 ) {
  103.             return( aligned_qsort( in_base, n, size, compar, context ) );
  104.         } else {
  105.             return( unaligned_qsort( (__unaligned void *)in_base, n, size, compar, context ) );
  106.         }
  107.     }
  108.  
  109. #else
  110.  
  111.     #define FUNCTION_LINKAGE    _WCRTLINK
  112.     #define FUNCTION_NAME       qsort_s
  113.     #define PTRATTR
  114.     #define MED3                med3
  115.     #define BYTESWAP            byteswap
  116.     #include "qsortr_s.c"
  117.  
  118. #endif
  119.