|
|
| Next: How to flush data most efficiently from memory to.. |
| Author |
Message |
!truth External

Since: Jun 21, 2007 Posts: 6
|
Posted: Tue Jul 03, 2007 2:27 am Post subject: a confusing function:netdev_priv Archived from groups: comp>os>linux>development>system (more info?) |
|
|
Hi all,
I cannot understand how the function 'netdev_priv' works, while
reading linux-driver.
The prototype of the function is as follows:
#define NETDEV_ALIGN 32
#define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1)
static inline void *netdev_priv(struct net_device *dev)
{ return (char *)dev + ((sizeof(struct net_device)+
NETDEV_ALIGN_CONST)
& ~NETDEV_ALIGN_CONST);
}
what made me confused is that
it says 'netdev_priv' is used for getting a member called 'priv' in
the structure 'net_device', but I think it can only get the last
member.
As you know the member 'priv' is not the last one, what's the story? |
|
| Back to top |
|
 |
!truth External

Since: Jun 21, 2007 Posts: 6
|
Posted: Tue Jul 03, 2007 4:21 am Post subject: Re: a confusing function:netdev_priv [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
On 7 3 , 6 11 , e....TakeThisOut@no.spam () wrote:
> In article <1183454828.384659.19....TakeThisOut@a26g2000pre.googlegroups.com>,
>
>
>
>
>
> !truth <noddy_zh....TakeThisOut@asustek.com.cn> wrote:
> > I cannot understand how the function 'netdev_priv' works, while
> >reading linux-driver.
> >The prototype of the function is as follows:
>
> >#define NETDEV_ALIGN 32
> >#define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1)
>
> >static inline void *netdev_priv(struct net_device *dev)
> >{ return (char *)dev + ((sizeof(struct net_device)+
> >NETDEV_ALIGN_CONST)
> > & ~NETDEV_ALIGN_CONST);
> > }
>
> >what made me confused is that
> >it says 'netdev_priv' is used for getting a member called 'priv' in
> >the structure 'net_device', but I think it can only get the last
> >member.
> >As you know the member 'priv' is not the last one, what's the story?
>
> It returns a pointer to the beginning of the private data part
> of the structure. In other words the address just beyond the
> public part of the structure padded by the alignment needed.
>
> --http://www.spinics.net/lists/netdev/- -
>
> - -
thanks, but what does 'beyond the public part of the structure ' mean?
Is there something marked for it? |
|
| Back to top |
|
 |
!truth External

Since: Jun 21, 2007 Posts: 6
|
Posted: Tue Jul 03, 2007 4:31 am Post subject: Re: a confusing function:netdev_priv [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
On 7 3 , 6 11 , e....RemoveThis@no.spam () wrote:
> In article <1183454828.384659.19....RemoveThis@a26g2000pre.googlegroups.com>,
>
>
>
>
>
> !truth <noddy_zh....RemoveThis@asustek.com.cn> wrote:
> > I cannot understand how the function 'netdev_priv' works, while
> >reading linux-driver.
> >The prototype of the function is as follows:
>
> >#define NETDEV_ALIGN 32
> >#define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1)
>
> >static inline void *netdev_priv(struct net_device *dev)
> >{ return (char *)dev + ((sizeof(struct net_device)+
> >NETDEV_ALIGN_CONST)
> > & ~NETDEV_ALIGN_CONST);
> > }
>
> >what made me confused is that
> >it says 'netdev_priv' is used for getting a member called 'priv' in
> >the structure 'net_device', but I think it can only get the last
> >member.
> >As you know the member 'priv' is not the last one, what's the story?
>
> It returns a pointer to the beginning of the private data part
> of the structure. In other words the address just beyond the
> public part of the structure padded by the alignment needed.
>
> --http://www.spinics.net/lists/netdev/- -
>
> - -
I write down a similar program to show my question:
In fact , the result are not equal
#include <stdio.h>
struct device{
int a;
void *priv;
int c;
char d;
};
#define NETDEV_ALIGN 32
#define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1)
static inline void *netdev_priv(struct device *dev)
{ return (char *)dev + ((sizeof(struct device)+
NETDEV_ALIGN_CONST)
& ~NETDEV_ALIGN_CONST);
}
int main()
{
struct device devs;
struct device *dev;
dev=&devs;
/*get pointer by dev->priv*/
printf("%p\n",dev->priv);
/*get pointer using function:netdev_priv*/
printf("%p\n",netdev_priv(dev));
return 0;
} |
|
| Back to top |
|
 |
ellis External

Since: Apr 02, 2004 Posts: 193
|
Posted: Tue Jul 03, 2007 10:11 am Post subject: Re: a confusing function:netdev_priv [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
In article <1183454828.384659.19480 RemoveThis @a26g2000pre.googlegroups.com>,
!truth <noddy_zhang RemoveThis @asustek.com.cn> wrote:
> I cannot understand how the function 'netdev_priv' works, while
>reading linux-driver.
>The prototype of the function is as follows:
>
>#define NETDEV_ALIGN 32
>#define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1)
>
>
>static inline void *netdev_priv(struct net_device *dev)
>{ return (char *)dev + ((sizeof(struct net_device)+
>NETDEV_ALIGN_CONST)
> & ~NETDEV_ALIGN_CONST);
> }
>
>what made me confused is that
>it says 'netdev_priv' is used for getting a member called 'priv' in
>the structure 'net_device', but I think it can only get the last
>member.
>As you know the member 'priv' is not the last one, what's the story?
It returns a pointer to the beginning of the private data part
of the structure. In other words the address just beyond the
public part of the structure padded by the alignment needed.
--
http://www.spinics.net/lists/netdev/ |
|
| Back to top |
|
 |
Gil Hamilton External

Since: Jan 08, 2007 Posts: 22
|
Posted: Tue Jul 03, 2007 1:31 pm Post subject: Re: a confusing function:netdev_priv [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
ellis.RemoveThis@no.spam () wrote in news:1183457510.48121@no.spam:
> In article <1183454828.384659.19480.RemoveThis@a26g2000pre.googlegroups.com>,
> !truth <noddy_zhang.RemoveThis@asustek.com.cn> wrote:
>
>> I cannot understand how the function 'netdev_priv' works, while
>>reading linux-driver.
>>it says 'netdev_priv' is used for getting a member called 'priv' in
>>the structure 'net_device', but I think it can only get the last
>>member.
>>As you know the member 'priv' is not the last one, what's the story?
>
> It returns a pointer to the beginning of the private data part
> of the structure. In other words the address just beyond the
> public part of the structure padded by the alignment needed.
I would just add that drivers don't need to use netdev_priv (although
many drivers seem to do so, I think for historical reasons). netdev_priv
is used inside the function alloc_netdev() to set up the value of the
'priv' member to point to the private data area allocated just beyond the
net_device portion. Once set up, drivers can simply refer to the priv
member:
struct net_device *my_dev = alloc_netdev(sizeof(struct my_priv),
"eth%d", ether_setup);
struct my_priv *my_private = my_dev->priv;
The last line above would be exactly equivalent to:
struct my_priv *my_private = netdev_priv(my_dev);
GH |
|
| Back to top |
|
 |
Gil Hamilton External

Since: Jan 08, 2007 Posts: 22
|
Posted: Tue Jul 03, 2007 1:34 pm Post subject: Re: a confusing function:netdev_priv [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
!truth <noddy_zhang DeleteThis @asustek.com.cn> wrote in news:1183462263.324580.17010
@m37g2000prh.googlegroups.com:
> I write down a similar program to show my question:
> In fact , the result are not equal
> /*get pointer by dev->priv*/
> printf("%p\n",dev->priv);
>
> /*get pointer using function:netdev_priv*/
> printf("%p\n",netdev_priv(dev));
See my previous post. After you have called alloc_netdev(), the two *will*
be equivalent.
GH |
|
| Back to top |
|
 |
!truth External

Since: Jun 21, 2007 Posts: 6
|
Posted: Tue Jul 03, 2007 8:48 pm Post subject: Re: a confusing function:netdev_priv [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
On Jul 3, 7:34 am, Gil Hamilton <gil_hamil... DeleteThis @hotmail.com> wrote:
> !truth <noddy_zh... DeleteThis @asustek.com.cn> wrote in news:1183462263.324580.17010
> @m37g2000prh.googlegroups.com:
>
> > I write down a similar program to show my question:
> > In fact , the result are not equal
> > /*get pointer by dev->priv*/
> > printf("%p\n",dev->priv);
>
> > /*get pointer using function:netdev_priv*/
> > printf("%p\n",netdev_priv(dev));
>
> See my previous post. After you have called alloc_netdev(), the two *will*
> be
>
> GH
Thanks , I have read the code about alloc_netdev, and found that it
made the member 'priv' point to the end of structure. Right?
But I'm very curious that why it does so? |
|
| Back to top |
|
 |
ellis External

Since: Apr 02, 2004 Posts: 193
|
Posted: Tue Jul 03, 2007 11:15 pm Post subject: Re: a confusing function:netdev_priv [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
In article <1183461710.646347.142710.DeleteThis@x35g2000prf.googlegroups.com>,
!truth <noddy_zhang.DeleteThis@asustek.com.cn> wrote:
>Is there something marked for it?
Marked?
--
http://www.spinics.net/lists/netdev/ |
|
| Back to top |
|
 |
!truth External

Since: Jun 21, 2007 Posts: 6
|
Posted: Wed Jul 04, 2007 3:13 am Post subject: Re: a confusing function:netdev_priv [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
On 7 4 , 4 57 , e....DeleteThis@no.spam () wrote:
> In article <1183520939.933084.243....DeleteThis@e9g2000prf.googlegroups.com>,
>
> !truth <noddy_zh....DeleteThis@asustek.com.cn> wrote:
> >But I'm very curious that why it does so?
>
> You've been told why several times. Perhaps C just
> isn't your forte.
>
> --http://www.spinics.net/lists/netdev/http://www.spinics.net/lists/linux-net/
oh, My god.I want to change my job. |
|
| Back to top |
|
 |
ellis External

Since: Apr 02, 2004 Posts: 193
|
Posted: Wed Jul 04, 2007 8:57 am Post subject: Re: a confusing function:netdev_priv [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
In article <1183520939.933084.243030 RemoveThis @e9g2000prf.googlegroups.com>,
!truth <noddy_zhang RemoveThis @asustek.com.cn> wrote:
>But I'm very curious that why it does so?
You've been told why several times. Perhaps C just
isn't your forte.
--
http://www.spinics.net/lists/netdev/
http://www.spinics.net/lists/linux-net/ |
|
| Back to top |
|
 |
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
| |
|
|