Check or don't check for allocation failure? Provide both alternatives.
authorBruno Haible <bruno@clisp.org>
Thu, 3 Mar 2005 16:21:00 +0000 (16:21 +0000)
committerBruno Haible <bruno@clisp.org>
Thu, 3 Mar 2005 16:21:00 +0000 (16:21 +0000)
lib/pagealign_alloc.c
lib/pagealign_alloc.h

index e848b06..117f471 100644 (file)
@@ -95,7 +95,7 @@ get_memnode (void *aligned_ptr)
     if (c->aligned_ptr == aligned_ptr)
       break;
 
-  if (!c)
+  if (c == NULL)
     /* An attempt to free untracked memory.  A wrong pointer was passed
        to pagealign_free().  */
     abort ();
@@ -117,13 +117,13 @@ pagealign_alloc (size_t size)
   void *ret;
 #if HAVE_MMAP
   int flags;
-  static int fd = -1;  /* Only open /dev/zero once in order to avoid limiting
-                         the amount of memory we may allocate based on the
-                         number of open file descriptors.  */
 # ifdef HAVE_MAP_ANONYMOUS
+  const int fd = -1;
   flags = MAP_ANONYMOUS | MAP_PRIVATE;
-  fd = -1;
 # else /* !HAVE_MAP_ANONYMOUS */
+  static int fd = -1;  /* Only open /dev/zero once in order to avoid limiting
+                         the amount of memory we may allocate based on the
+                         number of open file descriptors.  */
   flags = MAP_FILE | MAP_PRIVATE;
   if (fd == -1)
     {
@@ -134,15 +134,17 @@ pagealign_alloc (size_t size)
 # endif /* HAVE_MAP_ANONYMOUS */
   ret = mmap (NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
   if (!ret)
-    error (EXIT_FAILURE, errno, "mmap to /dev/zero failed");
+    return NULL;
   new_memnode (ret, size);
 #elif HAVE_POSIX_MEMALIGN
   int status = posix_memalign (&ret, getpagesize (), size);
   if (status)
-    error (EXIT_FAILURE, status, "posix_memalign failed");
+    return NULL;
 #else /* !HAVE_MMAP && !HAVE_POSIX_MEMALIGN */
   size_t pagesize = getpagesize ();
-  void *unaligned_ptr = xmalloc (size + pagesize - 1);
+  void *unaligned_ptr = malloc (size + pagesize - 1);
+  if (unaligned_ptr == NULL)
+    return NULL;
   ret = (char *) unaligned_ptr
         + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
   new_memnode (ret, unaligned_ptr);
@@ -151,6 +153,18 @@ pagealign_alloc (size_t size)
 }
 
 
+void *
+pagealign_xalloc (size_t size)
+{
+  void *ret;
+
+  ret = pagealign_alloc (size);
+  if (ret == NULL)
+    xalloc_die ();
+  return ret;
+}
+
+
 void
 pagealign_free (void *aligned_ptr)
 {
index 2d63eb1..849148b 100644 (file)
    failed.  */
 extern void *pagealign_alloc (size_t size);
 
+/* Like pagealign_alloc, except it exits the program if the allocation
+   fails.  */
+extern void *pagealign_xalloc (size_t size);
+
 /* Free a memory block.
-   PTR must be a pointer returned by pagealign_alloc.  */
+   PTR must be a non-NULL pointer returned by pagealign_alloc or
+   pagealign_xalloc.  */
 extern void pagealign_free (void *ptr);
 
 #endif /* _PAGEALIGN_ALLOC_H */