Skip to content

Commit c837534

Browse files
committed
fix code style
1 parent aa66bef commit c837534

File tree

2 files changed

+45
-50
lines changed

2 files changed

+45
-50
lines changed

src/base/time_serise_pool.h

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@
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

2426
namespace openmldb {
2527
namespace base {
2628

27-
2829
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
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

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

src/base/time_serise_pool_test.cc

Lines changed: 5 additions & 6 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;
37-
char data[datasize];
35+
const int datasize = 1024 / 2;
36+
char *data = new char[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)