mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-29 10:31:14 -03:00
Moved LRU to its own file
This commit is contained in:
119
autoload.cpp
119
autoload.cpp
@@ -37,125 +37,6 @@ file_access_attempt_t access_file(const wcstring &path, int mode) {
|
||||
return result;
|
||||
}
|
||||
|
||||
lru_cache_impl_t::lru_cache_impl_t(size_t size) : max_node_count(size), node_count(0), mouth(L"") {
|
||||
/* Hook up the mouth to itself: a one node circularly linked list */
|
||||
mouth.prev = mouth.next = &mouth;
|
||||
}
|
||||
|
||||
void lru_cache_impl_t::node_was_evicted(lru_node_t *node) { }
|
||||
|
||||
void lru_cache_impl_t::evict_node(lru_node_t *condemned_node) {
|
||||
/* We should never evict the mouth */
|
||||
assert(condemned_node != NULL && condemned_node != &mouth);
|
||||
|
||||
/* Remove it from the linked list */
|
||||
condemned_node->prev->next = condemned_node->next;
|
||||
condemned_node->next->prev = condemned_node->prev;
|
||||
|
||||
/* Remove us from the set */
|
||||
node_set.erase(condemned_node);
|
||||
node_count--;
|
||||
|
||||
/* Tell ourselves */
|
||||
this->node_was_evicted(condemned_node);
|
||||
}
|
||||
|
||||
void lru_cache_impl_t::evict_last_node(void) {
|
||||
/* Simple */
|
||||
evict_node(mouth.prev);
|
||||
}
|
||||
|
||||
bool lru_cache_impl_t::evict_node(const wcstring &key) {
|
||||
/* Construct a fake node as our key */
|
||||
lru_node_t node_key(key);
|
||||
|
||||
/* Look for it in the set */
|
||||
node_set_t::iterator iter = node_set.find(&node_key);
|
||||
if (iter == node_set.end())
|
||||
return false;
|
||||
|
||||
/* Evict the given node */
|
||||
evict_node(*iter);
|
||||
return true;
|
||||
}
|
||||
|
||||
void lru_cache_impl_t::promote_node(lru_node_t *node) {
|
||||
/* We should never promote the mouth */
|
||||
assert(node != &mouth);
|
||||
|
||||
/* First unhook us */
|
||||
node->prev->next = node->next;
|
||||
node->next->prev = node->prev;
|
||||
|
||||
/* Put us after the mouth */
|
||||
node->next = mouth.next;
|
||||
node->next->prev = node;
|
||||
node->prev = &mouth;
|
||||
mouth.next = node;
|
||||
}
|
||||
|
||||
bool lru_cache_impl_t::add_node(lru_node_t *node) {
|
||||
/* Add our node without eviction */
|
||||
if (! this->add_node_without_eviction(node))
|
||||
return false;
|
||||
|
||||
/* Evict */
|
||||
while (node_count > max_node_count)
|
||||
evict_last_node();
|
||||
|
||||
/* Success */
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lru_cache_impl_t::add_node_without_eviction(lru_node_t *node) {
|
||||
assert(node != NULL && node != &mouth);
|
||||
|
||||
/* Try inserting; return false if it was already in the set */
|
||||
if (! node_set.insert(node).second)
|
||||
return false;
|
||||
|
||||
/* Add the node after the mouth */
|
||||
node->next = mouth.next;
|
||||
node->next->prev = node;
|
||||
node->prev = &mouth;
|
||||
mouth.next = node;
|
||||
|
||||
/* Update the count */
|
||||
node_count++;
|
||||
|
||||
/* Evict */
|
||||
while (node_count > max_node_count)
|
||||
evict_last_node();
|
||||
|
||||
/* Success */
|
||||
return true;
|
||||
}
|
||||
|
||||
lru_node_t *lru_cache_impl_t::get_node(const wcstring &key) {
|
||||
lru_node_t *result = NULL;
|
||||
|
||||
/* Construct a fake node as our key */
|
||||
lru_node_t node_key(key);
|
||||
|
||||
/* Look for it in the set */
|
||||
node_set_t::iterator iter = node_set.find(&node_key);
|
||||
|
||||
/* If we found a node, promote and return it */
|
||||
if (iter != node_set.end()) {
|
||||
result = *iter;
|
||||
promote_node(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void lru_cache_impl_t::evict_all_nodes() {
|
||||
while (node_count > 0) {
|
||||
evict_last_node();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
autoload_t::autoload_t(const wcstring &env_var_name_var, const builtin_script_t * const scripts, size_t script_count) :
|
||||
env_var_name(env_var_name_var),
|
||||
builtin_scripts(scripts),
|
||||
|
||||
Reference in New Issue
Block a user