Compat.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2010 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. #ifndef __LIB_UTILS_COMPAT_H
  17. #define __LIB_UTILS_COMPAT_H
  18. #include <unistd.h>
  19. #if !defined(__MINGW32__)
  20. #include <sys/mman.h>
  21. #endif
  22. #if defined(__APPLE__)
  23. /* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */
  24. static_assert(sizeof(off_t) >= 8, "This code requires that Mac OS have at least a 64-bit off_t.");
  25. typedef off_t off64_t;
  26. static inline void* mmap64(void* addr, size_t length, int prot, int flags, int fd, off64_t offset) {
  27. return mmap(addr, length, prot, flags, fd, offset);
  28. }
  29. static inline off64_t lseek64(int fd, off64_t offset, int whence) {
  30. return lseek(fd, offset, whence);
  31. }
  32. static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) {
  33. return pread(fd, buf, nbytes, offset);
  34. }
  35. static inline ssize_t pwrite64(int fd, const void* buf, size_t nbytes, off64_t offset) {
  36. return pwrite(fd, buf, nbytes, offset);
  37. }
  38. static inline int ftruncate64(int fd, off64_t length) {
  39. return ftruncate(fd, length);
  40. }
  41. #endif /* __APPLE__ */
  42. #if defined(_WIN32)
  43. #define O_CLOEXEC O_NOINHERIT
  44. #define O_NOFOLLOW 0
  45. #define DEFFILEMODE 0666
  46. #endif /* _WIN32 */
  47. #define ZD "%zd"
  48. #define ZD_TYPE ssize_t
  49. /*
  50. * Needed for cases where something should be constexpr if possible, but not
  51. * being constexpr is fine if in pre-C++11 code (such as a const static float
  52. * member variable).
  53. */
  54. #if __cplusplus >= 201103L
  55. #define CONSTEXPR constexpr
  56. #else
  57. #define CONSTEXPR
  58. #endif
  59. /*
  60. * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
  61. * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
  62. * not already defined, then define it here.
  63. */
  64. #ifndef TEMP_FAILURE_RETRY
  65. /* Used to retry syscalls that can return EINTR. */
  66. #define TEMP_FAILURE_RETRY(exp) ({ \
  67. decltype (exp) _rc; \
  68. do { \
  69. _rc = (exp); \
  70. } while (_rc == -1 && errno == EINTR); \
  71. _rc; })
  72. #endif
  73. #if defined(_WIN32)
  74. #define OS_PATH_SEPARATOR '\\'
  75. #else
  76. #define OS_PATH_SEPARATOR '/'
  77. #endif
  78. #endif /* __LIB_UTILS_COMPAT_H */