Malloc page than realloc to smaller does not work ?
Bastien
Le 25 nov. 2011 14:38, "Eric Blake" <eblake(a)redhat.com> a écrit :
[adding bug-gnulib; replies can drop libvirt]
On 11/25/2011 05:51 AM, Paolo Bonzini wrote:
> Indeed; Linux has posix_memalign, and mingw never runs the io
helper
> (although it does compile it, hence the #if). If gnulib would give
> us posix_memalign on mingw, we could nuke this #if altogether.
That's pretty difficult (unless you also add a posix_memalign_free)
because at the time posix_memalign returns you have lost the base
pointer for free().
Providing a posix_memalign_free defeats the purpose - POSIX requires
that plain free() will cover the memory returned by posix_memalign. The
list of platforms missing posix_memalign is a bit daunting:
MacOS X 10.5, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, Minix 3.1.8, AIX
5.1, HP-UX 11,
IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x, mingw, MSVC 9, Interix
3.5, BeOS.
but what would be interesting to know is how many of those platforms
return page-aligned pointers for any malloc() request of a page or more
of memory. That is, we may be able to coerce malloc into aligned
results by over-allocating and over-aligning the user's request, if the
system malloc() has at least one mode of returning page-aligned memory.
Another alternative is to override free() at the same time as providing
posix_memalign(). The gnulib malloca module provides a freea() function
that can distinguish whether it is paired with an alloca() (nop) or
mmalloca() (call the real free()), by over-allocating and probing for a
magic header. Similar concepts could be used in writing an rpl_free()
that determines whether it was allocated by posix_memalign(): if we
guarantee that posix_memalign always returns something aligned to at
least a page, then rpl_free() can make a quick check for whether the
pointer passed in is page-aligned; if so, do a hash lookup to see if it
was a page reserved by our posix_memalign (if so, we know the real
address to free); if not or the hash lookup fails, it came from malloc
(in which case, regular free should work). Of course, unlike freea()
that probes for a magic header, we can't safely probe a header by
crossing page boundaries, since that would fault if we didn't allocate
the previous page at the same time.
--
Eric Blake eblake(a)redhat.com +1-919-301-3266
Libvirt virtualization library
http://libvirt.org