Lines Matching defs:lc

29 	BUG_ON(!lc);			\
30 BUG_ON(!lc->nr_elements); \
31 BUG_ON(test_and_set_bit(__LC_PARANOIA, &lc->flags)); \
35 clear_bit_unlock(__LC_PARANOIA, &lc->flags); \
38 /* BUG() if e is not one of the elements tracked by lc */
39 #define PARANOIA_LC_ELEMENT(lc, e) do { \
40 struct lru_cache *lc_ = (lc); \
51 * Because of PARANOIA_ENTRY() above abusing lc->flags as well,
53 * return 0 == cmpxchg(&lc->flags, 0, LC_LOCKED);
55 int lc_try_lock(struct lru_cache *lc)
59 val = cmpxchg(&lc->flags, 0, LC_LOCKED);
68 old = lc->flags & LC_PARANOIA;
70 val = cmpxchg(&lc->flags, old, new);
93 struct lru_cache *lc;
114 lc = kzalloc(sizeof(*lc), GFP_KERNEL);
115 if (!lc)
118 INIT_LIST_HEAD(&lc->in_use);
119 INIT_LIST_HEAD(&lc->lru);
120 INIT_LIST_HEAD(&lc->free);
121 INIT_LIST_HEAD(&lc->to_be_changed);
123 lc->name = name;
124 lc->element_size = e_size;
125 lc->element_off = e_off;
126 lc->nr_elements = e_count;
127 lc->max_pending_changes = max_pending_changes;
128 lc->lc_cache = cache;
129 lc->lc_element = element;
130 lc->lc_slot = slot;
137 memset(p, 0, lc->element_size);
142 list_add(&e->list, &lc->free);
146 return lc;
153 kfree(lc);
160 static void lc_free_by_index(struct lru_cache *lc, unsigned i)
162 void *p = lc->lc_element[i];
165 p -= lc->element_off;
166 kmem_cache_free(lc->lc_cache, p);
172 * @lc: the lru cache to destroy
174 void lc_destroy(struct lru_cache *lc)
177 if (!lc)
179 for (i = 0; i < lc->nr_elements; i++)
180 lc_free_by_index(lc, i);
181 kfree(lc->lc_element);
182 kfree(lc->lc_slot);
183 kfree(lc);
187 * lc_reset - does a full reset for @lc and the hash table slots.
188 * @lc: the lru cache to operate on
191 * basically a short cut to lc_destroy(lc); lc = lc_create(...);
193 void lc_reset(struct lru_cache *lc)
197 INIT_LIST_HEAD(&lc->in_use);
198 INIT_LIST_HEAD(&lc->lru);
199 INIT_LIST_HEAD(&lc->free);
200 INIT_LIST_HEAD(&lc->to_be_changed);
201 lc->used = 0;
202 lc->hits = 0;
203 lc->misses = 0;
204 lc->starving = 0;
205 lc->locked = 0;
206 lc->changed = 0;
207 lc->pending_changes = 0;
208 lc->flags = 0;
209 memset(lc->lc_slot, 0, sizeof(struct hlist_head) * lc->nr_elements);
211 for (i = 0; i < lc->nr_elements; i++) {
212 struct lc_element *e = lc->lc_element[i];
214 p -= lc->element_off;
215 memset(p, 0, lc->element_size);
220 list_add(&e->list, &lc->free);
225 * lc_seq_printf_stats - print stats about @lc into @seq
227 * @lc: the lru cache to print statistics of
229 void lc_seq_printf_stats(struct seq_file *seq, struct lru_cache *lc)
239 lc->name, lc->used, lc->nr_elements,
240 lc->hits, lc->misses, lc->starving, lc->locked, lc->changed);
243 static struct hlist_head *lc_hash_slot(struct lru_cache *lc, unsigned int enr)
245 return lc->lc_slot + (enr % lc->nr_elements);
249 static struct lc_element *__lc_find(struct lru_cache *lc, unsigned int enr,
254 BUG_ON(!lc);
255 BUG_ON(!lc->nr_elements);
256 hlist_for_each_entry(e, lc_hash_slot(lc, enr), colision) {
271 * @lc: The lru_cache object
280 struct lc_element *lc_find(struct lru_cache *lc, unsigned int enr)
282 return __lc_find(lc, enr, 0);
287 * @lc: The lru_cache object
295 bool lc_is_used(struct lru_cache *lc, unsigned int enr)
297 struct lc_element *e = __lc_find(lc, enr, 1);
303 * @lc: The lru_cache object
309 void lc_del(struct lru_cache *lc, struct lc_element *e)
312 PARANOIA_LC_ELEMENT(lc, e);
317 list_move(&e->list, &lc->free);
321 static struct lc_element *lc_prepare_for_change(struct lru_cache *lc, unsigned new_number)
326 if (!list_empty(&lc->free))
327 n = lc->free.next;
328 else if (!list_empty(&lc->lru))
329 n = lc->lru.prev;
334 PARANOIA_LC_ELEMENT(lc, e);
339 hlist_add_head(&e->colision, lc_hash_slot(lc, new_number));
340 list_move(&e->list, &lc->to_be_changed);
345 static int lc_unused_element_available(struct lru_cache *lc)
347 if (!list_empty(&lc->free))
349 if (!list_empty(&lc->lru))
361 static struct lc_element *__lc_get(struct lru_cache *lc, unsigned int enr, unsigned int flags)
366 if (lc->flags & LC_STARVING) {
367 ++lc->starving;
371 e = __lc_find(lc, enr, 1);
387 ++lc->hits;
391 ++lc->hits;
393 lc->used++;
394 list_move(&e->list, &lc->in_use); /* Not evictable... */
399 ++lc->misses;
405 test_and_set_bit(__LC_DIRTY, &lc->flags);
410 if (test_bit(__LC_LOCKED, &lc->flags)) {
411 ++lc->locked;
418 if (!lc_unused_element_available(lc)) {
419 __set_bit(__LC_STARVING, &lc->flags);
426 if (lc->pending_changes >= lc->max_pending_changes)
429 e = lc_prepare_for_change(lc, enr);
432 clear_bit(__LC_STARVING, &lc->flags);
434 lc->used++;
435 lc->pending_changes++;
442 * @lc: the lru cache to operate on
457 * and a changing transaction is still pending (@lc was marked %LC_DIRTY).
458 * Or no unused or free element could be recycled (@lc will be marked as
475 * lc_committed(lc) and lc_unlock(), to finish the change.
480 struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr)
482 return __lc_get(lc, enr, LC_GET_MAY_CHANGE);
487 * @lc: the lru cache to operate on
500 struct lc_element *lc_get_cumulative(struct lru_cache *lc, unsigned int enr)
502 return __lc_get(lc, enr, LC_GET_MAY_CHANGE|LC_GET_MAY_USE_UNCOMMITTED);
507 * @lc: the lru cache to operate on
521 struct lc_element *lc_try_get(struct lru_cache *lc, unsigned int enr)
523 return __lc_get(lc, enr, 0);
527 * lc_committed - tell @lc that pending changes have been recorded
528 * @lc: the lru cache to operate on
534 void lc_committed(struct lru_cache *lc)
539 list_for_each_entry_safe(e, tmp, &lc->to_be_changed, list) {
541 ++lc->changed;
543 list_move(&e->list, &lc->in_use);
545 lc->pending_changes = 0;
552 * @lc: the lru cache to operate on
559 unsigned int lc_put(struct lru_cache *lc, struct lc_element *e)
562 PARANOIA_LC_ELEMENT(lc, e);
567 list_move(&e->list, &lc->lru);
568 lc->used--;
569 clear_bit_unlock(__LC_STARVING, &lc->flags);
576 * @lc: the lru cache to operate on
579 struct lc_element *lc_element_by_index(struct lru_cache *lc, unsigned i)
581 BUG_ON(i >= lc->nr_elements);
582 BUG_ON(lc->lc_element[i] == NULL);
583 BUG_ON(lc->lc_element[i]->lc_index != i);
584 return lc->lc_element[i];
589 * @lc: the lru cache to operate on
590 * @e: the element to query for its index position in lc->element
592 unsigned int lc_index_of(struct lru_cache *lc, struct lc_element *e)
594 PARANOIA_LC_ELEMENT(lc, e);
600 * @lc: the lru cache to operate on
606 void lc_set(struct lru_cache *lc, unsigned int enr, int index)
611 if (index < 0 || index >= lc->nr_elements)
614 e = lc_element_by_index(lc, index);
621 lh = &lc->free;
623 hlist_add_head(&e->colision, lc_hash_slot(lc, enr));
624 lh = &lc->lru;
631 * @lc: the lru cache to operate on
638 void lc_seq_dump_details(struct seq_file *seq, struct lru_cache *lc, char *utext,
641 unsigned int nr_elements = lc->nr_elements;
647 e = lc_element_by_index(lc, i);