From 050ad6283e85182e6b094f35088ab040fbe79385 Mon Sep 17 00:00:00 2001 From: "andrew (from workstation)" Date: Thu, 21 May 2020 23:59:36 +0200 Subject: [PATCH] deallocate all --- td/telegram/files/FileId.h | 5 ++++- td/telegram/files/FileManager.cpp | 18 +++++++++++------- tdutils/td/utils/Enumerator.h | 23 +++++++++++++---------- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/td/telegram/files/FileId.h b/td/telegram/files/FileId.h index d2a9f711..2a9bb43d 100644 --- a/td/telegram/files/FileId.h +++ b/td/telegram/files/FileId.h @@ -41,10 +41,13 @@ class FileId { } int32 get() const { - const_cast(this)->time_ = std::time(nullptr); return id; } + void set_time() { + time_ = std::time(nullptr); + } + int64 get_time() const { return time_; } diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index a2079d33..4410bce1 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -981,6 +981,7 @@ bool FileManager::try_fix_partial_local_location(FileNodePtr node) { } FileManager::FileIdInfo *FileManager::get_file_id_info(FileId file_id) { + file_id.set_time(); LOG_CHECK(0 <= file_id.get() && file_id.get() < static_cast(file_id_info_.size())) << file_id << " " << file_id_info_.size(); return &file_id_info_[file_id.get()]; @@ -3194,12 +3195,15 @@ FileId FileManager::next_file_id() { if (!empty_file_ids_.empty()) { auto res = empty_file_ids_.back(); empty_file_ids_.pop_back(); - return FileId{res, 0}; + auto ret = FileId{res, 0}; + ret.set_time(); + return ret; } auto id = (int32) file_id_info_.size(); FileId res(static_cast(id), 0); file_id_info_.push_back({}); + res.set_time(); return res; } @@ -3739,7 +3743,7 @@ void FileManager::memory_cleanup() { if (main_node_id != 0) { auto &node = file_nodes_[main_node_id]; if (node != nullptr && ((int32) i) == node->main_file_id_.fast_get()) { - if (time - node->main_file_id_.get_time() > 20 /* MAIN FILE TTL */) { + if (time - node->main_file_id_.get_time() > 60 * 5 /* MAIN FILE TTL */) { node->main_file_id_.reset_time(); for (auto &file_id : node->file_ids_) { @@ -3831,7 +3835,7 @@ void FileManager::memory_cleanup() { /* DESTROY INVALID file_hash_to_file_id_ */ for (const auto &pair : file_hash_to_file_id_) { auto &file = file_id_info_[pair.second.fast_get()]; - if (file_nodes_[file.node_id_] == nullptr) { + if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) { file_hash_to_file_id_.erase(pair.first); } } @@ -3839,7 +3843,7 @@ void FileManager::memory_cleanup() { /* DESTROY INVALID local_location_to_file_id_ */ for (const auto &pair : local_location_to_file_id_) { auto &file = file_id_info_[pair.second.fast_get()]; - if (file_nodes_[file.node_id_] == nullptr) { + if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) { local_location_to_file_id_.erase(pair.first); } } @@ -3847,7 +3851,7 @@ void FileManager::memory_cleanup() { /* DESTROY INVALID generate_location_to_file_id_ */ for (const auto &pair : generate_location_to_file_id_) { auto &file = file_id_info_[pair.second.fast_get()]; - if (file_nodes_[file.node_id_] == nullptr) { + if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) { generate_location_to_file_id_.erase(pair.first); } } @@ -3855,7 +3859,7 @@ void FileManager::memory_cleanup() { /* DESTROY INVALID remote_location_info_ */ for (const auto &pair : remote_location_info_.get_map()) { auto &file = file_id_info_[pair.first.file_id_.fast_get()]; - if (file_nodes_[file.node_id_] == nullptr) { + if (&file == nullptr || file_nodes_[file.node_id_] == nullptr) { remote_location_info_.erase_map_key(pair.first, pair.second); } } @@ -3866,7 +3870,7 @@ void FileManager::memory_cleanup() { empty_file_ids_.shrink_to_fit(); empty_node_ids_.shrink_to_fit(); - LOG(INFO) << empty_file_ids_.size() << " empty ids and " << queries_container_.size() << " running queries"; + LOG(ERROR) << empty_file_ids_.size() << " empty ids and " << queries_container_.size() << " running queries"; is_closed_ = false; } } diff --git a/tdutils/td/utils/Enumerator.h b/tdutils/td/utils/Enumerator.h index 236dcdce..b22107e7 100644 --- a/tdutils/td/utils/Enumerator.h +++ b/tdutils/td/utils/Enumerator.h @@ -23,37 +23,40 @@ class Enumerator { return map_; } - Key next() { + std::pair next() { if (!empty_id_.empty()) { auto res = empty_id_.back(); empty_id_.pop_back(); - return res; + return std::make_pair(res, true); } - return (Key) (arr_.size() + 1); + return std::make_pair((Key) (arr_.size() + 1), false); } void erase_map_key(ValueT key_x, Key key_y) { + auto pos_y = static_cast(key_y - 1); empty_id_.push_back(key_y); map_.erase(key_x); - arr_[key_y] = nullptr; + arr_[pos_y] = nullptr; } Key add(ValueT v) { - CHECK(arr_.size() < static_cast(std::numeric_limits::max() - 1)); - Key next_id = next(); + auto next_id = next(); bool was_inserted; decltype(map_.begin()) it; - std::tie(it, was_inserted) = map_.emplace(std::move(v), next_id); - if (was_inserted) { + std::tie(it, was_inserted) = map_.emplace(std::move(v), next_id.first); + if (was_inserted && next_id.second) { + arr_[next_id.first - 1] = &it->first; + } else if (was_inserted) { arr_.push_back(&it->first); + } else if (next_id.second) { + empty_id_.push_back(next_id.first); } return it->second; } const ValueT &get(Key key) const { - auto pos = static_cast(key - 1); - CHECK(pos < arr_.size()); + auto pos = static_cast(key - 1); return *arr_[pos]; }