<br><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Dec 14, 2012 at 12:02 AM, Ronald Oussoren <span dir="ltr"><<a href="mailto:ronaldoussoren@mac.com" target="_blank">ronaldoussoren@mac.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div class="im">

<div>On 14 Dec, 2012, at 8:27, "Gregory P. Smith" <<a href="mailto:greg@krypto.org" target="_blank">greg@krypto.org</a>> wrote:</div><br><blockquote type="cite"><br><div class="gmail_extra"><div class="gmail_quote">

On Mon, Dec 10, 2012 at 11:16 PM, Antoine Pitrou <span dir="ltr"><<a href="mailto:solipsis@pitrou.net" target="_blank">solipsis@pitrou.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">



<div>On Tue, 11 Dec 2012 03:05:19 +0100 (CET)<br>
gregory.p.smith <<a href="mailto:python-checkins@python.org" target="_blank">python-checkins@python.org</a>> wrote:<br>
>   Using 'long double' to force this structure to be worst case aligned is no<br>
> longer required as of Python 2.5+ when the gc_refs changed from an int (4<br>
> bytes) to a Py_ssize_t (8 bytes) as the minimum size is 16 bytes.<br>
><br>
> The use of a 'long double' triggered a warning by Clang trunk's<br>
> Undefined-Behavior Sanitizer as on many platforms a long double requires<br>
> 16-byte alignment but the Python memory allocator only guarantees 8 byte<br>
> alignment.<br>
><br>
> So our code would allocate and use these structures with technically improper<br>
> alignment.  Though it didn't matter since the 'dummy' field is never used.<br>
> This silences that warning.<br>
><br>
> Spelunking into code history, the double was added in 2001 to force better<br>
> alignment on some platforms and changed to a long double in 2002 to appease<br>
> Tru64.  That issue should no loner be present since the upgrade from int to<br>
> Py_ssize_t where the minimum structure size increased to 16 (unless anyone<br>
> knows of a platform where ssize_t is 4 bytes?)<br>
<br>
</div>What?? Every 32-bit platform has a 4 bytes ssize_t (and size_t).<br></blockquote><div><br></div><div>No they don't.</div></div></div></blockquote><blockquote type="cite"><div class="gmail_extra"><div class="gmail_quote">

<div><br></div><div>size_t and ssize_t exist in large part because they are often larger than an int or long on 32bit platforms.  They are 64-bit on Linux regardless of platform (i think there is a way to force a compile in ancient mode that forces them and the APIs being used to be 32-bit size_t variants but nobody does that).</div>

</div></div></blockquote><div><br></div></div>Are you sure about this, what you describe seem to be loff_t (the typedef for file offsets), not size_t (the typedef for sizes of memory blocks). The size of memory blocks is limited to a 32-bit number on 32-bit systems (for the obvious reason).<br>

<div><br></div><div>Size_t is 32-bit on at least some platforms:</div><div><br></div><div>$ cat t.c</div><div>#include <stdlib.h></div><div>#include <stdio.h></div><div><br></div><div>int main(void)</div><div>

{</div><div><span style="white-space:pre-wrap">     </span>printf("sizeof(size_t): %d\n", (int)sizeof(size_t));</div><div><span style="white-space:pre-wrap">   </span>return 0;</div><div>}</div><div><br></div><div>$ cc -o t t.c -arch i386</div>

<div>$ ./t</div><div>sizeof(size_t): 4</div><div><br></div><div><br></div><div>This session is on an OSX system, but you'll get the same output on a 32-bit linux system with default compiler settings (I've tested this on a SLES10 system).</div>

</div></div></blockquote><div><br></div><div>You are correct.  My bad.  size_t vs off_t vs my full brain strikes again.</div><div><br></div><div>Regardless it doesn't change the correctness of my change.  Though I'd love it if someone would figure out the cross platform compiler macro based struct alignment incantations to get rid of the need for the union with dummy all together.</div>

<div><br></div><div>It wasn't even clear from the 2002 change description if changing double to long double over 10 years ago was actually _fixing_ a bug or was done for no good reason?  Our allocator (obmalloc.c) doesn't allocate memory at many platform's required 16 byte long double alignment, it uses 8 byte alignment so the change couldn't have had any impact unless someone compiled --without-pymalloc using a system allocator that guaranteed a larger value.  I wouldn't expect that and have seen no indication of that anywhere.<br>

</div><div><br></div><div>The history of the long double and double additions:</div><div><br></div><div> <a href="http://hg.python.org/cpython/rev/7065135f9202">http://hg.python.org/cpython/rev/7065135f9202</a>   long double</div>

<div> - <a href="http://hg.python.org/cpython/rev/b4f829941f3d">http://hg.python.org/cpython/rev/b4f829941f3d</a>   double</div><div>  - <a href="http://bugs.python.org/issue467145">http://bugs.python.org/issue467145</a>   why double was added, it fixed the HPUX 11 build.</div>

<div><br></div><div>-gps</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">

<div><blockquote type="cite"><div class="im"><div class="gmail_extra"><div class="gmail_quote">

<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div><br>
> We can probably get rid of the double and this union hack all together today.<br>
> That is a slightly more invasive change that can be left for later.<br>
<br>
</div>How do you suggest to get rid of it? Some platforms still have strict<br>
alignment rules and we must enforce that PyObjects (*) are always<br>
aligned to the largest possible alignment, since a PyObject-derived<br>
struct can hold arbitrary C types.<br>
<br>
(*) GC-enabled PyObjects, anyway. Others will be naturally aligned<br>
thanks to the memory allocator.<br>
<br>
<br>
What's more, I think you shouldn't be doing this kind of change in a<br>
bugfix release. It might break compiled C extensions since you are<br>
changing some characteristics of object layout (although you would<br>
probably only break those extensions which access the GC header, which<br>
is probably not many of them). Resource consumption improvements<br>
generally go only into the next feature release.<br></blockquote><div><br></div><div>This isn't a resource consumption improvement.  It is a compilation correctness change with zero impact on the generated code or ABI compatibility before and after.  The structure, as defined, is was flagged as problematic by Clang's undefined behavior sanitizer because it contains a 'long double' which requires 16-byte alignment but Python's own memory allocator was using an 8 byte boundary.</div>



<div><br></div><div>So changing the definition of the dummy side of the union makes zero difference to already compiled code as it (a) doesn't change the structure's size and (b) all existing implementations already align these on an 8 byte boundary.</div>



<div><br></div><div>-gps</div><div><br></div></div></div></div><div class="im">
_______________________________________________<br>Python-Dev mailing list<br><a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>

</div>Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/ronaldoussoren%40mac.com" target="_blank">http://mail.python.org/mailman/options/python-dev/ronaldoussoren%40mac.com</a><br></blockquote></div>

<br></div></blockquote></div><br></div>