Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * C utilities |
3 | | * |
4 | | * Copyright (c) 2017 Fabrice Bellard |
5 | | * Copyright (c) 2018 Charlie Gordon |
6 | | * |
7 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | | * of this software and associated documentation files (the "Software"), to deal |
9 | | * in the Software without restriction, including without limitation the rights |
10 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11 | | * copies of the Software, and to permit persons to whom the Software is |
12 | | * furnished to do so, subject to the following conditions: |
13 | | * |
14 | | * The above copyright notice and this permission notice shall be included in |
15 | | * all copies or substantial portions of the Software. |
16 | | * |
17 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
20 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23 | | * THE SOFTWARE. |
24 | | */ |
25 | | #include <stdlib.h> |
26 | | #include <stdio.h> |
27 | | #include <stdarg.h> |
28 | | #include <string.h> |
29 | | |
30 | | #include "cutils.h" |
31 | | |
32 | | void pstrcpy(char *buf, int buf_size, const char *str) |
33 | 1 | { |
34 | 1 | int c; |
35 | 1 | char *q = buf; |
36 | | |
37 | 1 | if (buf_size <= 0) |
38 | 0 | return; |
39 | | |
40 | 7 | for(;;) { |
41 | 7 | c = *str++; |
42 | 7 | if (c == 0 || q >= buf + buf_size - 1) |
43 | 1 | break; |
44 | 6 | *q++ = c; |
45 | 6 | } |
46 | 1 | *q = '\0'; |
47 | 1 | } |
48 | | |
49 | | /* strcat and truncate. */ |
50 | | char *pstrcat(char *buf, int buf_size, const char *s) |
51 | 1 | { |
52 | 1 | int len; |
53 | 1 | len = strlen(buf); |
54 | 1 | if (len < buf_size) |
55 | 1 | pstrcpy(buf + len, buf_size - len, s); |
56 | 1 | return buf; |
57 | 1 | } |
58 | | |
59 | | int strstart(const char *str, const char *val, const char **ptr) |
60 | 0 | { |
61 | 0 | const char *p, *q; |
62 | 0 | p = str; |
63 | 0 | q = val; |
64 | 0 | while (*q != '\0') { |
65 | 0 | if (*p != *q) |
66 | 0 | return 0; |
67 | 0 | p++; |
68 | 0 | q++; |
69 | 0 | } |
70 | 0 | if (ptr) |
71 | 0 | *ptr = p; |
72 | 0 | return 1; |
73 | 0 | } |
74 | | |
75 | | int has_suffix(const char *str, const char *suffix) |
76 | 0 | { |
77 | 0 | size_t len = strlen(str); |
78 | 0 | size_t slen = strlen(suffix); |
79 | 0 | return (len >= slen && !memcmp(str + len - slen, suffix, slen)); |
80 | 0 | } |
81 | | |
82 | | /* Dynamic buffer package */ |
83 | | |
84 | | static void *dbuf_default_realloc(void *opaque, void *ptr, size_t size) |
85 | 0 | { |
86 | 0 | return realloc(ptr, size); |
87 | 0 | } |
88 | | |
89 | | void dbuf_init2(DynBuf *s, void *opaque, DynBufReallocFunc *realloc_func) |
90 | 23 | { |
91 | 23 | memset(s, 0, sizeof(*s)); |
92 | 23 | if (!realloc_func) |
93 | 0 | realloc_func = dbuf_default_realloc; |
94 | 23 | s->opaque = opaque; |
95 | 23 | s->realloc_func = realloc_func; |
96 | 23 | } |
97 | | |
98 | | void dbuf_init(DynBuf *s) |
99 | 0 | { |
100 | 0 | dbuf_init2(s, NULL, NULL); |
101 | 0 | } |
102 | | |
103 | | /* return < 0 if error */ |
104 | | int dbuf_realloc(DynBuf *s, size_t new_size) |
105 | 131 | { |
106 | 131 | size_t size; |
107 | 131 | uint8_t *new_buf; |
108 | 131 | if (new_size > s->allocated_size) { |
109 | 129 | if (s->error) |
110 | 0 | return -1; |
111 | 129 | size = s->allocated_size * 3 / 2; |
112 | 129 | if (size > new_size) |
113 | 63 | new_size = size; |
114 | 129 | new_buf = s->realloc_func(s->opaque, s->buf, new_size); |
115 | 129 | if (!new_buf) { |
116 | 0 | s->error = TRUE; |
117 | 0 | return -1; |
118 | 0 | } |
119 | 129 | s->buf = new_buf; |
120 | 129 | s->allocated_size = new_size; |
121 | 129 | } |
122 | 131 | return 0; |
123 | 131 | } |
124 | | |
125 | | int dbuf_write(DynBuf *s, size_t offset, const uint8_t *data, size_t len) |
126 | 0 | { |
127 | 0 | size_t end; |
128 | 0 | end = offset + len; |
129 | 0 | if (dbuf_realloc(s, end)) |
130 | 0 | return -1; |
131 | 0 | memcpy(s->buf + offset, data, len); |
132 | 0 | if (end > s->size) |
133 | 0 | s->size = end; |
134 | 0 | return 0; |
135 | 0 | } |
136 | | |
137 | | int dbuf_put(DynBuf *s, const uint8_t *data, size_t len) |
138 | 279 | { |
139 | 279 | if (unlikely((s->size + len) > s->allocated_size)) { |
140 | 126 | if (dbuf_realloc(s, s->size + len)) |
141 | 0 | return -1; |
142 | 126 | } |
143 | 279 | memcpy_no_ub(s->buf + s->size, data, len); |
144 | 279 | s->size += len; |
145 | 279 | return 0; |
146 | 279 | } |
147 | | |
148 | | int dbuf_put_self(DynBuf *s, size_t offset, size_t len) |
149 | 0 | { |
150 | 0 | if (unlikely((s->size + len) > s->allocated_size)) { |
151 | 0 | if (dbuf_realloc(s, s->size + len)) |
152 | 0 | return -1; |
153 | 0 | } |
154 | 0 | memcpy(s->buf + s->size, s->buf + offset, len); |
155 | 0 | s->size += len; |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | | int dbuf_putc(DynBuf *s, uint8_t c) |
160 | 147 | { |
161 | 147 | return dbuf_put(s, &c, 1); |
162 | 147 | } |
163 | | |
164 | | int dbuf_putstr(DynBuf *s, const char *str) |
165 | 0 | { |
166 | 0 | return dbuf_put(s, (const uint8_t *)str, strlen(str)); |
167 | 0 | } |
168 | | |
169 | | int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s, |
170 | | const char *fmt, ...) |
171 | 5 | { |
172 | 5 | va_list ap; |
173 | 5 | char buf[128]; |
174 | 5 | int len; |
175 | | |
176 | 5 | va_start(ap, fmt); |
177 | 5 | len = vsnprintf(buf, sizeof(buf), fmt, ap); |
178 | 5 | va_end(ap); |
179 | 5 | if (len < 0) |
180 | 0 | return -1; |
181 | 5 | if (len < sizeof(buf)) { |
182 | | /* fast case */ |
183 | 5 | return dbuf_put(s, (uint8_t *)buf, len); |
184 | 5 | } else { |
185 | 0 | if (dbuf_realloc(s, s->size + len + 1)) |
186 | 0 | return -1; |
187 | 0 | va_start(ap, fmt); |
188 | 0 | vsnprintf((char *)(s->buf + s->size), s->allocated_size - s->size, |
189 | 0 | fmt, ap); |
190 | 0 | va_end(ap); |
191 | 0 | s->size += len; |
192 | 0 | } |
193 | 0 | return 0; |
194 | 5 | } |
195 | | |
196 | | void dbuf_free(DynBuf *s) |
197 | 13 | { |
198 | | /* we test s->buf as a fail safe to avoid crashing if dbuf_free() |
199 | | is called twice */ |
200 | 13 | if (s->buf) { |
201 | 12 | s->realloc_func(s->opaque, s->buf, 0); |
202 | 12 | } |
203 | 13 | memset(s, 0, sizeof(*s)); |
204 | 13 | } |
205 | | |
206 | | /* Note: at most 31 bits are encoded. At most UTF8_CHAR_LEN_MAX bytes |
207 | | are output. */ |
208 | | int unicode_to_utf8(uint8_t *buf, unsigned int c) |
209 | 0 | { |
210 | 0 | uint8_t *q = buf; |
211 | |
|
212 | 0 | if (c < 0x80) { |
213 | 0 | *q++ = c; |
214 | 0 | } else { |
215 | 0 | if (c < 0x800) { |
216 | 0 | *q++ = (c >> 6) | 0xc0; |
217 | 0 | } else { |
218 | 0 | if (c < 0x10000) { |
219 | 0 | *q++ = (c >> 12) | 0xe0; |
220 | 0 | } else { |
221 | 0 | if (c < 0x00200000) { |
222 | 0 | *q++ = (c >> 18) | 0xf0; |
223 | 0 | } else { |
224 | 0 | if (c < 0x04000000) { |
225 | 0 | *q++ = (c >> 24) | 0xf8; |
226 | 0 | } else if (c < 0x80000000) { |
227 | 0 | *q++ = (c >> 30) | 0xfc; |
228 | 0 | *q++ = ((c >> 24) & 0x3f) | 0x80; |
229 | 0 | } else { |
230 | 0 | return 0; |
231 | 0 | } |
232 | 0 | *q++ = ((c >> 18) & 0x3f) | 0x80; |
233 | 0 | } |
234 | 0 | *q++ = ((c >> 12) & 0x3f) | 0x80; |
235 | 0 | } |
236 | 0 | *q++ = ((c >> 6) & 0x3f) | 0x80; |
237 | 0 | } |
238 | 0 | *q++ = (c & 0x3f) | 0x80; |
239 | 0 | } |
240 | 0 | return q - buf; |
241 | 0 | } |
242 | | |
243 | | static const unsigned int utf8_min_code[5] = { |
244 | | 0x80, 0x800, 0x10000, 0x00200000, 0x04000000, |
245 | | }; |
246 | | |
247 | | static const unsigned char utf8_first_code_mask[5] = { |
248 | | 0x1f, 0xf, 0x7, 0x3, 0x1, |
249 | | }; |
250 | | |
251 | | /* return -1 if error. *pp is not updated in this case. max_len must |
252 | | be >= 1. The maximum length for a UTF8 byte sequence is 6 bytes. */ |
253 | | int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp) |
254 | 1 | { |
255 | 1 | int l, c, b, i; |
256 | | |
257 | 1 | c = *p++; |
258 | 1 | if (c < 0x80) { |
259 | 1 | *pp = p; |
260 | 1 | return c; |
261 | 1 | } |
262 | 0 | switch(c) { |
263 | 0 | case 0xc0: case 0xc1: case 0xc2: case 0xc3: |
264 | 0 | case 0xc4: case 0xc5: case 0xc6: case 0xc7: |
265 | 0 | case 0xc8: case 0xc9: case 0xca: case 0xcb: |
266 | 0 | case 0xcc: case 0xcd: case 0xce: case 0xcf: |
267 | 0 | case 0xd0: case 0xd1: case 0xd2: case 0xd3: |
268 | 0 | case 0xd4: case 0xd5: case 0xd6: case 0xd7: |
269 | 0 | case 0xd8: case 0xd9: case 0xda: case 0xdb: |
270 | 0 | case 0xdc: case 0xdd: case 0xde: case 0xdf: |
271 | 0 | l = 1; |
272 | 0 | break; |
273 | 0 | case 0xe0: case 0xe1: case 0xe2: case 0xe3: |
274 | 0 | case 0xe4: case 0xe5: case 0xe6: case 0xe7: |
275 | 0 | case 0xe8: case 0xe9: case 0xea: case 0xeb: |
276 | 0 | case 0xec: case 0xed: case 0xee: case 0xef: |
277 | 0 | l = 2; |
278 | 0 | break; |
279 | 0 | case 0xf0: case 0xf1: case 0xf2: case 0xf3: |
280 | 0 | case 0xf4: case 0xf5: case 0xf6: case 0xf7: |
281 | 0 | l = 3; |
282 | 0 | break; |
283 | 0 | case 0xf8: case 0xf9: case 0xfa: case 0xfb: |
284 | 0 | l = 4; |
285 | 0 | break; |
286 | 0 | case 0xfc: case 0xfd: |
287 | 0 | l = 5; |
288 | 0 | break; |
289 | 0 | default: |
290 | 0 | return -1; |
291 | 0 | } |
292 | | /* check that we have enough characters */ |
293 | 0 | if (l > (max_len - 1)) |
294 | 0 | return -1; |
295 | 0 | c &= utf8_first_code_mask[l - 1]; |
296 | 0 | for(i = 0; i < l; i++) { |
297 | 0 | b = *p++; |
298 | 0 | if (b < 0x80 || b >= 0xc0) |
299 | 0 | return -1; |
300 | 0 | c = (c << 6) | (b & 0x3f); |
301 | 0 | } |
302 | 0 | if (c < utf8_min_code[l - 1]) |
303 | 0 | return -1; |
304 | 0 | *pp = p; |
305 | 0 | return c; |
306 | 0 | } |
307 | | |
308 | | #if 0 |
309 | | |
310 | | #if defined(EMSCRIPTEN) || defined(__ANDROID__) |
311 | | |
312 | | static void *rqsort_arg; |
313 | | static int (*rqsort_cmp)(const void *, const void *, void *); |
314 | | |
315 | | static int rqsort_cmp2(const void *p1, const void *p2) |
316 | | { |
317 | | return rqsort_cmp(p1, p2, rqsort_arg); |
318 | | } |
319 | | |
320 | | /* not reentrant, but not needed with emscripten */ |
321 | | void rqsort(void *base, size_t nmemb, size_t size, |
322 | | int (*cmp)(const void *, const void *, void *), |
323 | | void *arg) |
324 | | { |
325 | | rqsort_arg = arg; |
326 | | rqsort_cmp = cmp; |
327 | | qsort(base, nmemb, size, rqsort_cmp2); |
328 | | } |
329 | | |
330 | | #endif |
331 | | |
332 | | #else |
333 | | |
334 | | typedef void (*exchange_f)(void *a, void *b, size_t size); |
335 | | typedef int (*cmp_f)(const void *, const void *, void *opaque); |
336 | | |
337 | 0 | static void exchange_bytes(void *a, void *b, size_t size) { |
338 | 0 | uint8_t *ap = (uint8_t *)a; |
339 | 0 | uint8_t *bp = (uint8_t *)b; |
340 | |
|
341 | 0 | while (size-- != 0) { |
342 | 0 | uint8_t t = *ap; |
343 | 0 | *ap++ = *bp; |
344 | 0 | *bp++ = t; |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | 0 | static void exchange_one_byte(void *a, void *b, size_t size) { |
349 | 0 | uint8_t *ap = (uint8_t *)a; |
350 | 0 | uint8_t *bp = (uint8_t *)b; |
351 | 0 | uint8_t t = *ap; |
352 | 0 | *ap = *bp; |
353 | 0 | *bp = t; |
354 | 0 | } |
355 | | |
356 | 0 | static void exchange_int16s(void *a, void *b, size_t size) { |
357 | 0 | uint16_t *ap = (uint16_t *)a; |
358 | 0 | uint16_t *bp = (uint16_t *)b; |
359 | |
|
360 | 0 | for (size /= sizeof(uint16_t); size-- != 0;) { |
361 | 0 | uint16_t t = *ap; |
362 | 0 | *ap++ = *bp; |
363 | 0 | *bp++ = t; |
364 | 0 | } |
365 | 0 | } |
366 | | |
367 | 0 | static void exchange_one_int16(void *a, void *b, size_t size) { |
368 | 0 | uint16_t *ap = (uint16_t *)a; |
369 | 0 | uint16_t *bp = (uint16_t *)b; |
370 | 0 | uint16_t t = *ap; |
371 | 0 | *ap = *bp; |
372 | 0 | *bp = t; |
373 | 0 | } |
374 | | |
375 | 0 | static void exchange_int32s(void *a, void *b, size_t size) { |
376 | 0 | uint32_t *ap = (uint32_t *)a; |
377 | 0 | uint32_t *bp = (uint32_t *)b; |
378 | |
|
379 | 0 | for (size /= sizeof(uint32_t); size-- != 0;) { |
380 | 0 | uint32_t t = *ap; |
381 | 0 | *ap++ = *bp; |
382 | 0 | *bp++ = t; |
383 | 0 | } |
384 | 0 | } |
385 | | |
386 | 0 | static void exchange_one_int32(void *a, void *b, size_t size) { |
387 | 0 | uint32_t *ap = (uint32_t *)a; |
388 | 0 | uint32_t *bp = (uint32_t *)b; |
389 | 0 | uint32_t t = *ap; |
390 | 0 | *ap = *bp; |
391 | 0 | *bp = t; |
392 | 0 | } |
393 | | |
394 | 0 | static void exchange_int64s(void *a, void *b, size_t size) { |
395 | 0 | uint64_t *ap = (uint64_t *)a; |
396 | 0 | uint64_t *bp = (uint64_t *)b; |
397 | |
|
398 | 0 | for (size /= sizeof(uint64_t); size-- != 0;) { |
399 | 0 | uint64_t t = *ap; |
400 | 0 | *ap++ = *bp; |
401 | 0 | *bp++ = t; |
402 | 0 | } |
403 | 0 | } |
404 | | |
405 | 0 | static void exchange_one_int64(void *a, void *b, size_t size) { |
406 | 0 | uint64_t *ap = (uint64_t *)a; |
407 | 0 | uint64_t *bp = (uint64_t *)b; |
408 | 0 | uint64_t t = *ap; |
409 | 0 | *ap = *bp; |
410 | 0 | *bp = t; |
411 | 0 | } |
412 | | |
413 | 72 | static void exchange_int128s(void *a, void *b, size_t size) { |
414 | 72 | uint64_t *ap = (uint64_t *)a; |
415 | 72 | uint64_t *bp = (uint64_t *)b; |
416 | | |
417 | 108 | for (size /= sizeof(uint64_t) * 2; size-- != 0; ap += 2, bp += 2) { |
418 | 36 | uint64_t t = ap[0]; |
419 | 36 | uint64_t u = ap[1]; |
420 | 36 | ap[0] = bp[0]; |
421 | 36 | ap[1] = bp[1]; |
422 | 36 | bp[0] = t; |
423 | 36 | bp[1] = u; |
424 | 36 | } |
425 | 72 | } |
426 | | |
427 | 290 | static void exchange_one_int128(void *a, void *b, size_t size) { |
428 | 290 | uint64_t *ap = (uint64_t *)a; |
429 | 290 | uint64_t *bp = (uint64_t *)b; |
430 | 290 | uint64_t t = ap[0]; |
431 | 290 | uint64_t u = ap[1]; |
432 | 290 | ap[0] = bp[0]; |
433 | 290 | ap[1] = bp[1]; |
434 | 290 | bp[0] = t; |
435 | 290 | bp[1] = u; |
436 | 290 | } |
437 | | |
438 | 8 | static inline exchange_f exchange_func(const void *base, size_t size) { |
439 | 8 | switch (((uintptr_t)base | (uintptr_t)size) & 15) { |
440 | 8 | case 0: |
441 | 8 | if (size == sizeof(uint64_t) * 2) |
442 | 4 | return exchange_one_int128; |
443 | 4 | else |
444 | 4 | return exchange_int128s; |
445 | 0 | case 8: |
446 | 0 | if (size == sizeof(uint64_t)) |
447 | 0 | return exchange_one_int64; |
448 | 0 | else |
449 | 0 | return exchange_int64s; |
450 | 0 | case 4: |
451 | 0 | case 12: |
452 | 0 | if (size == sizeof(uint32_t)) |
453 | 0 | return exchange_one_int32; |
454 | 0 | else |
455 | 0 | return exchange_int32s; |
456 | 0 | case 2: |
457 | 0 | case 6: |
458 | 0 | case 10: |
459 | 0 | case 14: |
460 | 0 | if (size == sizeof(uint16_t)) |
461 | 0 | return exchange_one_int16; |
462 | 0 | else |
463 | 0 | return exchange_int16s; |
464 | 0 | default: |
465 | 0 | if (size == 1) |
466 | 0 | return exchange_one_byte; |
467 | 0 | else |
468 | 0 | return exchange_bytes; |
469 | 8 | } |
470 | 8 | } |
471 | | |
472 | | static void heapsortx(void *base, size_t nmemb, size_t size, cmp_f cmp, void *opaque) |
473 | 0 | { |
474 | 0 | uint8_t *basep = (uint8_t *)base; |
475 | 0 | size_t i, n, c, r; |
476 | 0 | exchange_f swap = exchange_func(base, size); |
477 | |
|
478 | 0 | if (nmemb > 1) { |
479 | 0 | i = (nmemb / 2) * size; |
480 | 0 | n = nmemb * size; |
481 | |
|
482 | 0 | while (i > 0) { |
483 | 0 | i -= size; |
484 | 0 | for (r = i; (c = r * 2 + size) < n; r = c) { |
485 | 0 | if (c < n - size && cmp(basep + c, basep + c + size, opaque) <= 0) |
486 | 0 | c += size; |
487 | 0 | if (cmp(basep + r, basep + c, opaque) > 0) |
488 | 0 | break; |
489 | 0 | swap(basep + r, basep + c, size); |
490 | 0 | } |
491 | 0 | } |
492 | 0 | for (i = n - size; i > 0; i -= size) { |
493 | 0 | swap(basep, basep + i, size); |
494 | |
|
495 | 0 | for (r = 0; (c = r * 2 + size) < i; r = c) { |
496 | 0 | if (c < i - size && cmp(basep + c, basep + c + size, opaque) <= 0) |
497 | 0 | c += size; |
498 | 0 | if (cmp(basep + r, basep + c, opaque) > 0) |
499 | 0 | break; |
500 | 0 | swap(basep + r, basep + c, size); |
501 | 0 | } |
502 | 0 | } |
503 | 0 | } |
504 | 0 | } |
505 | | |
506 | | static inline void *med3(void *a, void *b, void *c, cmp_f cmp, void *opaque) |
507 | 36 | { |
508 | 36 | return cmp(a, b, opaque) < 0 ? |
509 | 20 | (cmp(b, c, opaque) < 0 ? b : (cmp(a, c, opaque) < 0 ? c : a )) : |
510 | 36 | (cmp(b, c, opaque) > 0 ? b : (cmp(a, c, opaque) < 0 ? a : c )); |
511 | 36 | } |
512 | | |
513 | | /* pointer based version with local stack and insertion sort threshhold */ |
514 | | void rqsort(void *base, size_t nmemb, size_t size, cmp_f cmp, void *opaque) |
515 | 4 | { |
516 | 4 | struct { uint8_t *base; size_t count; int depth; } stack[50], *sp = stack; |
517 | 4 | uint8_t *ptr, *pi, *pj, *plt, *pgt, *top, *m; |
518 | 4 | size_t m4, i, lt, gt, span, span2; |
519 | 4 | int c, depth; |
520 | 4 | exchange_f swap = exchange_func(base, size); |
521 | 4 | exchange_f swap_block = exchange_func(base, size | 128); |
522 | | |
523 | 4 | if (nmemb < 2 || size <= 0) |
524 | 0 | return; |
525 | | |
526 | 4 | sp->base = (uint8_t *)base; |
527 | 4 | sp->count = nmemb; |
528 | 4 | sp->depth = 0; |
529 | 4 | sp++; |
530 | | |
531 | 44 | while (sp > stack) { |
532 | 40 | sp--; |
533 | 40 | ptr = sp->base; |
534 | 40 | nmemb = sp->count; |
535 | 40 | depth = sp->depth; |
536 | | |
537 | 76 | while (nmemb > 6) { |
538 | 36 | if (++depth > 50) { |
539 | | /* depth check to ensure worst case logarithmic time */ |
540 | 0 | heapsortx(ptr, nmemb, size, cmp, opaque); |
541 | 0 | nmemb = 0; |
542 | 0 | break; |
543 | 0 | } |
544 | | /* select median of 3 from 1/4, 1/2, 3/4 positions */ |
545 | | /* should use median of 5 or 9? */ |
546 | 36 | m4 = (nmemb >> 2) * size; |
547 | 36 | m = med3(ptr + m4, ptr + 2 * m4, ptr + 3 * m4, cmp, opaque); |
548 | 36 | swap(ptr, m, size); /* move the pivot to the start or the array */ |
549 | 36 | i = lt = 1; |
550 | 36 | pi = plt = ptr + size; |
551 | 36 | gt = nmemb; |
552 | 36 | pj = pgt = top = ptr + nmemb * size; |
553 | 182 | for (;;) { |
554 | 354 | while (pi < pj && (c = cmp(ptr, pi, opaque)) >= 0) { |
555 | 172 | if (c == 0) { |
556 | 0 | swap(plt, pi, size); |
557 | 0 | lt++; |
558 | 0 | plt += size; |
559 | 0 | } |
560 | 172 | i++; |
561 | 172 | pi += size; |
562 | 172 | } |
563 | 380 | while (pi < (pj -= size) && (c = cmp(ptr, pj, opaque)) <= 0) { |
564 | 198 | if (c == 0) { |
565 | 0 | gt--; |
566 | 0 | pgt -= size; |
567 | 0 | swap(pgt, pj, size); |
568 | 0 | } |
569 | 198 | } |
570 | 182 | if (pi >= pj) |
571 | 36 | break; |
572 | 146 | swap(pi, pj, size); |
573 | 146 | i++; |
574 | 146 | pi += size; |
575 | 146 | } |
576 | | /* array has 4 parts: |
577 | | * from 0 to lt excluded: elements identical to pivot |
578 | | * from lt to pi excluded: elements smaller than pivot |
579 | | * from pi to gt excluded: elements greater than pivot |
580 | | * from gt to n excluded: elements identical to pivot |
581 | | */ |
582 | | /* move elements identical to pivot in the middle of the array: */ |
583 | | /* swap values in ranges [0..lt[ and [i-lt..i[ |
584 | | swapping the smallest span between lt and i-lt is sufficient |
585 | | */ |
586 | 36 | span = plt - ptr; |
587 | 36 | span2 = pi - plt; |
588 | 36 | lt = i - lt; |
589 | 36 | if (span > span2) |
590 | 0 | span = span2; |
591 | 36 | swap_block(ptr, pi - span, span); |
592 | | /* swap values in ranges [gt..top[ and [i..top-(top-gt)[ |
593 | | swapping the smallest span between top-gt and gt-i is sufficient |
594 | | */ |
595 | 36 | span = top - pgt; |
596 | 36 | span2 = pgt - pi; |
597 | 36 | pgt = top - span2; |
598 | 36 | gt = nmemb - (gt - i); |
599 | 36 | if (span > span2) |
600 | 0 | span = span2; |
601 | 36 | swap_block(pi, top - span, span); |
602 | | |
603 | | /* now array has 3 parts: |
604 | | * from 0 to lt excluded: elements smaller than pivot |
605 | | * from lt to gt excluded: elements identical to pivot |
606 | | * from gt to n excluded: elements greater than pivot |
607 | | */ |
608 | | /* stack the larger segment and keep processing the smaller one |
609 | | to minimize stack use for pathological distributions */ |
610 | 36 | if (lt > nmemb - gt) { |
611 | 12 | sp->base = ptr; |
612 | 12 | sp->count = lt; |
613 | 12 | sp->depth = depth; |
614 | 12 | sp++; |
615 | 12 | ptr = pgt; |
616 | 12 | nmemb -= gt; |
617 | 24 | } else { |
618 | 24 | sp->base = pgt; |
619 | 24 | sp->count = nmemb - gt; |
620 | 24 | sp->depth = depth; |
621 | 24 | sp++; |
622 | 24 | nmemb = lt; |
623 | 24 | } |
624 | 36 | } |
625 | | /* Use insertion sort for small fragments */ |
626 | 160 | for (pi = ptr + size, top = ptr + nmemb * size; pi < top; pi += size) { |
627 | 228 | for (pj = pi; pj > ptr && cmp(pj - size, pj, opaque) > 0; pj -= size) |
628 | 108 | swap(pj, pj - size, size); |
629 | 120 | } |
630 | 40 | } |
631 | 4 | } |
632 | | |
633 | | #endif |