rename pcu-sync -> pcu-fsync
[pcu.git] / compat-util.h
blob00f2fee7f352a301017b6084f04d766cc7caf858
1 #ifndef OS_COMPAT_H
2 #define OS_COMPAT_H
4 #define _LARGE_FILES
5 #define _FILE_OFFSET_BITS 64
6 #define _BSD_SOURCE /* for mincore */
7 #define _XOPEN_SOURCE 600 /* for posix_fadvise */
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <sys/mman.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <assert.h>
18 #include <stdint.h>
20 #ifndef O_NOATIME
21 # define O_NOATIME 0
22 #endif /* O_NOATIME */
24 #define PAGE_MASK (~(page_size() -1))
25 #define PAGE_ALIGN(addr) (((addr) + page_size() - 1) & PAGE_MASK)
26 #define PAGE_ALIGN_DOWN(addr) \
27 (addr > page_size() ? PAGE_ALIGN(addr) - page_size() : 0)
29 static inline size_t page_size(void)
31 static size_t size;
33 if (!size)
34 size = sysconf(_SC_PAGESIZE);
36 return size;
39 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
42 * converts a C string to a non-negative off_t value, taking base into
43 * account. On error, it'll return a negative value and set errno
44 * to EINVAL
46 static off_t cstr_to_off_t(const char *nptr, char **endptr, int base)
48 if (sizeof(long) == 8 || (sizeof(long) == 4 && sizeof(off_t) == 4))
49 return (off_t)strtol(nptr, endptr, base);
50 else if (sizeof(off_t) == 8 && sizeof(long) == 4)
51 return (off_t)strtoll(nptr, endptr, base);
53 fprintf(stderr, "unrecognized sizes:\n\toff_t: %u\n\tlong: %u\n",
54 (unsigned)sizeof(off_t), (unsigned)sizeof(long));
55 exit(1);
58 #endif /* OS_COMPAT_H */