#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.
thanks, but what does 'beyond the public part of the structure ' mean?
Is there something marked for it?
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;
}
>>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
> 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
>Is there something marked for it?
Marked?
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?
>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.