1From abb7391f38893af7d6108f97ac8597468f113a41 Mon Sep 17 00:00:00 2001 2From: Thomas Haller <thaller@redhat.com> 3Date: Wed, 15 Apr 2020 12:42:05 +0200 4Subject: [PATCH] lib: add "include/netlink-private/nl-auto.h" header 5 6Conflict:NA 7Reference:https://github.com/thom311/libnl/commit/abb7391f38893af7d6108f97ac8597468f113a41 8 9--- 10 Makefile.am | 1 + 11 include/netlink-private/nl-auto.h | 60 +++++++++++++++++++++++++++++++ 12 include/netlink-private/utils.h | 14 ++------ 13 3 files changed, 63 insertions(+), 12 deletions(-) 14 create mode 100644 include/netlink-private/nl-auto.h 15 16diff --git a/Makefile.am b/Makefile.am 17index b2e87379..085da53f 100644 18--- a/Makefile.am 19+++ b/Makefile.am 20@@ -261,6 +261,7 @@ noinst_HEADERS = \ 21 include/netlink-private/cache-api.h \ 22 include/netlink-private/genl.h \ 23 include/netlink-private/netlink.h \ 24+ include/netlink-private/nl-auto.h \ 25 include/netlink-private/object-api.h \ 26 include/netlink-private/route/link/api.h \ 27 include/netlink-private/route/link/sriov.h \ 28diff --git a/include/netlink-private/nl-auto.h b/include/netlink-private/nl-auto.h 29new file mode 100644 30index 00000000..7b923f26 31--- /dev/null 32+++ b/include/netlink-private/nl-auto.h 33@@ -0,0 +1,60 @@ 34+// SPDX-License-Identifier: LGPL-2.1+ 35+ 36+#ifndef NETLINK_NL_AUTO_H_ 37+#define NETLINK_NL_AUTO_H_ 38+ 39+#define _nl_auto(fcn) __attribute__ ((__cleanup__(fcn))) 40+ 41+#define _NL_AUTO_DEFINE_FCN_VOID0(CastType, name, func) \ 42+static inline void name(void *v) \ 43+{ \ 44+ if (*((CastType *) v)) \ 45+ func(*((CastType *) v)); \ 46+} 47+ 48+#define _NL_AUTO_DEFINE_FCN_TYPED0(CastType, name, func) \ 49+static inline void name(CastType *v) \ 50+{ \ 51+ if (*v) \ 52+ func(*v); \ 53+} 54+ 55+#define _nl_auto_free _nl_auto(_nl_auto_free_fcn) 56+_NL_AUTO_DEFINE_FCN_VOID0(void *, _nl_auto_free_fcn, free) 57+ 58+struct nl_addr; 59+void nl_addr_put(struct nl_addr *); 60+#define _nl_auto_nl_addr _nl_auto(_nl_auto_nl_addr_fcn) 61+_NL_AUTO_DEFINE_FCN_TYPED0(struct nl_addr *, _nl_auto_nl_addr_fcn, nl_addr_put) 62+ 63+struct nl_msg; 64+void nlmsg_free(struct nl_msg *); 65+#define _nl_auto_nl_msg _nl_auto(_nl_auto_nl_msg_fcn) 66+_NL_AUTO_DEFINE_FCN_TYPED0(struct nl_msg *, _nl_auto_nl_msg_fcn, nlmsg_free) 67+ 68+struct rtnl_link; 69+void rtnl_link_put(struct rtnl_link *); 70+#define _nl_auto_rtnl_link _nl_auto(_nl_auto_rtnl_link_fcn) 71+_NL_AUTO_DEFINE_FCN_TYPED0(struct rtnl_link *, _nl_auto_rtnl_link_fcn, rtnl_link_put) 72+ 73+struct rtnl_route; 74+void rtnl_route_put(struct rtnl_route *); 75+#define _nl_auto_rtnl_route _nl_auto(_nl_auto_rtnl_route_fcn) 76+_NL_AUTO_DEFINE_FCN_TYPED0(struct rtnl_route *, _nl_auto_rtnl_route_fcn, rtnl_route_put) 77+ 78+struct rtnl_nexthop; 79+void rtnl_route_nh_free(struct rtnl_nexthop *); 80+#define _nl_auto_rtnl_nexthop _nl_auto(_nl_auto_rtnl_nexthop_fcn) 81+_NL_AUTO_DEFINE_FCN_TYPED0(struct rtnl_nexthop *, _nl_auto_rtnl_nexthop_fcn, rtnl_route_nh_free) 82+ 83+struct nl_cache; 84+void nl_cache_put(struct nl_cache *); 85+#define _nl_auto_nl_cache _nl_auto(_nl_auto_nl_cache_fcn) 86+_NL_AUTO_DEFINE_FCN_TYPED0(struct nl_cache *, _nl_auto_nl_cache_fcn, nl_cache_put) 87+ 88+struct rtnl_link_af_ops; 89+void rtnl_link_af_ops_put(struct rtnl_link_af_ops *); 90+#define _nl_auto_rtnl_link_af_ops _nl_auto(_nl_auto_rtnl_link_af_ops_fcn) 91+_NL_AUTO_DEFINE_FCN_TYPED0(struct rtnl_link_af_ops *, _nl_auto_rtnl_link_af_ops_fcn, rtnl_link_af_ops_put) 92+ 93+#endif /* NETLINK_NL_AUTO_H_ */ 94diff --git a/include/netlink-private/utils.h b/include/netlink-private/utils.h 95index 9c5a3d82..46e77451 100644 96--- a/include/netlink-private/utils.h 97+++ b/include/netlink-private/utils.h 98@@ -85,18 +85,6 @@ 99 100 /*****************************************************************************/ 101 102-#define _NL_AUTO_DEFINE_FCN_VOID0(CastType, name, func) \ 103-static inline void name (void *v) \ 104-{ \ 105- if (*((CastType *) v)) \ 106- func (*((CastType *) v)); \ 107-} 108- 109-#define _nl_auto_free _nl_auto(_nl_auto_free_fcn) 110-_NL_AUTO_DEFINE_FCN_VOID0 (void *, _nl_auto_free_fcn, free) 111- 112-/*****************************************************************************/ 113- 114 extern const char *nl_strerror_l(int err); 115 116 /*****************************************************************************/ 117@@ -222,4 +210,6 @@ _nl_strncpy(char *dst, const char *src, size_t len) 118 return dst; 119 } 120 121+#include "nl-auto.h" 122+ 123 #endif 124