Mercurial > hg > octave-lyh > gnulib-hg
changeset 5675:9fb0004d9fa6
Check or don't check for allocation failure? Provide both alternatives.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 03 Mar 2005 16:21:00 +0000 |
parents | 8799b424105a |
children | 4b054c347e26 |
files | lib/pagealign_alloc.c lib/pagealign_alloc.h |
diffstat | 2 files changed, 28 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/pagealign_alloc.c +++ b/lib/pagealign_alloc.c @@ -95,7 +95,7 @@ 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 @@ void *ret; #if HAVE_MMAP int flags; +# ifdef HAVE_MAP_ANONYMOUS + const int fd = -1; + flags = MAP_ANONYMOUS | MAP_PRIVATE; +# 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. */ -# ifdef HAVE_MAP_ANONYMOUS - flags = MAP_ANONYMOUS | MAP_PRIVATE; - fd = -1; -# else /* !HAVE_MAP_ANONYMOUS */ flags = MAP_FILE | MAP_PRIVATE; if (fd == -1) { @@ -134,15 +134,17 @@ # 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 @@ } +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) {
--- a/lib/pagealign_alloc.h +++ b/lib/pagealign_alloc.h @@ -30,8 +30,13 @@ 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 */