root/maint/gnulib/tests/test-aligned-malloc.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test of allocating memory with given alignment.
   2 
   3    Copyright (C) 2020-2021 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 /* Written by Bruno Haible <bruno@clisp.org>, 2020.  */
  19 
  20 #include <config.h>
  21 
  22 #include <stdint.h>
  23 #include <stdlib.h>
  24 
  25 #define ALIGNMENT 4
  26 #define aligned_malloc aligned4_malloc
  27 #define aligned_free aligned4_free
  28 #include "aligned-malloc.h"
  29 #undef aligned_free
  30 #undef aligned_malloc
  31 #undef ALIGNMENT
  32 
  33 #define ALIGNMENT 8
  34 #define aligned_malloc aligned8_malloc
  35 #define aligned_free aligned8_free
  36 #include "aligned-malloc.h"
  37 #undef aligned_free
  38 #undef aligned_malloc
  39 #undef ALIGNMENT
  40 
  41 #define ALIGNMENT 16
  42 #define aligned_malloc aligned16_malloc
  43 #define aligned_free aligned16_free
  44 #include "aligned-malloc.h"
  45 #undef aligned_free
  46 #undef aligned_malloc
  47 #undef ALIGNMENT
  48 
  49 #define ALIGNMENT 32
  50 #define aligned_malloc aligned32_malloc
  51 #define aligned_free aligned32_free
  52 #include "aligned-malloc.h"
  53 #undef aligned_free
  54 #undef aligned_malloc
  55 #undef ALIGNMENT
  56 
  57 #include <string.h>
  58 
  59 #include "macros.h"
  60 
  61 int
  62 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  63 {
  64   static size_t sizes[] =
  65     { 13, 8, 17, 450, 320, 1, 99, 4, 15, 16, 2, 76, 37, 127, 2406, 641 };
  66   void *volatile aligned4_blocks[SIZEOF (sizes)];
  67   void *volatile aligned8_blocks[SIZEOF (sizes)];
  68   void *volatile aligned16_blocks[SIZEOF (sizes)];
  69   void *volatile aligned32_blocks[SIZEOF (sizes)];
  70   size_t i;
  71 
  72   for (i = 0; i < SIZEOF (sizes); i++)
  73     {
  74       size_t size = sizes[i];
  75 
  76       aligned4_blocks[i] = aligned4_malloc (size);
  77       ASSERT (((uintptr_t) aligned4_blocks[i] % 4) == 0);
  78       memset (aligned4_blocks[i], 'w', size);
  79 
  80       aligned8_blocks[i] = aligned8_malloc (size);
  81       ASSERT (((uintptr_t) aligned8_blocks[i] % 8) == 0);
  82       memset (aligned8_blocks[i], 'x', size);
  83 
  84       aligned16_blocks[i] = aligned16_malloc (size);
  85       ASSERT (((uintptr_t) aligned16_blocks[i] % 16) == 0);
  86       memset (aligned16_blocks[i], 'y', size);
  87 
  88       aligned32_blocks[i] = aligned32_malloc (size);
  89       ASSERT (((uintptr_t) aligned32_blocks[i] % 32) == 0);
  90       memset (aligned32_blocks[i], 'z', size);
  91     }
  92 
  93   for (i = 0; i < SIZEOF (sizes); i++)
  94     {
  95       aligned4_free (aligned4_blocks[i]);
  96       aligned8_free (aligned8_blocks[i]);
  97       aligned16_free (aligned16_blocks[i]);
  98       aligned32_free (aligned32_blocks[i]);
  99     }
 100 
 101   return 0;
 102 }

/* [previous][next][first][last][top][bottom][index][help] */