[PATCH] lib: fwts_mmap: add fwts_page_size()
Colin King
colin.king at canonical.com
Sat Oct 20 19:49:01 UTC 2012
From: Colin Ian King <colin.king at canonical.com>
Add a helper function fwts_page_size() to determine the system
page size. Default to 4096 if sysconf() does not help us.
Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
src/lib/include/fwts_mmap.h | 2 ++
src/lib/src/fwts_mmap.c | 25 ++++++++++++++++++-------
2 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/src/lib/include/fwts_mmap.h b/src/lib/include/fwts_mmap.h
index 62d8f1c..4a76489 100644
--- a/src/lib/include/fwts_mmap.h
+++ b/src/lib/include/fwts_mmap.h
@@ -20,8 +20,10 @@
#ifndef __FWTS_MMAP_H__
#define __FWTS_MMAP_H__
+#include <stddef.h>
#include <sys/mman.h>
+const size_t fwts_page_size(void);
void *fwts_mmap(const off_t start, const size_t size);
int fwts_munmap(void *mem, const size_t size);
diff --git a/src/lib/src/fwts_mmap.c b/src/lib/src/fwts_mmap.c
index c47960d..cee0854 100644
--- a/src/lib/src/fwts_mmap.c
+++ b/src/lib/src/fwts_mmap.c
@@ -24,7 +24,22 @@
#include "fwts.h"
-#define PAGE_SIZE 4096
+#define FWTS_DEFAULT_PAGE_SIZE (4096)
+
+/*
+ * fwts_page_size()
+ * determine system page size, guess if we can't
+ * get it from sysconf().
+ */
+const size_t fwts_page_size(void)
+{
+ size_t page_size;
+
+ page_size = sysconf(_SC_PAGESIZE);
+
+ /* If sysconf() returns -1, default it 4K */
+ return page_size == -1 ? FWTS_DEFAULT_PAGE_SIZE : page_size;
+}
/*
* fwts_mmap()
@@ -40,9 +55,7 @@ void *fwts_mmap(const off_t start, const size_t size)
void *mem;
void *ret = FWTS_MAP_FAILED;
- if ((page_size = sysconf(_SC_PAGE_SIZE)) == -1)
- page_size = PAGE_SIZE; /* Guess */
-
+ page_size = fwts_page_size();
offset = ((size_t)start) & (page_size - 1);
length = (size_t)size + offset;
@@ -66,9 +79,7 @@ int fwts_munmap(void *mem, const size_t size)
int page_size;
off_t offset;
- if ((page_size = sysconf(_SC_PAGE_SIZE)) == -1)
- page_size = PAGE_SIZE; /* Guess */
-
+ page_size = fwts_page_size();
offset = ((off_t)(mem)) & (page_size - 1);
if (munmap(mem - offset, size + offset) < 0)
--
1.7.10.4
More information about the fwts-devel
mailing list