Patchwork [RESEND] lib: fwts_mmap: add fwts_page_size()

login
register
mail settings
Submitter Colin King
Date Oct. 21, 2012, 12:32 p.m.
Message ID <1350822731-32309-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/192993/
State Accepted
Headers show

Comments

Colin King - Oct. 21, 2012, 12:32 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(-)
Alex Hung - Oct. 24, 2012, 9:32 a.m.
On 10/21/2012 08:32 PM, 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>
>
> +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().
> + */
> +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)
>

Acked-by: Alex Hung <alex.hung@canonical.com>
Keng-Yu Lin - Oct. 25, 2012, 6:50 a.m.
On Sun, Oct 21, 2012 at 8:32 PM, Colin King <colin.king@canonical.com> 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>
>
> +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().
> + */
> +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
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>

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>
 
+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().
+ */
+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)