2 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
3 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
7 #define MOG_STR(s) MOG_STR0(s)
11 * some systems define EWOULDBLOCK to a different value than EAGAIN,
12 * but POSIX allows them to be identical.
14 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
15 # define case_EAGAIN case EAGAIN: case EWOULDBLOCK
17 # define case_EAGAIN case EAGAIN
20 /* free(3) causes compiler warnings on const, so we de-const here */
21 static inline void mog_free(const void *ptr
)
23 union { const void *in
; void *out
; } deconst
= { .in
= ptr
};
28 #define PRESERVE_ERRNO(code) do { \
29 int save_err = errno; \
34 # define CHECK(type, expect, expr) do { \
35 type checkvar = (expr); \
36 assert(checkvar==(expect)&& "BUG" && __FILE__ && __LINE__); \
39 /* compiler should optimize this away */
40 __attribute__((const)) static inline off_t
off_t_max(void)
42 return (off_t
)(sizeof(long) == sizeof(off_t
) ? LONG_MAX
: LLONG_MAX
);
45 #if defined(HAVE_IOCTL) && defined(FIONBIO)
47 * FIONBIO * requires only one syscall for reliable operation rather than
48 * two syscalls with fcntl() calls, so use it if possible.
50 static inline int mog_set_nonblocking(int fd
, const bool value
)
52 int flag
= value
? 1 : 0;
54 return ioctl(fd
, FIONBIO
, &flag
);
56 #else /* use gnulib */
57 #include "nonblocking.h"
58 #define mog_set_nonblocking(fd, value) set_nonblocking_flag((fd), (value))
62 * the only FD_* flag that exists as of 2012 is FD_CLOEXEC, if new ones
63 * ever get defined, we wouldn't be using them in the first place without
64 * updating this code... (no way they'd be on by default).
66 static inline int mog_set_cloexec(int fd
, const bool set
) /* vfork-safe */
68 return fcntl(fd
, F_SETFD
, set
? FD_CLOEXEC
: 0);
71 static inline bool mog_pthread_create_retryable(const int err
)
74 * older versions of glibc return ENOMEM instead of EAGAIN
75 * ref: https://www.sourceware.org/bugzilla/show_bug.cgi?id=386
76 * Remove the ENOMEM check by 2023 (unless other OSes have this
79 return (err
== EAGAIN
|| err
== ENOMEM
);