Skip to content

Commit 80b9159

Browse files
committed
Refact Project Structure And Add Wolf Header
1 parent e6ac7ec commit 80b9159

12 files changed

+123
-45
lines changed

CMakeLists.txt

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ cmake_minimum_required(VERSION 3.3)
33
PROJECT (Wolf)
44

55
ADD_COMPILE_OPTIONS(-std=c++11 -g -Wall)
6-
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
6+
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src)
77
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/)
88

99
SET (SRCS
1010
demo/cpp_demo.cpp
11-
area/shm_area.cpp
12-
container/comparator.h
13-
container/shm_manager.cpp
14-
container/random.h
15-
container/lockfree_linklist.h
16-
container/lockfree_skiplist.h)
11+
src/core/area/shm_area.cpp
12+
src/core/container/comparator.h
13+
src/core/container/shm_manager.cpp
14+
src/core/container/random.h
15+
src/core/container/lockfree_linklist.h
16+
src/core/container/lockfree_skiplist.h src/wolf.h)
1717

1818
ADD_EXECUTABLE(demo ${SRCS})

demo/cpp_demo.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@
1010
#include <chrono>
1111
#include <map>
1212
#include <set>
13-
#include "area/shm_area.h"
14-
15-
#include "container/lockfree_skiplist.h"
16-
#include "container/lockfree_linklist.h"
17-
#include "container/shm_manager.h"
13+
//#include "core/area/shm_area.h"
14+
//
15+
//#include "core/container/lockfree_skiplist.h"
16+
//#include "core/container/lockfree_linklist.h"
17+
//#include "core/container/shm_manager.h"
1818
// #include <folly/ConcurrentSkipList.h>
1919

2020
// using namespace folly;
2121

22+
#include "wolf.h"
23+
2224

