Elliott Hughes | 6c1e5f4 | 2013-01-25 15:10:58 -0800 | [diff] [blame] | 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. |
| 42 | static 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> |
| 47 | static 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 |
| 60 | static 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 | #define f_frsize f_bsize // TODO: close enough? |
| 68 | |
| 69 | #else |
| 70 | |
| 71 | // Bionic or glibc. |
| 72 | |
| 73 | #include <byteswap.h> |
| 74 | #include <sys/sendfile.h> |
Elliott Hughes | 721ceca | 2013-07-09 14:45:43 -0700 | [diff] [blame] | 75 | #include <sys/statvfs.h> |
Elliott Hughes | 6c1e5f4 | 2013-01-25 15:10:58 -0800 | [diff] [blame] | 76 | |
Michael Wright | d87d883 | 2013-09-25 17:49:44 -0700 | [diff] [blame] | 77 | // The prebuilt toolchains tend to be rather old and don't include the newest |
| 78 | // kernel headers. CAP_BLOCK_SUSPEND was introduced in 3.5, so until all of |
| 79 | // headers update to at least that version, we need this hack. |
| 80 | #ifndef CAP_BLOCK_SUSPEND |
| 81 | #define CAP_BLOCK_SUSPEND 36 |
| 82 | #define CAP_LAST_CAP CAP_BLOCK_SUSPEND |
| 83 | #endif // ifndef CAP_BLOCK_SUSPEND |
| 84 | |
Elliott Hughes | 6c1e5f4 | 2013-01-25 15:10:58 -0800 | [diff] [blame] | 85 | #endif |
| 86 | |
| 87 | #endif // PORTABILITY_H_included |