Skip to content

Commit 5069b61

Browse files
committed
Use aligned allocation for allocator::allocate to support overaligned types.
1 parent 89e70d1 commit 5069b61

5 files changed

Lines changed: 97 additions & 9 deletions

File tree

include/boost/interprocess/allocators/allocator.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class allocator
161161
if(size_overflows<sizeof(T)>(count)){
162162
throw bad_alloc();
163163
}
164-
return pointer(static_cast<value_type*>(mp_mngr->allocate(count*sizeof(T))));
164+
return pointer(static_cast<value_type*>(mp_mngr->allocate_aligned(count*sizeof(T), boost::container::dtl::alignment_of<T>::value)));
165165
}
166166

167167
//!Deallocates memory previously allocated.
@@ -242,15 +242,15 @@ class allocator
242242
if(size_overflows<sizeof(T)>(elem_size)){
243243
throw bad_alloc();
244244
}
245-
mp_mngr->allocate_many(elem_size*sizeof(T), num_elements, chain);
245+
mp_mngr->allocate_many(elem_size*sizeof(T), num_elements, boost::container::dtl::alignment_of<T>::value, chain);
246246
}
247247

248248
//!Allocates n_elements elements, each one of size elem_sizes[i]in a
249249
//!contiguous block
250250
//!of memory. The elements must be deallocated
251251
void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
252252
{
253-
mp_mngr->allocate_many(elem_sizes, n_elements, sizeof(T), chain);
253+
mp_mngr->allocate_many(elem_sizes, n_elements, sizeof(T), boost::container::dtl::alignment_of<T>::value, chain);
254254
}
255255

256256
//!Allocates many elements of size elem_size in a contiguous block

test/list_test.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ typedef boost::container::list<test::movable_and_copyable_int, ShmemCopyMoveAllo
3434
typedef allocator<test::copyable_int, managed_shared_memory::segment_manager> ShmemCopyAllocator;
3535
typedef boost::container::list<test::copyable_int, ShmemCopyAllocator> MyCopyList;
3636

