[PATCH] Don't fail memalign with an alignment < sizeof(void *)

Mike Hommey mh+jemalloc at glandium.org
Tue Feb 21 08:21:40 PST 2012


From: Mike Hommey <mh at glandium.org>

While posix_memalign needs alignment to be a power of two and a multiple
of sizeof(void *), memalign only requires the former.

Combined with interposing of glibc's memalign, we can end up in situations
where glibc calls memalign with a power of two smaller than sizeof(void *),
and where the failure in imemalign breaks normal functionality.
---
 src/jemalloc.c |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/jemalloc.c b/src/jemalloc.c
index f678358..e819b7e 100644
--- a/src/jemalloc.c
+++ b/src/jemalloc.c
@@ -965,9 +965,8 @@ imemalign(void **memptr, size_t alignment, size_t size)
 			}
 		}
 
-		/* Make sure that alignment is a large enough power of 2. */
-		if (((alignment - 1) & alignment) != 0
-		    || alignment < sizeof(void *)) {
+		/* Make sure that alignment is a power of 2. */
+		if (((alignment - 1) & alignment) != 0) {
 			if (config_xmalloc && opt_xmalloc) {
 				malloc_write("<jemalloc>: Error in "
 				    "posix_memalign(): invalid alignment\n");
@@ -1038,7 +1037,14 @@ JEMALLOC_ATTR(visibility("default"))
 int
 JEMALLOC_P(posix_memalign)(void **memptr, size_t alignment, size_t size)
 {
-
+	/*
+	 * posix_memalign needs alignment to be a power of two and a multiple
+	 * of sizeof(void *). If it's not, pass an alignment that is not a
+	 * power of two so that imemalign fails appropriately.
+	 */
+	if (alignment < sizeof(void *)) {
+		alignment = 3;
+	}
 	return imemalign(memptr, alignment, size);
 }
 
-- 
1.7.9




More information about the jemalloc-discuss mailing list