blob: 7d96310272e117204fa2fd158fd749136dd78210 [file] [log] [blame]
Kenny Roote2fa7dc2010-11-24 12:56:06 -08001/*
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
Elliott Hughescb3d6532014-09-22 20:50:54 -070022#if defined(__APPLE__)
23
24/* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */
Kenny Roote2fa7dc2010-11-24 12:56:06 -080025
26typedef off_t off64_t;
27
28static inline off64_t lseek64(int fd, off64_t offset, int whence) {
29 return lseek(fd, offset, whence);
30}
31
Kenny Roote2fa7dc2010-11-24 12:56:06 -080032static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) {
33 return pread(fd, buf, nbytes, offset);
34}
Kenny Roote2fa7dc2010-11-24 12:56:06 -080035
Elliott Hughescb3d6532014-09-22 20:50:54 -070036#endif /* __APPLE__ */
Kenny Roote2fa7dc2010-11-24 12:56:06 -080037
Elliott Hughes714196d2015-02-03 14:26:58 -080038#if defined(_WIN32)
Dan Albertc6b30f32015-03-26 23:22:56 -070039#define O_CLOEXEC O_NOINHERIT
Elliott Hughes714196d2015-02-03 14:26:58 -080040#define O_NOFOLLOW 0
41#define DEFFILEMODE 0666
42#endif /* _WIN32 */
43
Elliott Hughes146c2442015-04-03 12:53:36 -070044#if defined(_WIN32)
45#define ZD "%ld"
46#define ZD_TYPE long
Kenny Rootb9fd6f92012-10-16 11:34:02 -070047#else
Elliott Hughes146c2442015-04-03 12:53:36 -070048#define ZD "%zd"
49#define ZD_TYPE ssize_t
Kenny Rootb9fd6f92012-10-16 11:34:02 -070050#endif
51
52/*
Dan Alberte4c649c2014-11-20 10:47:55 -080053 * Needed for cases where something should be constexpr if possible, but not
54 * being constexpr is fine if in pre-C++11 code (such as a const static float
55 * member variable).
56 */
57#if __cplusplus >= 201103L
58#define CONSTEXPR constexpr
59#else
60#define CONSTEXPR
61#endif
62
63/*
Kenny Rootb9fd6f92012-10-16 11:34:02 -070064 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
65 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
66 * not already defined, then define it here.
67 */
68#ifndef TEMP_FAILURE_RETRY
69/* Used to retry syscalls that can return EINTR. */
70#define TEMP_FAILURE_RETRY(exp) ({ \
71 typeof (exp) _rc; \
72 do { \
73 _rc = (exp); \
74 } while (_rc == -1 && errno == EINTR); \
75 _rc; })
76#endif
77
Kenny Roote2fa7dc2010-11-24 12:56:06 -080078#endif /* __LIB_UTILS_COMPAT_H */