auto &additional_vec = get_item_specific_vector(*deleted_element_refcount);
#if defined(__cpp_lib_erase_if)
- std::erase_if(additional_vec, [id_to_disable](const cache_item_ptr &elt) {
+ std::erase_if(additional_vec, [id_to_disable](cache_item *elt) {
return elt->id == id_to_disable;
});
#else
auto it = std::remove_if(additional_vec.begin(),
- additional_vec.end(), [id_to_disable](const cache_item_ptr &elt) {
+ additional_vec.end(), [id_to_disable](cache_item *elt) {
return elt->id == id_to_disable;
});
additional_vec.erase(it, additional_vec.end());
return it->second->get_parent(*this);
}
- return it->second.get();
+ return it->second;
}
auto symcache::get_item_by_name_mut(std::string_view name, bool resolve_parent) const -> cache_item *
return (cache_item *) it->second->get_parent(*this);
}
- return it->second.get();
+ return it->second;
}
auto symcache::add_dependency(int id_from, std::string_view to, int virtual_id_from) -> void
const auto &source = items_by_id[id_from];
g_assert (source.get() != nullptr);
- source->deps.emplace_back(cache_item_ptr{nullptr},
+ source->deps.emplace_back(nullptr,
std::string(to),
id_from,
-1);
/* We need that for settings id propagation */
const auto &vsource = items_by_id[virtual_id_from];
g_assert (vsource.get() != nullptr);
- vsource->deps.emplace_back(cache_item_ptr{nullptr},
+ vsource->deps.emplace_back(nullptr,
std::string(to),
-1,
virtual_id_from);
total_hits += it->st->total_hits;
/* Unmask topological order */
it->order = 0;
- ord->d.emplace_back(it);
+ ord->d.emplace_back(it->getptr());
}
}
for (const auto &dep: it->deps) {
msg_debug_cache_lambda("visiting dep: %s (%d)", dep.item->symbol.c_str(), cur_order + 1);
- rec(dep.item.get(), cur_order + 1, rec);
+ rec(dep.item, cur_order + 1, rec);
}
it->order = cur_order;
constexpr auto append_items_vec = [](const auto &vec, auto &out) {
for (const auto &it: vec) {
if (it) {
- out.emplace_back(it);
+ out.emplace_back(it->getptr());
}
}
};
/* After sorting is done, we can assign all elements in the by_symbol hash */
for (const auto [i, it] : rspamd::enumerate(ord->d)) {
- ord->by_symbol[it->get_name()] = i;
+ ord->by_symbol.emplace(it->get_name(), i);
ord->by_cache_id[it->id] = i;
}
/* Finally set the current order */
priority, func, user_data,
real_type_pair.first, real_type_pair.second);
- items_by_symbol[item->get_name()] = item;
- get_item_specific_vector(*item).push_back(item);
- items_by_id.emplace(id, item);
+ items_by_symbol.emplace(item->get_name(), item.get());
+ get_item_specific_vector(*item).push_back(item.get());
+ items_by_id.emplace(id, std::move(item)); // Takes ownership
if (!(real_type_pair.second & SYMBOL_TYPE_NOSTAT)) {
cksum = t1ha(name.data(), name.size(), cksum);
id,
std::string{name},
parent_id, real_type_pair.first, real_type_pair.second);
- const auto &parent = items_by_id[parent_id];
- parent->add_child(item);
- items_by_symbol[item->get_name()] = item;
- get_item_specific_vector(*item).push_back(item);
- items_by_id.emplace(id, item);
+ const auto &parent = items_by_id[parent_id].get();
+ parent->add_child(item.get());
+ items_by_symbol.emplace(item->get_name(), item.get());
+ get_item_specific_vector(*item).push_back(item.get());
+ items_by_id.emplace(id, std::move(item)); // Takes ownership
return id;
}
auto log_func = RSPAMD_LOG_FUNC;
ankerl::unordered_dense::set<const cache_item *> seen_items;
- auto get_item_timeout = [](const cache_item_ptr &it) {
+ auto get_item_timeout = [](cache_item *it) {
return it->get_numeric_augmentation("timeout").value_or(0.0);
};
/* This function returns the timeout for an item and all it's dependencies */
- auto get_filter_timeout = [&](const cache_item_ptr &it, auto self) -> double {
+ auto get_filter_timeout = [&](cache_item *it, auto self) -> double {
auto own_timeout = get_item_timeout(it);
auto max_child_timeout = 0.0;
if (timeout > max_timeout) {
max_timeout = timeout;
- max_elt = it.get();
+ max_elt = it;
}
}
if (timeout > max_filters_timeout) {
max_filters_timeout = timeout;
- if (!seen_items.contains(it.get())) {
- elts.emplace_back(timeout, it.get());
- seen_items.insert(it.get());
+ if (!seen_items.contains(it)) {
+ elts.emplace_back(timeout, it);
+ seen_items.insert(it);
}
}
}
class symcache {
private:
- using items_ptr_vec = std::vector<cache_item_ptr>;
+ using items_ptr_vec = std::vector<cache_item *>;
/* Map indexed by symbol name: all symbols must have unique names, so this map holds ownership */
- ankerl::unordered_dense::map<std::string_view, cache_item_ptr> items_by_symbol;
+ ankerl::unordered_dense::map<std::string_view, cache_item *> items_by_symbol;
ankerl::unordered_dense::map<int, cache_item_ptr> items_by_id;
/* Items sorted into some order */
template<typename Functor>
auto symbols_foreach(Functor f) -> void {
for (const auto &sym_it : items_by_symbol) {
- f(sym_it.second.get());
+ f(sym_it.second);
}
}
template<typename Functor>
auto composites_foreach(Functor f) -> void {
for (const auto &sym_it : composites) {
- f(sym_it.get());
+ f(sym_it);
}
}
auto connfilters_foreach(Functor f) -> bool {
return std::all_of(std::begin(connfilters), std::end(connfilters),
[&](const auto &sym_it){
- return f(sym_it.get());
+ return f(sym_it);
});
}
template<typename Functor>
auto prefilters_foreach(Functor f) -> bool {
return std::all_of(std::begin(prefilters), std::end(prefilters),
[&](const auto &sym_it){
- return f(sym_it.get());
+ return f(sym_it);
});
}
template<typename Functor>
auto postfilters_foreach(Functor f) -> bool {
return std::all_of(std::begin(postfilters), std::end(postfilters),
[&](const auto &sym_it){
- return f(sym_it.get());
+ return f(sym_it);
});
}
template<typename Functor>
auto idempotent_foreach(Functor f) -> bool {
return std::all_of(std::begin(idempotent), std::end(idempotent),
[&](const auto &sym_it){
- return f(sym_it.get());
+ return f(sym_it);
});
}
template<typename Functor>
auto filters_foreach(Functor f) -> bool {
return std::all_of(std::begin(filters), std::end(filters),
[&](const auto &sym_it){
- return f(sym_it.get());
+ return f(sym_it);
});
}
auto *parent = get_parent_mut(cache);
if (parent) {
- dit->rdeps.emplace_back(parent->getptr(), dep.sym, parent->id, -1);
- dep.item = dit->getptr();
+ dit->rdeps.emplace_back(parent, parent->symbol, parent->id, -1);
+ dep.item = dit;
dep.id = dit->id;
msg_debug_cache ("added reverse dependency from %d on %d", parent->id,
}
}
else {
- dep.item = dit->getptr();
+ dep.item = dit;
dep.id = dit->id;
- dit->rdeps.emplace_back(getptr(), dep.sym, id, -1);
+ dit->rdeps.emplace_back(this, symbol, id, -1);
msg_debug_cache ("added reverse dependency from %d on %d", id,
dit->id);
}
auto virtual_item::get_parent(const symcache &cache) const -> const cache_item *
{
if (parent) {
- return parent.get();
+ return parent;
}
return cache.get_item_by_id(parent_id, false);
auto virtual_item::get_parent_mut(const symcache &cache) -> cache_item *
{
if (parent) {
- return parent.get();
+ return parent;
}
return const_cast<cache_item *>(cache.get_item_by_id(parent_id, false));
auto item_ptr = cache.get_item_by_id(parent_id, true);
if (item_ptr) {
- parent = const_cast<cache_item *>(item_ptr)->getptr();
+ parent = const_cast<cache_item *>(item_ptr);
return true;
}
class normal_item {
private:
- symbol_func_t func;
- void *user_data;
- std::vector<cache_item_ptr> virtual_children;
+ symbol_func_t func = nullptr;
+ void *user_data = nullptr;
+ std::vector<cache_item *> virtual_children;
std::vector<item_condition> conditions;
public:
explicit normal_item(symbol_func_t _func, void *_user_data) : func(_func), user_data(_user_data)
return user_data;
}
- auto add_child(const cache_item_ptr &ptr) -> void {
+ auto add_child(cache_item *ptr) -> void {
virtual_children.push_back(ptr);
}
- auto get_childen() const -> const std::vector<cache_item_ptr>& {
+ auto get_childen() const -> const std::vector<cache_item *>& {
return virtual_children;
}
};
class virtual_item {
private:
- int parent_id;
- cache_item_ptr parent;
+ int parent_id = -1;
+ cache_item *parent = nullptr;
public:
explicit virtual_item(int _parent_id) : parent_id(_parent_id)
{
};
struct cache_dependency {
- cache_item_ptr item; /* Real dependency */
+ cache_item *item; /* Real dependency */
std::string sym; /* Symbolic dep name */
int id; /* Real from */
int vid; /* Virtual from */
public:
/* Default piecewise constructor */
- cache_dependency(cache_item_ptr _item, std::string _sym, int _id, int _vid) :
- item(std::move(_item)), sym(std::move(_sym)), id(_id), vid(_vid)
+ explicit cache_dependency(cache_item *_item, std::string _sym, int _id, int _vid) :
+ item(_item), sym(std::move(_sym)), id(_id), vid(_vid)
{
}
};
* @param flags
* @return
*/
- [[nodiscard]] static auto create_with_function(rspamd_mempool_t *pool,
+ template <typename T>
+ static auto create_with_function(rspamd_mempool_t *pool,
int id,
- std::string &&name,
+ T &&name,
int priority,
symbol_func_t func,
void *user_data,
int flags) -> cache_item_ptr
{
return std::shared_ptr<cache_item>(new cache_item(pool,
- id, std::move(name), priority,
+ id, std::forward<T>(name), priority,
func, user_data,
type, flags));
}
* @param flags
* @return
*/
- [[nodiscard]] static auto create_with_virtual(rspamd_mempool_t *pool,
+ template <typename T>
+ static auto create_with_virtual(rspamd_mempool_t *pool,
int id,
- std::string &&name,
+ T &&name,
int parent,
symcache_item_type type,
int flags) -> cache_item_ptr
{
- return std::shared_ptr<cache_item>(new cache_item(pool, id, std::move(name),
+ return std::shared_ptr<cache_item>(new cache_item(pool, id, std::forward<T>(name),
parent, type, flags));
}
/**
* Share ownership on the item
- * @return
- */
+ * @return
+ */
auto getptr() -> cache_item_ptr
{
return shared_from_this();
* Add a virtual symbol as a child of some normal symbol
* @param ptr
*/
- auto add_child(const cache_item_ptr &ptr) -> void {
+ auto add_child(cache_item *ptr) -> void {
if (std::holds_alternative<normal_item>(specific)) {
auto &filter_data = std::get<normal_item>(specific);
* @param ptr
* @return
*/
- auto get_children() const -> std::optional<std::reference_wrapper<const std::vector<cache_item_ptr>>> {
+ auto get_children() const -> std::optional<std::reference_wrapper<const std::vector<cache_item *>>> {
if (std::holds_alternative<normal_item>(specific)) {
const auto &filter_data = std::get<normal_item>(specific);
/* Not started */
if (!check_only) {
if (!rec_functor(recursion + 1,
- dep.item.get(),
+ dep.item,
dep_dyn_item,
rec_functor)) {
"symbol %d(%s)",
dep.id, dep.sym.c_str(), item->id, item->symbol.c_str());
}
- else if (!process_symbol(task, cache, dep.item.get(), dep_dyn_item)) {
+ else if (!process_symbol(task, cache, dep.item, dep_dyn_item)) {
/* Now started, but has events pending */
ret = false;
msg_debug_cache_task_lambda("started check of %d(%s) symbol "
msg_debug_cache_task ("check item %d(%s) rdep of %s ",
rdep.item->id, rdep.item->symbol.c_str(), item->symbol.c_str());
- if (!check_item_deps(task, *cache_ptr, rdep.item.get(), dyn_item, false)) {
+ if (!check_item_deps(task, *cache_ptr, rdep.item, dyn_item, false)) {
msg_debug_cache_task ("blocked execution of %d(%s) rdep of %s "
"unless deps are resolved",
rdep.item->id, rdep.item->symbol.c_str(), item->symbol.c_str());
}
else {
- process_symbol(task, *cache_ptr, rdep.item.get(),
+ process_symbol(task, *cache_ptr, rdep.item,
dyn_item);
}
}