Commit eeec66a1 authored by Vladimír Čunát's avatar Vladimír Čunát

lru_get_new *can* return NULL

... and that doesn't necessarily mean that malloc() failed.
We do *not* want to evict a heavy-hitter by an unfrequent element.

Note: even the implementation currently in master *did* return NULL,
so some parts of the code were just wrongly returning ENOMEM.
parent 9d5beac5
......@@ -62,11 +62,9 @@ int kr_cookie_lru_set(kr_cookie_lru_t *cache, const struct sockaddr *sa,
}
struct cookie_opt_data *cached = lru_get_new(cache, addr, addr_len);
if (!cached) {
return kr_error(ENOMEM);
if (cached) {
memcpy(cached->opt_data, opt, opt_size);
}
memcpy(cached->opt_data, opt, opt_size);
return kr_ok();
}
......@@ -260,7 +260,7 @@ int kr_nsrep_update_rtt(struct kr_nsrep *ns, const struct sockaddr *addr,
}
unsigned *cur = lru_get_new(cache, addr_in, addr_len);
if (!cur) {
return kr_error(ENOMEM);
return kr_ok();
}
/* Score limits */
if (score > KR_NS_MAX_SCORE) {
......@@ -294,9 +294,8 @@ int kr_nsrep_update_rep(struct kr_nsrep *ns, unsigned reputation, kr_nsrep_lru_t
ns->reputation = reputation;
/* Store reputation in the LRU cache */
unsigned *cur = lru_get_new(cache, (const char *)ns->name, knot_dname_size(ns->name));
if (!cur) {
return kr_error(ENOMEM);
if (cur) {
*cur = reputation;
}
*cur = reputation;
return kr_ok();
}
......@@ -62,7 +62,9 @@ static void test_insert(void **state)
for (i = 0; i < dict_size; i++) {
int *data = lru_get_new(lru, dict[i], KEY_LEN(dict[i]));
assert_non_null(data);
if (!data) {
continue;
}
*data = i;
assert_true(*lru_get_try(lru, dict[i], KEY_LEN(dict[i])) == i);
}
......@@ -83,7 +85,7 @@ static void test_eviction(void **state)
test_randstr(key, sizeof(key));
int *data = lru_get_new(lru, key, sizeof(key));
if (!data) {
assert_true(0);
continue;
}
*data = i;
if (*lru_get_try(lru, key, sizeof(key)) != i) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment