libufdt_sysdeps_vendor.c 6.6 KB

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