1818#define SRC_BASE_TIME_SERISE_POOL_H_
1919
2020#include < malloc.h>
21- # include < memory >
21+
2222#include < map>
23+ #include < memory>
2324
2425namespace openmldb {
2526namespace base {
2627
27-
2828class TimeBucket {
29- public:
30- TimeBucket (uint32_t block_size) : block_size_(block_size), current_offset_(block_size + 1 ), object_num_(0 ) {
31- head_ = ( Block*) malloc (sizeof (Block*)); // empty block at end
29+ public:
30+ explicit TimeBucket (uint32_t block_size) : block_size_(block_size), current_offset_(block_size + 1 ), object_num_(0 ) {
31+ head_ = reinterpret_cast < Block*>( malloc (sizeof (Block*))); // empty block at end
3232 head_->next = NULL ;
3333 }
34- void Clear ()
35- {
34+ void Clear () {
3635 auto p = head_;
37- while (p)
38- {
36+ while (p) {
3937 auto q = p->next ;
4038 free (p);
4139 p = q;
@@ -48,66 +46,61 @@ class TimeBucket {
4846 current_offset_ += size;
4947 return addr;
5048 } else {
51- auto block = ( Block*) malloc (block_size_);
49+ auto block = reinterpret_cast < Block*>( malloc (block_size_) );
5250 current_offset_ = size;
5351 block->next = head_->next ;
5452 head_ = block;
5553 return head_->data ;
5654 }
5755 }
58- bool Free () // ret if fully freed
59- {
60- if (!--object_num_)
61- {
62- Clear ();
63- return true ;
64- }
65- else return false ;
56+ bool Free () { // ret if fully freed
57+ if (!--object_num_) {
58+ Clear ();
59+ return true ;
60+ } else
61+ return false ;
6662 }
67- private:
63+
64+ private:
6865 uint32_t block_size_;
6966 uint32_t current_offset_;
7067 uint32_t object_num_;
7168 struct Block {
7269 Block* next;
7370 char data[];
74- } *head_;
71+ } * head_;
7572};
7673
7774class TimeSerisePool {
78- public:
79- explicit TimeSerisePool (uint32_t block_size) : block_size_(block_size) {
80-
81- }
75+ public:
76+ explicit TimeSerisePool (uint32_t block_size) : block_size_(block_size) {}
8277 void * Alloc (uint32_t size, uint64_t time) {
83- auto pair = pool_.find (keyof (time));
84- if (pair == pool_.end ()){
85- auto bucket = new TimeBucket (block_size_);
86- pool_.insert (std::pair<uint32_t , std::unique_ptr<TimeBucket>>(keyof (time), std::unique_ptr<TimeBucket>(bucket)));
87- return bucket->Alloc (size);
88- }
78+ auto pair = pool_.find (keyof (time));
79+ if (pair == pool_.end ()) {
80+ auto bucket = new TimeBucket (block_size_);
81+ pool_.insert (
82+ std::pair<uint32_t , std::unique_ptr<TimeBucket>>(keyof (time), std::unique_ptr<TimeBucket>(bucket)));
83+ return bucket->Alloc (size);
84+ }
8985
90- return pair->second ->Alloc (size);
86+ return pair->second ->Alloc (size);
9187 }
9288 void Free (uint64_t time) {
93- auto pair = pool_.find (keyof (time));
94- if (pair->second ->Free ()) {
95- pool_.erase (pair);
96- }
97- }
98- bool Empty (){
99- return pool_.empty ();
89+ auto pair = pool_.find (keyof (time));
90+ if (pair->second ->Free ()) {
91+ pool_.erase (pair);
92+ }
10093 }
101- private:
94+ bool Empty () { return pool_.empty (); }
95+
96+ private:
10297 // key is the time / (60 * 60 * 1000)
10398 uint32_t block_size_;
10499 std::map<uint32_t , std::unique_ptr<TimeBucket>> pool_;
105- inline static uint32_t keyof (uint64_t time) {
106- return time / (60 * 60 * 1000 );
107- }
100+ inline static uint32_t keyof (uint64_t time) { return time / (60 * 60 * 1000 ); }
108101};
109102
110- }
111- }
103+ } // namespace base
104+ } // namespace openmldb
112105
113- #endif // SRC_BASE_TIME_SERISE_POOL_H_
106+ #endif // SRC_BASE_TIME_SERISE_POOL_H_
0 commit comments