From bd57644f4ccf6eeed85753a05ef323ba5dab3970 Mon Sep 17 00:00:00 2001 From: leiguangyu Date: Tue, 27 Aug 2024 14:54:14 +0800 Subject: [PATCH] Avoid taking the remainder operation with a denominator of 0 Change-Id: Icbde475b4b4f20dcb7ab3722745e65017b0a9f8e Signed-off-by: leiguangyu --- include/unique_stack_table.h | 2 +- src/unique_stack_table.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/unique_stack_table.h b/include/unique_stack_table.h index 329a534..ff48edf 100644 --- a/include/unique_stack_table.h +++ b/include/unique_stack_table.h @@ -128,7 +128,7 @@ private: uint32_t totalNodes_ = 0; // current available node count, include index 0 uint32_t availableNodes_ = 0; - uint32_t hashModulus_ = 0; + uint32_t hashModulus_ = 1; // 0 for reserved, start from 1 uint32_t availableIndex_ = 1; // for de-conflict diff --git a/src/unique_stack_table.cpp b/src/unique_stack_table.cpp index 6f7434f..8b4ffff 100644 --- a/src/unique_stack_table.cpp +++ b/src/unique_stack_table.cpp @@ -31,7 +31,7 @@ bool UniqueStackTable::Init() } availableNodes_ = totalNodes_; - hashModulus_ = availableNodes_ >= 1 ? availableNodes_ - 1 : 0; + hashModulus_ = availableNodes_ > 1 ? availableNodes_ - 1 : 1; hashStep_ = (totalNodes_ / (deconflictTimes_ * HASH_STEP_BASE_MULTIPLE + HASH_STEP_BASE_NUM)); tableBuf_ = std::make_unique(tableSize_); @@ -71,7 +71,7 @@ bool UniqueStackTable::Resize() availableIndex_ += availableNodes_; totalNodes_ = ((newtableSize / sizeof(Node)) >> 1) << 1; // make it even. availableNodes_ = totalNodes_ - oldNumNodes; - hashModulus_ = availableNodes_ >= 1 ? availableNodes_ - 1 : 0; + hashModulus_ = availableNodes_ > 1 ? availableNodes_ - 1 : 1; hashStep_ = availableNodes_ / (deconflictTimes_ * HASH_STEP_BASE_MULTIPLE + HASH_STEP_BASE_NUM); HLOGI("After resize, totalNodes_: %u, availableNodes_: %u, availableIndex_: %u hashStep_: %" PRIu64 "", totalNodes_, availableNodes_, availableIndex_, hashStep_);