msvcrt: Added qsort_s implementation.

This commit is contained in:
Vincas Miliūnas 2010-11-17 18:07:47 +02:00 committed by Alexandre Julliard
parent 63316d0967
commit 955e2f9050
5 changed files with 71 additions and 4 deletions

View File

@ -1550,7 +1550,7 @@
@ cdecl putwc(long ptr) msvcrt.putwc
@ cdecl putwchar(long) msvcrt.putwchar
@ cdecl qsort(ptr long long ptr) msvcrt.qsort
@ stub qsort_s
@ cdecl qsort_s(ptr long long ptr ptr) msvcrt.qsort_s
@ cdecl raise(long) msvcrt.raise
@ cdecl rand() msvcrt.rand
@ cdecl rand_s(ptr) msvcrt.rand_s

View File

@ -1404,7 +1404,7 @@
@ cdecl putwc(long ptr) msvcrt.putwc
@ cdecl putwchar(long) msvcrt.putwchar
@ cdecl qsort(ptr long long ptr) msvcrt.qsort
@ stub qsort_s
@ cdecl qsort_s(ptr long long ptr ptr) msvcrt.qsort_s
@ cdecl raise(long) msvcrt.raise
@ cdecl rand() msvcrt.rand
@ cdecl rand_s(ptr) msvcrt.rand_s

View File

@ -1388,7 +1388,7 @@
@ cdecl putwc(long ptr) msvcrt.putwc
@ cdecl putwchar(long) msvcrt.putwchar
@ cdecl qsort(ptr long long ptr) msvcrt.qsort
@ stub qsort_s
@ cdecl qsort_s(ptr long long ptr ptr) msvcrt.qsort_s
@ cdecl raise(long) msvcrt.raise
@ cdecl rand() msvcrt.rand
@ cdecl rand_s(ptr) msvcrt.rand_s

View File

@ -186,3 +186,70 @@ void CDECL _chkesp(void)
# endif /* __GNUC__ */
#endif /* __i386__ */
/*********************************************************************
* Helper function for MSVCRT_qsort_s.
*
* Based on NTDLL_qsort in dlls/ntdll/misc.c
*/
static void MSVCRT_mergesort( void *arr, void *barr, size_t elemsize,
int (CDECL *compar)(void *, const void *, const void *),
size_t left, size_t right, void *context )
{
if (right>left) {
size_t i, j, k, m;
m=left+(right-left)/2;
MSVCRT_mergesort(arr, barr, elemsize, compar, left, m, context);
MSVCRT_mergesort(arr, barr, elemsize, compar, m+1, right, context);
#define X(a,i) ((char*)a+elemsize*(i))
for (i=m+1; i>left; i--)
memcpy (X(barr,(i-1)),X(arr,(i-1)),elemsize);
for (j=m; j<right; j++)
memcpy (X(barr,(right+m-j)),X(arr,(j+1)),elemsize);
/* i=left; j=right; */
for (k=left; i<=m && j>m; k++) {
if (i==j || compar(context, X(barr,i),X(barr,j))<=0) {
memcpy(X(arr,k),X(barr,i),elemsize);
i++;
} else {
memcpy(X(arr,k),X(barr,j),elemsize);
j--;
}
}
for (; i<=m; i++, k++)
memcpy(X(arr,k),X(barr,i),elemsize);
for (; j>m; j--, k++)
memcpy(X(arr,k),X(barr,j),elemsize);
}
#undef X
}
/*********************************************************************
* qsort_s (MSVCRT.@)
*
* Based on NTDLL_qsort in dlls/ntdll/misc.c
*/
void CDECL MSVCRT_qsort_s(void *base, MSVCRT_size_t nmemb, MSVCRT_size_t size,
int (CDECL *compar)(void *, const void *, const void *), void *context)
{
void *secondarr;
const size_t total_size = nmemb*size;
if (!MSVCRT_CHECK_PMT(base != NULL || (base == NULL && nmemb == 0)) ||
!MSVCRT_CHECK_PMT(size > 0) || !MSVCRT_CHECK_PMT(compar != NULL) ||
total_size / size != nmemb)
{
*MSVCRT__errno() = MSVCRT_EINVAL;
return;
}
if (nmemb < 2) return;
secondarr = MSVCRT_malloc(total_size);
if (!secondarr)
return;
MSVCRT_mergesort(base, secondarr, size, compar, 0, nmemb-1, context);
MSVCRT_free(secondarr);
}

View File

@ -1344,7 +1344,7 @@
@ cdecl putwc(long ptr) MSVCRT_fputwc
@ cdecl putwchar(long) _fputwchar
@ cdecl qsort(ptr long long ptr) ntdll.qsort
# stub qsort_s
@ cdecl qsort_s(ptr long long ptr ptr) MSVCRT_qsort_s
@ cdecl raise(long) MSVCRT_raise
@ cdecl rand() MSVCRT_rand
@ cdecl rand_s(ptr) MSVCRT_rand_s