diff options
| author | Lars Ellenberg <lars.ellenberg@linbit.com> | 2011-02-21 07:20:58 -0500 |
|---|---|---|
| committer | Philipp Reisner <philipp.reisner@linbit.com> | 2011-10-14 10:47:43 -0400 |
| commit | a9efc748d679efb39fe7a8a536dde94cee691604 (patch) | |
| tree | 0af3013566cd1d5aa638ed97ab18c7131791f17b /lib | |
| parent | 0097f0405d365eff66235f887d47fa0b62b28599 (diff) | |
lru_cache: consolidate lc_get and lc_try_get
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/lru_cache.c | 120 |
1 files changed, 61 insertions, 59 deletions
diff --git a/lib/lru_cache.c b/lib/lru_cache.c index 4f638b86674f..17621684758a 100644 --- a/lib/lru_cache.c +++ b/lib/lru_cache.c | |||
| @@ -308,45 +308,7 @@ static int lc_unused_element_available(struct lru_cache *lc) | |||
| 308 | return 0; | 308 | return 0; |
| 309 | } | 309 | } |
| 310 | 310 | ||
| 311 | 311 | static struct lc_element *__lc_get(struct lru_cache *lc, unsigned int enr, bool may_change) | |
| 312 | /** | ||
| 313 | * lc_get - get element by label, maybe change the active set | ||
| 314 | * @lc: the lru cache to operate on | ||
| 315 | * @enr: the label to look up | ||
| 316 | * | ||
| 317 | * Finds an element in the cache, increases its usage count, | ||
| 318 | * "touches" and returns it. | ||
| 319 | * | ||
| 320 | * In case the requested number is not present, it needs to be added to the | ||
| 321 | * cache. Therefore it is possible that an other element becomes evicted from | ||
| 322 | * the cache. In either case, the user is notified so he is able to e.g. keep | ||
| 323 | * a persistent log of the cache changes, and therefore the objects in use. | ||
| 324 | * | ||
| 325 | * Return values: | ||
| 326 | * NULL | ||
| 327 | * The cache was marked %LC_STARVING, | ||
| 328 | * or the requested label was not in the active set | ||
| 329 | * and a changing transaction is still pending (@lc was marked %LC_DIRTY). | ||
| 330 | * Or no unused or free element could be recycled (@lc will be marked as | ||
| 331 | * %LC_STARVING, blocking further lc_get() operations). | ||
| 332 | * | ||
| 333 | * pointer to the element with the REQUESTED element number. | ||
| 334 | * In this case, it can be used right away | ||
| 335 | * | ||
| 336 | * pointer to an UNUSED element with some different element number, | ||
| 337 | * where that different number may also be %LC_FREE. | ||
| 338 | * | ||
| 339 | * In this case, the cache is marked %LC_DIRTY (blocking further changes), | ||
| 340 | * and the returned element pointer is removed from the lru list and | ||
| 341 | * hash collision chains. The user now should do whatever housekeeping | ||
| 342 | * is necessary. | ||
| 343 | * Then he must call lc_changed(lc,element_pointer), to finish | ||
| 344 | * the change. | ||
| 345 | * | ||
| 346 | * NOTE: The user needs to check the lc_number on EACH use, so he recognizes | ||
| 347 | * any cache set change. | ||
| 348 | */ | ||
| 349 | struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr) | ||
| 350 | { | 312 | { |
| 351 | struct lc_element *e; | 313 | struct lc_element *e; |
| 352 | 314 | ||
| @@ -366,6 +328,8 @@ struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr) | |||
| 366 | } | 328 | } |
| 367 | 329 | ||
| 368 | ++lc->misses; | 330 | ++lc->misses; |
| 331 | if (!may_change) | ||
| 332 | RETURN(NULL); | ||
| 369 | 333 | ||
| 370 | /* In case there is nothing available and we can not kick out | 334 | /* In case there is nothing available and we can not kick out |
| 371 | * the LRU element, we have to wait ... | 335 | * the LRU element, we have to wait ... |
| @@ -397,29 +361,67 @@ struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr) | |||
| 397 | RETURN(e); | 361 | RETURN(e); |
| 398 | } | 362 | } |
| 399 | 363 | ||
| 400 | /* similar to lc_get, | 364 | /** |
| 401 | * but only gets a new reference on an existing element. | 365 | * lc_get - get element by label, maybe change the active set |
| 402 | * you either get the requested element, or NULL. | 366 | * @lc: the lru cache to operate on |
| 403 | * will be consolidated into one function. | 367 | * @enr: the label to look up |
| 368 | * | ||
| 369 | * Finds an element in the cache, increases its usage count, | ||
| 370 | * "touches" and returns it. | ||
| 371 | * | ||
| 372 | * In case the requested number is not present, it needs to be added to the | ||
| 373 | * cache. Therefore it is possible that an other element becomes evicted from | ||
| 374 | * the cache. In either case, the user is notified so he is able to e.g. keep | ||
| 375 | * a persistent log of the cache changes, and therefore the objects in use. | ||
| 376 | * | ||
| 377 | * Return values: | ||
| 378 | * NULL | ||
| 379 | * The cache was marked %LC_STARVING, | ||
| 380 | * or the requested label was not in the active set | ||
| 381 | * and a changing transaction is still pending (@lc was marked %LC_DIRTY). | ||
| 382 | * Or no unused or free element could be recycled (@lc will be marked as | ||
| 383 | * %LC_STARVING, blocking further lc_get() operations). | ||
| 384 | * | ||
| 385 | * pointer to the element with the REQUESTED element number. | ||
| 386 | * In this case, it can be used right away | ||
| 387 | * | ||
| 388 | * pointer to an UNUSED element with some different element number, | ||
| 389 | * where that different number may also be %LC_FREE. | ||
| 390 | * | ||
| 391 | * In this case, the cache is marked %LC_DIRTY (blocking further changes), | ||
| 392 | * and the returned element pointer is removed from the lru list and | ||
| 393 | * hash collision chains. The user now should do whatever housekeeping | ||
| 394 | * is necessary. | ||
| 395 | * Then he must call lc_changed(lc,element_pointer), to finish | ||
| 396 | * the change. | ||
| 397 | * | ||
| 398 | * NOTE: The user needs to check the lc_number on EACH use, so he recognizes | ||
| 399 | * any cache set change. | ||
| 404 | */ | 400 | */ |
| 405 | struct lc_element *lc_try_get(struct lru_cache *lc, unsigned int enr) | 401 | struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr) |
| 406 | { | 402 | { |
| 407 | struct lc_element *e; | 403 | return __lc_get(lc, enr, 1); |
| 408 | 404 | } | |
| 409 | PARANOIA_ENTRY(); | ||
| 410 | if (lc->flags & LC_STARVING) { | ||
| 411 | ++lc->starving; | ||
| 412 | RETURN(NULL); | ||
| 413 | } | ||
| 414 | 405 | ||
| 415 | e = lc_find(lc, enr); | 406 | /** |
| 416 | if (e) { | 407 | * lc_try_get - get element by label, if present; do not change the active set |
| 417 | ++lc->hits; | 408 | * @lc: the lru cache to operate on |
| 418 | if (e->refcnt++ == 0) | 409 | * @enr: the label to look up |
| 419 | lc->used++; | 410 | * |
| 420 | list_move(&e->list, &lc->in_use); /* Not evictable... */ | 411 | * Finds an element in the cache, increases its usage count, |
| 421 | } | 412 | * "touches" and returns it. |
| 422 | RETURN(e); | 413 | * |
| 414 | * Return values: | ||
| 415 | * NULL | ||
| 416 | * The cache was marked %LC_STARVING, | ||
| 417 | * or the requested label was not in the active set | ||
| 418 | * | ||
| 419 | * pointer to the element with the REQUESTED element number. | ||
| 420 | * In this case, it can be used right away | ||
| 421 | */ | ||
| 422 | struct lc_element *lc_try_get(struct lru_cache *lc, unsigned int enr) | ||
| 423 | { | ||
| 424 | return __lc_get(lc, enr, 0); | ||
| 423 | } | 425 | } |
| 424 | 426 | ||
| 425 | /** | 427 | /** |
