diff options
author | Thierry Reding <thierry.reding@avionic-design.de> | 2012-02-16 12:57:42 +0100 |
---|---|---|
committer | Thomas Graf <tgraf@redhat.com> | 2012-02-16 13:42:49 +0100 |
commit | 0b40364150452cb8a8467d10143637bfbb99fb89 (patch) | |
tree | cc3d3f6f41768e918a1018cba5a99409a61f737b | |
parent | a17970b974bb3896f253817f98a9fa6176fcd422 (diff) | |
download | libnl-0b40364150452cb8a8467d10143637bfbb99fb89.tar.gz |
Add new nl_cache_clone() function.
The function can be used to make a copy of an existing cache. It is very
similar to nl_cache_subset() except that it allows no filtering but
copies every object.
Signed-off-by: Thierry Reding <thierry.reding@avionic-design.de>
Signed-off-by: Thomas Graf <tgraf@redhat.com>
-rw-r--r-- | include/netlink/cache.h | 1 | ||||
-rw-r--r-- | lib/cache.c | 30 |
2 files changed, 31 insertions, 0 deletions
diff --git a/include/netlink/cache.h b/include/netlink/cache.h index f8073f0..59886ac 100644 --- a/include/netlink/cache.h +++ b/include/netlink/cache.h @@ -44,6 +44,7 @@ extern int nl_cache_alloc_name(const char *, struct nl_cache **); extern struct nl_cache * nl_cache_subset(struct nl_cache *, struct nl_object *); +extern struct nl_cache * nl_cache_clone(struct nl_cache *); extern void nl_cache_clear(struct nl_cache *); extern void nl_cache_free(struct nl_cache *); diff --git a/lib/cache.c b/lib/cache.c index a1c8eae..814c616 100644 --- a/lib/cache.c +++ b/lib/cache.c @@ -288,6 +288,36 @@ struct nl_cache *nl_cache_subset(struct nl_cache *orig, } /** + * Allocate new cache and copy the contents of an existing cache + * @arg cache Original cache to base new cache on + * + * Allocates a new cache matching the type of the cache specified by + * \p cache. Iterates over the \p cache cache and copies all objects + * to the new cache. + * + * The copied objects are clones but do not contain a reference to each + * other. Later modifications to objects in the original cache will + * not affect objects in the new cache. + * + * @return A newly allocated cache or NULL. + */ +struct nl_cache *nl_cache_clone(struct nl_cache *cache) +{ + struct nl_cache_ops *ops = nl_cache_get_ops(cache); + struct nl_cache *clone; + struct nl_object *obj; + + clone = nl_cache_alloc(ops); + if (!clone) + return NULL; + + nl_list_for_each_entry(obj, &cache->c_items, ce_list) + nl_cache_add(clone, obj); + + return clone; +} + +/** * Remove all objects of a cache. * @arg cache Cache to clear * |