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