Small doubt in how chain works in RTEMS
Richi Dubey
richidubey at gmail.com
Sat Jun 13 12:49:17 UTC 2020
Hi Dr. Bloom,
Thanks for the link, The article has another link to an article(
http://www.kroah.com/log/linux/container_of.html) by one of the maintainers
of Linux and both the articles are really well written. The idea is, we
have the size of the structure and the size of the pointer(for our chain),
so we should be able to figure out the start of the structure. Smart!
I can't do justice to RTEMS if I write with the current knowledge that I
have :p. I'll do it after a month or so.
Thanks,
Richi.
On Fri, Jun 12, 2020 at 7:28 PM Gedare Bloom <gedare at rtems.org> wrote:
> On Fri, Jun 12, 2020 at 7:50 AM Richi Dubey <richidubey at gmail.com> wrote:
> >
> > Hi everyone,
> >
> > While going through testsuites/sptests/spchain/init.c, I noticed the
> following code:
> >
> > -----------------------------------------------
> > rtems_task Init(
> > rtems_task_argument ignored
> > )
> > {
> > rtems_chain_control chain1;
> > rtems_chain_node *p;
> > test_node node1, node2;
> > int id;
> >
> > TEST_BEGIN();
> >
> > puts( "Init - Initialize chain empty" );
> > rtems_chain_initialize_empty( &chain1 );
> > rtems_chain_initialize_node( &node1.Node );
> > rtems_chain_initialize_node( &node2.Node );
> >
> > /* verify that the chain append and insert work */
> > puts( "INIT - Verify rtems_chain_insert" );
> > node1.id = 1;
> > node2.id = 2;
> > rtems_chain_append( &chain1, &node1.Node );
> > rtems_chain_insert( &node1.Node, &node2.Node );
> >
> > for ( p = rtems_chain_first(&chain1), id = 1 ;
> > !rtems_chain_is_tail(&chain1, p) ;
> > p = p->next , id++ ) {
> > test_node *t = (test_node *)p;
> > if ( id > 2 ) {
> > puts( "INIT - TOO MANY NODES ON CHAIN" );
> > rtems_test_exit(0);
> > }
> > if ( t->id != id ) {
> > puts( "INIT - ERROR ON CHAIN ID MISMATCH" );
> > rtems_test_exit(0);
> > }
> > }
> >
> > -----------------------------------------
> > Where test_node is defined as:
> >
> > typedef struct {
> > rtems_chain_node Node;
> > int id;
> > } test_node;
> >
> > ----------------------------------
> >
> >
> > Now when we are inserting or appending our structure into the chain, we
> are only inserting the part of strucuture correspoing to rtems_chain_node,
> i.e.:
> > rtems_chain_append( &chain1, &node1.Node );
> > rtems_chain_insert( &node1.Node, &node2.Node );
> >
> > So, how do we ever get our node1.id ? How can we (or how do we ever)
> access our data from the structure when we are only adding some part of the
> structure into the node?
> >
> > I hope my doubt is not too vague.
> >
> Hi Richi,
>
> This is an old C programming trick.
>
> This looks like a reasonable explanation:
>
> https://medium.com/@414apache/kernel-data-structures-linkedlist-b13e4f8de4bf
>
> Feel free to write something for rtems ;)
>
> > Thanks,
> > Richi.
> >
> >
> > _______________________________________________
> > devel mailing list
> > devel at rtems.org
> > http://lists.rtems.org/mailman/listinfo/devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20200613/e999845a/attachment.html>
More information about the devel
mailing list