|
21 | 21 | #include <cstring> //std::memset |
22 | 22 | #include <typeinfo> |
23 | 23 |
|
| 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 | + |
24 | 33 | namespace boost { namespace interprocess { namespace test { |
25 | 34 |
|
26 | 35 | enum deallocation_type { DirectDeallocation, InverseDeallocation, MixedDeallocation, EndDeallocationType }; |
@@ -396,8 +405,8 @@ bool test_allocation_with_reuse(SegMngr &sm) |
396 | 405 | , received_size = prf_size, reuse, sizeof_object, al); |
397 | 406 | if(!ret) |
398 | 407 | break; |
399 | | - if(((std::size_t)ret & (al - 1)) != 0) |
400 | | - return 1; |
| 408 | + if(!is_aligned(ret, al)) |
| 409 | + return false; |
401 | 410 | //If we have memory, this must be a buffer reuse |
402 | 411 | if(!reuse) |
403 | 412 | return 1; |
@@ -432,7 +441,7 @@ bool test_aligned_allocation(SegMngr &sm) |
432 | 441 | break; |
433 | 442 | } |
434 | 443 |
|
435 | | - if(((std::size_t)ptr & (j - 1)) != 0) |
| 444 | + if(!is_aligned(ptr, j)) |
436 | 445 | return false; |
437 | 446 | std::memset(ptr, 0xFF, i - 1); |
438 | 447 | sm.deallocate(ptr); |
@@ -469,7 +478,7 @@ bool test_continuous_aligned_allocation(SegMngr &sm) |
469 | 478 | any_allocated = true; |
470 | 479 | } |
471 | 480 |
|
472 | | - if(((std::size_t)ptr & (j - 1)) != 0) |
| 481 | + if(!is_aligned(ptr, j)) |
473 | 482 | return false; |
474 | 483 | } |
475 | 484 | //Deallocate all |
@@ -710,7 +719,10 @@ bool test_many_equal_allocation(SegMngr &sm) |
710 | 719 |
|
711 | 720 | typename multiallocation_chain::size_type n = chain.size(); |
712 | 721 | 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); |
714 | 726 | } |
715 | 727 | if(n != std::size_t((i+1)*2)) |
716 | 728 | return false; |
@@ -823,7 +835,10 @@ bool test_many_different_allocation(SegMngr &sm) |
823 | 835 | break; |
824 | 836 | typename multiallocation_chain::size_type n = chain.size(); |
825 | 837 | 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); |
827 | 842 | } |
828 | 843 | if(n != ArraySize) |
829 | 844 | return false; |
|
0 commit comments