1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef PORTABILITY_H_included 18#define PORTABILITY_H_included 19 20#if defined(__APPLE__) 21 22// Mac OS. 23#include <AvailabilityMacros.h> // For MAC_OS_X_VERSION_MAX_ALLOWED 24 25#include <libkern/OSByteOrder.h> 26#define bswap_16 OSSwapInt16 27#define bswap_32 OSSwapInt32 28#define bswap_64 OSSwapInt64 29 30#include <crt_externs.h> 31#define environ (*_NSGetEnviron()) 32 33// Mac OS has a 64-bit off_t and no 32-bit compatibility cruft. 34#define flock64 flock 35#define ftruncate64 ftruncate 36#define isnanf __inline_isnanf 37#define lseek64 lseek 38#define pread64 pread 39#define pwrite64 pwrite 40 41// TODO: Darwin appears to have an fdatasync syscall. 42static inline int fdatasync(int fd) { return fsync(fd); } 43 44// For Linux-compatible sendfile(3). 45#include <sys/socket.h> 46#include <sys/types.h> 47static inline ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count) { 48 off_t in_out_count = count; 49 int result = sendfile(in_fd, out_fd, *offset, &in_out_count, NULL, 0); 50 if (result == -1) { 51 return -1; 52 } 53 return in_out_count; 54} 55 56// For mincore(3). 57#define _DARWIN_C_SOURCE 58#include <sys/mman.h> 59#undef _DARWIN_C_SOURCE 60static inline int mincore(void* addr, size_t length, unsigned char* vec) { 61 return mincore(addr, length, reinterpret_cast<char*>(vec)); 62} 63 64// For statfs(3). 65#include <sys/param.h> 66#include <sys/mount.h> 67 68#else 69 70// Bionic or glibc. 71 72#include <byteswap.h> 73#include <sys/sendfile.h> 74#include <sys/statvfs.h> 75 76#endif 77 78#include <netdb.h> 79#if defined(__BIONIC__) 80extern "C" int android_getaddrinfofornet(const char*, const char*, const struct addrinfo*, unsigned, unsigned, struct addrinfo**); 81#else 82static inline int android_getaddrinfofornet(const char* hostname, const char* servname, 83 const struct addrinfo* hints, unsigned /*netid*/, unsigned /*mark*/, struct addrinfo** res) { 84 return getaddrinfo(hostname, servname, hints, res); 85} 86#endif 87 88#if defined(__GLIBC__) && !defined(__LP64__) 89 90#include <unistd.h> 91 92// 32 bit GLIBC hardcodes a "long int" as the return type for 93// TEMP_FAILURE_RETRY so the return value here gets truncated for 94// functions that return 64 bit types. 95#undef TEMP_FAILURE_RETRY 96#define TEMP_FAILURE_RETRY(exp) ({ \ 97 __typeof__(exp) _rc; \ 98 do { \ 99 _rc = (exp); \ 100 } while (_rc == -1 && errno == EINTR); \ 101 _rc; }) 102 103#endif // __GLIBC__ && !__LP64__ 104 105#endif // PORTABILITY_H_included 106