ndk_cruft.cpp revision 430cf1a6c32471ada4dad028acbfcc032da01fd3
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 29// This file perpetuates the mistakes of the past, but only for 32-bit targets. 30#if !defined(__LP64__) 31 32#include <ctype.h> 33#include <inttypes.h> 34#include <pthread.h> 35#include <stdio.h> 36#include <stdlib.h> 37#include <sys/resource.h> 38#include <sys/syscall.h> 39#include <sys/time.h> 40#include <sys/types.h> 41#include <sys/wait.h> 42#include <unistd.h> 43 44// These were accidentally declared in <unistd.h> because we stupidly used to inline 45// getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps. 46extern "C" { 47 unsigned int __page_size = PAGE_SIZE; 48 unsigned int __page_shift = 12; 49} 50 51// TODO: remove this backward compatibility hack (for jb-mr1 strace binaries). 52extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) { 53 return wait4(pid, status, options, rusage); 54} 55 56// TODO: does anything still need this? 57extern "C" int __open() { 58 abort(); 59} 60 61// TODO: does anything still need this? 62extern "C" void** __get_tls() { 63#include "private/__get_tls.h" 64 return __get_tls(); 65} 66 67// This non-standard function was in our <string.h> for some reason. 68extern "C" void memswap(void* m1, void* m2, size_t n) { 69 char* p = reinterpret_cast<char*>(m1); 70 char* p_end = p + n; 71 char* q = reinterpret_cast<char*>(m2); 72 while (p < p_end) { 73 char tmp = *p; 74 *p = *q; 75 *q = tmp; 76 p++; 77 q++; 78 } 79} 80 81extern "C" int pthread_attr_setstackaddr(pthread_attr_t*, void*) { 82 // This was removed from POSIX.1-2008, and is not implemented on bionic. 83 // Needed for ABI compatibility with the NDK. 84 return ENOSYS; 85} 86 87extern "C" int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) { 88 // This was removed from POSIX.1-2008. 89 // Needed for ABI compatibility with the NDK. 90 *stack_addr = (char*)attr->stack_base + attr->stack_size; 91 return 0; 92} 93 94// Non-standard cruft that should only ever have been in system/core/toolbox. 95extern "C" char* strtotimeval(const char* str, struct timeval* ts) { 96 char* s; 97 ts->tv_sec = strtoumax(str, &s, 10); 98 99 long fractional_seconds = 0; 100 if (*s == '.') { 101 s++; 102 int count = 0; 103 104 // Read up to 6 digits (microseconds). 105 while (*s && isdigit(*s)) { 106 if (++count < 7) { 107 fractional_seconds = fractional_seconds*10 + (*s - '0'); 108 } 109 s++; 110 } 111 112 for (; count < 6; count++) { 113 fractional_seconds *= 10; 114 } 115 } 116 117 ts->tv_usec = fractional_seconds; 118 return s; 119} 120 121static inline int digitval(int ch) { 122 unsigned d; 123 124 d = (unsigned)(ch - '0'); 125 if (d < 10) return (int)d; 126 127 d = (unsigned)(ch - 'a'); 128 if (d < 6) return (int)(d+10); 129 130 d = (unsigned)(ch - 'A'); 131 if (d < 6) return (int)(d+10); 132 133 return -1; 134} 135 136// This non-standard function was in our <inttypes.h> for some reason. 137extern "C" uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) { 138 const unsigned char* p = (const unsigned char *)nptr; 139 const unsigned char* end = p + n; 140 int minus = 0; 141 uintmax_t v = 0; 142 int d; 143 144 while (p < end && isspace(*p)) { 145 p++; 146 } 147 148 if (p < end) { 149 char c = p[0]; 150 if (c == '-' || c == '+') { 151 minus = (c == '-'); 152 p++; 153 } 154 } 155 156 if (base == 0) { 157 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 158 p += 2; 159 base = 16; 160 } else if (p+1 < end && p[0] == '0') { 161 p += 1; 162 base = 8; 163 } else { 164 base = 10; 165 } 166 } else if (base == 16) { 167 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 168 p += 2; 169 } 170 } 171 172 while (p < end && (d = digitval(*p)) >= 0 && d < base) { 173 v = v*base + d; 174 p += 1; 175 } 176 177 if (endptr) { 178 *endptr = (char*) p; 179 } 180 181 return minus ? -v : v; 182} 183 184// This non-standard function was in our <inttypes.h> for some reason. 185extern "C" intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) { 186 return (intmax_t) strntoumax(nptr, endptr, base, n); 187} 188 189// POSIX calls this vdprintf, but LP32 Android had fdprintf instead. 190extern "C" int vfdprintf(int fd, const char* fmt, va_list ap) { 191 return vdprintf(fd, fmt, ap); 192} 193 194#define __futex_wake __real_futex_wake 195#define __futex_wait __real_futex_wait 196#include "private/bionic_futex.h" 197#undef __futex_wake 198#undef __futex_wait 199 200// This used to be in <sys/atomics.h>. 201extern "C" int __futex_wake(volatile void* ftx, int count) { 202 return __real_futex_wake(ftx, count); 203} 204 205// This used to be in <sys/atomics.h>. 206extern "C" int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) { 207 return __real_futex_wait(ftx, value, timeout); 208} 209 210// Unity's libmono uses this. 211extern "C" int tkill(pid_t tid, int sig) { 212 return syscall(__NR_tkill, tid, sig); 213} 214 215#endif 216