diff options
author | dormando <dormando@rydia.net> | 2011-12-15 12:53:21 -0800 |
---|---|---|
committer | dormando <dormando@rydia.net> | 2011-12-15 12:53:21 -0800 |
commit | f58de2a4b115bfc00ba24e42244d26ee561cde3a (patch) | |
tree | 385c70b08a64e75f7f6b42e3abcd74dcd6227c49 | |
parent | 7066273828c42ccdd76dda1880b0accc7abf1f31 (diff) | |
download | memcached-f58de2a4b115bfc00ba24e42244d26ee561cde3a.tar.gz |
clean up the do_item_alloc logic
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.
-rw-r--r-- | items.c | 18 |
1 files changed, 7 insertions, 11 deletions
@@ -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(); @@ -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); @@ -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) { |