Lines Matching refs:cache

2  * lib/cache.c		Caching Module
14 * @defgroup cache Cache
26 * 2) destroy old cache +----------- pp_cb ---------|---+
45 #include <netlink/cache.h>
55 * Return the number of items in the cache
56 * @arg cache cache handle
58 int nl_cache_nitems(struct nl_cache *cache)
60 return cache->c_nitems;
64 * Return the number of items matching a filter in the cache
65 * @arg cache Cache object.
68 int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
74 if (cache->c_ops == NULL)
77 ops = cache->c_ops->co_obj_ops;
79 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
90 * Returns \b true if the cache is empty.
91 * @arg cache Cache to check
92 * @return \a true if the cache is empty, otherwise \b false is returned.
94 int nl_cache_is_empty(struct nl_cache *cache)
96 return nl_list_empty(&cache->c_items);
100 * Return the operations set of the cache
101 * @arg cache cache handle
103 struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache)
105 return cache->c_ops;
109 * Return the first element in the cache
110 * @arg cache cache handle
112 struct nl_object *nl_cache_get_first(struct nl_cache *cache)
114 if (nl_list_empty(&cache->c_items))
117 return nl_list_entry(cache->c_items.next,
122 * Return the last element in the cache
123 * @arg cache cache handle
125 struct nl_object *nl_cache_get_last(struct nl_cache *cache)
127 if (nl_list_empty(&cache->c_items))
130 return nl_list_entry(cache->c_items.prev,
135 * Return the next element in the cache
148 * Return the previous element in the cache
168 * Allocate an empty cache
169 * @arg ops cache operations to base the cache on
171 * @return A newly allocated and initialized cache.
175 struct nl_cache *cache;
177 cache = calloc(1, sizeof(*cache));
178 if (!cache)
181 nl_init_list_head(&cache->c_items);
182 cache->c_ops = ops;
184 NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
186 return cache;
192 struct nl_cache *cache;
195 if (!(cache = nl_cache_alloc(ops)))
198 if (sock && (err = nl_cache_refill(sock, cache)) < 0) {
199 nl_cache_free(cache);
203 *result = cache;
208 * Allocate an empty cache based on type name
209 * @arg kind Name of cache type
210 * @return A newly allocated and initialized cache.
215 struct nl_cache *cache;
221 if (!(cache = nl_cache_alloc(ops)))
224 *result = cache;
229 * Allocate a new cache containing a subset of a cache
230 * @arg orig Original cache to be based on
231 * @arg filter Filter defining the subset to be filled into new cache
232 * @return A newly allocated cache or NULL.
237 struct nl_cache *cache;
244 cache = nl_cache_alloc(orig->c_ops);
245 if (!cache)
254 nl_cache_add(cache, obj);
257 return cache;
261 * Clear a cache.
262 * @arg cache cache to clear
264 * Removes all elements of a cache.
266 void nl_cache_clear(struct nl_cache *cache)
270 NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
272 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
277 * Free a cache.
278 * @arg cache Cache to free.
280 * Removes all elements of a cache and frees all memory.
284 void nl_cache_free(struct nl_cache *cache)
286 if (!cache)
289 nl_cache_clear(cache);
290 NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
291 free(cache);
301 static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
303 obj->ce_cache = cache;
305 nl_list_add_tail(&obj->ce_list, &cache->c_items);
306 cache->c_nitems++;
308 NL_DBG(1, "Added %p to cache %p <%s>.\n",
309 obj, cache, nl_cache_name(cache));
315 * Add object to a cache.
316 * @arg cache Cache to add object to
317 * @arg obj Object to be added to the cache
319 * Adds the given object to the specified cache. The object is cloned
320 * if it has been added to another cache already.
324 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
328 if (cache->c_ops->co_obj_ops != obj->ce_ops)
340 return __cache_add(cache, new);
344 * Move object from one cache to another
345 * @arg cache Cache to move object to.
348 * Removes the given object from its associated cache if needed
349 * and adds it to the new cache.
353 int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
355 if (cache->c_ops->co_obj_ops != obj->ce_ops)
358 NL_DBG(3, "Moving object %p to cache %p\n", obj, cache);
360 /* Acquire reference, if already in a cache this will be
367 return __cache_add(cache, obj);
371 * Removes an object from a cache.
372 * @arg obj Object to remove from its cache
374 * Removes the object \c obj from the cache it is assigned to, since
375 * an object can only be assigned to one cache at a time, the cache
380 struct nl_cache *cache = obj->ce_cache;
382 if (cache == NULL)
388 cache->c_nitems--;
390 NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
391 obj, cache, nl_cache_name(cache));
395 * Search for an object in a cache
396 * @arg cache Cache to search in.
399 * Iterates over the cache and looks for an object with identical
405 struct nl_object *nl_cache_search(struct nl_cache *cache,
410 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
429 * Request a full dump from the kernel to fill a cache
431 * @arg cache Cache subjected to be filled.
434 * related to the specified cache to the netlink socket.
437 * into a cache.
439 int nl_cache_request_full_dump(struct nl_sock *sk, struct nl_cache *cache)
441 NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n",
442 cache, nl_cache_name(cache));
444 if (cache->c_ops->co_request_update == NULL)
447 return cache->c_ops->co_request_update(cache, sk);
464 int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
470 .ops = cache->c_ops,
474 NL_DBG(1, "Picking up answer for cache %p <%s>...\n",
475 cache, nl_cache_name(cache));
486 "%d: %s", cache, nl_cache_name(cache),
500 * Pickup a netlink dump response and put it into a cache.
502 * @arg cache Cache to put items into.
505 * the specified cache.
509 int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
513 .pp_arg = cache,
516 return __cache_pickup(sk, cache, &p);
519 static int cache_include(struct nl_cache *cache, struct nl_object *obj,
527 old = nl_cache_search(cache, obj);
532 cb(cache, old, NL_ACT_DEL, data);
538 nl_cache_move(cache, obj);
540 cb(cache, obj, NL_ACT_NEW, data);
543 cb(cache, obj, NL_ACT_CHANGE, data);
557 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
560 struct nl_cache_ops *ops = cache->c_ops;
568 return cache_include(cache, obj, &ops->co_msgtypes[i],
581 int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache,
586 .ca_cache = cache,
596 NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
599 nl_cache_mark_all(cache);
601 err = nl_cache_request_full_dump(sk, cache);
605 err = __cache_pickup(sk, cache, &p);
609 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) {
614 change_cb(cache, obj, NL_ACT_DEL, data);
619 NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
658 * Parse a netlink message and add it to the cache.
659 * @arg cache cache to add element to
662 * Parses a netlink message by calling the cache specific message parser
663 * and adds the new element to the cache.
667 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
671 .pp_arg = cache,
674 return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
678 * (Re)fill a cache with the contents in the kernel.
680 * @arg cache cache to update
682 * Clears the specified cache and fills it with the current state in
687 int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
691 err = nl_cache_request_full_dump(sk, cache);
695 NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
696 cache, nl_cache_name(cache));
697 nl_cache_clear(cache);
699 return nl_cache_pickup(sk, cache);
710 * Mark all objects in a cache
711 * @arg cache Cache to mark all objects in
713 void nl_cache_mark_all(struct nl_cache *cache)
717 NL_DBG(2, "Marking all objects in cache %p <%s>...\n",
718 cache, nl_cache_name(cache));
720 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
732 * Dump all elements of a cache.
733 * @arg cache cache to dump
736 * Dumps all elements of the \a cache to the file descriptor \a fd.
738 void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
740 nl_cache_dump_filter(cache, params, NULL);
744 * Dump all elements of a cache (filtered).
745 * @arg cache cache to dump
749 * Dumps all elements of the \a cache to the file descriptor \a fd
752 void nl_cache_dump_filter(struct nl_cache *cache,
760 NL_DBG(2, "Dumping cache %p <%s> filter %p\n",
761 cache, nl_cache_name(cache), filter);
766 if (cache->c_ops == NULL)
769 ops = cache->c_ops->co_obj_ops;
773 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
790 * Call a callback on each element of the cache.
791 * @arg cache cache to iterate on
795 * Calls a callback function \a cb on each element of the \a cache.
798 void nl_cache_foreach(struct nl_cache *cache,
801 nl_cache_foreach_filter(cache, NULL, cb, arg);
805 * Call a callback on each element of the cache (filtered).
806 * @arg cache cache to iterate on
811 * Calls a callback function \a cb on each element of the \a cache
815 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter,
821 if (cache->c_ops == NULL)
824 ops = cache->c_ops->co_obj_ops;
826 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {