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