]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
pagealign_alloc: prefer posix_memalign
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 11 Sep 2025 01:23:58 +0000 (18:23 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 11 Sep 2025 01:24:35 +0000 (18:24 -0700)
Problem reported by Harry Sintonen in:
https://lists.gnu.org/r/bug-gnulib/2025-09/msg00108.html
* lib/pagealign_alloc.c (info_t, memnode_t, struct memnode_s)
(memnode_table, new_memnode, get_memnode):
Omit if HAVE_POSIX_MEMALIGN, even if HAVE_MMAP.
(pagealign_alloc, pagealign_free): Prefer posix_memalign to mmap.

ChangeLog
lib/pagealign_alloc.c

index b85b67cf98b462f12959e7121beabb683ed49d75..2fb14e6f2ad2bfffa528dda4bc6a6804aa8e94a2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2025-09-10  Paul Eggert  <eggert@cs.ucla.edu>
+
+       pagealign_alloc: prefer posix_memalign
+       Problem reported by Harry Sintonen in:
+       https://lists.gnu.org/r/bug-gnulib/2025-09/msg00108.html
+       * lib/pagealign_alloc.c (info_t, memnode_t, struct memnode_s)
+       (memnode_table, new_memnode, get_memnode):
+       Omit if HAVE_POSIX_MEMALIGN, even if HAVE_MMAP.
+       (pagealign_alloc, pagealign_free): Prefer posix_memalign to mmap.
+
 2025-09-10  Bruno Haible  <bruno@clisp.org>
 
        Remove support for IRIX.
index 06aad1ee5222454efc3c2e000a09473670b37162..fdfffb4514e988afb5b5c2497cc2cc10f4ffc87a 100644 (file)
@@ -46,7 +46,7 @@
 #endif
 
 
-#if HAVE_MMAP || ! HAVE_POSIX_MEMALIGN
+#if ! HAVE_POSIX_MEMALIGN
 
 # if HAVE_MMAP
 /* For each memory region, we store its size.  */
@@ -108,29 +108,30 @@ get_memnode (void *aligned_ptr)
   return ret;
 }
 
-#endif /* HAVE_MMAP || !HAVE_POSIX_MEMALIGN */
+#endif /* !HAVE_POSIX_MEMALIGN */
 
 
 void *
 pagealign_alloc (size_t size)
 {
   void *ret;
-  /* We prefer the mmap() approach over the posix_memalign() or malloc()
-     based approaches, since the latter often waste an entire memory page
-     per call.  */
-#if HAVE_MMAP
-  ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE,
-              -1, 0);
-  if (ret == MAP_FAILED)
-    return NULL;
-  new_memnode (ret, size);
-#elif HAVE_POSIX_MEMALIGN
+#if HAVE_POSIX_MEMALIGN
+  /* Prefer posix_memalign to malloc and mmap,
+     as it typically scales better when there are many allocations.  */
   int status = posix_memalign (&ret, getpagesize (), size);
   if (status)
     {
       errno = status;
       return NULL;
     }
+#elif HAVE_MMAP
+  /* Prefer mmap to malloc, since the latter often wastes an entire
+     memory page per call.  */
+  ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE,
+              -1, 0);
+  if (ret == MAP_FAILED)
+    return NULL;
+  new_memnode (ret, size);
 #else /* !HAVE_MMAP && !HAVE_POSIX_MEMALIGN */
   size_t pagesize = getpagesize ();
   void *unaligned_ptr = malloc (size + pagesize - 1);
@@ -164,11 +165,11 @@ pagealign_xalloc (size_t size)
 void
 pagealign_free (void *aligned_ptr)
 {
-#if HAVE_MMAP
+#if HAVE_POSIX_MEMALIGN
+  free (aligned_ptr);
+#elif HAVE_MMAP
   if (munmap (aligned_ptr, get_memnode (aligned_ptr)) < 0)
     error (EXIT_FAILURE, errno, "Failed to unmap memory");
-#elif HAVE_POSIX_MEMALIGN
-  free (aligned_ptr);
 #else
   free (get_memnode (aligned_ptr));
 #endif