netdev
[Top] [All Lists]

Re: Casting (struct rtable*) to (struct dst_entry*)

To: "David S. Miller" <davem@xxxxxxxxxx>
Subject: Re: Casting (struct rtable*) to (struct dst_entry*)
From: N N Ashok <nalkunda@xxxxxxxxxxx>
Date: Thu, 20 Mar 2003 01:02:38 -0500
Cc: netdev@xxxxxxxxxxx, linux-net@xxxxxxxxxxxxxxx
In-reply-to: <20030319.213352.50358237.davem@xxxxxxxxxx>
References: <200303192355.02509.nalkunda@xxxxxxxxxxx> <200303200027.41923.nalkunda@xxxxxxxxxxx> <20030319.213352.50358237.davem@xxxxxxxxxx>
Sender: netdev-bounce@xxxxxxxxxxx
User-agent: KMail/1.4.3
On Thursday 20 March 2003 00:33, David S. Miller wrote:
>    From: N N Ashok <nalkunda@xxxxxxxxxxx>
>    Date: Thu, 20 Mar 2003 00:27:41 -0500
>
>    I did see that. But however, I could not understand how "struct rtable"
> can be casted to "struct dst_entry" and then back again, all the while
> accessing fields of both structures.
>
> You miss the point that they are the same structure.  It is allocated
> the size of "struct rtable" but it may be casted back and forth
> between rtable and dst_entry as desired.
>
> void foo(void)
> {
>       struct rtable rt;
>       struct dst_entry *dst;
>
>       rt->u.dst.bar = 1;
>
>       dst = (struct dst_entry *) &rt;
>       ASSERT(dst->bar == 1);
>
>       dst = &rt->u.dst;
>       ASSERT(dst->bar == 1);
> }

I think I finally understand the whole setup. Please correct if I'm wrong.
"struct rtable" has its first field "u" which is a union of "dst_entry" and 
"struct rtable *". Thus when we cast rtable to dst_entry, we are accessing 
the rtable.u.dst_entry itself and not any other part of rtable.
Since originally the data was allocated the size of "rtable", when we cast 
"dst_entry" to "struct rtable" we can access all the fields of "struct 
table".

Thanks a lot for the clarification.

Ashok


<Prev in Thread] Current Thread [Next in Thread>