1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
Index: libc/usr/src/common/util/qsort.c
===================================================================
--- libc.orig/usr/src/common/util/qsort.c
+++ libc/usr/src/common/util/qsort.c
@@ -50,9 +50,15 @@ static void swapb(char *r1, char *r2, si
* note: cstyle specifically prohibits nested conditional operators
* but this is the only way to do the median of 3 function in-line
*/
+#ifdef QSORT_R
+#define med3(a, b, c) (cmp((a), (b), arg) < 0) \
+ ? ((cmp((b), (c), arg) < 0) ? (b) : (cmp((a), (c), arg) < 0) ? (c) : (a)) \
+ : ((cmp((b), (c), arg) > 0) ? (b) : (cmp((a), (c), arg) > 0) ? (c) : (a))
+#else
#define med3(a, b, c) (cmp((a), (b)) < 0) \
? ((cmp((b), (c)) < 0) ? (b) : (cmp((a), (c)) < 0) ? (c) : (a)) \
: ((cmp((b), (c)) > 0) ? (b) : (cmp((a), (c)) > 0) ? (c) : (a))
+#endif
#define THRESH_L 5 /* threshold for insertion sort */
#define THRESH_M3 20 /* threshold for median of 3 */
@@ -104,12 +110,22 @@ typedef struct {
* 9) The user compare function modifies the data records
*/
+#ifdef QSORT_R
+void
+qsort_r(
+ void *basep,
+ size_t nrec,
+ size_t rsiz,
+ int (*cmp)(const void *, const void *, void *),
+ void *arg)
+#else
void
qsort(
void *basep,
size_t nrec,
size_t rsiz,
int (*cmp)(const void *, const void *))
+#endif
{
size_t i; /* temporary variable */
@@ -209,7 +225,12 @@ qsort(
b_par = t_par;
while (b_par > b_lim) {
b_par -= rsiz;
- if ((*cmp)(b_par, b_par + rsiz) <= 0) {
+#ifdef QSORT_R
+ if ((*cmp)(b_par, b_par + rsiz, arg) <= 0)
+#else
+ if ((*cmp)(b_par, b_par + rsiz) <= 0)
+#endif
+ {
break;
}
(*swapf)(b_par, b_par + rsiz, loops);
@@ -323,7 +344,11 @@ qsort(
if (b_par == m2) {
continue;
}
+#ifdef QSORT_R
+ cv = cmp(b_par, m2, arg);
+#else
cv = cmp(b_par, m2);
+#endif
if (cv > 0) {
break;
}
@@ -342,7 +367,11 @@ qsort(
if (t_par == m2) {
continue;
}
+#ifdef QSORT_R
+ cv = cmp(t_par, m2, arg);
+#else
cv = cmp(t_par, m2);
+#endif
if (cv < 0) {
break;
}
Index: libc/usr/src/lib/libc/port/mapfile-vers
===================================================================
--- libc.orig/usr/src/lib/libc/port/mapfile-vers
+++ libc/usr/src/lib/libc/port/mapfile-vers
@@ -289,6 +289,7 @@ SYMBOL_VERSION DYSON_1 {
memrchr;
program_invocation_name;
program_invocation_short_name;
+ qsort_r;
rawmemchr;
sendfile;
sendfilev;
Index: libc/usr/src/lib/libc/i386/Makefile.com
===================================================================
--- libc.orig/usr/src/lib/libc/i386/Makefile.com
+++ libc/usr/src/lib/libc/i386/Makefile.com
@@ -101,6 +101,7 @@ COMOBJS= \
bsearch.o \
bzero.o \
qsort.o \
+ qsort_r.o \
strtol.o \
strtoul.o \
strtoll.o \
Index: libc/usr/src/common/util/qsort_r.c
===================================================================
--- /dev/null
+++ libc/usr/src/common/util/qsort_r.c
@@ -0,0 +1,2 @@
+#define QSORT_R
+#include "qsort.c"
Index: libc/usr/src/lib/libc/amd64/Makefile
===================================================================
--- libc.orig/usr/src/lib/libc/amd64/Makefile
+++ libc/usr/src/lib/libc/amd64/Makefile
@@ -100,6 +100,7 @@ COMOBJS= \
bsearch.o \
bzero.o \
qsort.o \
+ qsort_r.o \
strtol.o \
strtoul.o \
strtoll.o \
Index: libc/usr/src/head/stdlib.h
===================================================================
--- libc.orig/usr/src/head/stdlib.h
+++ libc/usr/src/head/stdlib.h
@@ -34,6 +34,8 @@
#ifndef _STDLIB_H
#define _STDLIB_H
+#include <features.h>
+
#include <iso/stdlib_iso.h>
#include <iso/stdlib_c99.h>
@@ -43,7 +45,17 @@
#if defined(_GNU_SOURCE) || defined(__EXTENSIONS__)
# include <alloca.h>
+
+#if __cplusplus >= 199711L
+namespace std {
#endif
+void qsort_r(void *, size_t, size_t, int (*)(const void *, const void *, void *), void *);
+
+#if __cplusplus >= 199711L
+}
+#endif /* end of namespace std */
+
+#endif /* _GNU_SOURCE */
/*
@@ -73,6 +85,9 @@ using std::mblen;
using std::mbstowcs;
using std::mbtowc;
using std::qsort;
+#if defined(_GNU_SOURCE) || defined(__EXTENSIONS__)
+using std::qsort_r;
+#endif
using std::rand;
using std::realloc;
using std::srand;
|