Skip to content

Commit 9f1c4b2

Browse files
committed
Add alignment checks now that allocate_aligned is used in several functions
1 parent aab8703 commit 9f1c4b2

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

test/memory_algorithm_test_template.hpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
#include <cstring> //std::memset
2222
#include <typeinfo>
2323

24+
namespace {
25+
26+
bool is_aligned(void *ptr, std::size_t alignment)
27+
{
28+
return ((std::size_t)ptr & (alignment - 1)) == 0;
29+
}
30+
31+
} //namespace {
32+
2433
namespace boost { namespace interprocess { namespace test {
2534

2635
enum deallocation_type { DirectDeallocation, InverseDeallocation, MixedDeallocation, EndDeallocationType };
@@ -396,8 +405,8 @@ bool test_allocation_with_reuse(SegMngr &sm)
396405
, received_size = prf_size, reuse, sizeof_object, al);
397406
if(!ret)
398407
break;
399-
if(((std::size_t)ret & (al - 1)) != 0)
400-
return 1;
408+
if(!is_aligned(ret, al))
409+
return false;
401410
//If we have memory, this must be a buffer reuse
402411
if(!reuse)
403412
return 1;
@@ -432,7 +441,7 @@ bool test_aligned_allocation(SegMngr &sm)
432441
break;
433442
}
434443

435-
if(((std::size_t)ptr & (j - 1)) != 0)
444+
if(!is_aligned(ptr, j))
436445
return false;
437446
std::memset(ptr, 0xFF, i - 1);
438447
sm.deallocate(ptr);
@@ -469,7 +478,7 @@ bool test_continuous_aligned_allocation(SegMngr &sm)
469478
any_allocated = true;
470479
}
471480

472-
if(((std::size_t)ptr & (j - 1)) != 0)
481+
if(!is_aligned(ptr, j))
473482
return false;
474483
}
475484
//Deallocate all
@@ -710,7 +719,10 @@ bool test_many_equal_allocation(SegMngr &sm)
710719

711720
typename multiallocation_chain::size_type n = chain.size();
712721
while(!chain.empty()){
713-
buffers.push_back(ipcdetail::to_raw_pointer(chain.pop_front()));
722+
void *ptr = ipcdetail::to_raw_pointer(chain.pop_front());
723+
if(!is_aligned(ptr, al))
724+
return false;
725+
buffers.push_back(ptr);
714726
}
715727
if(n != std::size_t((i+1)*2))
716728
return false;
@@ -823,7 +835,10 @@ bool test_many_different_allocation(SegMngr &sm)
823835
break;
824836
typename multiallocation_chain::size_type n = chain.size();
825837
while(!chain.empty()){
826-
buffers.push_back(ipcdetail::to_raw_pointer(chain.pop_front()));
838+
void *ptr = ipcdetail::to_raw_pointer(chain.pop_front());
839+
if(!is_aligned(ptr, al))
840+
return false;
841+
buffers.push_back(ptr);
827842
}
828843
if(n != ArraySize)
829844
return false;

0 commit comments

Comments
 (0)