-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathmanaged_heap_memory_test.cpp
More file actions
143 lines (111 loc) · 3.76 KB
/
Copy pathmanaged_heap_memory_test.cpp
File metadata and controls
143 lines (111 loc) · 3.76 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2025-2025. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/container/vector.hpp>
#include <boost/interprocess/managed_heap_memory.hpp>
#include <cstdio>
#include <string>
#include "get_process_id_name.hpp"
using namespace boost::interprocess;
template <class CharT>
struct filename_traits;
template <>
struct filename_traits<char>
{
static const char* get()
{ return filename.c_str(); }
static std::string filename;
};
std::string filename_traits<char>::filename = get_filename();
#ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
template <>
struct filename_traits<wchar_t>
{
static const wchar_t* get()
{ return filename.c_str(); }
static std::wstring filename;
};
std::wstring filename_traits<wchar_t>::filename = get_wfilename();
#endif //#ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
template<class CharT>
int test_managed_heap_memory()
{
const int MemSize = 65536*10;
//STL compatible allocator object for heap
typedef allocator<int, managed_heap_memory::segment_manager>
allocator_int_t;
//A vector that uses that allocator
typedef boost::container::vector<int, allocator_int_t> MyVect;
{
const int max = 100;
void *array[std::size_t(max)];
//Named allocate capable shared memory allocator
managed_heap_memory mheap(MemSize);
std::size_t i;
//Let's allocate some memory
for(i = 0; i < max; ++i){
array[std::ptrdiff_t(i)] = mheap.allocate(i+1u);
}
//Deallocate allocated memory
for(i = 0; i < max; ++i){
mheap.deallocate(array[std::ptrdiff_t(i)]);
}
//Construct the STL-like allocator with the segment manager
const allocator_int_t myallocator (mheap.get_segment_manager());
//Construct vector
MyVect *mheap_vect = mheap.construct<MyVect> ("MyVector") (myallocator);
//Test that vector can be found via name
if(mheap_vect != mheap.find<MyVect>("MyVector").first)
return -1;
//Destroy and check it is not present
mheap.destroy<MyVect> ("MyVector");
mheap_vect = 0;
if(0 != mheap.find<MyVect>("MyVector").first)
return -1;
//Construct a vector in the heap
mheap_vect = mheap.construct<MyVect> ("MyVector") (myallocator);
managed_heap_memory::size_type old_free_memory = mheap.get_free_memory();
//Now grow the file
mheap.grow(MemSize);
//Check vector is still there
mheap_vect = mheap.find<MyVect>("MyVector").first;
if(!mheap_vect)
return -1;
if(mheap.get_size() != (MemSize*2))
return -1;
if(mheap.get_free_memory() <= old_free_memory)
return -1;
//Destroy and check it is not present
mheap.destroy_ptr(mheap_vect);
mheap_vect = 0;
if(0 != mheap.find<MyVect>("MyVector").first)
return -1;
{
//Now test move semantics
managed_heap_memory original(1024u);
managed_heap_memory move_ctor(boost::move(original));
managed_heap_memory move_assign;
move_assign = boost::move(move_ctor);
move_assign.swap(original);
}
}
return 0;
}
int main ()
{
int r;
r = test_managed_heap_memory<char>();
if(r) return r;
#ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
r = test_managed_heap_memory<wchar_t>();
if(r) return r;
#endif
return 0;
}