Question in _st_stack_new

Alfred Zhong <[email protected]> Tue, 20 Sep 2011 21:40:58 -0400
Newsgroups gmane.comp.lib.state-threads.user,gmane.comp.lib.state-threads.devel
Message-ID <CAFU3Ti+g3o22qL6ahO9_Z9GrxGwNQCyhRO1WWomhHFLy=1vOjQ@mail.gmail.com>
--===============3976929342243093597==
Content-Type: multipart/alternative; boundary=bcaec501c0a0f4895d04ad69aa59

--bcaec501c0a0f4895d04ad69aa59
Content-Type: text/plain; charset=ISO-8859-1

Hi Dear ST Developers and Users,

I am reading ST 1.9's source has encounter a piece of code that I can't
figure out for a week.

In stk.c,

in the body of the function

_st_stack_t *_st_stack_new(int stack_size)

I understand this function is to allocate memory space for the stack. And
update the stack records.

There is this piece

for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next)
 //when is _st_free_stack.next!= qp?
{
ts = _ST_THREAD_STACK_PTR(qp);
/*
Here I lookup what _ST_THREAD_STACK_PTR does and found

#define offsetof(type, identifier) (/***/ (size_t)&(/**/( (type
*)0)->identifier/**/) /***/)
It looks like this macro finds a instance of "_st_stack_t"'s element "links"
's address and cast it into size_t type?
I am totally confused about this! Cast address 0 points to _st_stack_t type
variable, but the space is never allocated yet. How does this work?


#define _ST_THREAD_STACK_PTR(_qp)   \
    (/**/ (_st_stack_t *)(/***/ (char*)(_qp) - offsetof(_st_stack_t, links)
/***/) /**/)
cast _st_clist_t type variable _qp to into byte point and minus this
"offset" that I don't understand. Confused again.

_qp is of _st_clist_t type,

*/
if (ts->stk_size >= stack_size)
{
/* Found a stack that is big enough */
ST_REMOVE_LINK(&ts->links);
_st_num_free_stacks--;
ts->links.next = NULL;
ts->links.prev = NULL;
return ts;
}
}

What does this for loop do?
I notice at the beginning of stk.c,
_st_clist_t _st_free_stacks = ST_INIT_STATIC_CLIST(&_st_free_stacks);
This set _st_free stack 's next and prev pointer pointing to itself.
Inside _st_stack_new(...), I don't see _st_free_stacks is changed.

I guess _st_list_t type is a link list. However, it has only next and prev
pointers. Where is the data?

Thanks a lot for any suggestions!
Alfred

--bcaec501c0a0f4895d04ad69aa59
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi Dear ST Developers and Users,<div><br></div><div>I am reading ST 1.9&#39=
;s source has encounter a piece of code that I can&#39;t figure out for a w=
eek.</div><div><br></div><div>In stk.c,</div><div><br></div><div>in the bod=
y of the function=A0</div>
<div><div><br></div><div>_st_stack_t *_st_stack_new(int stack_size)</div></=
div><div><br></div><div>I understand this function is to allocate memory sp=
ace for the stack. And update the stack records.</div><div><br></div><div>
There is this piece</div><div><div><br></div><div><span class=3D"Apple-tab-=
span" style=3D"white-space:pre">	</span>for (qp =3D _st_free_stacks.next; q=
p !=3D &amp;_st_free_stacks; qp =3D qp-&gt;next) =A0//when is _st_free_stac=
k.next!=3D qp?</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>{</di=
v><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>ts=
 =3D _ST_THREAD_STACK_PTR(qp); =A0</div><div>/*</div><div>Here I lookup wha=
t=A0_ST_THREAD_STACK_PTR does and found=A0</div>
<div><br></div><div>#define offsetof(type, identifier) (/***/ (size_t)&amp;=
(/**/( (type *)0)-&gt;identifier/**/) /***/)</div><div>It looks like this m=
acro finds a instance of &quot;_st_stack_t&quot;&#39;s element &quot;links&=
quot; &#39;s address and cast it into size_t type?</div>
<div>I am totally confused about this! Cast address 0 points to _st_stack_t=
 type variable, but the space is never allocated yet. How does this work?</=
div><div><br></div><div><br></div><div><div>#define _ST_THREAD_STACK_PTR(_q=
p) =A0 \</div>
<div>=A0 =A0 (/**/ (_st_stack_t *)(/***/ (char*)(_qp) - offsetof(_st_stack_=
t, links) /***/) /**/)</div><div>cast _st_clist_t type variable _qp to into=
 byte point and minus this &quot;offset&quot; that I don&#39;t understand. =
Confused again.</div>
</div><div><br></div><div>_qp is of _st_clist_t type,=A0</div><div><br></di=
v><div>*/</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre=
">		</span>if (ts-&gt;stk_size &gt;=3D stack_size)=A0</div><div><span class=
=3D"Apple-tab-span" style=3D"white-space:pre">		</span>{</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">			</span>/* =
Found a stack that is big enough */</div><div><span class=3D"Apple-tab-span=
" style=3D"white-space:pre">			</span>ST_REMOVE_LINK(&amp;ts-&gt;links);</d=
iv><div>
<span class=3D"Apple-tab-span" style=3D"white-space:pre">			</span>_st_num_=
free_stacks--;</div><div><span class=3D"Apple-tab-span" style=3D"white-spac=
e:pre">			</span>ts-&gt;links.next =3D NULL;</div><div><span class=3D"Apple=
-tab-span" style=3D"white-space:pre">			</span>ts-&gt;links.prev =3D NULL;<=
/div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">			</span>ret=
urn ts;</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">=
		</span>}</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pr=
e">	</span>}</div>
</div><div><br></div><div>What does this for loop do?</div><div>I notice at=
 the beginning of stk.c,=A0</div><div>_st_clist_t _st_free_stacks =3D ST_IN=
IT_STATIC_CLIST(&amp;_st_free_stacks);</div><div>This set _st_free stack &#=
39;s next and prev pointer pointing to itself. Inside=A0_st_stack_new(...),=
 I don&#39;t see _st_free_stacks is changed.=A0</div>
<div><br></div><div>I guess _st_list_t type is a link list. However, it has=
 only next and prev pointers. Where is the data?</div><div><br></div><div>T=
hanks a lot for any suggestions!</div><div>Alfred</div><div><br></div>

--bcaec501c0a0f4895d04ad69aa59--


--===============3976929342243093597==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
--===============3976929342243093597==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
State-threads-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/state-threads-users

--===============3976929342243093597==--