Skip to content

Commit bf15505

Browse files
committed
fix code style
1 parent aa66bef commit bf15505

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

src/base/time_serise_pool.h

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,22 @@
1818
#define SRC_BASE_TIME_SERISE_POOL_H_
1919

2020
#include <malloc.h>
21-
#include <memory>
21+
2222
#include <map>
23+
#include <memory>
2324

2425
namespace openmldb {
2526
namespace base {
2627

27-
2828
class 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

7774
class 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_

src/base/time_serise_pool_test.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
1817
#include "base/time_serise_pool.h"
1918

20-
#include "gtest/gtest.h"
21-
2219
#include <vector>
2320

21+
#include "gtest/gtest.h"
22+
2423
namespace openmldb {
2524
namespace base {
2625

@@ -33,10 +32,10 @@ class TimeSerisePoolTest : public ::testing::Test {
3332
TEST_F(TimeSerisePoolTest, FreeToEmpty) {
3433
TimeSerisePool pool(1024);
3534
std::vector<uint64_t> times;
36-
const int datasize = 1024/2;
35+
const int datasize = 1024 / 2;
3736
char data[datasize];
3837
for (int i = 0; i < datasize; ++i) data[i] = i * i * i;
39-
for (int i = 0; i < 1000; ++i){
38+
for (int i = 0; i < 1000; ++i) {
4039
auto time = (i * i % 7) * (60 * 60 * 1000);
4140
auto ptr = pool.Alloc(datasize, time);
4241
memcpy(ptr, data, datasize);

0 commit comments

Comments
 (0)