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