[PATCH] add user data to change_func_t for caches

Andreas Fett a.fett at gmx.de
Wed Sep 22 14:10:26 EDT 2010


Hi,

the patch below adds the possibility to
pass user data to callbacks of type 
change_func_t when using the nl_cache_mngr_*
family of functions.

If there is any better way to do this,
without duplicating the code in
cache_mngr.c please let me know.

Cheers Andreas

---
 include/netlink-types.h |    1 +
 include/netlink/cache.h |    9 ++++++---
 lib/cache.c             |   19 ++++++++++---------
 lib/cache_mngr.c        |    5 +++--
 4 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/include/netlink-types.h b/include/netlink-types.h
index ace5dde..ff699bb 100644
--- a/include/netlink-types.h
+++ b/include/netlink-types.h
@@ -83,6 +83,7 @@ struct nl_cache_assoc
 {
 	struct nl_cache *	ca_cache;
 	change_func_t		ca_change;
+	void *			ca_change_data;
 };
 
 struct nl_cache_mngr
diff --git a/include/netlink/cache.h b/include/netlink/cache.h
index f4db3b9..c752920 100644
--- a/include/netlink/cache.h
+++ b/include/netlink/cache.h
@@ -24,7 +24,7 @@ extern "C" {
 
 struct nl_cache;
 
-typedef void (*change_func_t)(struct nl_cache *, struct nl_object *, int);
+typedef void (*change_func_t)(struct nl_cache *, struct nl_object *, int, void *);
 
 /* Access Functions */
 extern int			nl_cache_nitems(struct nl_cache *);
@@ -59,10 +59,12 @@ extern int			nl_cache_pickup(struct nl_sock *,
 						struct nl_cache *);
 extern int			nl_cache_resync(struct nl_sock *,
 						struct nl_cache *,
-						change_func_t);
+						change_func_t,
+						void *);
 extern int			nl_cache_include(struct nl_cache *,
 						 struct nl_object *,
-						 change_func_t);
+						 change_func_t,
+						 void *);
 
 /* General */
 extern int			nl_cache_is_empty(struct nl_cache *);
@@ -112,6 +114,7 @@ extern int			nl_cache_mngr_alloc(struct nl_sock *,
 extern int			nl_cache_mngr_add(struct nl_cache_mngr *,
 						  const char *,
 						  change_func_t,
+						  void *,
 						  struct nl_cache **);
 extern int			nl_cache_mngr_get_fd(struct nl_cache_mngr *);
 extern int			nl_cache_mngr_poll(struct nl_cache_mngr *,
diff --git a/lib/cache.c b/lib/cache.c
index 5fab32a..2b24946 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -517,7 +517,7 @@ int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
 }
 
 static int cache_include(struct nl_cache *cache, struct nl_object *obj,
-			 struct nl_msgtype *type, change_func_t cb)
+			 struct nl_msgtype *type, change_func_t cb, void *data)
 {
 	struct nl_object *old;
 
@@ -529,7 +529,7 @@ static int cache_include(struct nl_cache *cache, struct nl_object *obj,
 			nl_cache_remove(old);
 			if (type->mt_act == NL_ACT_DEL) {
 				if (cb)
-					cb(cache, old, NL_ACT_DEL);
+					cb(cache, old, NL_ACT_DEL, data);
 				nl_object_put(old);
 			}
 		}
@@ -537,10 +537,10 @@ static int cache_include(struct nl_cache *cache, struct nl_object *obj,
 		if (type->mt_act == NL_ACT_NEW) {
 			nl_cache_move(cache, obj);
 			if (old == NULL && cb)
-				cb(cache, obj, NL_ACT_NEW);
+				cb(cache, obj, NL_ACT_NEW, data);
 			else if (old) {
 				if (nl_object_diff(old, obj) && cb)
-					cb(cache, obj, NL_ACT_CHANGE);
+					cb(cache, obj, NL_ACT_CHANGE, data);
 
 				nl_object_put(old);
 			}
@@ -555,7 +555,7 @@ static int cache_include(struct nl_cache *cache, struct nl_object *obj,
 }
 
 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
-		     change_func_t change_cb)
+		     change_func_t change_cb, void *data)
 {
 	struct nl_cache_ops *ops = cache->c_ops;
 	int i;
@@ -566,7 +566,7 @@ int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
 	for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
 		if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
 			return cache_include(cache, obj, &ops->co_msgtypes[i],
-					     change_cb);
+					     change_cb, data);
 
 	return -NLE_MSGTYPE_NOSUPPORT;
 }
@@ -575,16 +575,17 @@ static int resync_cb(struct nl_object *c, struct nl_parser_param *p)
 {
 	struct nl_cache_assoc *ca = p->pp_arg;
 
-	return nl_cache_include(ca->ca_cache, c, ca->ca_change);
+	return nl_cache_include(ca->ca_cache, c, ca->ca_change, ca->ca_change_data);
 }
 
 int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache,
-		    change_func_t change_cb)
+		    change_func_t change_cb, void *data)
 {
 	struct nl_object *obj, *next;
 	struct nl_cache_assoc ca = {
 		.ca_cache = cache,
 		.ca_change = change_cb,
+		.ca_change_data = data,
 	};
 	struct nl_parser_param p = {
 		.pp_cb = resync_cb,
@@ -610,7 +611,7 @@ int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache,
 			nl_object_get(obj);
 			nl_cache_remove(obj);
 			if (change_cb)
-				change_cb(cache, obj, NL_ACT_DEL);
+				change_cb(cache, obj, NL_ACT_DEL, data);
 			nl_object_put(obj);
 		}
 	}
diff --git a/lib/cache_mngr.c b/lib/cache_mngr.c
index 0675316..81052aa 100644
--- a/lib/cache_mngr.c
+++ b/lib/cache_mngr.c
@@ -95,7 +95,7 @@ static int include_cb(struct nl_object *obj, struct nl_parser_param *p)
 	if (nl_debug >= 4)
 		nl_object_dump(obj, &nl_debug_dp);
 #endif
-	return nl_cache_include(ca->ca_cache, obj, ca->ca_change);
+	return nl_cache_include(ca->ca_cache, obj, ca->ca_change, ca->ca_change_data);
 }
 
 static int event_input(struct nl_msg *msg, void *arg)
@@ -207,7 +207,7 @@ errout:
  * @return 0 on success or a negative error code.
  */
 int nl_cache_mngr_add(struct nl_cache_mngr *mngr, const char *name,
-		      change_func_t cb, struct nl_cache **result)
+		      change_func_t cb, void *data, struct nl_cache **result)
 {
 	struct nl_cache_ops *ops;
 	struct nl_cache *cache;
@@ -264,6 +264,7 @@ retry:
 
 	mngr->cm_assocs[i].ca_cache = cache;
 	mngr->cm_assocs[i].ca_change = cb;
+	mngr->cm_assocs[i].ca_change_data = data;
 
 	if (mngr->cm_flags & NL_AUTO_PROVIDE)
 		nl_cache_mngt_provide(cache);
-- 
1.7.1


-- 
The three chief virtues of a programmer are:
Laziness, Impatience and Hubris. -- Larry Wall



More information about the libnl mailing list