linked lists

Ashi ashi08104 at gmail.com
Tue Aug 28 08:03:55 UTC 2012


Hi, Fered,

2012/8/23 Fered <a_Fered at yahoo.com>

> Hi;
> Some questions about linked lists in RTEMS:
>
> 1- Is the recommended method for having linked lists, using Chain_Control
> struct?
>
> 2- Please tell me how can I create a linked list, add node and delete node
> using Chain_Control.
>
> You can refer the Chain chapter in RTEMS C User's Guide about chain
operation:
http://rtems.org/onlinedocs/doc-current/share/rtems/html/c_user/c_user_513.html#Chains

3- If I have a pointer to a Chain_Node, how can I access to the original
> node?
> for example if I have
> struct n { int id; Chain_Node ch;};
> and I have a pointer to ch, how can I access id?
>
I actually have the same problem. And find there 2 ways:
1.  change your structure and let Chain_Node be the first member of your
struct type(as described in RTEMS C User's Guide):

struct n { Chain_Node ch; int id};

suppose you have the pointer ch_ptr to ch, then do pointer cast: (struct
n*)ch_ptr, now ch_ptr's type is (struct n *), you can just access id by
ch_ptr->id

2. use offsetof(), there are some examples in RTEMS source, like the
_RBTree_Container_of macro. In your case, it can be:

#define _Chain_Container_of(node, container_type, node_field_name) \
( \
  (container_type*) \
    ( (uintptr_t)(node) - offsetof(container_type, node_field_name) ) \
)

suppose you have the pointer ch_ptr to ch, then you can access id like this:
struct n * p = _Chain_Container_of(ch, (struct n), ch);
p->id

this approach doesn't require Chain_Node member being the first member in
your structure.

4- Suppose I have an struct s such as bellow:
> struct s{
> int id;
> Chain_Control list;
> };
> if I have a pointer to a Chain_Node in the list, can I access id?
>
...I think once you get theChain_Control list, it's similar to 3.

>

Thanks a lot.
> _______________________________________________
> rtems-devel mailing list
> rtems-devel at rtems.org
> http://www.rtems.org/mailman/listinfo/rtems-devel
>



-- 
Best wishes!
Zhongwei Yao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20120828/b724b965/attachment-0001.html>


More information about the devel mailing list