fish-shell/src/autoload.h

96 lines
3.6 KiB
C
Raw Normal View History

// The classes responsible for autoloading functions and completions.
2012-01-26 03:47:45 +08:00
#ifndef FISH_AUTOLOAD_H
#define FISH_AUTOLOAD_H
2015-07-25 23:14:25 +08:00
#include <pthread.h>
#include <time.h>
#include <set>
Squashed commit of the following: commit 50f414a45d58fcab664ff662dd27befcfa0fdd95 Author: Mahmoud Al-Qudsi <mqudsi@neosmart.net> Date: Sat Aug 19 13:43:35 2017 -0500 Converted file_id_t set to unordered_set with custom hash commit 83ef2dd7cc1bc3e4fdf0b2d3546d6811326cc3c9 Author: Mahmoud Al-Qudsi <mqudsi@neosmart.net> Date: Sat Aug 19 13:43:14 2017 -0500 Converted remaining set<wcstring> to unordered_set<wcstring> commit 053da88f933f27505b3cf4810402e2a2be070203 Author: Mahmoud Al-Qudsi <mqudsi@neosmart.net> Date: Sat Aug 19 13:29:21 2017 -0500 Switched function sets to unordered_set commit d469742a14ac99599022a9258cda8255178826b5 Author: Mahmoud Al-Qudsi <mqudsi@neosmart.net> Date: Sat Aug 19 13:21:32 2017 -0500 Converted list of modified variables to an unordered set commit 5c06f866beeafb23878b1a932c7cd2558412c283 Author: Mahmoud Al-Qudsi <mqudsi@neosmart.net> Date: Sat Aug 19 13:15:20 2017 -0500 Convert const_string_set_t to std::unordered_set As it is a readonly-list of raw character pointer strings (not wcstring), this necessitated the addition of a hashing function since the C++ standard library does not come with a char pointer hash function. To that end, a zlib-licensed [0] port of the excellent, lightweight XXHash family of 32- and 64-bit hashing algorithms in the form of a C++ header-only include library has been included. XXHash32/64 is pretty much universally the fastest hashing library for general purpose applications, and has been thoroughly vetted and is used in countless open source projects. The single-header version of this library makes it a lot simpler to include in the fish project, and the license compatibility with fish' GPLv2 and the zero-lib nature should make it an easy decision. std::unordered_set brings a massive speedup as compared to the default std::set, and the further use of the fast XXHash library to provide the string hashing should make all forms of string lookups in fish significantly faster (to a user-noticeable extent). 0: http://create.stephan-brumme.com/about.html commit 30d7710be8f0c23a4d42f7e713fcb7850f99036e Author: Mahmoud Al-Qudsi <mqudsi@neosmart.net> Date: Sat Aug 19 12:29:39 2017 -0500 Using std::unordered_set for completions backing store While the completions shown to the user are sorted, their storage in memory does not need to be since they are re-sorted before they are shown in completions.cpp. commit 695e83331d7a60ba188e57f6ea0d9b6da54860c6 Author: Mahmoud Al-Qudsi <mqudsi@neosmart.net> Date: Sat Aug 19 12:06:53 2017 -0500 Updated is_loading to use unordered_set
2017-08-20 04:29:52 +08:00
#include <unordered_set>
#include "common.h"
#include "env.h"
2012-02-06 12:54:41 +08:00
#include "lru.h"
2016-06-06 12:30:24 +08:00
/// Record of an attempt to access a file.
struct file_access_attempt_t {
2016-06-06 12:30:24 +08:00
/// Modification time of the file
time_t mod_time;
2016-06-06 12:30:24 +08:00
/// When we last checked the file
time_t last_checked;
2016-06-06 12:30:24 +08:00
/// Whether or not we believe we can access this file
bool accessible;
2016-06-06 12:30:24 +08:00
/// The access attempt is stale
bool stale;
2016-06-06 12:30:24 +08:00
/// If we cannot access the file, the error code encountered.
int error;
};
2012-01-26 03:47:45 +08:00
file_access_attempt_t access_file(const wcstring &path, int mode);
struct autoload_function_t {
explicit autoload_function_t(bool placeholder)
: access(), is_loaded(false), is_placeholder(placeholder) {}
2016-06-06 12:30:24 +08:00
/// The last access attempt recorded
file_access_attempt_t access;
2016-06-06 12:30:24 +08:00
/// Have we actually loaded this function?
bool is_loaded;
/// Whether we are a placeholder that stands in for "no such function". If this is true, then
/// is_loaded must be false.
bool is_placeholder;
};
class env_vars_snapshot_t;
2016-06-06 12:30:24 +08:00
/// Class representing a path from which we can autoload and the autoloaded contents.
class autoload_t : public lru_cache_t<autoload_t, autoload_function_t> {
private:
2016-06-06 12:30:24 +08:00
/// Lock for thread safety.
std::mutex lock;
2016-06-06 12:30:24 +08:00
/// The environment variable name.
const wcstring env_var_name;
2016-06-06 12:30:24 +08:00
/// The path from which we most recently autoloaded.
env_var_t last_path;
2016-06-06 12:30:24 +08:00
/// the most reecently autoloaded path, tokenized (split on separators).
wcstring_list_t last_path_tokenized;
2016-06-06 12:30:24 +08:00
/// A table containing all the files that are currently being loaded.
/// This is here to help prevent recursion.
std::unordered_set<wcstring> is_loading_set;
// Function invoked when a command is removed
typedef void (*command_removed_function_t)(const wcstring &);
const command_removed_function_t command_removed;
void remove_all_functions() { this->evict_all_nodes(); }
bool locate_file_and_maybe_load_it(const wcstring &cmd, bool really_load, bool reload,
const wcstring_list_t &path_list);
autoload_function_t *get_autoloaded_function_with_creation(const wcstring &cmd,
bool allow_eviction);
public:
// CRTP override
void entry_was_evicted(wcstring key, autoload_function_t node);
// Create an autoload_t for the given environment variable name.
2017-02-08 13:52:35 +08:00
autoload_t(const wcstring &env_var_name_var, command_removed_function_t callback);
2016-06-06 12:30:24 +08:00
/// Autoload the specified file, if it exists in the specified path. Do not load it multiple
/// times unless its timestamp changes or parse_util_unload is called.
/// Autoloading one file may unload another.
/// @param cmd the filename to search for. The suffix '.fish' is always added to this name
/// @param reload wheter to recheck file timestamps on already loaded files
int load(const wcstring &cmd, bool reload);
2016-06-06 12:30:24 +08:00
/// Check whether we have tried loading the given command. Does not do any I/O.
bool has_tried_loading(const wcstring &cmd);
2016-06-06 12:30:24 +08:00
/// Tell the autoloader that the specified file, in the specified path, is no longer loaded.
/// Returns non-zero if the file was removed, zero if the file had not yet been loaded
int unload(const wcstring &cmd);
2016-06-06 12:30:24 +08:00
/// Check whether the given command could be loaded, but do not load it.
bool can_load(const wcstring &cmd, const env_vars_snapshot_t &vars);
};
#endif