I struggle to see any value of this concept:  If you put a cast in-line
in the code, as a coder you can know what it means.  But if you abstract
the typecast to some function in a header file, how do you remember
that it only does a typecast, and doesn't do something else?  I think this
is a foolish abstraction without value.

Klemens Nanni <[email protected]> wrote:

> satosin() gets a satocin() sibling, satosdl() gets satocsdl(), etc.
> 
> These inline functions are identical except the const version expects
> and returns a pointer to a const struct.
> 
> This only adds unused inline functions be used in upcoming commits,
> where we can then more easily add const to functions which commonly
> convert and pass around such arguments.
> 
> Idea from NetBSD, which defines identically names helpers as macros.
> 
> Feedback? Objection? OK?
> 
> diff --git a/sys/net/if_dl.h b/sys/net/if_dl.h
> index 229cdad5ace..de62897cf4e 100644
> --- a/sys/net/if_dl.h
> +++ b/sys/net/if_dl.h
> @@ -79,12 +79,24 @@ satosdl(struct sockaddr *sa)
>       return ((struct sockaddr_dl *)(sa));
>  }
>  
> +static inline const struct sockaddr_dl *
> +satocsdl(const struct sockaddr *sa)
> +{
> +     return ((const struct sockaddr_dl *)(sa));
> +}
> +
>  static inline struct sockaddr *
>  sdltosa(struct sockaddr_dl *sdl)
>  {
>       return ((struct sockaddr *)(sdl));
>  }
>  
> +static inline const struct sockaddr *
> +sdltocsa(const struct sockaddr_dl *sdl)
> +{
> +     return ((const struct sockaddr *)(sdl));
> +}
> +
>  #else /* _KERNEL */
>  
>  __BEGIN_DECLS
> diff --git a/sys/netinet/in.h b/sys/netinet/in.h
> index 432a483f778..4face3d2e51 100644
> --- a/sys/netinet/in.h
> +++ b/sys/netinet/in.h
> @@ -808,16 +808,35 @@ satosin(struct sockaddr *sa)
>       return ((struct sockaddr_in *)(sa));
>  }
>  
> +static inline const struct sockaddr_in *
> +satocsin(const struct sockaddr *sa)
> +{
> +     return ((const struct sockaddr_in *)(sa));
> +}
> +
>  static inline struct sockaddr *
>  sintosa(struct sockaddr_in *sin)
>  {
>       return ((struct sockaddr *)(sin));
>  }
>  
> +static inline const struct sockaddr *
> +sintocsa(const struct sockaddr_in *sin)
> +{
> +     return ((const struct sockaddr *)(sin));
> +}
> +
>  static inline struct in_ifaddr *
>  ifatoia(struct ifaddr *ifa)
>  {
>       return ((struct in_ifaddr *)(ifa));
>  }
> +
> +static inline const struct in_ifaddr *
> +ifatocia(const struct ifaddr *ifa)
> +{
> +     return ((const struct in_ifaddr *)(ifa));
> +}
> +
>  #endif /* _KERNEL */
>  #endif /* _NETINET_IN_H_ */
> diff --git a/sys/netinet6/in6.h b/sys/netinet6/in6.h
> index c7ce800c8a6..b95c35e3285 100644
> --- a/sys/netinet6/in6.h
> +++ b/sys/netinet6/in6.h
> @@ -446,18 +446,36 @@ satosin6(struct sockaddr *sa)
>       return ((struct sockaddr_in6 *)(sa));
>  }
>  
> +static inline const struct sockaddr_in6 *
> +satocsin6(const struct sockaddr *sa)
> +{
> +     return ((const struct sockaddr_in6 *)(sa));
> +}
> +
>  static inline struct sockaddr *
>  sin6tosa(struct sockaddr_in6 *sin6)
>  {
>       return ((struct sockaddr *)(sin6));
>  }
>  
> +static inline const struct sockaddr *
> +sin6tocsa(const struct sockaddr_in6 *sin6)
> +{
> +     return ((const struct sockaddr *)(sin6));
> +}
> +
>  static inline struct in6_ifaddr *
>  ifatoia6(struct ifaddr *ifa)
>  {
>       return ((struct in6_ifaddr *)(ifa));
>  }
>  
> +static inline const struct in6_ifaddr *
> +ifatocia6(const struct ifaddr *ifa)
> +{
> +     return ((const struct in6_ifaddr *)(ifa));
> +}
> +
>  #endif /* _KERNEL */
>  
>  #if __BSD_VISIBLE
> 

Reply via email to