Home | History | Annotate | Download | only in lib

Lines Matching refs:cache

2  * lib/cache.c		Caching Module
14 * @defgroup cache Cache
17 * Cache Management | | Type Specific Cache Operations
26 * 2) destroy old cache +----------- pp_cb ---------|---+
48 * #include <netlink/cache.h>
54 #include <netlink/cache.h>
65 * Return the number of items in the cache
66 * @arg cache cache handle
68 int nl_cache_nitems(struct nl_cache *cache)
70 return cache->c_nitems;
74 * Return the number of items matching a filter in the cache
75 * @arg cache Cache object.
78 int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
83 if (cache->c_ops == NULL)
86 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
97 * Returns \b true if the cache is empty.
98 * @arg cache Cache to check
99 * @return \a true if the cache is empty, otherwise \b false is returned.
101 int nl_cache_is_empty(struct nl_cache *cache)
103 return nl_list_empty(&cache->c_items);
107 * Return the operations set of the cache
108 * @arg cache cache handle
110 struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache)
112 return cache->c_ops;
116 * Return the first element in the cache
117 * @arg cache cache handle
119 struct nl_object *nl_cache_get_first(struct nl_cache *cache)
121 if (nl_list_empty(&cache->c_items))
124 return nl_list_entry(cache->c_items.next,
129 * Return the last element in the cache
130 * @arg cache cache handle
132 struct nl_object *nl_cache_get_last(struct nl_cache *cache)
134 if (nl_list_empty(&cache->c_items))
137 return nl_list_entry(cache->c_items.prev,
142 * Return the next element in the cache
155 * Return the previous element in the cache
170 * @name Cache Allocation/Deletion
175 * Allocate new cache
176 * @arg ops Cache operations
178 * Allocate and initialize a new cache based on the cache operations
181 * @return Allocated cache or NULL if allocation failed.
185 struct nl_cache *cache;
187 cache = calloc(1, sizeof(*cache));
188 if (!cache)
191 nl_init_list_head(&cache->c_items);
192 cache->c_ops = ops;
193 cache->c_flags |= ops->co_flags;
194 cache->c_refcnt = 1;
199 * cache objects for faster lookups
209 cache->hashtable = nl_hash_table_alloc(hashtable_size);
212 NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
214 return cache;
218 * Allocate new cache and fill it
219 * @arg ops Cache operations
223 * Allocate new cache and fill it. Equivalent to calling:
225 * cache = nl_cache_alloc(ops);
226 * nl_cache_refill(sock, cache);
236 struct nl_cache *cache;
239 if (!(cache = nl_cache_alloc(ops)))
242 if (sock && (err = nl_cache_refill(sock, cache)) < 0) {
243 nl_cache_free(cache);
247 *result = cache;
252 * Allocate new cache based on type name
253 * @arg kind Name of cache type
256 * Lookup cache ops via nl_cache_ops_lookup() and allocate the cache
257 * by calling nl_cache_alloc(). Stores the allocated cache in the
267 struct nl_cache *cache;
273 cache = nl_cache_alloc(ops);
275 if (!cache)
278 *result = cache;
283 * Allocate new cache containing a subset of an existing cache
284 * @arg orig Original cache to base new cache on
285 * @arg filter Filter defining the subset to be filled into the new cache
287 * Allocates a new cache matching the type of the cache specified by
288 * \p orig. Iterates over the \p orig cache applying the specified
289 * \p filter and copies all objects that match to the new cache.
292 * other. Later modifications to objects in the original cache will
293 * not affect objects in the new cache.
295 * @return A newly allocated cache or NULL.
300 struct nl_cache *cache;
306 cache = nl_cache_alloc(orig->c_ops);
307 if (!cache)
310 NL_DBG(2, "Filling subset of cache %p <%s> with filter %p into %p\n",
311 orig, nl_cache_name(orig), filter, cache);
317 nl_cache_add(cache, obj);
320 return cache;
324 * Allocate new cache and copy the contents of an existing cache
325 * @arg cache Original cache to base new cache on
327 * Allocates a new cache matching the type of the cache specified by
328 * \p cache. Iterates over the \p cache cache and copies all objects
329 * to the new cache.
332 * other. Later modifications to objects in the original cache will
333 * not affect objects in the new cache.
335 * @return A newly allocated cache or NULL.
337 struct nl_cache *nl_cache_clone(struct nl_cache *cache)
339 struct nl_cache_ops *ops = nl_cache_get_ops(cache);
347 NL_DBG(2, "Cloning %p into %p\n", cache, clone);
349 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
356 * Remove all objects of a cache.
357 * @arg cache Cache to clear
359 * The objects are unliked/removed from the cache by calling
360 * nl_cache_remove() on each object in the cache. If any of the objects
364 * Unlike with nl_cache_free(), the cache is not freed just emptied.
366 void nl_cache_clear(struct nl_cache *cache)
370 NL_DBG(2, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
372 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
376 static void __nl_cache_free(struct nl_cache *cache)
378 nl_cache_clear(cache);
380 if (cache->hashtable)
381 nl_hash_table_free(cache->hashtable);
383 NL_DBG(2, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
384 free(cache);
388 * Increase reference counter of cache
389 * @arg cache Cache
391 void nl_cache_get(struct nl_cache *cache)
393 cache->c_refcnt++;
395 NL_DBG(3, "Incremented cache %p <%s> reference count to %d\n",
396 cache, nl_cache_name(cache), cache->c_refcnt);
400 * Free a cache.
401 * @arg cache Cache to free.
404 * cache and frees the cache afterwards.
408 void nl_cache_free(struct nl_cache *cache)
410 if (!cache)
413 cache->c_refcnt--;
415 NL_DBG(3, "Decremented cache %p <%s> reference count, %d remaining\n",
416 cache, nl_cache_name(cache), cache->c_refcnt);
418 if (cache->c_refcnt <= 0)
419 __nl_cache_free(cache);
422 void nl_cache_put(struct nl_cache *cache)
424 return nl_cache_free(cache);
430 * @name Cache Modifications
434 static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
438 obj->ce_cache = cache;
440 if (cache->hashtable) {
441 ret = nl_hash_table_add(cache->hashtable, obj);
448 nl_list_add_tail(&obj->ce_list, &cache->c_items);
449 cache->c_nitems++;
451 NL_DBG(3, "Added object %p to cache %p <%s>, nitems %d\n",
452 obj, cache, nl_cache_name(cache), cache->c_nitems);
458 * Add object to cache.
459 * @arg cache Cache
460 * @arg obj Object to be added to the cache
462 * Adds the object \p obj to the specified \p cache. In case the object
463 * is already associated with another cache, the object is cloned before
464 * adding it to the cache. In this case, the sole reference to the object
465 * will be the one of the cache. Therefore clearing/freeing the cache
468 * If the object has not been associated with a cache yet, the reference
472 * The type of the object and cache must match, otherwise an error is
479 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
484 if (cache->c_ops->co_obj_ops != obj->ce_ops)
488 NL_DBG(3, "Object %p already in cache, cloning new object\n", obj);
498 ret = __cache_add(cache, new);
506 * Move object from one cache to another
507 * @arg cache Cache to move object to.
510 * Removes the the specified object \p obj from its associated cache
511 * and moves it to another cache.
513 * If the object is not associated with a cache, the function behaves
516 * The type of the object and cache must match, otherwise an error is
523 int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
525 if (cache->c_ops->co_obj_ops != obj->ce_ops)
528 NL_DBG(3, "Moving object %p from cache %p to cache %p\n",
529 obj, obj->ce_cache, cache);
531 /* Acquire reference, if already in a cache this will be
538 return __cache_add(cache, obj);
542 * Remove object from cache.
543 * @arg obj Object to remove from cache
545 * Removes the object \c obj from the cache it is associated with. The
549 * If no cache is associated with the object, this function is a NOP.
554 struct nl_cache *cache = obj->ce_cache;
556 if (cache == NULL)
559 if (cache->hashtable) {
560 ret = nl_hash_table_del(cache->hashtable, obj);
562 NL_DBG(2, "Failed to delete %p from cache %p <%s>.\n",
563 obj, cache, nl_cache_name(cache));
569 cache->c_nitems--;
571 NL_DBG(2, "Deleted object %p from cache %p <%s>.\n",
572 obj, cache, nl_cache_name(cache));
583 * Set synchronization arg1 of cache
584 * @arg cache Cache
590 void nl_cache_set_arg1(struct nl_cache *cache, int arg)
592 cache->c_iarg1 = arg;
596 * Set synchronization arg2 of cache
597 * @arg cache Cache
603 void nl_cache_set_arg2(struct nl_cache *cache, int arg)
605 cache->c_iarg2 = arg;
609 * Set cache flags
610 * @arg cache Cache
613 void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags)
615 cache->c_flags |= flags;
621 * @arg cache Cache
623 * This function causes the \e request-update function of the cache
630 * the \e request_update function of each individual type of cache.
632 * This function will not have any effects on the cache (unless the
633 * request_update implementation of the cache operations does so).
636 * and fill them into the cache.
643 struct nl_cache *cache)
645 if (sk->s_proto != cache->c_ops->co_protocol)
648 if (cache->c_ops->co_request_update == NULL)
651 NL_DBG(2, "Requesting update from kernel for cache %p <%s>\n",
652 cache, nl_cache_name(cache));
654 return cache->c_ops->co_request_update(cache, sk);
679 * @arg cache Cache
682 static int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
688 .ops = cache->c_ops,
692 NL_DBG(2, "Picking up answer for cache %p <%s>\n",
693 cache, nl_cache_name(cache));
704 cache, nl_cache_name(cache), err, nl_geterror(err));
713 struct nl_cache *cache = (struct nl_cache *)p->pp_arg;
716 old = nl_cache_search(cache, c);
727 return nl_cache_add(cache, c);
731 * Pickup a netlink dump response and put it into a cache.
733 * @arg cache Cache to put items into.
736 * the specified cache. If an old object with same key attributes is
737 * present in the cache, it is replaced with the new object.
743 int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
747 .pp_arg = cache,
750 if (sk->s_proto != cache->c_ops->co_protocol)
753 return __cache_pickup(sk, cache, &p);
756 static int cache_include(struct nl_cache *cache, struct nl_object *obj,
764 old = nl_cache_search(cache, obj);
768 * object with the old existing cache object.
773 cb(cache, old, NL_ACT_CHANGE, data);
781 cb(cache, old, NL_ACT_DEL, data);
787 nl_cache_move(cache, obj);
789 cb(cache, obj, NL_ACT_NEW, data);
792 cb(cache, obj, NL_ACT_CHANGE, data);
806 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
809 struct nl_cache_ops *ops = cache->c_ops;
817 return cache_include(cache, obj, &ops->co_msgtypes[i],
820 NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n",
821 obj, cache, nl_cache_name(cache));
833 int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache,
839 .ca_cache = cache,
849 if (sk->s_proto != cache->c_ops->co_protocol)
852 NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
855 nl_cache_mark_all(cache);
857 grp = cache->c_ops->co_groups;
860 (cache->c_flags & NL_CACHE_AF_ITER))
861 nl_cache_set_arg1(cache, grp->ag_family);
864 err = nl_cache_request_full_dump(sk, cache);
868 err = __cache_pickup(sk, cache, &p);
877 (cache->c_flags & NL_CACHE_AF_ITER));
879 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) {
884 change_cb(cache, obj, NL_ACT_DEL, data);
889 NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
928 * Parse a netlink message and add it to the cache.
929 * @arg cache cache to add element to
932 * Parses a netlink message by calling the cache specific message parser
933 * and adds the new element to the cache. If an old object with same key
934 * attributes is present in the cache, it is replaced with the new object.
940 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
944 .pp_arg = cache,
947 return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
951 * (Re)fill a cache with the contents in the kernel.
953 * @arg cache cache to update
955 * Clears the specified cache and fills it with the current state in
960 int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
965 if (sk->s_proto != cache->c_ops->co_protocol)
968 nl_cache_clear(cache);
969 grp = cache->c_ops->co_groups;
972 (cache->c_flags & NL_CACHE_AF_ITER))
973 nl_cache_set_arg1(cache, grp->ag_family);
976 err = nl_cache_request_full_dump(sk, cache);
980 NL_DBG(2, "Updating cache %p <%s> for family %u, request sent, waiting for reply\n",
981 cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC);
983 err = nl_cache_pickup(sk, cache);
993 (cache->c_flags & NL_CACHE_AF_ITER));
1004 static struct nl_object *__cache_fast_lookup(struct nl_cache *cache,
1009 obj = nl_hash_table_lookup(cache->hashtable, needle);
1019 * Search object in cache
1020 * @arg cache Cache
1023 * Searches the cache for an object which matches the object \p needle.
1033 struct nl_object *nl_cache_search(struct nl_cache *cache,
1038 if (cache->hashtable)
1039 return __cache_fast_lookup(cache, needle);
1041 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1052 * Find object in cache
1053 * @arg cache Cache
1056 * Searches the cache for an object which matches the object filter.
1058 * and the cache supports hash lookups, a faster hashtable lookup
1068 struct nl_object *nl_cache_find(struct nl_cache *cache,
1073 if (cache->c_ops == NULL)
1077 && cache->hashtable)
1078 return __cache_fast_lookup(cache, filter);
1080 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1091 * Mark all objects of a cache
1092 * @arg cache Cache
1094 * Marks all objects of a cache by calling nl_object_mark() on each
1095 * object associated with the cache.
1097 void nl_cache_mark_all(struct nl_cache *cache)
1101 NL_DBG(2, "Marking all objects in cache %p <%s>\n",
1102 cache, nl_cache_name(cache));
1104 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
1116 * Dump all elements of a cache.
1117 * @arg cache cache to dump
1120 * Dumps all elements of the \a cache to the file descriptor \a fd.
1122 void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
1124 nl_cache_dump_filter(cache, params, NULL);
1128 * Dump all elements of a cache (filtered).
1129 * @arg cache cache to dump
1133 * Dumps all elements of the \a cache to the file descriptor \a fd
1136 void nl_cache_dump_filter(struct nl_cache *cache,
1144 NL_DBG(2, "Dumping cache %p <%s> with filter %p\n",
1145 cache, nl_cache_name(cache), filter);
1150 if (cache->c_ops == NULL)
1153 ops = cache->c_ops->co_obj_ops;
1160 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1177 * Call a callback on each element of the cache.
1178 * @arg cache cache to iterate on
1182 * Calls a callback function \a cb on each element of the \a cache.
1185 void nl_cache_foreach(struct nl_cache *cache,
1188 nl_cache_foreach_filter(cache, NULL, cb, arg);
1192 * Call a callback on each element of the cache (filtered).
1193 * @arg cache cache to iterate on
1198 * Calls a callback function \a cb on each element of the \a cache
1202 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter,
1207 if (cache->c_ops == NULL)
1210 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {