Makefile: ghetto generation task for the website
[pcu.git] / compat-util.h
blobd3520e1785cea1a225d844f7585abae3d74153eb
1 #ifndef OS_COMPAT_H
2 #define OS_COMPAT_H
4 #define _GNU_SOURCE
5 #define _LARGE_FILES
6 #define _FILE_OFFSET_BITS 64
7 #define _BSD_SOURCE /* for mincore */
8 #define _XOPEN_SOURCE 600 /* for posix_fadvise */
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <sys/mman.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <errno.h>
18 #include <assert.h>
19 #include <stdint.h>
21 #ifndef O_NOATIME
22 # define O_NOATIME 0
23 #endif /* O_NOATIME */
25 #define PAGE_MASK (~(page_size() -1))
26 #define PAGE_ALIGN(addr) (((addr) + page_size() - 1) & PAGE_MASK)
27 #define PAGE_ALIGN_DOWN(addr) \
28 (addr > page_size() ? PAGE_ALIGN(addr) - page_size() : 0)
30 static inline size_t page_size(void)
32 static size_t size;
34 if (!size)
35 size = sysconf(_SC_PAGESIZE);
37 return size;
40 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
43 * converts a C string to a non-negative off_t value, taking base into
44 * account. On error, it'll return a negative value and set errno
45 * to EINVAL
47 static inline off_t cstr_to_off_t(const char *nptr, char **endptr, int base)
49 if (sizeof(long) == 8 || (sizeof(long) == 4 && sizeof(off_t) == 4))
50 return (off_t)strtol(nptr, endptr, base);
51 else if (sizeof(off_t) == 8 && sizeof(long) == 4)
52 return (off_t)strtoll(nptr, endptr, base);
54 fprintf(stderr, "unrecognized sizes:\n\toff_t: %u\n\tlong: %u\n",
55 (unsigned)sizeof(off_t), (unsigned)sizeof(long));
56 exit(1);
59 #endif /* OS_COMPAT_H */