Compat.h 2.3 KB

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