<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix"><br>
<blockquote type="cite">theory would be better with [0]</blockquote>
It depends on the gcc version you have and well that is probably
the difference between theory and practice.<br>
<br>
In theory it should work, but in practice we had tons of problems
with that.<br>
<br>
Best practice I think is to still calculate the end of the
structure manually and don't embed the array into the structure.<br>
<br>
Take a look at drivers/dma-buf/fence-array.c fence_array_create()
and fence_array_enable_signaling() for an example on how to use
it.<br>
<br>
So please don't use this,<br>
Christian.<br>
<br>
Am 18.08.2016 um 17:39 schrieb StDenis, Tom:<br>
</div>
<blockquote
cite="mid:DM5PR12MB1132AF7ACC738F339571ECAFF7150@DM5PR12MB1132.namprd12.prod.outlook.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
<div id="divtagdefaultwrapper"
style="font-size:12pt;color:#000000;background-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p>It had to be something more complicated because this demo
program</p>
<p><br>
</p>
<div><span style="font-family: "Courier New",
monospace;">#include <stdio.h></span></div>
<div><span style="font-family: "Courier New",
monospace;">#include <stdlib.h></span></div>
<div><br>
</div>
<div><span style="font-family: "Courier New",
monospace;">struct one {</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">char
*foo;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">int
bar[0];</span></div>
<div><span style="font-family: "Courier New",
monospace;">};</span></div>
<div><br>
</div>
<div><span style="font-family: "Courier New",
monospace;">struct two {</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">char
*foo;</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">int
bar[1];</span></div>
<div><span style="font-family: "Courier New",
monospace;">};</span></div>
<div><br>
</div>
<div><span style="font-family: "Courier New",
monospace;">int main(void)</span></div>
<div><span style="font-family: "Courier New",
monospace;">{</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">struct
one *a = calloc(1, sizeof(struct one) + 4 * sizeof(int));</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">struct
two *b = calloc(1, sizeof(struct two) + 3 * sizeof(int));</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">int
x;</span></div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">printf("a
== %p\n", a);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">for
(x = 0; x < 4; x++)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">printf("&a.bar[%d]
= %p\n", x, &a->bar[x]);</span></div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">printf("b
== %p\n", b);</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">for
(x = 0; x < 4; x++)</span></div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">printf("&b.bar[%d]
= %p\n", x, &b->bar[x]);</span></div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space: pre; font-family: "Courier New", monospace;"></span><span
style="font-family: "Courier New", monospace;">return
0;</span></div>
<div><span style="font-family: "Courier New",
monospace;">}</span></div>
<div><br>
</div>
<p>produces this output</p>
<p><br>
</p>
<div><span style="font-family: "Courier New",
monospace;">tom@fx8:~$ gcc test.c -o test</span></div>
<div><span style="font-family: "Courier New",
monospace;">tom@fx8:~$ ./test</span></div>
<div><span style="font-family: "Courier New",
monospace;">a == 0x1fd4010</span></div>
<div><span style="font-family: "Courier New",
monospace;">&a.bar[0] = 0x1fd4018</span></div>
<div><span style="font-family: "Courier New",
monospace;">&a.bar[1] = 0x1fd401c</span></div>
<div><span style="font-family: "Courier New",
monospace;">&a.bar[2] = 0x1fd4020</span></div>
<div><span style="font-family: "Courier New",
monospace;">&a.bar[3] = 0x1fd4024</span></div>
<div><span style="font-family: "Courier New",
monospace;">b == 0x1fd4030</span></div>
<div><span style="font-family: "Courier New",
monospace;">&b.bar[0] = 0x1fd4038</span></div>
<div><span style="font-family: "Courier New",
monospace;">&b.bar[1] = 0x1fd403c</span></div>
<div><span style="font-family: "Courier New",
monospace;">&b.bar[2] = 0x1fd4040</span></div>
<div><span style="font-family: "Courier New",
monospace;">&b.bar[3] = 0x1fd4044</span></div>
<div><br>
</div>
Which is exactly what you'd expect. I'm not strongly advocating
we change the PP code just noting it's not really clear that
it's correct from a first reading and in theory would be better
with [0].
<div><br>
</div>
Tom<br>
<br>
<div style="color: rgb(0, 0, 0);">
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt"
color="#000000" face="Calibri, sans-serif"><b>From:</b>
Alex Deucher <a class="moz-txt-link-rfc2396E" href="mailto:alexdeucher@gmail.com"><alexdeucher@gmail.com></a><br>
<b>Sent:</b> Thursday, August 18, 2016 11:33<br>
<b>To:</b> StDenis, Tom<br>
<b>Cc:</b> Christian König; amd-gfx list<br>
<b>Subject:</b> Re: tidy'ing up cz_hwmgr.c</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div>
<div>
<div>The problem we ran into was when we had a struct
like this:<br>
<br>
</div>
struct table {<br>
</div>
uint16_t size;<br>
</div>
struct element elements[0];<br>
<div>
<div>};<br>
<div>
<div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">and then we would try and
index the array:<br>
<br>
</div>
<div class="gmail_extra">for (i = 0; i <
table->size; i++) {<br>
</div>
<div class="gmail_extra"> element =
&table->elements[i];<br>
</div>
<div class="gmail_extra">}<br>
<br>
</div>
<div class="gmail_extra">element ended up off in
the weeds. The only thing that seems to make
some versions of gcc happy was pointer
arithmetic. E.g.,<br>
</div>
<div class="gmail_extra"><br>
</div>
<div class="gmail_extra">element = (struct element
*)((char *)&table->elements[0] +
(sizeof(struct element) * i));<br>
<br>
</div>
<div class="gmail_extra">Alex<br>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Thu, Aug 18, 2016 at
11:21 AM, StDenis, Tom <span dir="ltr">
<<a moz-do-not-send="true"
href="mailto:Tom.StDenis@amd.com"
target="_blank">Tom.StDenis@amd.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote"
style="margin:0 0 0 .8ex; border-left:1px
#ccc solid; padding-left:1ex">
<div dir="ltr">
<div style="font-size:12pt; color:#000000;
background-color:#ffffff;
font-family:Calibri,Arial,Helvetica,sans-serif">
<p>Any modern GCC should support [0] at
the tail of a struct. This came up
because when I was reading the code I
saw they allocated 7 slots (plus the
size of the struct) but then fill 8
slots. It's just weird
<img alt="😊" style="vertical-align:
bottom; -webkit-user-select: none;"
tabindex="0"
src="cid:part2.12A01FCD.899B127C@vodafone.de"></p>
<p><br>
</p>
<p>Using [0] in the struct and
allocating for 8 entries makes more
sense and is clearer to read.</p>
<p><br>
</p>
<p>Tom</p>
<br>
<br>
<div style="color:rgb(0,0,0)">
<hr style="display:inline-block;
width:98%">
<div dir="ltr"><font
style="font-size:11pt"
color="#000000" face="Calibri,
sans-serif"><b>From:</b> Christian
König <<a
moz-do-not-send="true"
href="mailto:deathsimple@vodafone.de"
target="_blank">deathsimple@vodafone.de</a>><br>
<b>Sent:</b> Thursday, August 18,
2016 11:17<br>
<b>To:</b> StDenis, Tom; amd-gfx
list<br>
<b>Subject:</b> Re: tidy'ing up
cz_hwmgr.c</font>
<div> </div>
</div>
<div>
<div class="h5">
<div>
<div>
<blockquote type="cite">
<div>Has a [1] array at the
tail which is then
kzalloc'ed with N-1
entries. Shouldn't that
just be a [0] with N
entries allocated for
clarity?</div>
</blockquote>
Actually the starting address
of a dynamic array should be
manually calculated instead of
using [1] or [0].<br>
<br>
We had tons of problems with
that because some gcc versions
get this wrong and the
atombios code used this as
well.<br>
<br>
Alex how did we resolved such
issues?<br>
<br>
Regards,<br>
Christian.<br>
<br>
Am 18.08.2016 um 16:26 schrieb
StDenis, Tom:<br>
</div>
<blockquote type="cite">
<div style="font-size:12pt;
color:#000000;
background-color:#ffffff;
font-family:Calibri,Arial,Helvetica,sans-serif">
<p>Tidying up cz_hwmgr.c I
noted a couple of things
but first is </p>
<p><br>
</p>
<div><span
style="font-family:"Courier
New",monospace">static
bool
cz_dpm_check_smu_features(<wbr>struct
pp_hwmgr *hwmgr,</span></div>
<div><span style="white-space:pre-wrap; font-family:"Courier New",monospace"></span><span
style="font-family:"Courier New",monospace">unsigned long
check_feature);</span></div>
<div><br>
</div>
<div>Which will return
"true" if the smu call
fails <b>or</b> the
feature is set. </div>
<div><br>
</div>
<div>The structure </div>
<div><br>
</div>
<div>
<div><span
style="font-family:"Courier
New",monospace">struct
phm_clock_voltage_dependency_<wbr>table;</span></div>
</div>
<div><br>
</div>
<div>Has a [1] array at the
tail which is then
kzalloc'ed with N-1
entries. Shouldn't that
just be a [0] with N
entries allocated for
clarity?</div>
<div><br>
</div>
<div>Tom</div>
<div><br>
</div>
</div>
<br>
<fieldset></fieldset>
<br>
<pre>______________________________<wbr>_________________
amd-gfx mailing list
<a moz-do-not-send="true" href="mailto:amd-gfx@lists.freedesktop.org" target="_blank">amd-gfx@lists.freedesktop.org</a>
<a moz-do-not-send="true" href="https://lists.freedesktop.org/mailman/listinfo/amd-gfx" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/amd-gfx</a>
</pre>
</blockquote>
<p>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
______________________________<wbr>_________________
amd-gfx mailing list
<a moz-do-not-send="true" href="mailto:amd-gfx@lists.freedesktop.org">amd-gfx@lists.freedesktop.org</a>
<a moz-do-not-send="true" href="https://lists.freedesktop.org/mailman/listinfo/amd-gfx" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/amd-gfx</a>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre wrap="">_______________________________________________
amd-gfx mailing list
<a class="moz-txt-link-abbreviated" href="mailto:amd-gfx@lists.freedesktop.org">amd-gfx@lists.freedesktop.org</a>
<a class="moz-txt-link-freetext" href="https://lists.freedesktop.org/mailman/listinfo/amd-gfx">https://lists.freedesktop.org/mailman/listinfo/amd-gfx</a>
</pre>
</blockquote><p>
</p></body></html>