| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // 2014-04-16 RĂ¼diger Sonderfeld <[email protected]> |
| 4 | |
| 5 | // Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 6 | // |
| 7 | // This file is part of the GNU ISO C++ Library. This library is free |
| 8 | // software; you can redistribute it and/or modify it under the terms |
| 9 | // of the GNU General Public License as published by the Free Software |
| 10 | // Foundation; either version 3, or (at your option) any later |
| 11 | // version. |
| 12 | |
| 13 | // This library is distributed in the hope that it will be useful, but |
| 14 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // General Public License for more details. |
| 17 | |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this library; see the file COPYING3. If not see |
| 20 | // <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | // C++11 [ptr.align] (20.6.5): std::align |
| 23 | |
| 24 | // { dg-require-cstdint "" } |
| 25 | |
| 26 | #include <memory> |
| 27 | #include <cstdint> |
| 28 | #include <testsuite_hooks.h> |
| 29 | |
| 30 | void |
| 31 | test01() |
| 32 | { |
| 33 | using std::size_t; |
| 34 | |
| 35 | size_t space = 100; |
| 36 | void* ptr = new char[space]; |
| 37 | char* const orig_ptr = static_cast<char*>(ptr); |
| 38 | char* old_ptr = orig_ptr; |
| 39 | const size_t orig_space = space; |
| 40 | size_t old_space = space; |
| 41 | const size_t alignment = 16; |
| 42 | const size_t size = 10; |
| 43 | while( void* const r = std::align(alignment, size, ptr, space) ) |
| 44 | { |
| 45 | VERIFY( r == ptr ); |
| 46 | uintptr_t p = reinterpret_cast<uintptr_t>(ptr); |
| 47 | VERIFY( p % alignment == 0 ); |
| 48 | char* const x = static_cast<char*>(ptr); |
| 49 | VERIFY( size_t(x - old_ptr) == old_space - space ); |
| 50 | VERIFY( (void*)x < (void*)(orig_ptr + orig_space) ); |
| 51 | VERIFY( (void*)(x + size) < (void*)(orig_ptr + orig_space) ); |
| 52 | ptr = x + size; |
| 53 | old_ptr = x; |
| 54 | old_space = space; |
| 55 | space -= size; |
| 56 | } |
| 57 | delete [] orig_ptr; |
| 58 | } |
| 59 | |
| 60 | int main() |
| 61 | { |
| 62 | test01(); |
| 63 | } |