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