diff --git a/src/base/time_serise_pool.h b/src/base/time_serise_pool.h index 6aada6db51c..82d6956abe2 100644 --- a/src/base/time_serise_pool.h +++ b/src/base/time_serise_pool.h @@ -18,24 +18,24 @@ #define SRC_BASE_TIME_SERISE_POOL_H_ #include -#include + #include +#include +#include namespace openmldb { namespace base { - class TimeBucket { - public: - TimeBucket(uint32_t block_size) : block_size_(block_size), current_offset_(block_size + 1), object_num_(0) { - head_ = (Block*)malloc(sizeof(Block*)); //empty block at end + public: + explicit TimeBucket(uint32_t block_size) + : block_size_(block_size), current_offset_(block_size + 1), object_num_(0) { + head_ = reinterpret_cast(malloc(sizeof(Block*))); // empty block at end head_->next = NULL; } - void Clear() - { + void Clear() { auto p = head_; - while (p) - { + while (p) { auto q = p->next; free(p); p = q; @@ -48,66 +48,62 @@ class TimeBucket { current_offset_ += size; return addr; } else { - auto block = (Block*)malloc(block_size_); + auto block = reinterpret_cast(malloc(block_size_)); current_offset_ = size; block->next = head_->next; head_ = block; return head_->data; } } - bool Free() // ret if fully freed - { - if (!--object_num_) - { - Clear(); - return true; + bool Free() { // ret if fully freed + if (!--object_num_) { + Clear(); + return true; + } else { + return false; } - else return false; } - private: + + private: uint32_t block_size_; uint32_t current_offset_; uint32_t object_num_; struct Block { Block* next; char data[]; - } *head_; + } * head_; }; class TimeSerisePool { - public: - explicit TimeSerisePool(uint32_t block_size) : block_size_(block_size) { - - } + public: + explicit TimeSerisePool(uint32_t block_size) : block_size_(block_size) {} void* Alloc(uint32_t size, uint64_t time) { - auto pair = pool_.find(keyof(time)); - if (pair == pool_.end()){ - auto bucket = new TimeBucket(block_size_); - pool_.insert(std::pair>(keyof(time), std::unique_ptr(bucket))); - return bucket->Alloc(size); - } + auto pair = pool_.find(keyof(time)); + if (pair == pool_.end()) { + auto bucket = new TimeBucket(block_size_); + pool_.insert( + std::pair>(keyof(time), std::unique_ptr(bucket))); + return bucket->Alloc(size); + } - return pair->second->Alloc(size); + return pair->second->Alloc(size); } void Free(uint64_t time) { - auto pair = pool_.find(keyof(time)); - if (pair->second->Free()) { - pool_.erase(pair); - } - } - bool Empty(){ - return pool_.empty(); + auto pair = pool_.find(keyof(time)); + if (pair->second->Free()) { + pool_.erase(pair); + } } - private: + bool Empty() { return pool_.empty(); } + + private: // key is the time / (60 * 60 * 1000) uint32_t block_size_; std::map> pool_; - inline static uint32_t keyof(uint64_t time) { - return time / (60 * 60 * 1000); - } + inline static uint32_t keyof(uint64_t time) { return time / (60 * 60 * 1000); } }; -} -} +} // namespace base +} // namespace openmldb -#endif // SRC_BASE_TIME_SERISE_POOL_H_ \ No newline at end of file +#endif // SRC_BASE_TIME_SERISE_POOL_H_ diff --git a/src/base/time_serise_pool_test.cc b/src/base/time_serise_pool_test.cc index e9a9e3576f0..2c5dc13ddac 100644 --- a/src/base/time_serise_pool_test.cc +++ b/src/base/time_serise_pool_test.cc @@ -14,13 +14,12 @@ * limitations under the License. */ - #include "base/time_serise_pool.h" -#include "gtest/gtest.h" - #include +#include "gtest/gtest.h" + namespace openmldb { namespace base { @@ -33,10 +32,10 @@ class TimeSerisePoolTest : public ::testing::Test { TEST_F(TimeSerisePoolTest, FreeToEmpty) { TimeSerisePool pool(1024); std::vector times; - const int datasize = 1024/2; - char data[datasize]; + const int datasize = 1024 / 2; + char *data = new char[datasize]; for (int i = 0; i < datasize; ++i) data[i] = i * i * i; - for (int i = 0; i < 1000; ++i){ + for (int i = 0; i < 1000; ++i) { auto time = (i * i % 7) * (60 * 60 * 1000); auto ptr = pool.Alloc(datasize, time); memcpy(ptr, data, datasize);