[libvirt] [PATCH] util: fix thinko in runIO

Eric Blake eblake at redhat.com
Fri Nov 25 13:38:20 UTC 2011


[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 at redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 620 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20111125/344ad4e9/attachment-0001.sig>


More information about the libvir-list mailing list