PR tree-optimization/82549
[official-gcc.git] / libitm / testsuite / libitm.c / memset-1.c
blob91ae2857cb8a81e22bda0ebde7de8bcbb77940a7
1 /* This program is free software; you can redistribute it and/or modify
2 it under the terms of the GNU General Public License as published by
3 the Free Software Foundation; either version 2 of the License, or
4 (at your option) any later version.
6 This program is distributed in the hope that it will be useful, but
7 WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 General Public License for more details.
11 You should have received a copy of the GNU General Public License
12 along with this program; if not, write to the Free Software
13 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
14 02110-1301, USA. */
16 /* Verify memcpy operation. */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/mman.h>
22 #include <libitm.h>
24 #define BEG_TRANSACTION \
25 _ITM_beginTransaction (pr_instrumentedCode | pr_hasNoAbort \
26 | pr_hasNoIrrevocable)
27 #define END_TRANSACTION \
28 _ITM_commitTransaction ()
30 #define MEMSET _ITM_memsetW
32 static unsigned char *buf;
33 static size_t bufsize, page_size;
34 static int fail;
35 int getpagesize (void);
37 #ifndef MAP_ANONYMOUS
38 # ifdef MAP_ANON
39 # define MAP_ANONYMOUS MAP_ANON
40 # endif
41 #endif
43 static void
44 do_test (size_t align, size_t len)
46 size_t i, j;
47 unsigned char c1, c2;
49 if (align + len >= bufsize)
50 return;
52 c1 = random () >> 8;
53 c2 = random () >> 8;
54 if (c1 == c2)
55 c1++;
56 memset (buf, c1, bufsize);
58 BEG_TRANSACTION;
59 MEMSET (buf + align, c2, len);
60 END_TRANSACTION;
62 i = (align > 64 ? align - 64 : 0);
63 for (; i < align; ++i)
64 if (buf[i] != c1)
66 printf ("Garbage before: ofs %zd\n", i);
67 fail = 1;
68 break;
70 for (; i < align + len; ++i)
71 if (buf[i] != c2)
73 printf ("Wrong result: ofs %zd\n", i);
74 fail = 1;
75 break;
77 for (j = i + 64 < bufsize ? i + 64 : bufsize; i < j; ++i)
78 if (buf[i] != c1)
80 printf ("Garbage after: ofs %zd\n", i);
81 fail = 1;
82 break;
86 int main()
88 size_t i, j;
90 page_size = getpagesize ();
91 bufsize = 2 * page_size;
93 buf = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
94 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
95 if (buf == MAP_FAILED)
96 return 1;
98 if (mprotect (buf, page_size, PROT_NONE))
99 return 1;
100 buf += page_size;
101 if (mprotect (buf + bufsize, page_size, PROT_NONE))
102 return 1;
104 for (i = 0; i < 18; ++i)
106 size_t len = 1 << i;
107 do_test (0, len);
108 do_test (bufsize - len, len);
111 for (i = 0; i < 32; ++i)
112 for (j = 0; j < 32; ++j)
113 do_test (j, i);
115 for (i = 3; i < 32; ++i)
117 if ((i & (i - 1)) == 0)
118 continue;
119 do_test (0, 16 * i);
120 do_test (i, 16 * i);
123 return fail;