From 421ad270d488ee436f4be0ba9fdaa5f814cb4676 Mon Sep 17 00:00:00 2001 From: tanruixiang <819464715@qq.com> Date: Fri, 4 Nov 2022 19:06:57 +0800 Subject: [PATCH] Fix part of the clang-tidy warning --- src/cluster/cluster.cc | 39 ++++++++------- src/cluster/cluster.h | 6 +-- src/cluster/redis_slot.cc | 6 +-- src/cluster/replication.cc | 22 ++++----- src/cluster/replication.h | 6 +-- src/cluster/slot_migrate.cc | 26 +++++----- src/cluster/slot_migrate.h | 4 +- src/common/encoding.cc | 46 ++++++++--------- src/common/rand.cc | 98 ++++++++++++++++++++++++------------- 9 files changed, 144 insertions(+), 109 deletions(-) diff --git a/src/cluster/cluster.cc b/src/cluster/cluster.cc index 36ce43f95c6..da14d81c5d9 100644 --- a/src/cluster/cluster.cc +++ b/src/cluster/cluster.cc @@ -43,12 +43,17 @@ const char *errInvalidImportState = "Invalid import state"; ClusterNode::ClusterNode(std::string id, std::string host, int port, int role, std::string master_id, std::bitset slots) - : id_(id), host_(host), port_(port), role_(role), master_id_(master_id), slots_(slots) {} + : id_(std::move(id)), + host_(std::move(host)), + port_(port), + role_(role), + master_id_(std::move(master_id)), + slots_(slots) {} Cluster::Cluster(Server *svr, std::vector binds, int port) - : svr_(svr), binds_(binds), port_(port), size_(0), version_(-1), myself_(nullptr) { - for (unsigned i = 0; i < kClusterSlots; i++) { - slots_nodes_[i] = nullptr; + : svr_(svr), binds_(std::move(binds)), port_(port), size_(0), version_(-1), myself_(nullptr) { + for (auto &slots_node : slots_nodes_) { + slots_node = nullptr; } } @@ -70,7 +75,7 @@ bool Cluster::SubCommandIsExecExclusive(const std::string &subcommand) { return false; } -Status Cluster::SetNodeId(std::string node_id) { +Status Cluster::SetNodeId(const std::string &node_id) { if (node_id.size() != kClusterNodeIdLen) { return Status(Status::ClusterInvalidInfo, errInvalidNodeID); } @@ -100,7 +105,7 @@ Status Cluster::SetNodeId(std::string node_id) { // This is different with CLUSTERX SETNODES commands because it uses new version // topology to cover current version, it allows kvrocks nodes lost some topology // updates since of network failure, it is state instead of operation. -Status Cluster::SetSlot(int slot, std::string node_id, int64_t new_version) { +Status Cluster::SetSlot(int slot, const std::string &node_id, int64_t new_version) { // Parameters check if (new_version <= 0 || new_version != version_ + 1) { return Status(Status::NotOK, errInvalidClusterVersion); @@ -130,9 +135,9 @@ Status Cluster::SetSlot(int slot, std::string node_id, int64_t new_version) { // 3. Update the map of slots to nodes. std::shared_ptr old_node = slots_nodes_[slot]; if (old_node != nullptr) { - old_node->slots_[slot] = 0; + old_node->slots_[slot] = false; } - to_assign_node->slots_[slot] = 1; + to_assign_node->slots_[slot] = true; slots_nodes_[slot] = to_assign_node; // Clear data of migrated slot or record of imported slot @@ -236,7 +241,7 @@ void Cluster::SetMasterSlaveRepl() { } else if (nodes_.find(myself_->master_id_) != nodes_.end()) { // Slave mode and master node is existing std::shared_ptr master = nodes_[myself_->master_id_]; - Status s = svr_->AddMaster(master->host_, master->port_, 0); + Status s = svr_->AddMaster(master->host_, master->port_, false); if (s.IsOK()) { LOG(INFO) << "SLAVE OF " << master->host_ << ":" << master->port_ << " enabled by cluster topology setting"; } else { @@ -348,8 +353,8 @@ Status Cluster::GetClusterInfo(std::string *cluster_infos) { cluster_infos->clear(); int ok_slot = 0; - for (int i = 0; i < kClusterSlots; i++) { - if (slots_nodes_[i] != nullptr) ok_slot++; + for (auto &slots_node : slots_nodes_) { + if (slots_node != nullptr) ok_slot++; } *cluster_infos = @@ -425,7 +430,7 @@ Status Cluster::GetSlotsInfo(std::vector *slots_infos) { return Status::OK(); } -SlotInfo Cluster::GenSlotNodeInfo(int start, int end, std::shared_ptr n) { +SlotInfo Cluster::GenSlotNodeInfo(int start, int end, const std::shared_ptr &n) { std::vector vn; vn.push_back({n->host_, n->port_, n->id_}); // itself @@ -539,7 +544,7 @@ Status Cluster::ParseClusterNodes(const std::string &nodes_str, ClusterNodes *no int port = *parse_result; // 4) role - int role; + int role = 0; if (strcasecmp(fields[3].c_str(), "master") == 0) { role = kClusterMaster; } else if (strcasecmp(fields[3].c_str(), "slave") == 0 || strcasecmp(fields[3].c_str(), "replica") == 0) { @@ -569,7 +574,7 @@ Status Cluster::ParseClusterNodes(const std::string &nodes_str, ClusterNodes *no // 6) slot info auto valid_range = NumericRange{0, kClusterSlots - 1}; for (unsigned i = 5; i < fields.size(); i++) { - int start, stop; + int start = 0, stop = 0; std::vector ranges = Util::Split(fields[i], "-"); if (ranges.size() == 1) { auto parse_result = ParseInt(ranges[0], valid_range, 10); @@ -577,7 +582,7 @@ Status Cluster::ParseClusterNodes(const std::string &nodes_str, ClusterNodes *no return Status(Status::ClusterInvalidInfo, errSlotOutOfRange); } start = *parse_result; - slots.set(start, 1); + slots.set(start, true); if (role == kClusterMaster) { if (slots_nodes->find(start) != slots_nodes->end()) { return Status(Status::ClusterInvalidInfo, errSlotOverlapped); @@ -594,7 +599,7 @@ Status Cluster::ParseClusterNodes(const std::string &nodes_str, ClusterNodes *no start = *parse_start; stop = *parse_stop; for (int j = start; j <= stop; j++) { - slots.set(j, 1); + slots.set(j, true); if (role == kClusterMaster) { if (slots_nodes->find(j) != slots_nodes->end()) { return Status(Status::ClusterInvalidInfo, errSlotOverlapped); @@ -624,7 +629,7 @@ bool Cluster::IsWriteForbiddenSlot(int slot) { Status Cluster::CanExecByMySelf(const Redis::CommandAttributes *attributes, const std::vector &cmd_tokens, Redis::Connection *conn) { std::vector keys_indexes; - auto s = Redis::GetKeysFromCommand(attributes->name, cmd_tokens.size(), &keys_indexes); + auto s = Redis::GetKeysFromCommand(attributes->name, static_cast(cmd_tokens.size()), &keys_indexes); // No keys if (!s.IsOK()) return Status::OK(); if (keys_indexes.size() == 0) return Status::OK(); diff --git a/src/cluster/cluster.h b/src/cluster/cluster.h index 695fdfb123c..b2c014adb0f 100644 --- a/src/cluster/cluster.h +++ b/src/cluster/cluster.h @@ -78,8 +78,8 @@ class Cluster { explicit Cluster(Server *svr, std::vector binds, int port); Status SetClusterNodes(const std::string &nodes_str, int64_t version, bool force); Status GetClusterNodes(std::string *nodes_str); - Status SetNodeId(std::string node_id); - Status SetSlot(int slot, std::string node_id, int64_t version); + Status SetNodeId(const std::string &node_id); + Status SetSlot(int slot, const std::string &node_id, int64_t version); Status SetSlotMigrated(int slot, const std::string &ip_port); Status SetSlotImported(int slot); Status GetSlotsInfo(std::vector *slot_infos); @@ -99,7 +99,7 @@ class Cluster { private: std::string GenNodesDescription(); - SlotInfo GenSlotNodeInfo(int start, int end, std::shared_ptr n); + SlotInfo GenSlotNodeInfo(int start, int end, const std::shared_ptr &n); Status ParseClusterNodes(const std::string &nodes_str, ClusterNodes *nodes, std::unordered_map *slots_nodes); Server *svr_; diff --git a/src/cluster/redis_slot.cc b/src/cluster/redis_slot.cc index 26f520f3c44..2d664507ad6 100644 --- a/src/cluster/redis_slot.cc +++ b/src/cluster/redis_slot.cc @@ -50,7 +50,7 @@ static const uint16_t crc16tab[256] = { 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0}; uint16_t crc16(const char *buf, int len) { - int i; + int i = 0; uint16_t crc = 0; for (i = 0; i < len; i++) crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ *buf++) & 0x00FF]; return crc; @@ -67,9 +67,9 @@ uint16_t GetSlotNumFromKey(const std::string &key) { } std::string GetTagFromKey(const std::string &key) { - auto left_pos = key.find("{"); + auto left_pos = key.find('{'); if (left_pos == std::string::npos) return std::string(); - auto right_pos = key.find("}", left_pos + 1); + auto right_pos = key.find('}', left_pos + 1); // Note that we hash the whole key if there is nothing between {}. if (right_pos == std::string::npos || right_pos <= left_pos + 1) { return std::string(); diff --git a/src/cluster/replication.cc b/src/cluster/replication.cc index 859ba25016d..d2f0e6afe05 100644 --- a/src/cluster/replication.cc +++ b/src/cluster/replication.cc @@ -150,7 +150,7 @@ void send_string(bufferevent *bev, const std::string &data) { void ReplicationThread::CallbacksStateMachine::ConnEventCB(bufferevent *bev, int16_t events, void *state_machine_ptr) { if (events & BEV_EVENT_CONNECTED) { // call write_cb when connected - bufferevent_data_cb write_cb; + bufferevent_data_cb write_cb = nullptr; bufferevent_getcb(bev, nullptr, &write_cb, nullptr, nullptr); if (write_cb) write_cb(bev, state_machine_ptr); return; @@ -219,7 +219,7 @@ void ReplicationThread::CallbacksStateMachine::EvCallback(bufferevent *bev, void } void ReplicationThread::CallbacksStateMachine::Start() { - int cfd; + int cfd = 0; struct bufferevent *bev = nullptr; if (handlers_.empty()) { @@ -524,7 +524,7 @@ ReplicationThread::CBState ReplicationThread::incrementBatchLoopCB(bufferevent * case Incr_batch_data: // Read bulk data (batch data) if (self->incr_bulk_len_ + 2 <= evbuffer_get_length(input)) { // We got enough data - bulk_data = reinterpret_cast(evbuffer_pullup(input, self->incr_bulk_len_ + 2)); + bulk_data = reinterpret_cast(evbuffer_pullup(input, static_cast(self->incr_bulk_len_ + 2))); std::string bulk_string = std::string(bulk_data, self->incr_bulk_len_); // master would send the ping heartbeat packet to check whether the slave was alive or not, // don't write ping to db here. @@ -614,7 +614,7 @@ ReplicationThread::CBState ReplicationThread::fullSyncReadCB(bufferevent *bev, v return CBState::RESTART; } std::vector need_files = Util::Split(std::string(line.get()), ","); - for (auto f : need_files) { + for (const auto &f : need_files) { meta.files.emplace_back(f, 0); } @@ -698,7 +698,7 @@ Status ReplicationThread::parallelFetchFile(const std::string &dir, if (this->stop_flag_) { return Status(Status::NotOK, "replication thread was stopped"); } - int sock_fd; + int sock_fd = 0; Status s = Util::SockConnect(this->host_, this->port_, &sock_fd); if (!s.IsOK()) { return Status(Status::NotOK, "connect the server err: " + s.Msg()); @@ -730,7 +730,7 @@ Status ReplicationThread::parallelFetchFile(const std::string &dir, crcs.push_back(f_crc); } unsigned files_count = files.size(); - fetch_file_callback fn = [&fetch_cnt, &skip_cnt, files_count](const std::string fetch_file, + fetch_file_callback fn = [&fetch_cnt, &skip_cnt, files_count](const std::string &fetch_file, const uint32_t fetch_crc) { fetch_cnt.fetch_add(1); uint32_t cur_skip_cnt = skip_cnt.load(); @@ -787,9 +787,9 @@ Status ReplicationThread::sendAuth(int sock_fd) { return Status::OK(); } -Status ReplicationThread::fetchFile(int sock_fd, evbuffer *evbuf, const std::string &dir, std::string file, - uint32_t crc, fetch_file_callback fn) { - size_t file_size; +Status ReplicationThread::fetchFile(int sock_fd, evbuffer *evbuf, const std::string &dir, const std::string &file, + uint32_t crc, const fetch_file_callback &fn) { + size_t file_size = 0; // Read file size line while (true) { @@ -849,9 +849,9 @@ Status ReplicationThread::fetchFile(int sock_fd, evbuffer *evbuf, const std::str } Status ReplicationThread::fetchFiles(int sock_fd, const std::string &dir, const std::vector &files, - const std::vector &crcs, fetch_file_callback fn) { + const std::vector &crcs, const fetch_file_callback &fn) { std::string files_str; - for (auto file : files) { + for (const auto &file : files) { files_str += file; files_str.push_back(','); } diff --git a/src/cluster/replication.h b/src/cluster/replication.h index bb00b80f31e..64841266678 100644 --- a/src/cluster/replication.h +++ b/src/cluster/replication.h @@ -186,10 +186,10 @@ class ReplicationThread { // Synchronized-Blocking ops Status sendAuth(int sock_fd); - Status fetchFile(int sock_fd, evbuffer *evbuf, const std::string &dir, const std::string file, uint32_t crc, - fetch_file_callback fn); + Status fetchFile(int sock_fd, evbuffer *evbuf, const std::string &dir, const std::string &file, uint32_t crc, + const fetch_file_callback &fn); Status fetchFiles(int sock_fd, const std::string &dir, const std::vector &files, - const std::vector &crcs, fetch_file_callback fn); + const std::vector &crcs, const fetch_file_callback &fn); Status parallelFetchFile(const std::string &dir, const std::vector> &files); static bool isRestoringError(const char *err); static bool isWrongPsyncNum(const char *err); diff --git a/src/cluster/slot_migrate.cc b/src/cluster/slot_migrate.cc index 90f1131117d..b880cea3e58 100644 --- a/src/cluster/slot_migrate.cc +++ b/src/cluster/slot_migrate.cc @@ -82,7 +82,7 @@ SlotMigrate::SlotMigrate(Server *svr, int speed, int pipeline_size, int seq_gap) } } -Status SlotMigrate::MigrateStart(Server *svr, const std::string &node_id, const std::string dst_ip, int dst_port, +Status SlotMigrate::MigrateStart(Server *svr, const std::string &node_id, const std::string &dst_ip, int dst_port, int slot, int speed, int pipeline_size, int seq_gap) { // Only one slot migration job at the same time int16_t no_slot = -1; @@ -127,7 +127,7 @@ SlotMigrate::~SlotMigrate() { } } -Status SlotMigrate::CreateMigrateHandleThread(void) { +Status SlotMigrate::CreateMigrateHandleThread() { try { t_ = std::thread([this]() { Util::ThreadSetName("slot-migrate"); @@ -166,7 +166,7 @@ void SlotMigrate::Loop() { } } -void SlotMigrate::StateMachine(void) { +void SlotMigrate::StateMachine() { state_machine_ = kSlotMigrateStart; while (true) { if (IsTerminated()) { @@ -236,7 +236,7 @@ void SlotMigrate::StateMachine(void) { } } -Status SlotMigrate::Start(void) { +Status SlotMigrate::Start() { // Get snapshot and sequence slot_snapshot_ = storage_->GetDB()->GetSnapshot(); if (slot_snapshot_ == nullptr) { @@ -275,7 +275,7 @@ Status SlotMigrate::Start(void) { return Status::OK(); } -Status SlotMigrate::SendSnapshot(void) { +Status SlotMigrate::SendSnapshot() { // Create DB iter of snapshot uint64_t migratedkey_cnt = 0, expiredkey_cnt = 0, emptykey_cnt = 0; std::string restore_cmds; @@ -338,7 +338,7 @@ Status SlotMigrate::SendSnapshot(void) { return Status::OK(); } -Status SlotMigrate::SyncWal(void) { +Status SlotMigrate::SyncWal() { // Send incremental data in wal circularly until new increment less than a certain amount auto s = SyncWalBeforeForbidSlot(); if (!s.IsOK()) { @@ -355,7 +355,7 @@ Status SlotMigrate::SyncWal(void) { return Status::OK(); } -Status SlotMigrate::Success(void) { +Status SlotMigrate::Success() { if (stop_migrate_) { LOG(ERROR) << "[migrate] Stop migrating slot " << migrate_slot_; return Status(Status::NotOK); @@ -375,7 +375,7 @@ Status SlotMigrate::Success(void) { return Status::OK(); } -Status SlotMigrate::Fail(void) { +Status SlotMigrate::Fail() { // Set destination status if (!SetDstImportStatus(slot_job_->slot_fd_, kImportFailed)) { LOG(INFO) << "[migrate] Failed to notify the destination that data migration failed"; @@ -386,7 +386,7 @@ Status SlotMigrate::Fail(void) { return Status::OK(); } -Status SlotMigrate::Clean(void) { +Status SlotMigrate::Clean() { LOG(INFO) << "[migrate] Clean resources of migrating slot " << migrate_slot_; if (slot_snapshot_) { storage_->GetDB()->ReleaseSnapshot(slot_snapshot_); @@ -404,7 +404,7 @@ Status SlotMigrate::Clean(void) { return Status::OK(); } -bool SlotMigrate::AuthDstServer(int sock_fd, std::string password) { +bool SlotMigrate::AuthDstServer(int sock_fd, const std::string &password) { std::string cmd = Redis::MultiBulkString({"auth", password}, false); auto s = Util::SockSend(sock_fd, cmd); if (!s.IsOK()) { @@ -718,7 +718,7 @@ bool SlotMigrate::MigrateComplexKey(const rocksdb::Slice &key, const Metadata &m bool SlotMigrate::MigrateBitmapKey(const InternalKey &inkey, std::unique_ptr *iter, std::vector *user_cmd, std::string *restore_cmds) { - uint32_t index, offset; + uint32_t index = 0, offset = 0; std::string index_str = inkey.GetSubKey().ToString(); std::string fragment = (*iter)->value().ToString(); auto parse_result = ParseInt(index_str, 10); @@ -803,7 +803,7 @@ void SlotMigrate::ReleaseForbiddenSlot() { forbidden_slot_ = -1; } -void SlotMigrate::MigrateSpeedLimit(void) { +void SlotMigrate::MigrateSpeedLimit() { if (migrate_speed_ > 0) { uint64_t current_time = Util::GetTimeStampUS(); uint64_t per_request_time = 1000000 * pipeline_size_limit_ / migrate_speed_; @@ -892,7 +892,7 @@ Status SlotMigrate::MigrateIncrementData(std::unique_ptrGetDB()->GetLatestSequenceNumber(); diff --git a/src/cluster/slot_migrate.h b/src/cluster/slot_migrate.h index 08f3ead1ea3..77e3ca38d9b 100644 --- a/src/cluster/slot_migrate.h +++ b/src/cluster/slot_migrate.h @@ -84,7 +84,7 @@ class SlotMigrate : public Redis::Database { Status CreateMigrateHandleThread(void); void Loop(); - Status MigrateStart(Server *svr, const std::string &node_id, const std::string dst_ip, int dst_port, int slot, + Status MigrateStart(Server *svr, const std::string &node_id, const std::string &dst_ip, int dst_port, int slot, int speed, int pipeline_size, int seq_gap); void ReleaseForbiddenSlot(); void SetMigrateSpeedLimit(int speed) { @@ -113,7 +113,7 @@ class SlotMigrate : public Redis::Database { Status Fail(void); Status Clean(void); - bool AuthDstServer(int sock_fd, std::string password); + bool AuthDstServer(int sock_fd, const std::string &password); bool SetDstImportStatus(int sock_fd, int status); bool CheckResponseOnce(int sock_fd); bool CheckResponseWithCounts(int sock_fd, int total); diff --git a/src/common/encoding.cc b/src/common/encoding.cc index c02928625dd..3f8cdb70a72 100644 --- a/src/common/encoding.cc +++ b/src/common/encoding.cc @@ -96,14 +96,14 @@ #error "Undefined or invalid BYTE_ORDER" #endif -void EncodeFixed8(char *buf, uint8_t value) { buf[0] = static_cast(value & 0xff); } +void EncodeFixed8(char *buf, uint8_t value) { buf[0] = static_cast(value & 0xff); } void EncodeFixed16(char *buf, uint16_t value) { if (BYTE_ORDER == BIG_ENDIAN) { memcpy(buf, &value, sizeof(value)); } else { - buf[0] = static_cast((value >> 8) & 0xff); - buf[1] = static_cast(value & 0xff); + buf[0] = static_cast((value >> 8) & 0xff); + buf[1] = static_cast(value & 0xff); } } @@ -111,10 +111,10 @@ void EncodeFixed32(char *buf, uint32_t value) { if (BYTE_ORDER == BIG_ENDIAN) { memcpy(buf, &value, sizeof(value)); } else { - buf[0] = static_cast((value >> 24) & 0xff); - buf[1] = static_cast((value >> 16) & 0xff); - buf[2] = static_cast((value >> 8) & 0xff); - buf[3] = static_cast(value & 0xff); + buf[0] = static_cast((value >> 24) & 0xff); + buf[1] = static_cast((value >> 16) & 0xff); + buf[2] = static_cast((value >> 8) & 0xff); + buf[3] = static_cast(value & 0xff); } } @@ -122,20 +122,20 @@ void EncodeFixed64(char *buf, uint64_t value) { if (BYTE_ORDER == BIG_ENDIAN) { memcpy(buf, &value, sizeof(value)); } else { - buf[0] = static_cast((value >> 56) & 0xff); - buf[1] = static_cast((value >> 48) & 0xff); - buf[2] = static_cast((value >> 40) & 0xff); - buf[3] = static_cast((value >> 32) & 0xff); - buf[4] = static_cast((value >> 24) & 0xff); - buf[5] = static_cast((value >> 16) & 0xff); - buf[6] = static_cast((value >> 8) & 0xff); - buf[7] = static_cast(value & 0xff); + buf[0] = static_cast((value >> 56) & 0xff); + buf[1] = static_cast((value >> 48) & 0xff); + buf[2] = static_cast((value >> 40) & 0xff); + buf[3] = static_cast((value >> 32) & 0xff); + buf[4] = static_cast((value >> 24) & 0xff); + buf[5] = static_cast((value >> 16) & 0xff); + buf[6] = static_cast((value >> 8) & 0xff); + buf[7] = static_cast(value & 0xff); } } void PutFixed8(std::string *dst, uint8_t value) { char buf[1]; - buf[0] = static_cast(value & 0xff); + buf[0] = static_cast(value & 0xff); dst->append(buf, 1); } @@ -158,7 +158,7 @@ void PutFixed64(std::string *dst, uint64_t value) { } void PutDouble(std::string *dst, double value) { - uint64_t u64; + uint64_t u64 = 0; memcpy(&u64, &value, sizeof(value)); auto ptr = &u64; if ((*ptr >> 63) == 1) { @@ -172,7 +172,7 @@ void PutDouble(std::string *dst, double value) { } bool GetFixed8(rocksdb::Slice *input, uint8_t *value) { - const char *data; + const char *data = nullptr; if (input->size() < sizeof(uint8_t)) { return false; } @@ -218,7 +218,7 @@ bool GetDouble(rocksdb::Slice *input, double *value) { uint16_t DecodeFixed16(const char *ptr) { if (BYTE_ORDER == BIG_ENDIAN) { - uint16_t value; + uint16_t value = 0; memcpy(&value, ptr, sizeof(value)); return value; } else { @@ -229,7 +229,7 @@ uint16_t DecodeFixed16(const char *ptr) { uint32_t DecodeFixed32(const char *ptr) { if (BYTE_ORDER == BIG_ENDIAN) { - uint32_t value; + uint32_t value = 0; memcpy(&value, ptr, sizeof(value)); return value; } else { @@ -242,7 +242,7 @@ uint32_t DecodeFixed32(const char *ptr) { uint64_t DecodeFixed64(const char *ptr) { if (BYTE_ORDER == BIG_ENDIAN) { - uint64_t value; + uint64_t value = 0; memcpy(&value, ptr, sizeof(value)); return value; } else { @@ -259,14 +259,14 @@ double DecodeDouble(const char *ptr) { } else { decoded &= 0x7fffffffffffffff; } - double value; + double value = 0; memcpy(&value, &decoded, sizeof(value)); return value; } char *EncodeVarint32(char *dst, uint32_t v) { // Operate on characters as unsigneds - unsigned char *ptr = reinterpret_cast(dst); + auto *ptr = reinterpret_cast(dst); do { *ptr = 0x80 | v; v >>= 7, ++ptr; diff --git a/src/common/rand.cc b/src/common/rand.cc index 87107778c00..dae0fc3fbce 100644 --- a/src/common/rand.cc +++ b/src/common/rand.cc @@ -65,48 +65,78 @@ #include -#define N 16 -#define MASK ((1 << (N - 1)) + (1 << (N - 1)) - 1) -#define LOW(x) ((unsigned)(x)&MASK) -#define HIGH(x) LOW((x) >> N) -#define MUL(x, y, z) \ - { \ - int32_t l = (int64_t)(x) * (int64_t)(y); \ - (z)[0] = LOW(l); \ - (z)[1] = HIGH(l); \ - } -#define CARRY(x, y) ((int32_t)(x) + (int64_t)(y) > MASK) -#define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y))) -#define X0 0x330E -#define X1 0xABCD -#define X2 0x1234 -#define A0 0xE66D -#define A1 0xDEEC -#define A2 0x5 -#define C 0xB -#define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2)) -#define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n) + 1]), LOW((y)[(n) + 2])) -#define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C) -#define REST(v) \ - for (i = 0; i < 3; i++) { \ - xsubi[i] = x[i]; \ - x[i] = temp[i]; \ - } \ - return (v); -#define HI_BIT (1L << (2 * N - 1)) +constexpr const int N = 16; +constexpr const int MASK = ((1 << (N - 1)) + (1 << (N - 1)) - 1); +template +constexpr T LOW(const T x) { + return ((unsigned)(x)&MASK); +} + +template +constexpr T HIGH(const T x) { + return LOW(x >> N); +} + +template +constexpr void MUL(const T x, const T y, T *z) { + int32_t l = (int64_t)(x) * (int64_t)(y); + (z)[0] = LOW(l); + (z)[1] = HIGH(l); +} + +template +constexpr T CARRY(const T x, const T y) { + return ((int32_t)(x) + (int64_t)(y) > MASK); +} + +template +constexpr void ADDEQU(T &x, const T y, T &z) { + z = CARRY(x, (y)), x = LOW(x + (y)); +} + +constexpr const uint32_t X0 = 0x330E; +constexpr const uint32_t X1 = 0xABCD; +constexpr const uint32_t X2 = 0x1234; +constexpr const uint32_t A0 = 0xE66D; +constexpr const uint32_t A1 = 0xDEEC; +constexpr const uint32_t A2 = 0x5; +constexpr const uint32_t C = 0xB; static uint32_t x[3] = {X0, X1, X2}, a[3] = {A0, A1, A2}, c = C; -static void next(void); + +template +constexpr void SET3(T *x, const T x0, const T x1, const T x2) { + (x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2); +} + +template +constexpr void SETLOW(T *x, const T *y, const T n) { + SET3(x, LOW((y)[n]), LOW((y)[(n) + 1]), LOW((y)[(n) + 2])); +} + +template +constexpr void SEED(const T x0, const T x1, const T x2) { + SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C; +} + +template +constexpr T HI_BIT(const T x) { + return (1L << (2 * N - 1)); +} + +static void next(); int32_t redisLrand48() { next(); - return (((int32_t)x[2] << (N - 1)) + (x[1] >> 1)); + return static_cast(x[2] << (N - 1)) + static_cast(x[1] >> 1); } -void redisSrand48(int32_t seedval) { SEED(X0, LOW(seedval), HIGH(seedval)); } +void redisSrand48(int32_t seedval) { + SEED(X0, static_cast(LOW(seedval)), static_cast(HIGH(seedval))); +} -static void next(void) { - uint32_t p[2], q[2], r[2], carry0, carry1; +static void next() { + uint32_t p[2], q[2], r[2], carry0 = 0, carry1 = 0; MUL(a[0], x[0], p); ADDEQU(p[0], c, carry0);