Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Yabin Cui | 7fb680b | 2015-02-24 13:18:25 -0800 | [diff] [blame] | 29 | // This file perpetuates the mistakes of the past. |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 31 | #include <ctype.h> |
Elliott Hughes | d1ead2a | 2014-06-06 15:24:20 -0700 | [diff] [blame] | 32 | #include <dirent.h> |
Elliott Hughes | 4c5891d | 2015-02-19 22:49:44 -0800 | [diff] [blame] | 33 | #include <errno.h> |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 34 | #include <inttypes.h> |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 35 | #include <pthread.h> |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 36 | #include <signal.h> |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 37 | #include <stdio.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 38 | #include <stdlib.h> |
David 'Digit' Turner | 891dedb | 2014-06-13 12:28:11 +0200 | [diff] [blame] | 39 | #include <string.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 40 | #include <sys/resource.h> |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 41 | #include <sys/syscall.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 42 | #include <sys/time.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/wait.h> |
| 45 | #include <unistd.h> |
Dan Albert | 001f8f0 | 2014-06-04 09:53:06 -0700 | [diff] [blame] | 46 | #include <wchar.h> |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 47 | |
Evgenii Stepanov | d13e9a6 | 2016-07-15 16:31:42 -0700 | [diff] [blame] | 48 | #include "private/bionic_macros.h" |
Yabin Cui | 7fb680b | 2015-02-24 13:18:25 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 50 | extern "C" { |
| 51 | |
Elliott Hughes | 5ffed9b | 2016-08-10 14:06:14 -0700 | [diff] [blame] | 52 | // LP64 doesn't need to support any legacy cruft. |
| 53 | #if !defined(__LP64__) |
Yabin Cui | 7fb680b | 2015-02-24 13:18:25 -0800 | [diff] [blame] | 54 | |
Elliott Hughes | dfcb82d | 2017-05-11 15:29:03 -0700 | [diff] [blame] | 55 | // By the time any NDK-built code is running, there are plenty of threads. |
| 56 | int __isthreaded = 1; |
| 57 | |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 58 | // These were accidentally declared in <unistd.h> because we stupidly used to inline |
| 59 | // getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 60 | unsigned int __page_size = PAGE_SIZE; |
| 61 | unsigned int __page_shift = 12; |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 62 | |
| 63 | // TODO: remove this backward compatibility hack (for jb-mr1 strace binaries). |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 64 | pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) { |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 65 | return wait4(pid, status, options, rusage); |
| 66 | } |
| 67 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 68 | // TODO: does anything still need this? |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 69 | int __open() { |
Elliott Hughes | 567a8de | 2013-10-24 17:14:55 -0700 | [diff] [blame] | 70 | abort(); |
| 71 | } |
| 72 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 73 | // TODO: does anything still need this? |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 74 | void** __get_tls() { |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 75 | #include "private/__get_tls.h" |
| 76 | return __get_tls(); |
| 77 | } |
| 78 | |
Elliott Hughes | 152b9de | 2014-03-10 15:54:40 -0700 | [diff] [blame] | 79 | // This non-standard function was in our <string.h> for some reason. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 80 | void memswap(void* m1, void* m2, size_t n) { |
Elliott Hughes | 152b9de | 2014-03-10 15:54:40 -0700 | [diff] [blame] | 81 | char* p = reinterpret_cast<char*>(m1); |
| 82 | char* p_end = p + n; |
| 83 | char* q = reinterpret_cast<char*>(m2); |
| 84 | while (p < p_end) { |
| 85 | char tmp = *p; |
| 86 | *p = *q; |
| 87 | *q = tmp; |
| 88 | p++; |
| 89 | q++; |
| 90 | } |
| 91 | } |
| 92 | |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 93 | int pthread_attr_setstackaddr(pthread_attr_t*, void*) { |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 94 | // This was removed from POSIX.1-2008, and is not implemented on bionic. |
| 95 | // Needed for ABI compatibility with the NDK. |
| 96 | return ENOSYS; |
| 97 | } |
| 98 | |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 99 | int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) { |
Calin Juravle | a4eafa6 | 2014-03-10 18:10:04 +0000 | [diff] [blame] | 100 | // This was removed from POSIX.1-2008. |
| 101 | // Needed for ABI compatibility with the NDK. |
| 102 | *stack_addr = (char*)attr->stack_base + attr->stack_size; |
| 103 | return 0; |
| 104 | } |
| 105 | |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 106 | // Non-standard cruft that should only ever have been in system/core/toolbox. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 107 | char* strtotimeval(const char* str, struct timeval* ts) { |
Elliott Hughes | efbdb53 | 2014-04-07 15:17:19 -0700 | [diff] [blame] | 108 | char* s; |
| 109 | ts->tv_sec = strtoumax(str, &s, 10); |
| 110 | |
| 111 | long fractional_seconds = 0; |
| 112 | if (*s == '.') { |
| 113 | s++; |
| 114 | int count = 0; |
| 115 | |
| 116 | // Read up to 6 digits (microseconds). |
| 117 | while (*s && isdigit(*s)) { |
| 118 | if (++count < 7) { |
| 119 | fractional_seconds = fractional_seconds*10 + (*s - '0'); |
| 120 | } |
| 121 | s++; |
| 122 | } |
| 123 | |
| 124 | for (; count < 6; count++) { |
| 125 | fractional_seconds *= 10; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | ts->tv_usec = fractional_seconds; |
| 130 | return s; |
| 131 | } |
| 132 | |
Elliott Hughes | eae5902 | 2014-04-22 17:56:42 -0700 | [diff] [blame] | 133 | static inline int digitval(int ch) { |
| 134 | unsigned d; |
| 135 | |
| 136 | d = (unsigned)(ch - '0'); |
| 137 | if (d < 10) return (int)d; |
| 138 | |
| 139 | d = (unsigned)(ch - 'a'); |
| 140 | if (d < 6) return (int)(d+10); |
| 141 | |
| 142 | d = (unsigned)(ch - 'A'); |
| 143 | if (d < 6) return (int)(d+10); |
| 144 | |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | // This non-standard function was in our <inttypes.h> for some reason. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 149 | uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) { |
Elliott Hughes | eae5902 | 2014-04-22 17:56:42 -0700 | [diff] [blame] | 150 | const unsigned char* p = (const unsigned char *)nptr; |
| 151 | const unsigned char* end = p + n; |
| 152 | int minus = 0; |
| 153 | uintmax_t v = 0; |
| 154 | int d; |
| 155 | |
| 156 | while (p < end && isspace(*p)) { |
| 157 | p++; |
| 158 | } |
| 159 | |
| 160 | if (p < end) { |
| 161 | char c = p[0]; |
| 162 | if (c == '-' || c == '+') { |
| 163 | minus = (c == '-'); |
| 164 | p++; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (base == 0) { |
| 169 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 170 | p += 2; |
| 171 | base = 16; |
| 172 | } else if (p+1 < end && p[0] == '0') { |
| 173 | p += 1; |
| 174 | base = 8; |
| 175 | } else { |
| 176 | base = 10; |
| 177 | } |
| 178 | } else if (base == 16) { |
| 179 | if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 180 | p += 2; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | while (p < end && (d = digitval(*p)) >= 0 && d < base) { |
| 185 | v = v*base + d; |
| 186 | p += 1; |
| 187 | } |
| 188 | |
| 189 | if (endptr) { |
| 190 | *endptr = (char*) p; |
| 191 | } |
| 192 | |
| 193 | return minus ? -v : v; |
| 194 | } |
| 195 | |
| 196 | // This non-standard function was in our <inttypes.h> for some reason. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 197 | intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) { |
Elliott Hughes | eae5902 | 2014-04-22 17:56:42 -0700 | [diff] [blame] | 198 | return (intmax_t) strntoumax(nptr, endptr, base, n); |
| 199 | } |
| 200 | |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 201 | // POSIX calls this dprintf, but LP32 Android had fdprintf instead. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 202 | int fdprintf(int fd, const char* fmt, ...) { |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 203 | va_list ap; |
| 204 | va_start(ap, fmt); |
| 205 | int rc = vdprintf(fd, fmt, ap); |
| 206 | va_end(ap); |
| 207 | return rc; |
| 208 | } |
| 209 | |
| 210 | // POSIX calls this vdprintf, but LP32 Android had fdprintf instead. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 211 | int vfdprintf(int fd, const char* fmt, va_list ap) { |
Elliott Hughes | fcac8ff | 2014-05-22 01:24:30 -0700 | [diff] [blame] | 212 | return vdprintf(fd, fmt, ap); |
| 213 | } |
| 214 | |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 215 | #define __futex_wake __real_futex_wake |
| 216 | #define __futex_wait __real_futex_wait |
| 217 | #include "private/bionic_futex.h" |
| 218 | #undef __futex_wake |
| 219 | #undef __futex_wait |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 220 | |
| 221 | // This used to be in <sys/atomics.h>. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 222 | int __futex_wake(volatile void* ftx, int count) { |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 223 | return __real_futex_wake(ftx, count); |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // This used to be in <sys/atomics.h>. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 227 | int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) { |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 228 | return __real_futex_wait(ftx, value, timeout); |
Elliott Hughes | bd3a98c | 2014-05-24 17:19:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Anthony King | 0017073 | 2014-05-24 16:47:14 +0000 | [diff] [blame] | 231 | // Unity's libmono uses this. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 232 | int tkill(pid_t tid, int sig) { |
Anthony King | 0017073 | 2014-05-24 16:47:14 +0000 | [diff] [blame] | 233 | return syscall(__NR_tkill, tid, sig); |
| 234 | } |
| 235 | |
Elliott Hughes | 1628eb1 | 2014-08-06 10:47:33 -0700 | [diff] [blame] | 236 | // This was removed from POSIX 2008. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 237 | wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) { |
Dan Albert | 001f8f0 | 2014-06-04 09:53:06 -0700 | [diff] [blame] | 238 | return wcsstr(haystack, needle); |
| 239 | } |
| 240 | |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 241 | // This was removed from POSIX 2008. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 242 | sighandler_t bsd_signal(int signum, sighandler_t handler) { |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 243 | return signal(signum, handler); |
| 244 | } |
| 245 | |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 246 | // This was removed from POSIX 2008. |
Logan Chien | b33952c | 2019-08-27 20:50:24 -0700 | [diff] [blame^] | 247 | #undef bcopy |
| 248 | void bcopy(const void* src, void* dst, size_t n) { |
Rohit Agrawal | d51a0b0 | 2015-12-05 12:39:54 -0800 | [diff] [blame] | 249 | memmove(dst, src, n); |
Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Elliott Hughes | 01d5b94 | 2016-03-02 17:18:18 -0800 | [diff] [blame] | 252 | // This was removed from POSIX 2008. |
Logan Chien | b33952c | 2019-08-27 20:50:24 -0700 | [diff] [blame^] | 253 | #undef bzero |
| 254 | void bzero(void* dst, size_t n) { |
Elliott Hughes | 01d5b94 | 2016-03-02 17:18:18 -0800 | [diff] [blame] | 255 | memset(dst, 0, n); |
| 256 | } |
| 257 | |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 258 | // sysv_signal() was never in POSIX. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 259 | extern "C++" sighandler_t _signal(int signum, sighandler_t handler, int flags); |
| 260 | sighandler_t sysv_signal(int signum, sighandler_t handler) { |
Dan Albert | 205dd7d | 2014-06-04 10:14:19 -0700 | [diff] [blame] | 261 | return _signal(signum, handler, SA_RESETHAND); |
| 262 | } |
| 263 | |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame] | 264 | // This is a system call that was never in POSIX. Use readdir(3) instead. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 265 | int __getdents64(unsigned int, dirent*, unsigned int); |
| 266 | int getdents(unsigned int fd, dirent* dirp, unsigned int count) { |
Elliott Hughes | 3d5cb30 | 2014-06-06 11:44:55 -0700 | [diff] [blame] | 267 | return __getdents64(fd, dirp, count); |
| 268 | } |
| 269 | |
Elliott Hughes | bffbfee | 2014-06-06 20:41:42 -0700 | [diff] [blame] | 270 | // This is a BSDism that we never implemented correctly. Used by Firefox. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 271 | int issetugid() { |
Elliott Hughes | bffbfee | 2014-06-06 20:41:42 -0700 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
Dan Albert | 8229ae4 | 2014-06-13 16:04:41 -0700 | [diff] [blame] | 275 | // This was removed from POSIX 2004. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 276 | pid_t wait3(int* status, int options, struct rusage* rusage) { |
Dan Albert | 8229ae4 | 2014-06-13 16:04:41 -0700 | [diff] [blame] | 277 | return wait4(-1, status, options, rusage); |
| 278 | } |
| 279 | |
Dan Albert | 462abab | 2014-06-13 16:51:24 -0700 | [diff] [blame] | 280 | // This was removed from POSIX 2004. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 281 | int getdtablesize() { |
Dan Albert | 462abab | 2014-06-13 16:51:24 -0700 | [diff] [blame] | 282 | struct rlimit r; |
| 283 | |
| 284 | if (getrlimit(RLIMIT_NOFILE, &r) < 0) { |
| 285 | return sysconf(_SC_OPEN_MAX); |
| 286 | } |
| 287 | |
| 288 | return r.rlim_cur; |
| 289 | } |
| 290 | |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 291 | // A leaked BSD stdio implementation detail that's now a no-op. |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 292 | void __sinit() {} |
| 293 | int __sdidinit = 1; |
Elliott Hughes | bb46afd | 2015-12-04 18:03:12 -0800 | [diff] [blame] | 294 | |
Elliott Hughes | 1628eb1 | 2014-08-06 10:47:33 -0700 | [diff] [blame] | 295 | // Only used by ftime, which was removed from POSIX 2008. |
Dan Albert | ac64675 | 2014-06-05 02:10:49 +0000 | [diff] [blame] | 296 | struct timeb { |
| 297 | time_t time; |
| 298 | unsigned short millitm; |
| 299 | short timezone; |
| 300 | short dstflag; |
| 301 | }; |
| 302 | |
| 303 | // This was removed from POSIX 2008. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 304 | int ftime(struct timeb* tb) { |
Dan Albert | ac64675 | 2014-06-05 02:10:49 +0000 | [diff] [blame] | 305 | struct timeval tv; |
| 306 | struct timezone tz; |
| 307 | |
| 308 | if (gettimeofday(&tv, &tz) < 0) |
| 309 | return -1; |
| 310 | |
| 311 | tb->time = tv.tv_sec; |
| 312 | tb->millitm = (tv.tv_usec + 500) / 1000; |
| 313 | |
| 314 | if (tb->millitm == 1000) { |
| 315 | ++tb->time; |
| 316 | tb->millitm = 0; |
| 317 | } |
| 318 | |
| 319 | tb->timezone = tz.tz_minuteswest; |
| 320 | tb->dstflag = tz.tz_dsttime; |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
David 'Digit' Turner | 891dedb | 2014-06-13 12:28:11 +0200 | [diff] [blame] | 325 | // This was removed from POSIX 2008. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 326 | char* index(const char* str, int ch) { |
George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 327 | return const_cast<char*>(strchr(str, ch)); |
David 'Digit' Turner | 891dedb | 2014-06-13 12:28:11 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Elliott Hughes | 5dea472 | 2014-09-03 15:53:11 -0700 | [diff] [blame] | 330 | // This was removed from BSD. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 331 | void arc4random_stir(void) { |
Elliott Hughes | 5dea472 | 2014-09-03 15:53:11 -0700 | [diff] [blame] | 332 | // The current implementation stirs itself as needed. |
| 333 | } |
| 334 | |
Elliott Hughes | fc82973 | 2014-09-08 10:25:33 -0700 | [diff] [blame] | 335 | // This was removed from BSD. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 336 | void arc4random_addrandom(unsigned char*, int) { |
Elliott Hughes | fc82973 | 2014-09-08 10:25:33 -0700 | [diff] [blame] | 337 | // The current implementation adds randomness as needed. |
| 338 | } |
| 339 | |
Christopher Ferris | f903558 | 2014-09-05 16:39:22 -0700 | [diff] [blame] | 340 | // Old versions of the NDK did not export malloc_usable_size, but did |
| 341 | // export dlmalloc_usable_size. We are moving away from dlmalloc in L |
| 342 | // so make this call malloc_usable_size. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 343 | size_t dlmalloc_usable_size(void* ptr) { |
Christopher Ferris | f903558 | 2014-09-05 16:39:22 -0700 | [diff] [blame] | 344 | return malloc_usable_size(ptr); |
| 345 | } |
| 346 | |
Elliott Hughes | 0f001b6 | 2014-09-12 11:35:05 -0700 | [diff] [blame] | 347 | // In L we added a public pthread_gettid_np, but some apps were using the private API. |
Dimitry Ivanov | bba3954 | 2016-01-21 22:25:32 +0000 | [diff] [blame] | 348 | pid_t __pthread_gettid(pthread_t t) { |
Elliott Hughes | 0f001b6 | 2014-09-12 11:35:05 -0700 | [diff] [blame] | 349 | return pthread_gettid_np(t); |
| 350 | } |
| 351 | |
Christopher Ferris | f9554a1 | 2015-06-05 17:12:17 -0700 | [diff] [blame] | 352 | // Older versions of apportable used dlmalloc directly instead of malloc, |
Christopher Ferris | f183f95 | 2014-10-08 22:48:20 -0700 | [diff] [blame] | 353 | // so export this compatibility shim that simply calls malloc. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 354 | void* dlmalloc(size_t size) { |
Christopher Ferris | f183f95 | 2014-10-08 22:48:20 -0700 | [diff] [blame] | 355 | return malloc(size); |
| 356 | } |
| 357 | |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 358 | } // extern "C" |
| 359 | |
Yabin Cui | 2f836d4 | 2015-03-18 14:14:02 -0700 | [diff] [blame] | 360 | #define __get_thread __real_get_thread |
| 361 | #include "pthread_internal.h" |
| 362 | #undef __get_thread |
Ryan Prichard | 16455b5 | 2019-01-18 01:00:59 -0800 | [diff] [blame] | 363 | |
| 364 | extern "C" { |
| 365 | |
Yabin Cui | 2f836d4 | 2015-03-18 14:14:02 -0700 | [diff] [blame] | 366 | // Various third-party apps contain a backport of our pthread_rwlock implementation that uses this. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 367 | pthread_internal_t* __get_thread() { |
Yabin Cui | 2f836d4 | 2015-03-18 14:14:02 -0700 | [diff] [blame] | 368 | return __real_get_thread(); |
| 369 | } |
| 370 | |
Christopher Ferris | 9978a9a | 2015-10-29 18:11:32 -0700 | [diff] [blame] | 371 | // This one exists only for the LP32 NDK and is not present anywhere else. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 372 | extern long __set_errno_internal(int); |
| 373 | long __set_errno(int n) { |
Christopher Ferris | 9978a9a | 2015-10-29 18:11:32 -0700 | [diff] [blame] | 374 | return __set_errno_internal(n); |
| 375 | } |
| 376 | |
Christopher Ferris | f9554a1 | 2015-06-05 17:12:17 -0700 | [diff] [blame] | 377 | // Since dlmalloc_inspect_all and dlmalloc_trim are exported for systems |
| 378 | // that use dlmalloc, be consistent and export them everywhere. |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 379 | void dlmalloc_inspect_all(void (*)(void*, void*, size_t, void*), void*) { |
Christopher Ferris | f9554a1 | 2015-06-05 17:12:17 -0700 | [diff] [blame] | 380 | } |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 381 | int dlmalloc_trim(size_t) { |
Elliott Hughes | fb8fd50 | 2015-10-12 17:53:48 -0700 | [diff] [blame] | 382 | return 0; |
| 383 | } |
Elliott Hughes | fb8fd50 | 2015-10-12 17:53:48 -0700 | [diff] [blame] | 384 | |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 385 | // LP32's <stdio.h> had putw (but not getw). |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 386 | int putw(int value, FILE* fp) { |
| 387 | return fwrite(&value, sizeof(value), 1, fp) == 1 ? 0 : EOF; |
| 388 | } |
Elliott Hughes | 5ffed9b | 2016-08-10 14:06:14 -0700 | [diff] [blame] | 389 | |
| 390 | #endif // !defined (__LP64__) |
Elliott Hughes | 53cf348 | 2016-08-09 13:06:41 -0700 | [diff] [blame] | 391 | |
Elliott Hughes | cfd5a46 | 2015-12-04 15:57:51 -0800 | [diff] [blame] | 392 | } // extern "C" |