[Pixman] [PATCH] Replace the non-standard call to getpagesize() by POSIX sysconf()

Dmitri Vorobiev dmitri.vorobiev at movial.com
Fri Sep 24 04:43:32 PDT 2010


Unlike sysconf(_SC_PAGESIZE), getpagesize() is not in POSIX. So,
let's use the former and be closer to the standard.

While at it, this patch also removes an unnecessary typecast of
MAP_FAILED, replaces an erroneous free() by the correct munmap()
in the error path for a failing mprotect(), and, finally, removes
redundant calls to mprotect() that aren't necessary, because
munmap() doesn't call for any specific memory protection.
---
 configure.ac |    5 -----
 test/utils.c |   33 +++++++++------------------------
 2 files changed, 9 insertions(+), 29 deletions(-)

diff --git a/configure.ac b/configure.ac
index fc08def..30ef008 100644
--- a/configure.ac
+++ b/configure.ac
@@ -634,11 +634,6 @@ if test x$have_mprotect = xyes; then
    AC_DEFINE(HAVE_MPROTECT, 1, [Whether we have mprotect()])
 fi
 
-AC_CHECK_FUNC(getpagesize, have_getpagesize=yes, have_getpagesize=no)
-if test x$have_getpagesize = xyes; then
-   AC_DEFINE(HAVE_GETPAGESIZE, 1, [Whether we have getpagesize()])
-fi
-
 AC_CHECK_FUNC(gettimeofday, have_gettimeofday=yes, have_gettimeofday=no)
 AC_CHECK_HEADER(sys/time.h, have_sys_time_h=yes, have_sys_time_h=no)
 if test x$have_gettimeofday = xyes && test x$have_sys_time_h = xyes; then
diff --git a/test/utils.c b/test/utils.c
index 2ed5051..9d60b7c 100644
--- a/test/utils.c
+++ b/test/utils.c
@@ -218,7 +218,7 @@ typedef struct
     int n_bytes;
 } info_t;
 
-#if defined(HAVE_MPROTECT) && defined(HAVE_GETPAGESIZE) && defined(HAVE_SYS_MMAN_H)
+#if defined(HAVE_MPROTECT) && defined(HAVE_SYS_MMAN_H) && defined(HAVE_UNISTD_H)
 
 /* This is apparently necessary on at least OS X */
 #ifndef MAP_ANONYMOUS
@@ -228,7 +228,7 @@ typedef struct
 void *
 fence_malloc (uint32_t len)
 {
-    unsigned long page_size = getpagesize();
+    unsigned long page_size = sysconf (_SC_PAGESIZE);
     unsigned long page_mask = page_size - 1;
     uint32_t n_payload_bytes = (len + page_mask) & ~page_mask;
     uint32_t n_bytes =
@@ -243,7 +243,7 @@ fence_malloc (uint32_t len)
     addr = mmap (NULL, n_bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
 		 -1, 0);
 
-    if (addr == (void *)MAP_FAILED)
+    if (addr == MAP_FAILED)
     {
 	printf ("mmap failed on %u %u\n", len, n_bytes);
 	return NULL;
@@ -259,20 +259,12 @@ fence_malloc (uint32_t len)
     ((info_t *)initial_page)->trailing = trailing_protected;
     ((info_t *)initial_page)->n_bytes = n_bytes;
 
-    if (mprotect (leading_protected, N_LEADING_PROTECTED * page_size,
-		  PROT_NONE) == -1)
+    if ((mprotect (leading_protected, N_LEADING_PROTECTED * page_size,
+		  PROT_NONE) == -1) || 
+    	(mprotect (trailing_protected, N_TRAILING_PROTECTED * page_size,
+		  PROT_NONE) == -1))
     {
-	free (addr);
-	return NULL;
-    }
-
-    if (mprotect (trailing_protected, N_TRAILING_PROTECTED * page_size,
-		  PROT_NONE) == -1)
-    {
-	mprotect (leading_protected, N_LEADING_PROTECTED * page_size,
-		  PROT_READ | PROT_WRITE);
-
-	free (addr);
+	munmap (addr, n_bytes);
 	return NULL;
     }
 
@@ -282,18 +274,11 @@ fence_malloc (uint32_t len)
 void
 fence_free (void *data)
 {
-    uint32_t page_size = getpagesize();
+    uint32_t page_size = sysconf (_SC_PAGESIZE);
     uint8_t *payload = data;
     uint8_t *leading_protected = payload - N_LEADING_PROTECTED * page_size;
     uint8_t *initial_page = leading_protected - page_size;
     info_t *info = (info_t *)initial_page;
-    uint8_t *trailing_protected = info->trailing;
-
-    mprotect (leading_protected, N_LEADING_PROTECTED * page_size,
-	      PROT_READ | PROT_WRITE);
-
-    mprotect (trailing_protected, N_LEADING_PROTECTED * page_size,
-	      PROT_READ | PROT_WRITE);
 
     munmap (info->addr, info->n_bytes);
 }
-- 
1.6.3.3



More information about the Pixman mailing list