(root)/
coreutils-9.4/
gnulib-tests/
test-alignalloc.c
       1  /* Test alignalloc and alignfree.
       2  
       3     Copyright 2022-2023 Free Software Foundation, Inc.
       4  
       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.
       9  
      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.
      14  
      15     You should have received a copy of the GNU General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <config.h>
      19  
      20  #include <alignalloc.h>
      21  
      22  #include <stdint.h>
      23  #include <string.h>
      24  #include "intprops.h"
      25  
      26  #include "signature.h"
      27  SIGNATURE_CHECK (alignalloc, void *, (idx_t, idx_t));
      28  SIGNATURE_CHECK (alignfree, void, (void *));
      29  
      30  #include "macros.h"
      31  
      32  static void
      33  test_alignalloc (idx_t alignment, idx_t size)
      34  {
      35    void *p = alignalloc (alignment, size);
      36    if (p)
      37      {
      38        memset (p, 0, size);
      39        ASSERT ((uintptr_t) p % alignment == 0);
      40      }
      41    alignfree (p);
      42  }
      43  
      44  int
      45  main ()
      46  {
      47    /* Check that alignalloc returns properly aligned storage when it succeeds.
      48       Stop at 16 MiB alignments because circa-2022 AddressSanitizer goes
      49       catatonic with large alignments in posix_memalign,
      50       and there seems to be little point to testing them.  */
      51    for (idx_t alignment = 1; alignment <= 16 * 1024 * 1024; alignment *= 2)
      52      for (idx_t size = 1; size <= 1024; size *= 2)
      53        {
      54          test_alignalloc (alignment, size - 1);
      55          test_alignalloc (alignment, size);
      56          test_alignalloc (alignment, size + 1);
      57        }
      58  
      59    /* Check that alignfree is a no-op on null pointers.  */
      60    alignfree (NULL);
      61  
      62    return 0;
      63  }