-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_polymorphic_allocator.cpp
85 lines (62 loc) · 2.43 KB
/
examples_polymorphic_allocator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <dice/template-library/polymorphic_allocator.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <array>
#include <iostream>
#include <vector>
template<typename T>
using shm_allocator_type = boost::interprocess::allocator<T, boost::interprocess::managed_shared_memory::segment_manager>;
template<typename T>
using stl_allocator_type = dice::template_library::offset_ptr_stl_allocator<T>;
template<typename T>
using allocator_type = dice::template_library::polymorphic_allocator<T, stl_allocator_type, shm_allocator_type>;
using vector_type = boost::interprocess::vector<int, allocator_type<int>>;
inline constexpr char const *shm_name = "dice_template_library_polymorhic_allocator_example_shm";
inline constexpr char const *shm_vec_name = "example_vec";
vector_type vector_sum(std::vector<vector_type const *> const &operands) {
vector_type sum;
for (auto const &opr : operands) {
if (opr->size() > sum.size()) {
sum.resize(opr->size());
}
for (size_t ix = 0; ix < opr->size(); ++ix) {
sum[ix] += (*opr)[ix];
}
}
return sum;
}
int main() {
std::vector<vector_type const *> operands;
{ // populate shared memory
boost::interprocess::managed_shared_memory shm{boost::interprocess::create_only, shm_name, 4096};
// operand 1: allocated fully in shared memory
auto *shm_vec = shm.construct<vector_type>(shm_vec_name)(allocator_type<int>{std::in_place_type<shm_allocator_type<int>>, shm.get_segment_manager()});
shm_vec->push_back(1);
shm_vec->push_back(5);
operands.push_back(shm_vec);
}
boost::interprocess::managed_shared_memory shm{boost::interprocess::open_only, shm_name};
// operand 1: reloaded from shared memory
// this would segfault with std::pmr::polymorphic_allocator
auto [shm_vec, _] = shm.find<vector_type>(shm_vec_name);
// operand 2: allocated fully on heap
auto *normal_vec = new vector_type{allocator_type<int>{}};
normal_vec->push_back(2);
normal_vec->push_back(10);
normal_vec->push_back(20);
operands.push_back(normal_vec);
auto const sum = vector_sum(operands);
assert(sum.size() == 3);
assert(sum[0] == 3);
assert(sum[1] == 15);
assert(sum[2] == 20);
std::cout << "sum: ";
for (auto const val : sum) {
std::cout << val << " ";
}
std::cout << '\n';
shm.destroy_ptr(shm_vec);
delete normal_vec;
boost::interprocess::shared_memory_object::remove(shm_name);
}