Browse Source

Use size_t in appropriate places; fixes sorting of big arrays;

after the diff was written, I made it similar to the freebsd fix of
the same code; pr6287 ok millert@ guenther@
OPENBSD_4_7
otto 15 years ago
parent
commit
1fc05bec9f
1 changed files with 10 additions and 9 deletions
  1. +10
    -9
      src/lib/libc/stdlib/qsort.c

+ 10
- 9
src/lib/libc/stdlib/qsort.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
/* $OpenBSD: qsort.c,v 1.11 2010/02/08 11:04:07 otto Exp $ */
/*- /*-
* Copyright (c) 1992, 1993 * Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
@ -32,7 +32,7 @@
#include <stdlib.h> #include <stdlib.h>
static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *)); static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *));
static __inline void swapfunc(char *, char *, int, int);
static __inline void swapfunc(char *, char *, size_t, int);
#define min(a, b) (a) < (b) ? a : b #define min(a, b) (a) < (b) ? a : b
@ -40,7 +40,7 @@ static __inline void swapfunc(char *, char *, int, int);
* Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
*/ */
#define swapcode(TYPE, parmi, parmj, n) { \ #define swapcode(TYPE, parmi, parmj, n) { \
long i = (n) / sizeof (TYPE); \
size_t i = (n) / sizeof (TYPE); \
TYPE *pi = (TYPE *) (parmi); \ TYPE *pi = (TYPE *) (parmi); \
TYPE *pj = (TYPE *) (parmj); \ TYPE *pj = (TYPE *) (parmj); \
do { \ do { \
@ -54,7 +54,7 @@ static __inline void swapfunc(char *, char *, int, int);
es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
static __inline void static __inline void
swapfunc(char *a, char *b, int n, int swaptype)
swapfunc(char *a, char *b, size_t n, int swaptype)
{ {
if (swaptype <= 1) if (swaptype <= 1)
swapcode(long, a, b, n) swapcode(long, a, b, n)
@ -84,7 +84,8 @@ void
qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *)) qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *))
{ {
char *pa, *pb, *pc, *pd, *pl, *pm, *pn; char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
int d, r, swaptype, swap_cnt;
int cmp_result, swaptype, swap_cnt;
size_t d, r;
char *a = aa; char *a = aa;
loop: SWAPINIT(a, es); loop: SWAPINIT(a, es);
@ -113,16 +114,16 @@ loop: SWAPINIT(a, es);
pc = pd = (char *)a + (n - 1) * es; pc = pd = (char *)a + (n - 1) * es;
for (;;) { for (;;) {
while (pb <= pc && (r = cmp(pb, a)) <= 0) {
if (r == 0) {
while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
if (cmp_result == 0) {
swap_cnt = 1; swap_cnt = 1;
swap(pa, pb); swap(pa, pb);
pa += es; pa += es;
} }
pb += es; pb += es;
} }
while (pb <= pc && (r = cmp(pc, a)) >= 0) {
if (r == 0) {
while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
if (cmp_result == 0) {
swap_cnt = 1; swap_cnt = 1;
swap(pc, pd); swap(pc, pd);
pd -= es; pd -= es;


Loading…
Cancel
Save