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 @@ -1022,6 +1022,45 @@ struct nl_object *nl_cache_search(struct nl_cache *cache, return NULL; } +/** + * 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; +} + /** @} */ /** @} */