From cba0f62ba99f0485dfcd9dbff72d14e070ee9276 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 19 Dec 2018 23:35:13 +0300 Subject: [PATCH] Minor fixes. GitOrigin-RevId: ffac148f714da737dcc1b12e56352995866f93b4 --- td/mtproto/Transport.cpp | 2 + td/telegram/CallActor.cpp | 1 + td/telegram/NotificationManager.cpp | 1 + tdutils/CMakeLists.txt | 2 +- tdutils/td/utils/BufferedStdin.cpp | 20 --- tdutils/td/utils/BufferedStdin.h | 9 -- tdutils/td/utils/DecTree.h | 2 +- tdutils/td/utils/bits.h | 235 +++++++++++++++------------- tdutils/td/utils/filesystem.cpp | 27 +++- tdutils/td/utils/int_types.h | 10 -- tdutils/td/utils/port/IPAddress.cpp | 4 +- tdutils/td/utils/tests.cpp | 30 ++-- tdutils/td/utils/tests.h | 25 ++- tdutils/test/misc.cpp | 2 +- 14 files changed, 186 insertions(+), 184 deletions(-) delete mode 100644 tdutils/td/utils/BufferedStdin.cpp delete mode 100644 tdutils/td/utils/BufferedStdin.h diff --git a/td/mtproto/Transport.cpp b/td/mtproto/Transport.cpp index f9d10a84..4af65de1 100644 --- a/td/mtproto/Transport.cpp +++ b/td/mtproto/Transport.cpp @@ -19,6 +19,7 @@ namespace td { namespace mtproto { + // mtproto v1.0 template std::tuple Transport::calc_message_ack_and_key(const HeaderT &head, size_t data_size) { @@ -331,5 +332,6 @@ size_t Transport::write(const Storer &storer, const AuthKey &auth_key, PacketInf return write_crypto(storer, auth_key, info, dest); } } + } // namespace mtproto } // namespace td diff --git a/td/telegram/CallActor.cpp b/td/telegram/CallActor.cpp index fe240443..0d2597a1 100644 --- a/td/telegram/CallActor.cpp +++ b/td/telegram/CallActor.cpp @@ -15,6 +15,7 @@ #include "td/telegram/ConfigShared.h" #include "td/telegram/ContactsManager.h" #include "td/telegram/DhCache.h" +#include "td/telegram/DialogId.h" #include "td/telegram/Global.h" #include "td/telegram/misc.h" #include "td/telegram/net/NetQueryCreator.h" diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index f7c305f1..31c0765d 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -16,6 +16,7 @@ #include "td/utils/logging.h" #include "td/utils/misc.h" +#include "td/utils/Slice.h" #include "td/utils/Time.h" #include diff --git a/tdutils/CMakeLists.txt b/tdutils/CMakeLists.txt index 636e7b3c..1279e735 100644 --- a/tdutils/CMakeLists.txt +++ b/tdutils/CMakeLists.txt @@ -85,9 +85,9 @@ set(TDUTILS_SOURCE td/utils/StackAllocator.cpp td/utils/Status.cpp td/utils/StringBuilder.cpp + td/utils/tests.cpp td/utils/Time.cpp td/utils/Timer.cpp - td/utils/tests.cpp td/utils/tl_parsers.cpp td/utils/translit.cpp td/utils/unicode.cpp diff --git a/tdutils/td/utils/BufferedStdin.cpp b/tdutils/td/utils/BufferedStdin.cpp deleted file mode 100644 index 1e4a4a45..00000000 --- a/tdutils/td/utils/BufferedStdin.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -#include "td/utils/BufferedStdin.h" -#include "td/utils/buffer.h" -#include "td/utils/port/detail/PollableFd.h" - -namespace td { -class BufferedStdin { - public: - private: - PollableFdInfo info_; - ChainBufferWriter writer_; - ChainBufferReader reader_ = writer_.extract_reader(); -}; -} // namespace td - diff --git a/tdutils/td/utils/BufferedStdin.h b/tdutils/td/utils/BufferedStdin.h deleted file mode 100644 index 65941009..00000000 --- a/tdutils/td/utils/BufferedStdin.h +++ /dev/null @@ -1,9 +0,0 @@ -// -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -#pragma once - -namespace td {} diff --git a/tdutils/td/utils/DecTree.h b/tdutils/td/utils/DecTree.h index b18d5aa9..164da737 100644 --- a/tdutils/td/utils/DecTree.h +++ b/tdutils/td/utils/DecTree.h @@ -153,7 +153,7 @@ class DecTree { P.first = std::move(Tree); return P; } - } // namespace td + } static unique_ptr merge_node(unique_ptr left, unique_ptr right) { if (left == nullptr) { diff --git a/tdutils/td/utils/bits.h b/tdutils/td/utils/bits.h index bd177bb7..4f8a8b24 100644 --- a/tdutils/td/utils/bits.h +++ b/tdutils/td/utils/bits.h @@ -5,19 +5,138 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once + +#include "td/utils/common.h" #include "td/utils/logging.h" + #if TD_MSVC #include #endif + namespace td { -int32 count_leading_zeroes32(uint32 x); -int32 count_leading_zeroes64(uint64 x); -int32 count_trailing_zeroes32(uint32 x); -int32 count_trailing_zeroes64(uint64 x); -uint32 bswap32(uint32 x); -uint64 bswap64(uint64 x); -int32 count_bits32(uint32 x); -int32 count_bits64(uint64 x); + +inline int32 count_leading_zeroes32(uint32 x); +inline int32 count_leading_zeroes64(uint64 x); +inline int32 count_trailing_zeroes32(uint32 x); +inline int32 count_trailing_zeroes64(uint64 x); +inline uint32 bswap32(uint32 x); +inline uint64 bswap64(uint64 x); +inline int32 count_bits32(uint32 x); +inline int32 count_bits64(uint64 x); + +#if TD_MSVC +inline int32 count_leading_zeroes32(uint32 x) { + unsigned long res = 0; + if (_BitScanReverse(&res, x)) { + return 31 - res; + } + return 32; +} + +inline int32 count_leading_zeroes64(uint64 x) { +#if defined(_M_X64) + unsigned long res = 0; + if (_BitScanReverse64(&res, x)) { + return 63 - res; + } + return 64; +#else + if ((x >> 32) == 0) { + return count_leading_zeroes32(static_cast(x)) + 32; + } else { + return count_leading_zeroes32(static_cast(x >> 32)); + } +#endif +} + +inline int32 count_trailing_zeroes32(uint32 x) { + unsigned long res = 0; + if (_BitScanForward(&res, x)) { + return res; + } + return 32; +} + +inline int32 count_trailing_zeroes64(uint64 x) { +#if defined(_M_X64) + unsigned long res = 0; + if (_BitScanForward64(&res, x)) { + return res; + } + return 64; +#else + if (static_cast(x) == 0) { + return count_trailing_zeroes32(static_cast(x >> 32)) + 32; + } else { + return count_trailing_zeroes32(static_cast(x)); + } +#endif +} + +inline uint32 bswap32(uint32 x) { + return _byteswap_ulong(x); +} + +inline uint64 bswap64(uint64 x) { + return _byteswap_uint64(x); +} + +inline int32 count_bits32(uint32 x) { + return __popcnt(x); +} + +inline int32 count_bits64(uint64 x) { +#if defined(_M_X64) + return __popcnt64(x); +#else + return count_bits32(static_cast(x >> 32)) + count_bits32(static_cast(x)); +#endif +} +#else +inline int32 count_leading_zeroes32(uint32 x) { + if (x == 0) { + return 32; + } + return __builtin_clz(x); +} + +inline int32 count_leading_zeroes64(uint64 x) { + if (x == 0) { + return 64; + } + return __builtin_clzll(x); +} + +inline int32 count_trailing_zeroes32(uint32 x) { + if (x == 0) { + return 32; + } + return __builtin_ctz(x); +} + +inline int32 count_trailing_zeroes64(uint64 x) { + if (x == 0) { + return 64; + } + return __builtin_ctzll(x); +} + +inline uint32 bswap32(uint32 x) { + return __builtin_bswap32(x); +} + +inline uint64 bswap64(uint64 x) { + return __builtin_bswap64(x); +} + +inline int32 count_bits32(uint32 x) { + return __builtin_popcount(x); +} + +inline int32 count_bits64(uint64 x) { + return __builtin_popcountll(x); +} +#endif //TODO: optimize inline int32 count_leading_zeroes_non_zero32(uint32 x) { @@ -37,104 +156,4 @@ inline int32 count_trailing_zeroes_non_zero64(uint64 x) { return count_trailing_zeroes64(x); } -#if TD_MSVC -inline int32 count_leading_zeroes32(uint32 x) { - unsigned long res = 0; - if (_BitScanReverse(&res, x)) { - return 31 - res; - } - return 32; -} -inline int32 count_leading_zeroes64(uint64 x) { -#if defined(_M_X64) - unsigned long res = 0; - if (_BitScanReverse64(&res, x)) { - return 63 - res; - } - return 64; -#else - if ((x >> 32) == 0) { - return count_leading_zeroes32(static_cast(x)) + 32; - } else { - return count_leading_zeroes32(static_cast(x >> 32)); - } -#endif -} -inline int32 count_trailing_zeroes32(uint32 x) { - unsigned long res = 0; - if (_BitScanForward(&res, x)) { - return res; - } - return 32; -} -inline int32 count_trailing_zeroes64(uint64 x) { -#if defined(_M_X64) - unsigned long res = 0; - if (_BitScanForward64(&res, x)) { - return res; - } - return 64; -#else - if (static_cast(x) == 0) { - return count_trailing_zeroes32(static_cast(x >> 32)) + 32; - } else { - return count_trailing_zeroes32(static_cast(x)); - } -#endif -} -inline uint32 bswap32(uint32 x) { - return _byteswap_ulong(x); -} -inline uint64 bswap64(uint64 x) { - return _byteswap_uint64(x); -} -inline int32 count_bits32(uint32 x) { - return __popcnt(x); -} -inline int32 count_bits64(uint64 x) { -#if defined(_M_X64) - return __popcnt64(x); -#else - return count_bits32(static_cast(x >> 32)) + count_bits32(static_cast(x)); -#endif -} -#else -inline int32 count_leading_zeroes32(uint32 x) { - if (x == 0) { - return 32; - } - return __builtin_clz(x); -} -inline int32 count_leading_zeroes64(uint64 x) { - if (x == 0) { - return 64; - } - return __builtin_clzll(x); -} -inline int32 count_trailing_zeroes32(uint32 x) { - if (x == 0) { - return 32; - } - return __builtin_ctz(x); -} -inline int32 count_trailing_zeroes64(uint64 x) { - if (x == 0) { - return 64; - } - return __builtin_ctzll(x); -} -inline uint32 bswap32(uint32 x) { - return __builtin_bswap32(x); -} -inline uint64 bswap64(uint64 x) { - return __builtin_bswap64(x); -} -inline int32 count_bits32(uint32 x) { - return __builtin_popcount(x); -} -inline int32 count_bits64(uint64 x) { - return __builtin_popcountll(x); -} -#endif - } // namespace td diff --git a/tdutils/td/utils/filesystem.cpp b/tdutils/td/utils/filesystem.cpp index 3c3e10dd..c49c4126 100644 --- a/tdutils/td/utils/filesystem.cpp +++ b/tdutils/td/utils/filesystem.cpp @@ -8,6 +8,7 @@ #include "td/utils/buffer.h" #include "td/utils/logging.h" +#include "td/utils/misc.h" #include "td/utils/PathView.h" #include "td/utils/port/FileFd.h" #include "td/utils/Slice.h" @@ -17,31 +18,41 @@ namespace td { +// TODO remove copypaste Result read_file(CSlice path, int64 size, int64 offset) { TRY_RESULT(from_file, FileFd::open(path, FileFd::Read)); if (size == -1) { - size = from_file.get_size() - offset; + size = from_file.get_size(); } if (size < 0) { - return Status::Error("Failed to read file: invalid size or offset"); + return Status::Error("Failed to read file: invalid size"); } - BufferWriter content{static_cast(size), 0, 0}; - TRY_RESULT(got_size, from_file.pread(content.as_slice(), offset)); + if (offset < 0 || offset > size) { + return Status::Error("Failed to read file: invalid offset"); + } + size -= offset; + BufferSlice content{narrow_cast(size)}; + TRY_RESULT(got_size, from_file.pread(as_slice(content), offset)); if (got_size != static_cast(size)) { return Status::Error("Failed to read file"); } from_file.close(); - return content.as_buffer_slice(); + return std::move(content); } + Result read_file_str(CSlice path, int64 size, int64 offset) { TRY_RESULT(from_file, FileFd::open(path, FileFd::Read)); if (size == -1) { - size = from_file.get_size() - offset; + size = from_file.get_size(); } if (size < 0) { - return Status::Error("Failed to read file: invalid size or offset"); + return Status::Error("Failed to read file: invalid size"); } - std::string content(static_cast(size), '\0'); + if (offset < 0 || offset > size) { + return Status::Error("Failed to read file: invalid offset"); + } + size -= offset; + std::string content(narrow_cast(size), '\0'); TRY_RESULT(got_size, from_file.pread(content, offset)); if (got_size != static_cast(size)) { return Status::Error("Failed to read file"); diff --git a/tdutils/td/utils/int_types.h b/tdutils/td/utils/int_types.h index e3caf5c9..08dda747 100644 --- a/tdutils/td/utils/int_types.h +++ b/tdutils/td/utils/int_types.h @@ -91,16 +91,6 @@ td::UInt operator^(const UInt &a, const UInt &b) { return res; } -template -bool is_zero(const UInt &a) { - for (size_t i = 0; i * 8 < size; i++) { - if (a.raw[i]) { - return false; - } - } - return true; -} - template int get_kth_bit(const UInt &a, uint32 bit) { uint8 b = a.raw[bit / 8]; diff --git a/tdutils/td/utils/port/IPAddress.cpp b/tdutils/td/utils/port/IPAddress.cpp index 6da2739e..486e88d7 100644 --- a/tdutils/td/utils/port/IPAddress.cpp +++ b/tdutils/td/utils/port/IPAddress.cpp @@ -375,7 +375,7 @@ Status IPAddress::init_host_port(CSlice host, CSlice port, bool prefer_ipv6) { hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; - LOG(DEBUG + 10) << "Try to init IP address of " << host << " with port " << port; + LOG(INFO) << "Try to init IP address of " << host << " with port " << port; auto err = getaddrinfo(host.c_str(), port.c_str(), &hints, &info); if (err != 0) { #if TD_WINDOWS @@ -440,7 +440,7 @@ Status IPAddress::init_sockaddr(sockaddr *addr, socklen_t len) { } is_valid_ = true; - LOG(DEBUG + 10) << "Have address " << get_ip_str() << " with port " << get_port(); + LOG(INFO) << "Have address " << get_ip_str() << " with port " << get_port(); return Status::OK(); } diff --git a/tdutils/td/utils/tests.cpp b/tdutils/td/utils/tests.cpp index 0a3b4a95..9fe0410b 100644 --- a/tdutils/td/utils/tests.cpp +++ b/tdutils/td/utils/tests.cpp @@ -6,14 +6,20 @@ // #include "td/utils/tests.h" -#include "td/utils/base64.h" #include "td/utils/crypto.h" #include "td/utils/filesystem.h" #include "td/utils/Parser.h" #include "td/utils/PathView.h" #include "td/utils/port/path.h" +#include "td/utils/port/Stat.h" +#include "td/utils/ScopeGuard.h" +#include "td/utils/StringBuilder.h" +#include "td/utils/Time.h" + +#include namespace td { + struct TestInfo { string name; string result_hash; // base64 @@ -31,7 +37,7 @@ class RegressionTesterImpl : public RegressionTester { unlink(db_path).ignore(); } - explicit RegressionTesterImpl(string db_path, string db_cache_dir) : db_path_(db_path), db_cache_dir_(db_cache_dir) { + RegressionTesterImpl(string db_path, string db_cache_dir) : db_path_(db_path), db_cache_dir_(db_cache_dir) { load_db(db_path); if (db_cache_dir_.empty()) { db_cache_dir_ = PathView(db_path).without_extension().str() + ".cache/"; @@ -118,15 +124,17 @@ class RegressionTesterImpl : public RegressionTester { void RegressionTester::destroy(CSlice path) { RegressionTesterImpl::destroy(path); } -std::unique_ptr RegressionTester::create(string db_path, string db_cache_dir) { - return std::make_unique(std::move(db_path), std::move(db_cache_dir)); + +unique_ptr RegressionTester::create(string db_path, string db_cache_dir) { + return td::make_unique(std::move(db_path), std::move(db_cache_dir)); } + TestsRunner &TestsRunner::get_default() { static TestsRunner default_runner; return default_runner; } -void TestsRunner::add_test(string name, std::unique_ptr test) { +void TestsRunner::add_test(string name, unique_ptr test) { for (auto &it : tests_) { if (it.first == name) { LOG(FATAL) << "Test name collision " << name; @@ -135,14 +143,14 @@ void TestsRunner::add_test(string name, std::unique_ptr test) { tests_.emplace_back(name, std::move(test)); } -void TestsRunner::add_substr_filter(std::string str) { +void TestsRunner::add_substr_filter(string str) { if (str[0] != '+' && str[0] != '-') { str = "+" + str; } substr_filters_.push_back(std::move(str)); } -void TestsRunner::set_regression_tester(std::unique_ptr regression_tester) { +void TestsRunner::set_regression_tester(unique_ptr regression_tester) { regression_tester_ = std::move(regression_tester); } @@ -154,6 +162,7 @@ void TestsRunner::run_all() { while (run_all_step()) { } } + bool TestsRunner::run_all_step() { Guard guard(this); if (state_.it == state_.end) { @@ -167,14 +176,14 @@ bool TestsRunner::run_all_step() { if (!state_.is_running) { bool ok = true; for (const auto &filter : substr_filters_) { - bool is_match = name.find(filter.substr(1)) != std::string::npos; + bool is_match = name.find(filter.substr(1)) != string::npos; if (is_match != (filter[0] == '+')) { ok = false; break; } } if (!ok) { - state_.it++; + ++state_.it; continue; } LOG(ERROR) << "Run test " << tag("name", name); @@ -191,7 +200,7 @@ bool TestsRunner::run_all_step() { regression_tester_->save_db(); } state_.is_running = false; - state_.it++; + ++state_.it; } auto ret = state_.it != state_.end; @@ -214,4 +223,5 @@ Status TestsRunner::verify(Slice data) { } return regression_tester_->verify_test(PSLICE() << name() << "_default", data); } + } // namespace td diff --git a/tdutils/td/utils/tests.h b/tdutils/td/utils/tests.h index 59a18384..a118c4a8 100644 --- a/tdutils/td/utils/tests.h +++ b/tdutils/td/utils/tests.h @@ -9,17 +9,14 @@ #include "td/utils/common.h" #include "td/utils/Context.h" #include "td/utils/format.h" -#include "td/utils/List.h" #include "td/utils/logging.h" #include "td/utils/port/thread.h" #include "td/utils/Random.h" -#include "td/utils/optional.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" -#include "td/utils/Time.h" #include -#include +#include #define REGISTER_TESTS(x) \ void TD_CONCAT(register_tests_, x)() { \ @@ -33,7 +30,7 @@ class RegressionTester { public: virtual ~RegressionTester() = default; static void destroy(CSlice db_path); - static std::unique_ptr create(string db_path, string db_cache_dir = ""); + static unique_ptr create(string db_path, string db_cache_dir = ""); virtual Status verify_test(Slice name, Slice result) = 0; virtual void save_db() = 0; @@ -68,12 +65,12 @@ class TestsRunner : public TestContext { public: static TestsRunner &get_default(); - void add_test(string name, std::unique_ptr test); - void add_substr_filter(std::string str); + void add_test(string name, unique_ptr test); + void add_substr_filter(string str); void set_stress_flag(bool flag); void run_all(); bool run_all_step(); - void set_regression_tester(std::unique_ptr regression_tester); + void set_regression_tester(unique_ptr regression_tester); private: struct State { @@ -83,10 +80,10 @@ class TestsRunner : public TestContext { size_t end{0}; }; bool stress_flag_{false}; - std::vector substr_filters_; - std::vector>> tests_; + vector substr_filters_; + vector>> tests_; State state_; - std::unique_ptr regression_tester_; + unique_ptr regression_tester_; Slice name() override; Status verify(Slice data) override; @@ -96,7 +93,7 @@ template class RegisterTest { public: RegisterTest(string name, TestsRunner &runner = TestsRunner::get_default()) { - runner.add_test(name, std::make_unique()); + runner.add_test(name, make_unique()); } }; @@ -121,8 +118,8 @@ inline string rand_string(char from, char to, int len) { return res; } -inline std::vector rand_split(string str) { - std::vector res; +inline vector rand_split(string str) { + vector res; size_t pos = 0; while (pos < str.size()) { size_t len; diff --git a/tdutils/test/misc.cpp b/tdutils/test/misc.cpp index 32722b8c..1d952cd6 100644 --- a/tdutils/test/misc.cpp +++ b/tdutils/test/misc.cpp @@ -5,8 +5,8 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #include "td/utils/base64.h" -#include "td/utils/bits.h" #include "td/utils/BigNum.h" +#include "td/utils/bits.h" #include "td/utils/HttpUrl.h" #include "td/utils/invoke.h" #include "td/utils/logging.h"