pcu.git  about / heads / tags
page cache utilities for Linux
blob 8b70b526814edd4b901b9ae181c03b8cf1da5748 1448 bytes (raw)
$ git show v0.2.1:compat-util.h	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 
#ifndef OS_COMPAT_H
#define OS_COMPAT_H

#define _LARGE_FILES
#define _FILE_OFFSET_BITS 64
#define _BSD_SOURCE /* for mincore */
#define _XOPEN_SOURCE 600 /* for posix_fadvise */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <stdint.h>

#ifndef O_NOATIME
#  define O_NOATIME 0
#endif /* O_NOATIME */

#define PAGE_MASK               (~(page_size() -1))
#define PAGE_ALIGN(addr)        (((addr) + page_size() - 1) & PAGE_MASK)
#define PAGE_ALIGN_DOWN(addr) \
	(addr > page_size() ? PAGE_ALIGN(addr) - page_size() : 0)

static inline size_t page_size(void)
{
	static size_t size;

	if (!size)
		size = sysconf(_SC_PAGESIZE);

	return size;
}

#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))

/*
 * converts a C string to a non-negative off_t value, taking base into
 * account.  On error, it'll return a negative value and set errno
 * to EINVAL
 */
static off_t cstr_to_off_t(const char *nptr, char **endptr, int base)
{
	if (sizeof(long) == 8 || (sizeof(long) == 4 && sizeof(off_t) == 4))
		return (off_t)strtol(nptr, endptr, base);
	else if (sizeof(off_t) == 8 && sizeof(long) == 4)
		return (off_t)strtoll(nptr, endptr, base);

	fprintf(stderr, "unrecognized sizes:\n\toff_t: %u\n\tlong: %u\n",
	                sizeof(off_t), sizeof(long));
	exit(1);
}

#endif /* OS_COMPAT_H */

git clone https://yhbt.net/pcu.git