[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gnulib] valloc()?
From: |
Bruno Haible |
Subject: |
Re: [bug-gnulib] valloc()? |
Date: |
Wed, 2 Mar 2005 13:00:33 +0100 |
User-agent: |
KMail/1.5 |
Derek Price wrote:
> First, I found man pages stating that valloc() was obsoleted in favor of
> posix_memalign(). Does anyone have a feel for how portable
> posix_memalign() is
It appears to be implemented only by glibc.
> Second, if posix_memalign() is not portable, is there any interest in a
> GNULIB module either to replace it or valloc()? I've attached the
> valloc.c that CVS is currently using.
The spec says that the returned memory must be freeable through free().
I don't see how you can write a portable replacement for posix_memalign()
without digging in undocumented details of the malloc() implementation.
> void *
> valloc (bytes)
> size_t bytes;
> {
> long pagesize;
> void *ret;
>
> pagesize = getpagesize ();
> ret = malloc (bytes + pagesize - 1);
> if (ret)
> ret = (long) (ret + pagesize - 1) &~ (pagesize - 1);
> return ret;
> }
This is the best you can do in a portable way, but
1. the return value cannot be passed to free(),
2. it wastes 1/2 page of memory on average.
Therefore I'd suggest a new interface:
void* pagealign_alloc(size_t);
void pagealign_free(void*);
and do the implementation as follows:
- If mmap() is available, use mmap and some bookkeeping for pagealign_alloc,
and munmap() for pagealign_free,
- Otherwise, if posix_memalign() is available, use it and free(),
- Otherwise, use something similar to the valloc() above.
Is this all worth it? For what purpose do you need the memory to be
page-aligned?
Bruno