(root)/
glibc-2.38/
malloc/
tst-mallocalign1.c
       1  /* Verify that MALLOC_ALIGNMENT is honored by malloc.
       2     Copyright (C) 2012-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library 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 GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <stdio.h>
      20  #include <stdlib.h>
      21  #include <inttypes.h>
      22  #include <malloc-size.h>
      23  #include <support/check.h>
      24  
      25  static void *
      26  test (size_t s)
      27  {
      28    void *p = malloc (s);
      29  
      30    printf ("malloc: %zu, %p: %zu\n", s, p,
      31  	  ((uintptr_t) p) & MALLOC_ALIGN_MASK);
      32    return p;
      33  }
      34  
      35  #define ALIGNED(p) (((uintptr_t) p & MALLOC_ALIGN_MASK) == 0)
      36  
      37  static int
      38  do_test (void)
      39  {
      40    void *p;
      41  
      42    p = test (2);
      43    TEST_VERIFY (ALIGNED (p));
      44    free (p);
      45  
      46    p = test (8);
      47    TEST_VERIFY (ALIGNED (p));
      48    free (p);
      49  
      50    p = test (13);
      51    TEST_VERIFY (ALIGNED (p));
      52    free (p);
      53  
      54    p = test (16);
      55    TEST_VERIFY (ALIGNED (p));
      56    free (p);
      57  
      58    p = test (23);
      59    TEST_VERIFY (ALIGNED (p));
      60    free (p);
      61  
      62    p = test (43);
      63    TEST_VERIFY (ALIGNED (p));
      64    free (p);
      65  
      66    p = test (123);
      67    TEST_VERIFY (ALIGNED (p));
      68    free (p);
      69  
      70    return 0;
      71  }
      72  
      73  #include <support/test-driver.c>