Small doubt in how chain works in RTEMS

Richi Dubey richidubey at gmail.com
Fri Jul 24 14:26:28 UTC 2020


Hi,

I have a doubt regarding this.

We do
  rtems_chain_append( &chain1, &node1.Node );

And get the node pointer when we use:
  rtems_chain_node    *p= rtems_chain_first(&chain1),

After this, Do we really need to use the CONTAINER_OF and other such
methods?

Can we not simply do:
  test_node node1=(test_node*) p;


Since our structure test_node has the  rtems_chain_node Node; as its first
variable, it should work, right?

References:
https://stackoverflow.com/q/3766229
https://git.rtems.org/rtems/tree/cpukit/score/src/scheduleredfsmp.c#n178

Please let me know.
Thanks.

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/20200724/4afee674/attachment.html>


More information about the devel mailing list