util: detect page size runtime.

Some architectures support multiple machine types with diffenent
page sizes, and some machine types even support multiple
page sizes themselves.
This commit is contained in:
cee1 2011-03-18 10:03:41 +08:00 committed by Lennart Poettering
parent f9276855a1
commit 37f85e66e8
5 changed files with 23 additions and 10 deletions

View file

@ -27,8 +27,6 @@
#include <sys/uio.h>
#include <inttypes.h>
#define PAGE_SIZE 4096
#define _printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
#define _sentinel_ __attribute__ ((sentinel))
#define _noreturn_ __attribute__((noreturn))
@ -51,12 +49,9 @@
#define STRINGIFY(x) XSTRINGIFY(x)
/* Rounds up */
static inline size_t ALIGN(size_t l) {
return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1));
}
static inline size_t PAGE_ALIGN(size_t l) {
return ((l + PAGE_SIZE - 1) & ~(PAGE_SIZE -1));
#define ALIGN(l) ALIGN_TO((l), sizeof(void*))
static inline size_t ALIGN_TO(size_t l, size_t ali) {
return ((l + ali - 1) & ~(ali - 1));
}
#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))

View file

@ -119,9 +119,10 @@ static int pack_file(FILE *pack, const char *fn, bool on_btrfs) {
goto finish;
}
pages = l / PAGE_SIZE;
pages = l / page_size();
vec = alloca(pages);
memset(vec, 0, pages);
if (mincore(start, l, vec) < 0) {
log_warning("mincore(%s) failed: %m", fn);
r = -errno;

View file

@ -94,7 +94,7 @@ static int unpack_file(FILE *pack) {
any = true;
if (fd >= 0)
if (posix_fadvise(fd, b * PAGE_SIZE, (c - b) * PAGE_SIZE, POSIX_FADV_WILLNEED) < 0) {
if (posix_fadvise(fd, b * page_size(), (c - b) * page_size(), POSIX_FADV_WILLNEED) < 0) {
log_warning("posix_fadvise() failed: %m");
goto finish;
}

View file

@ -61,6 +61,20 @@
#include "exit-status.h"
#include "hashmap.h"
size_t page_size(void) {
static __thread size_t pgsz = 0;
long r;
if (pgsz)
return pgsz;
assert_se((r = sysconf(_SC_PAGESIZE)) > 0);
pgsz = (size_t) r;
return pgsz;
}
bool streq_ptr(const char *a, const char *b) {
/* Like streq(), but tries to make sense of NULL pointers */

View file

@ -83,6 +83,9 @@ struct timespec *timespec_store(struct timespec *ts, usec_t u);
usec_t timeval_load(const struct timeval *tv);
struct timeval *timeval_store(struct timeval *tv, usec_t u);
size_t page_size(void);
#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
#define streq(a,b) (strcmp((a),(b)) == 0)
#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)