Patchwork lib: fwts_mmap: add fwts_page_size()

login
register
mail settings
Submitter Colin King
Date Oct. 20, 2012, 7:49 p.m.
Message ID <1350762541-5606-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/192944/
State Rejected
Headers show

Comments

Colin King - Oct. 20, 2012, 7:49 p.m.
From: Colin Ian King <colin.king@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@canonical.com>
---
 src/lib/include/fwts_mmap.h |    2 ++
 src/lib/src/fwts_mmap.c     |   25 ++++++++++++++++++-------
 2 files changed, 20 insertions(+), 7 deletions(-)
Colin King - Oct. 21, 2012, 12:33 p.m.
NACK my own patch, resending.

On 20/10/12 20:49, Colin King wrote:
> From: Colin Ian King <colin.king@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@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)
>

Patch

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)