2325
int main(int argc, const char * argv[]) {
26+
27+
Wolf<int> wolf_home("/Users/wingerted/test_shared_memory_dat", 10000000, true);
28+
wolf_home.Add(1);
29+
30+
2431
// // insert code here...
2532
// flatbuffers::FlatBufferBuilder builder(1024);
2633
// auto key = builder.CreateString("Winger");

area/shm_area.cpp renamed to src/core/area/shm_area.cpp

+37-32
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,12 @@ namespace area {
2626
this->begin_alloc_ptr_ = shared_mem + sizeof(SuperBlock);
2727
};
2828

29-
char* ShmArea::Allocate(uint32_t bytes) {
30-
assert(bytes > 0);
31-
uint32_t current_memory_usage = this->super_block_->memory_usage.load();
32-
uint32_t max_memory_size = this->super_block_->max_memory_size.load();
33-
34-
if (bytes > max_memory_size - current_memory_usage) {
35-
throw std::bad_alloc();
36-
}
37-
38-
// 对共享内存中存储的memory_usage进行CAS操作,
39-
// 如果之前获取的current_memory_usage与当前共享内存中的值不一致
40-
// 则共享内存在此期间已经对外分配过, 需要重新load current_memory_usage的值,
41-
// 再次进行CAS操作, 以保证获取到bytes字节不会被别的线程/进程使用
42-
while (!this->super_block_->memory_usage.compare_exchange_strong(
43-
current_memory_usage, current_memory_usage + bytes)) {
44-
current_memory_usage = this->super_block_->memory_usage.load();
45-
}
46-
47-
// 对于同一块共享内存来说begin_alloc_ptr永远不变, 所以current_memory_usage +
48-
// begin_alloc_ptr可以确定一块内存的分配点
49-
return this->begin_alloc_ptr_ + current_memory_usage;
50-
}
51-
52-
uint32_t ShmArea::MemoryUsage() const {
53-
return this->super_block_->memory_usage.load();
54-
}
55-
56-
char* ShmArea::MemoryStart() const {
57-
return this->begin_alloc_ptr_;
58-
}
59-
6029
// Init函数可以多进程/线程调用, 但是实际对SharedMemory的Init操作只会被执行一次
6130
// force_reset很危险, 可以直接初始化SuperBlock中的参数,
6231
// 无论这块共享内存是否初始化过
6332
uint32_t ShmArea::Init(uint32_t max_memory_size, bool force_reset) {
64-
assert(max_memory_size > 0);
33+
assert(max_memory_size > 0 || !force_reset);
34+
6535
// 对共享内存文件上锁, lockf基于POSIX是线程安全的上锁方式
6636
::lockf(this->shm_fd_, F_LOCK, 0);
6737

@@ -93,4 +63,39 @@ namespace area {
9363

9464
return init_memory_file_size;
9565
}
66+
67+
char* ShmArea::Allocate(uint32_t bytes) {
68+
assert(bytes > 0);
69+
uint32_t current_memory_usage = this->super_block_->memory_usage.load();
70+
uint32_t max_memory_size = this->super_block_->max_memory_size.load();
71+
72+
if (bytes > max_memory_size - current_memory_usage) {
73+
throw std::bad_alloc();
74+
}
75+
76+
// 对共享内存中存储的memory_usage进行CAS操作,
77+
// 如果之前获取的current_memory_usage与当前共享内存中的值不一致
78+
// 则共享内存在此期间已经对外分配过, 需要重新load current_memory_usage的值,
79+
// 再次进行CAS操作, 以保证获取到bytes字节不会被别的线程/进程使用
80+
while (!this->super_block_->memory_usage.compare_exchange_strong(
81+
current_memory_usage, current_memory_usage + bytes)) {
82+
current_memory_usage = this->super_block_->memory_usage.load();
83+
}
84+
85+
// 对于同一块共享内存来说begin_alloc_ptr永远不变, 所以current_memory_usage +
86+
// begin_alloc_ptr可以确定一块内存的分配点
87+
return this->begin_alloc_ptr_ + current_memory_usage;
88+
}
89+
90+
uint32_t ShmArea::MemoryUsage() const {
91+
return this->super_block_->memory_usage.load();
92+
}
93+
94+
char* ShmArea::MemoryStart() const {
95+
return this->begin_alloc_ptr_;
96+
}
97+
98+
char *ShmArea::GetMemoryBuffer(uint32_t offset) {
99+
return this->begin_alloc_ptr_ + offset;
100+
}
96101
}

area/shm_area.h renamed to src/core/area/shm_area.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ShmArea {
3232
char *Allocate(uint32_t bytes);
3333
uint32_t MemoryUsage() const;
3434
char *MemoryStart() const;
35+
char *GetMemoryBuffer(uint32_t offset);
3536

3637
private:
3738
uint32_t Init(uint32_t max_memory_size, bool force_reset = false);
File renamed without changes.
File renamed without changes.
File renamed without changes.

container/shm_manager.h renamed to src/core/container/shm_manager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#pragma once
1010

1111
#include <utility>
12-
#include "area/shm_area.h"
12+
#include "core/area/shm_area.h"
1313

1414
namespace container {
1515

File renamed without changes.

src/wolf.h

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// Created by Winger Cheng on 2018/2/26.
3+
//
4+
5+
#pragma once
6+
7+
#include <string>
8+
#include "core/area/shm_area.h"
9+
10+
#include "core/container/lockfree_skiplist.h"
11+
#include "core/container/lockfree_linklist.h"
12+
#include "core/container/shm_manager.h"
13+
14+
template <typename Data>
15+
class Wolf{
16+
public:
17+
Wolf(const std::string& index_path,
18+
uint32_t index_max_size = 0,
19+
bool index_reset_flag = false,
20+
const std::string& data_path = "",
21+
uint32_t data_max_size = 0,
22+
bool data_reset_flag = false): index_path_(index_path),
23+
index_max_size_(index_max_size),
24+
index_reset_flag_(index_reset_flag),
25+
data_path_(data_path),
26+
data_max_size_(data_max_size),
27+
data_reset_flag_(data_reset_flag) {
28+
auto* allocator = new area::ShmArea(index_path, index_max_size, index_reset_flag);
29+
auto* shm_manager = new container::ShmManager(allocator);
30+
31+
if (data_path.empty()) {
32+
this->inner_data_ = new container::LockFreeSkipList<Data>(shm_manager, index_reset_flag);
33+
} else {
34+
this->index_ = new container::LockFreeSkipList<uint32_t>(shm_manager, index_reset_flag);
35+
this->data_area_ = new area::ShmArea(data_path, data_max_size, data_reset_flag);
36+
}
37+
}
38+
39+
void Add(const Data& data) {
40+
if (this->inner_data_ != nullptr) {
41+
this->inner_data_->Add(data);
42+
} else {
43+
// auto* buffer = this->data_area_->Allocate(sizeof(Data));
44+
// memcpy(buffer, &data, sizeof(Data));
45+
//
46+
// auto offset = (uint32_t)(buffer - this->data_area_->MemoryStart());
47+
//this->index_->Add(offset);
48+
}
49+
}
50+
51+
// void Find(const Data& data) {
52+
// this->inner_data_->FindLessThan(data);
53+
// }
54+
55+
private:
56+
const std::string index_path_;
57+
uint32_t index_max_size_;
58+
bool index_reset_flag_;
59+
const std::string data_path_;
60+
uint32_t data_max_size_;
61+
bool data_reset_flag_;
62+
container::LockFreeSkipList<Data>* inner_data_ = nullptr;
63+
container::LockFreeSkipList<uint32_t>* index_ = nullptr;
64+
area::ShmArea* data_area_ = nullptr;
65+
};

0 commit comments

Comments
 (0)