Skip to content

Commit

Permalink
clean up the do_item_alloc logic
Browse files Browse the repository at this point in the history
Fix an unlikely bug where search == NULL and the first alloc fails, which then
attempts to use search.

Also reorders branches from most likely to least likely, and removes all
redundant tests that I can see. No longer double checks things like refcount
or exptime for the eviction case.
  • Loading branch information
dormando committed Dec 15, 2011
1 parent 7066273 commit f58de2a
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions items.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
rel_time_t oldest_live = settings.oldest_live;

search = tails[id];
if (search == NULL) {
it = slabs_alloc(ntotal, id);
} else if (search->refcount == 0) {
if ((search->time < oldest_live) || // dead by flush
(search->exptime != 0 && search->exptime < current_time)) {
if (search != NULL && search->refcount == 0) {
if ((search->exptime != 0 && search->exptime < current_time)
|| (search->time < oldest_live)) { // dead by flush
STATS_LOCK();
stats.reclaimed++;
STATS_UNLOCK();
Expand All @@ -126,12 +124,7 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
/* Initialize the item block: */
it->slabs_clsid = 0;
it->refcount = 0;
}
}

if (it == NULL && (it = slabs_alloc(ntotal, id)) == NULL) {
if (search->refcount == 0 &&
(search->exptime == 0 || search->exptime > current_time)) {
} else if ((it = slabs_alloc(ntotal, id)) == NULL) {
if (settings.evict_to_free == 0) {
itemstats[id].outofmemory++;
pthread_mutex_unlock(&cache_lock);
Expand All @@ -158,6 +151,9 @@ item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_tim
it->slabs_clsid = 0;
it->refcount = 0;
}
} else {
/* If the LRU is empty or locked, attempt to allocate memory */
it = slabs_alloc(ntotal, id);
}

if (it == NULL) {
Expand Down

0 comments on commit f58de2a

Please sign in to comment.