net: Introduce possible_net_t
Having to say > #ifdef CONFIG_NET_NS > struct net *net; > #endif in structures is a little bit wordy and a little bit error prone. Instead it is possible to say: > typedef struct { > #ifdef CONFIG_NET_NS > struct net *net; > #endif > } possible_net_t; And then in a header say: > possible_net_t net; Which is cleaner and easier to use and easier to test, as the possible_net_t is always there no matter what the compile options. Further this allows read_pnet and write_pnet to be functions in all cases which is better at catching typos. This change adds possible_net_t, updates the definitions of read_pnet and write_pnet, updates optional struct net * variables that write_pnet uses on to have the type possible_net_t, and finally fixes up the b0rked users of read_pnet and write_pnet. Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> Acked-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:

committed by
David S. Miller

parent
efd7ef1c19
commit
0c5c9fb551
@@ -231,24 +231,27 @@ int net_eq(const struct net *net1, const struct net *net2)
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
#ifdef CONFIG_NET_NS
|
||||
|
||||
static inline void write_pnet(struct net **pnet, struct net *net)
|
||||
{
|
||||
*pnet = net;
|
||||
}
|
||||
|
||||
static inline struct net *read_pnet(struct net * const *pnet)
|
||||
{
|
||||
return *pnet;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define write_pnet(pnet, net) do { (void)(net);} while (0)
|
||||
#define read_pnet(pnet) (&init_net)
|
||||
|
||||
struct net *net;
|
||||
#endif
|
||||
} possible_net_t;
|
||||
|
||||
static inline void write_pnet(possible_net_t *pnet, struct net *net)
|
||||
{
|
||||
#ifdef CONFIG_NET_NS
|
||||
pnet->net = net;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline struct net *read_pnet(const possible_net_t *pnet)
|
||||
{
|
||||
#ifdef CONFIG_NET_NS
|
||||
return pnet->net;
|
||||
#else
|
||||
return &init_net;
|
||||
#endif
|
||||
}
|
||||
|
||||
#define for_each_net(VAR) \
|
||||
list_for_each_entry(VAR, &net_namespace_list, list)
|
||||
|
Reference in New Issue
Block a user