1 #include "libufdt_sysdeps.h" 2 3 #include <debug.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <sys/types.h> 7 8 int dto_print(const char *fmt, ...) { 9 int err; 10 11 va_list ap; 12 va_start(ap, fmt); 13 err = _dvprintf(fmt, ap); 14 va_end(ap); 15 16 return err; 17 } 18 19 /* Codes from 20 * https://android.googlesource.com/platform/bionic.git/+/eclair-release/libc/stdlib/qsort.c 21 * Start 22 */ 23 24 /* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ 25 /*- 26 * Copyright (c) 1992, 1993 27 * The Regents of the University of California. All rights reserved. 28 * 29 * Redistribution and use in source and binary forms, with or without 30 * modification, are permitted provided that the following conditions 31 * are met: 32 * 1. Redistributions of source code must retain the above copyright 33 * notice, this list of conditions and the following disclaimer. 34 * 2. Redistributions in binary form must reproduce the above copyright 35 * notice, this list of conditions and the following disclaimer in the 36 * documentation and/or other materials provided with the distribution. 37 * 3. Neither the name of the University nor the names of its contributors 38 * may be used to endorse or promote products derived from this software 39 * without specific prior written permission. 40 * 41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 */ 53 54 static __inline char *med3(char *, char *, char *, 55 int (*)(const void *, const void *)); 56 static __inline void swapfunc(char *, char *, int, int); 57 #define min(a, b) (a) < (b) ? a : b 58 59 /* 60 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". 61 */ 62 #define swapcode(TYPE, parmi, parmj, n) \ 63 { \ 64 long i = (n) / sizeof(TYPE); \ 65 TYPE *pi = (TYPE *)(parmi); \ 66 TYPE *pj = (TYPE *)(parmj); \ 67 do { \ 68 TYPE t = *pi; \ 69 *pi++ = *pj; \ 70 *pj++ = t; \ 71 } while (--i > 0); \ 72 } 73 #define SWAPINIT(a, es) \ 74 swaptype = ((char *)a - (char *)0) % sizeof(long) || es % sizeof(long) \ 75 ? 2 \ 76 : es == sizeof(long) ? 0 : 1; 77 78 static __inline void swapfunc(char *a, char *b, int n, int swaptype) { 79 if (swaptype <= 1) swapcode(long, a, b, n) else swapcode(char, a, b, n) 80 } 81 82 #define swap(a, b) \ 83 if (swaptype == 0) { \ 84 long t = *(long *)(a); \ 85 *(long *)(a) = *(long *)(b); \ 86 *(long *)(b) = t; \ 87 } else \ 88 swapfunc(a, b, es, swaptype) 89 #define vecswap(a, b, n) \ 90 if ((n) > 0) swapfunc(a, b, n, swaptype) 91 92 static __inline char *med3(char *a, char *b, char *c, 93 int (*cmp)(const void *, const void *)) { 94 return cmp(a, b) < 0 ? (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a)) 95 : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c)); 96 } 97 98 void qsort(void *aa, size_t n, size_t es, 99 int (*cmp)(const void *, const void *)) { 100 char *pa, *pb, *pc, *pd, *pl, *pm, *pn; 101 int d, r, swaptype, swap_cnt; 102 char *a = aa; 103 loop: 104 SWAPINIT(a, es); 105 swap_cnt = 0; 106 if (n < 7) { 107 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) 108 for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0; pl -= es) 109 swap(pl, pl - es); 110 return; 111 } 112 pm = (char *)a + (n / 2) * es; 113 if (n > 7) { 114 pl = (char *)a; 115 pn = (char *)a + (n - 1) * es; 116 if (n > 40) { 117 d = (n / 8) * es; 118 pl = med3(pl, pl + d, pl + 2 * d, cmp); 119 pm = med3(pm - d, pm, pm + d, cmp); 120 pn = med3(pn - 2 * d, pn - d, pn, cmp); 121 } 122 pm = med3(pl, pm, pn, cmp); 123 } 124 swap(a, pm); 125 pa = pb = (char *)a + es; 126 127 pc = pd = (char *)a + (n - 1) * es; 128 for (;;) { 129 while (pb <= pc && (r = cmp(pb, a)) <= 0) { 130 if (r == 0) { 131 swap_cnt = 1; 132 swap(pa, pb); 133 pa += es; 134 } 135 pb += es; 136 } 137 while (pb <= pc && (r = cmp(pc, a)) >= 0) { 138 if (r == 0) { 139 swap_cnt = 1; 140 swap(pc, pd); 141 pd -= es; 142 } 143 pc -= es; 144 } 145 if (pb > pc) break; 146 swap(pb, pc); 147 swap_cnt = 1; 148 pb += es; 149 pc -= es; 150 } 151 if (swap_cnt == 0) { /* Switch to insertion sort */ 152 for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) 153 for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0; pl -= es) 154 swap(pl, pl - es); 155 return; 156 } 157 pn = (char *)a + n * es; 158 r = min(pa - (char *)a, pb - pa); 159 vecswap(a, pb - r, r); 160 r = min(pd - pc, pn - pd - (int)es); 161 vecswap(pb, pn - r, r); 162 if ((r = pb - pa) > (int)es) qsort(a, r / es, es, cmp); 163 if ((r = pd - pc) > (int)es) { 164 /* Iterate rather than recurse to save stack space */ 165 a = pn - r; 166 n = r / es; 167 goto loop; 168 } 169 /* qsort(pn - r, r / es, es, cmp); */ 170 } 171 172 /* End of the copied qsort. */ 173 174 void dto_qsort(void *base, size_t nmemb, size_t size, 175 int (*compar)(const void *, const void *)) { 176 qsort(base, nmemb, size, compar); 177 } 178 179 /* Assuming the following functions are already defined in the 180 * bootloader source with the names conforming to POSIX. 181 */ 182 183 void *dto_malloc(size_t size) { return malloc(size); } 184 185 void dto_free(void *ptr) { free(ptr); } 186 187 char *dto_strchr(const char *s, int c) { return strchr(s, c); } 188 189 unsigned long int dto_strtoul(const char *nptr, char **endptr, int base) { 190 return strtoul(nptr, endptr, base); 191 } 192 193 size_t dto_strlen(const char *s) { return strlen(s); } 194 195 int dto_memcmp(const void *lhs, const void *rhs, size_t n) { 196 return memcmp(lhs, rhs, n); 197 } 198 199 void *dto_memcpy(void *dest, const void *src, size_t n) { 200 return memcpy(dest, src, n); 201 } 202 203 int dto_strcmp(const char *s1, const char *s2) { return strcmp(s1, s2); } 204 205 int dto_strncmp(const char *s1, const char *s2, size_t n) { 206 return strncmp(s1, s2, n); 207 } 208 209 void *dto_memchr(const void *s, int c, size_t n) { return memchr(s, c, n); } 210 211 void *dto_memset(void *s, int c, size_t n) { return memset(s, c, n); } 212