1 /* This test file is part of GDB, the GNU debugger.
3 Copyright 2021-2022 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* Return true if address P is ALIGNMENT-byte aligned. */
27 is_aligned (void *p, size_t alignment)
29 size_t mask = (alignment - 1);
30 return ((uintptr_t)p & mask) == 0;
33 /* Allocate SIZE memory with ALIGNMENT, and return it. If FREE_POINTER,
34 return in it the corresponding pointer to be passed to free.
36 Do the alignment precisely, in other words, if an alignment of 4 is
37 requested, make sure the pointer is 4-byte aligned, but not 8-byte
38 aligned. In other words, make sure the pointer is not overaligned.
40 The benefit of using precise alignment is that accidentally specifying
41 a too low alignment will not be compensated by accidental
45 precise_aligned_alloc (size_t alignment, size_t size, void **free_pointer)
47 /* Allocate extra to compensate for "p += alignment". */
48 size_t alloc_size = size + alignment;
50 /* Align extra, to be able to do precise align. */
51 void *p = aligned_alloc (alignment * 2, alloc_size);
54 void *p_end = p + alloc_size;
56 /* Make p precisely aligned. */
59 /* Verify p is without bounds, and points to large enough area. */
61 assert (p + size <= p_end);
63 /* Verify required alignment. */
64 assert (is_aligned (p, alignment));
66 /* Verify required alignment is precise. */
67 assert (! is_aligned (p, 2 * alignment));
69 if (free_pointer != NULL)
70 *free_pointer = p_orig;
75 /* Duplicate data SRC of size SIZE to a newly allocated, precisely aligned
76 location with alignment ALIGNMENT. */
79 precise_aligned_dup (size_t alignment, size_t size, void **free_pointer,
82 void *p = precise_aligned_alloc (alignment, size, free_pointer);
84 memcpy (p, src, size);