20 #include <scoped_allocator> 24 #include <boost/interprocess/allocators/adaptive_pool.hpp> 25 #include <boost/interprocess/managed_shared_memory.hpp> 38 using ShmAllocator = adaptive_pool<T, managed_shared_memory::segment_manager>;
40 template <
typename K,
typename M>
48 template <
typename K,
typename M>
52 folly::f14::DefaultHasher<K>,
53 folly::f14::DefaultKeyEqual<K>,
56 template <
typename K,
typename M>
60 folly::f14::DefaultHasher<K>,
61 folly::f14::DefaultKeyEqual<K>,
67 folly::f14::DefaultHasher<K>,
68 folly::f14::DefaultKeyEqual<K>,
74 folly::f14::DefaultHasher<K>,
75 folly::f14::DefaultKeyEqual<K>,
81 folly::f14::DefaultHasher<K>,
82 folly::f14::DefaultKeyEqual<K>,
85 using ShmVI = std::vector<int, ShmAllocator<int>>;
87 std::vector<ShmVI, std::scoped_allocator_adaptor<ShmAllocator<ShmVI>>>;
94 std::scoped_allocator_adaptor<ShmAllocator<std::pair<int const, ShmVVI>>>>;
99 folly::f14::DefaultHasher<int>,
100 folly::f14::DefaultKeyEqual<int>,
101 std::scoped_allocator_adaptor<ShmAllocator<std::pair<int const, ShmVVI>>>>;
106 folly::f14::DefaultHasher<int>,
107 folly::f14::DefaultKeyEqual<int>,
108 std::scoped_allocator_adaptor<ShmAllocator<std::pair<int const, ShmVVI>>>>;
115 std::shared_ptr<managed_shared_memory> makeShmSegment(
118 auto deleter = [=](managed_shared_memory* p) {
120 shared_memory_object::remove(
name.c_str());
123 auto segment =
new managed_shared_memory(create_only,
name.c_str(), n);
124 return std::shared_ptr<managed_shared_memory>(segment, deleter);
128 template <
typename M>
130 auto segment = makeShmSegment(8192);
131 auto mgr = segment->get_segment_manager();
134 for (
int i = 0;
i < 20; ++
i) {
138 for (
int i = 0;
i < 20; ++
i) {
143 template <
typename S>
145 auto segment = makeShmSegment(8192);
146 auto mgr = segment->get_segment_manager();
147 S s{
typename S::allocator_type{mgr}};
148 for (
int i = 0;
i < 20; ++
i) {
152 for (
int i = 0;
i < 40; ++
i) {
158 runSimpleMapTest<ShmF14ValueMap<int, int>>();
161 runSimpleMapTest<ShmF14NodeMap<int, int>>();
164 runSimpleMapTest<ShmF14VectorMap<int, int>>();
167 runSimpleSetTest<ShmF14ValueSet<int>>();
170 runSimpleSetTest<ShmF14NodeSet<int>>();
173 runSimpleSetTest<ShmF14VectorSet<int>>();
176 template <
typename M>
182 auto name = makeRandomName();
183 auto segment1 = makeShmSegment(8192,
name);
185 std::make_shared<managed_shared_memory>(open_only,
name.c_str());
187 auto m1 = segment1->construct<M>(
"m")(
188 typename M::allocator_type{segment1->get_segment_manager()});
189 auto m2 = segment2->find<M>(
"m").
first;
191 std::cout <<
"m in segment1 @ " << (uintptr_t)m1 <<
"\n";
192 std::cout <<
"m in segment2 @ " << (uintptr_t)m2 <<
"\n";
204 EXPECT_FALSE(m2->emplace(std::make_pair(3, 33)).second);
209 runSimultaneousAccessMapTest<ShmF14ValueMap<int, int>>();
212 runSimultaneousAccessMapTest<ShmF14NodeMap<int, int>>();
215 runSimultaneousAccessMapTest<ShmF14VectorMap<int, int>>();
218 template <
typename T>
221 std::shared_ptr<managed_shared_memory>
const& segment,
223 auto beginAddr =
reinterpret_cast<uintptr_t
>(segment->get_address());
224 auto endAddr = beginAddr + segment->get_size();
225 auto addr =
reinterpret_cast<uintptr_t
>(&
val);
227 << name <<
": begin @" << std::hex << beginAddr <<
", val @" << std::hex
228 <<
addr <<
", size" << std::hex <<
sizeof(
T) <<
", end @" << std::hex
234 std::shared_ptr<managed_shared_memory>
const& segment,
237 template <
typename A,
typename B>
240 std::shared_ptr<managed_shared_memory>
const& segment,
241 std::pair<A, B>
const&
val);
243 template <
typename T>
246 std::shared_ptr<managed_shared_memory>
const& segment,
251 std::shared_ptr<managed_shared_memory>
const& segment,
256 template <
typename A,
typename B>
259 std::shared_ptr<managed_shared_memory>
const& segment,
260 std::pair<A, B>
const&
val) {
266 template <
typename T>
269 std::shared_ptr<managed_shared_memory>
const& segment,
271 typename T::allocator_type alloc{segment->get_segment_manager()};
274 for (
auto&&
v :
val) {
279 template <
typename M>
281 auto segment = makeShmSegment(8192);
282 auto mgr = segment->get_segment_manager();
284 auto vi = segment->construct<
ShmVI>(anonymous_instance)(
285 typename ShmVI::allocator_type{mgr});
289 auto vvi = segment->construct<ShmVVI>(anonymous_instance)(
290 typename ShmVVI::allocator_type{mgr});
292 vvi->at(0).push_back(2);
295 auto m = segment->construct<M>(anonymous_instance)(
296 typename M::allocator_type{mgr});
297 (*m)[1].emplace_back();
298 (*m)[1][0].push_back(3);
305 runScopedAllocatorTest<ShmF14ValueI2VVI>();
308 runScopedAllocatorTest<ShmF14NodeI2VVI>();
311 runScopedAllocatorTest<ShmF14VectorI2VVI>();
314 template <
typename M>
316 auto segment1 = makeShmSegment(8192);
317 auto mgr1 = segment1->get_segment_manager();
319 auto segment2 = makeShmSegment(8192);
320 auto mgr2 = segment2->get_segment_manager();
322 auto a1 = segment1->construct<M>(anonymous_instance)(
323 typename M::allocator_type{mgr1});
324 (*a1)[1].emplace_back();
325 (*a1)[1][0].push_back(3);
326 auto b1 = segment1->construct<M>(anonymous_instance)(*a1);
327 auto c1 = segment1->construct<M>(anonymous_instance)(
std::move(*a1));
329 auto d2 = segment2->construct<M>(anonymous_instance)(
330 typename M::allocator_type{mgr2});
331 (*d2)[10].emplace_back();
332 (*d2)[10][0].push_back(6);
333 auto e2 = segment2->construct<M>(anonymous_instance)(*d2);
334 auto f2 = segment2->construct<M>(anonymous_instance)(
335 *b1,
typename M::allocator_type{mgr2});
380 auto g2 = segment2->construct<M>(anonymous_instance)(
381 std::move(*a1),
typename M::allocator_type{mgr2});
388 segment1->destroy_ptr(a1);
389 segment1->destroy_ptr(b1);
390 segment1->destroy_ptr(c1);
391 segment2->destroy_ptr(d2);
392 segment2->destroy_ptr(e2);
393 segment2->destroy_ptr(f2);
394 segment2->destroy_ptr(g2);
398 runMultiScopeTest<ShmF14ValueI2VVI>();
401 runMultiScopeTest<ShmF14NodeI2VVI>();
404 runMultiScopeTest<ShmF14VectorI2VVI>();
void runSimultaneousAccessMapTest()
void runScopedAllocatorTest()
std::string sformat(StringPiece fmt, Args &&...args)
#define EXPECT_EQ(val1, val2)
constexpr detail::Map< Move > move
void checkSingleLocation(std::string name, std::shared_ptr< managed_shared_memory > const &segment, T const &val)
std::vector< ShmVI, std::scoped_allocator_adaptor< ShmAllocator< ShmVI >>> ShmVVI
static constexpr F14IntrinsicsMode getF14IntrinsicsMode()
static map< string, int > m
type_t< void, Ts... > void_t
adaptive_pool< T, managed_shared_memory::segment_manager > ShmAllocator
void checkLocation(std::string name, std::shared_ptr< managed_shared_memory > const &segment, int const &val)
**Optimized Holders **The template hazptr_array< M > provides most of the functionality *of M hazptr_holder s but with faster construction destruction *for M
#define EXPECT_TRUE(condition)
std::vector< int, ShmAllocator< int >> ShmVI
#define EXPECT_NE(val1, val2)
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
#define EXPECT_FALSE(condition)
ThreadPoolListHook * addr
TEST(ShmF14ValueMap, simple)
constexpr detail::First first