1818#define SRC_BASE_TIME_SERISE_POOL_H_
1919
2020#include < malloc.h>
21- # include < memory >
21+
2222#include < map>
23+ #include < memory>
24+ #include < utility>
2325
2426namespace openmldb {
2527namespace base {
2628
27-
2829class 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
30+ public:
31+ explicit TimeBucket (uint32_t block_size)
32+ : block_size_(block_size), current_offset_(block_size + 1 ), object_num_(0 ) {
33+ head_ = reinterpret_cast <Block*>(malloc (sizeof (Block*))); // empty block at end
3234 head_->next = NULL ;
3335 }
34- void Clear ()
35- {
36+ void Clear () {
3637 auto p = head_;
37- while (p)
38- {
38+ while (p) {
3939 auto q = p->next ;
4040 free (p);
4141 p = q;
@@ -48,66 +48,62 @@ class TimeBucket {
4848 current_offset_ += size;
4949 return addr;
5050 } else {
51- auto block = ( Block*) malloc (block_size_);
51+ auto block = reinterpret_cast < Block*>( malloc (block_size_) );
5252 current_offset_ = size;
5353 block->next = head_->next ;
5454 head_ = block;
5555 return head_->data ;
5656 }
5757 }
58- bool Free () // ret if fully freed
59- {
60- if (!--object_num_)
61- {
62- Clear ();
63- return true ;
58+ bool Free () { // ret if fully freed
59+ if (!--object_num_) {
60+ Clear ();
61+ return true ;
62+ } else {
63+ return false ;
6464 }
65- else return false ;
6665 }
67- private:
66+
67+ private:
6868 uint32_t block_size_;
6969 uint32_t current_offset_;
7070 uint32_t object_num_;
7171 struct Block {
7272 Block* next;
7373 char data[];
74- } *head_;
74+ } * head_;
7575};
7676
7777class TimeSerisePool {
78- public:
79- explicit TimeSerisePool (uint32_t block_size) : block_size_(block_size) {
80-
81- }
78+ public:
79+ explicit TimeSerisePool (uint32_t block_size) : block_size_(block_size) {}
8280 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- }
81+ auto pair = pool_.find (keyof (time));
82+ if (pair == pool_.end ()) {
83+ auto bucket = new TimeBucket (block_size_);
84+ pool_.insert (
85+ std::pair<uint32_t , std::unique_ptr<TimeBucket>>(keyof (time), std::unique_ptr<TimeBucket>(bucket)));
86+ return bucket->Alloc (size);
87+ }
8988
90- return pair->second ->Alloc (size);
89+ return pair->second ->Alloc (size);
9190 }
9291 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 ();
92+ auto pair = pool_.find (keyof (time));
93+ if (pair->second ->Free ()) {
94+ pool_.erase (pair);
95+ }
10096 }
101- private:
97+ bool Empty () { return pool_.empty (); }
98+
99+ private:
102100 // key is the time / (60 * 60 * 1000)
103101 uint32_t block_size_;
104102 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- }
103+ inline static uint32_t keyof (uint64_t time) { return time / (60 * 60 * 1000 ); }
108104};
109105
110- }
111- }
106+ } // namespace base
107+ } // namespace openmldb
112108
113- #endif // SRC_BASE_TIME_SERISE_POOL_H_
109+ #endif // SRC_BASE_TIME_SERISE_POOL_H_
0 commit comments