Skip to content

Commit

Permalink
fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
cc004 committed Jul 5, 2021
1 parent aa66bef commit c837534
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 50 deletions.
84 changes: 40 additions & 44 deletions src/base/time_serise_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
#define SRC_BASE_TIME_SERISE_POOL_H_

#include <malloc.h>
#include <memory>

#include <map>
#include <memory>
#include <utility>

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<Block*>(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;
Expand All @@ -48,66 +48,62 @@ class TimeBucket {
current_offset_ += size;
return addr;
} else {
auto block = (Block*)malloc(block_size_);
auto block = reinterpret_cast<Block*>(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<uint32_t, std::unique_ptr<TimeBucket>>(keyof(time), std::unique_ptr<TimeBucket>(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<uint32_t, std::unique_ptr<TimeBucket>>(keyof(time), std::unique_ptr<TimeBucket>(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<uint32_t, std::unique_ptr<TimeBucket>> 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_
#endif // SRC_BASE_TIME_SERISE_POOL_H_
11 changes: 5 additions & 6 deletions src/base/time_serise_pool_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* limitations under the License.
*/


#include "base/time_serise_pool.h"

#include "gtest/gtest.h"

#include <vector>

#include "gtest/gtest.h"

namespace openmldb {
namespace base {

Expand All @@ -33,10 +32,10 @@ class TimeSerisePoolTest : public ::testing::Test {
TEST_F(TimeSerisePoolTest, FreeToEmpty) {
TimeSerisePool pool(1024);
std::vector<uint64_t> 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);
Expand Down

0 comments on commit c837534

Please sign in to comment.