37+
typedef allocator<test::overaligned_copyable_int, managed_shared_memory::segment_manager> ShmemOveralignedAllocator;
38+
typedef boost::container::list<test::overaligned_copyable_int, ShmemOveralignedAllocator> MyOveralignedList;
39+
3740
int main ()
3841
{
3942
if(test::list_test<managed_shared_memory, MyList, true>())
4043
return 1;
4144

42-
// if(test::list_test<managed_shared_memory, MyVolatileList, true>())
43-
// return 1;
44-
4545
if(test::list_test<managed_shared_memory, MyMoveList, true>())
4646
return 1;
4747

@@ -51,6 +51,9 @@ int main ()
5151
if(test::list_test<managed_shared_memory, MyCopyList, true>())
5252
return 1;
5353

54+
if(test::list_test<managed_shared_memory, MyOveralignedList, true>())
55+
return 1;
56+
5457
const test::EmplaceOptions Options = (test::EmplaceOptions)(test::EMPLACE_BACK | test::EMPLACE_FRONT | test::EMPLACE_BEFORE);
5558

5659
if(!boost::interprocess::test::test_emplace<boost::container::list<test::EmplaceInt>, Options>())

test/list_test.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int list_test (bool copied_allocators_equal = true)
9898
{
9999
typedef std::list<int> MyStdList;
100100
typedef typename MyShmList::value_type IntType;
101-
const int Memsize = 128u * 1024u;
101+
const int Memsize = 256u * 1024u;
102102
const char *const shMemName = test::get_process_id_name();
103103
const int max = 100;
104104
typedef push_data_function<DoublyLinked> push_data_t;

test/movable_int.hpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <boost/interprocess/detail/config_begin.hpp>
1515
#include <boost/interprocess/detail/workaround.hpp>
1616
#include <boost/move/utility_core.hpp>
17+
#include <boost/container/detail/type_traits.hpp> //alignment_of, aligned_storage
1718

1819
namespace boost {
1920
namespace interprocess {
@@ -287,6 +288,84 @@ class non_copymovable_int
287288
int m_int;
288289
};
289290

291+
292+
class overaligned_copyable_int
293+
{
294+
public:
295+
overaligned_copyable_int()
296+
{
297+
m_d.m_int = 0;
298+
}
299+
300+
explicit overaligned_copyable_int(int a)
301+
{
302+
m_d.m_int = a;
303+
}
304+
305+
overaligned_copyable_int(const overaligned_copyable_int& mmi)
306+
{
307+
m_d.m_int = mmi.m_d.m_int;
308+
}
309+
310+
overaligned_copyable_int & operator= (int i)
311+
{ this->m_d.m_int = i; return *this; }
312+
313+
overaligned_copyable_int & operator=(const overaligned_copyable_int& mmi)
314+
{ m_d.m_int = mmi.m_d.m_int; return *this; }
315+
316+
bool operator ==(const overaligned_copyable_int &mi) const
317+
{ return this->m_d.m_int == mi.m_d.m_int; }
318+
319+
bool operator !=(const overaligned_copyable_int &mi) const
320+
{ return this->m_d.m_int != mi.m_d.m_int; }
321+
322+
bool operator <(const overaligned_copyable_int &mi) const
323+
{ return this->m_d.m_int < mi.m_d.m_int; }
324+
325+
bool operator <=(const overaligned_copyable_int &mi) const
326+
{ return this->m_d.m_int <= mi.m_d.m_int; }
327+
328+
bool operator >=(const overaligned_copyable_int &mi) const
329+
{ return this->m_d.m_int >= mi.m_d.m_int; }
330+
331+
bool operator >(const overaligned_copyable_int &mi) const
332+
{ return this->m_d.m_int > mi.m_d.m_int; }
333+
334+
int get_int() const
335+
{ return m_d.m_int; }
336+
337+
friend bool operator==(const overaligned_copyable_int &l, int r)
338+
{ return l.get_int() == r; }
339+
340+
friend bool operator==(int l, const overaligned_copyable_int &r)
341+
{ return l == r.get_int(); }
342+
343+
private:
344+
345+
union data
346+
{
347+
boost::container::dtl::aligned_storage<sizeof(int), 64>::type aligner;
348+
int m_int;
349+
} m_d;
350+
};
351+
352+
template<class E, class T>
353+
std::basic_ostream<E, T> & operator<<
354+
(std::basic_ostream<E, T> & os, overaligned_copyable_int const & p)
355+
356+
{
357+
os << p.get_int();
358+
return os;
359+
}
360+
361+
template<>
362+
struct is_copyable<overaligned_copyable_int>
363+
{
364+
static const bool value = true;
365+
};
366+
367+
368+
290369
} //namespace test {
291370
} //namespace interprocess {
292371
} //namespace boost {

test/vector_test.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ int main()
7272

7373
typedef allocator<test::copyable_int, managed_shared_memory::segment_manager> ShmemCopyAllocator;
7474
typedef boost::container::vector<test::copyable_int, ShmemCopyAllocator> MyCopyVector;
75-
75+
/*
76+
typedef allocator<test::overaligned_copyable_int, managed_shared_memory::segment_manager> ShmemOveralignedAllocator;
77+
typedef boost::container::vector<test::overaligned_copyable_int, ShmemOveralignedAllocator> MyOveralignedVector;
78+
*/
7679
if(test::vector_test<managed_shared_memory, MyVector>())
7780
return 1;
7881

@@ -87,7 +90,10 @@ int main()
8790

8891
if(test::vector_test<managed_shared_memory, MyCopyVector>())
8992
return 1;
90-
93+
/*
94+
if(test::vector_test<managed_shared_memory, MyOveralignedVector>())
95+
return 1;
96+
*/
9197
if(test_expand_bwd())
9298
return 1;
9399

0 commit comments

Comments
 (0)