[PATCH] cache: Add new nl_cache_find api
roopa at cumulusnetworks.com
roopa at cumulusnetworks.com
Tue Jan 8 07:33:29 EST 2013
From: roopa <roopa at cumulusnetworks.com>
This patch adds new cache find api
nl_cache_find api was suggested by Thomas.
Unlike nl_cache_search, this patch uses
nl_object_match_filter() to look for an
object match.
Am not sure this matches what was decided
on the list few weeks back. I will be happy
to make any changes.
Signed-off-by: Roopa Prabhu <roopa at cumulusnetworks.com>
---
include/netlink/object.h | 1 +
lib/cache.c | 39 +++++++++++++++++++++++++++++++++++++++
lib/object.c | 22 ++++++++++++++++++++++
3 files changed, 62 insertions(+)
diff --git a/include/netlink/object.h b/include/netlink/object.h
index f25713e..a95feda 100644
--- a/include/netlink/object.h
+++ b/include/netlink/object.h
@@ -64,6 +64,7 @@ extern struct nl_cache * nl_object_get_cache(struct nl_object *);
extern const char * nl_object_get_type(const struct nl_object *);
extern int nl_object_get_msgtype(const struct nl_object *);
struct nl_object_ops * nl_object_get_ops(const struct nl_object *);
+uint32_t nl_object_get_id_attrs(struct nl_object *obj);
static inline void * nl_object_priv(struct nl_object *obj)
diff --git a/lib/cache.c b/lib/cache.c
index 42c0b5e..24ffa31 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -1023,6 +1023,45 @@ struct nl_object *nl_cache_search(struct nl_cache *cache,
}
/**
+ * Find object in cache
+ * @arg cache Cache
+ * @arg filter object acting as a filter
+ *
+ * Searches the cache for an object which matches the object filter.
+ * If the filter attributes matches the object type id attributes,
+ * and the cache supports hash lookups, a faster hashtable lookup
+ * is used to return the object. Else, function nl_object_match_filter() is
+ * used to determine if the objects match. If a matching object is
+ * found, the reference counter is incremented and the object is returned.
+ *
+ * Therefore, if an object is returned, the reference to the object
+ * must be returned by calling nl_object_put() after usage.
+ *
+ * @return Reference to object or NULL if not found.
+ */
+struct nl_object *nl_cache_find(struct nl_cache *cache,
+ struct nl_object *filter)
+{
+ struct nl_object *obj;
+
+ if (cache->c_ops == NULL)
+ BUG();
+
+ if ((nl_object_get_id_attrs(filter) == filter->ce_mask)
+ && cache->hashtable)
+ return __cache_fast_lookup(cache, filter);
+
+ nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
+ if (nl_object_match_filter(obj, filter)) {
+ nl_object_get(obj);
+ return obj;
+ }
+ }
+
+ return NULL;
+}
+
+/**
* Mark all objects of a cache
* @arg cache Cache
*
diff --git a/lib/object.c b/lib/object.c
index 806bae6..7361194 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -506,6 +506,28 @@ struct nl_object_ops *nl_object_get_ops(const struct nl_object *obj)
return obj->ce_ops;
}
+/**
+ * Return object id attribute mask
+ * @arg obj object
+ *
+ * @return object id attribute mask
+ */
+uint32_t nl_object_get_id_attrs(struct nl_object *obj)
+{
+ struct nl_object_ops *ops = obj_ops(obj);
+ uint32_t id_attrs;
+
+ if (!ops)
+ return 0;
+
+ if (ops->oo_id_attrs_get)
+ id_attrs = ops->oo_id_attrs_get(obj);
+ else
+ id_attrs = ops->oo_id_attrs;
+
+ return id_attrs;
+}
+
/** @} */
/** @} */
--
1.7.10.4
More information about the libnl
mailing list