OSDN Git Service

libitm:
[pf3gnuchains/gcc-fork.git] / libitm / testsuite / libitm.c / memset-1.c
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.
5
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.
10
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.  */
15
16 /* Verify memcpy operation.  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/mman.h>
22 #include <libitm.h>
23
24 #define BEG_TRANSACTION \
25   _ITM_beginTransaction (pr_instrumentedCode | pr_hasNoAbort \
26                          | pr_hasNoIrrevocable)
27 #define END_TRANSACTION \
28   _ITM_commitTransaction ()
29
30 #define MEMSET _ITM_memsetW
31
32 static unsigned char *buf;
33 static size_t bufsize, page_size;
34 static int fail;
35
36 #ifndef MAP_ANONYMOUS
37 #  ifdef MAP_ANON
38 #    define MAP_ANONYMOUS MAP_ANON
39 #  endif
40 #endif
41
42 static void
43 do_test (size_t align, size_t len)
44 {
45   size_t i, j;
46   unsigned char c1, c2;
47
48   if (align + len >= bufsize)
49     return;
50
51   c1 = random () >> 8;
52   c2 = random () >> 8;
53   if (c1 == c2)
54     c1++;
55   memset (buf, c1, bufsize);
56
57   BEG_TRANSACTION;
58   MEMSET (buf + align, c2, len);
59   END_TRANSACTION;
60
61   i = (align > 64 ? align - 64 : 0);
62   for (; i < align; ++i)
63     if (buf[i] != c1)
64       {
65         printf ("Garbage before: ofs %zd\n", i);
66         fail = 1;
67         break;
68       }
69   for (; i < align + len; ++i)
70     if (buf[i] != c2)
71       {
72         printf ("Wrong result: ofs %zd\n", i);
73         fail = 1;
74         break;
75       }
76   for (j = i + 64 < bufsize ? i + 64 : bufsize; i < j; ++i)
77     if (buf[i] != c1)
78       {
79         printf ("Garbage after: ofs %zd\n", i);
80         fail = 1;
81         break;
82       }
83 }
84
85 int main()
86 {
87   size_t i, j;
88
89   page_size = getpagesize ();
90   bufsize = 2 * page_size;
91
92   buf = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
93               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
94   if (buf == MAP_FAILED)
95     return 1;
96
97   if (mprotect (buf, page_size, PROT_NONE))
98     return 1;
99   buf += page_size;
100   if (mprotect (buf + bufsize, page_size, PROT_NONE))
101     return 1;
102
103   for (i = 0; i < 18; ++i)
104     {
105       size_t len = 1 << i;
106       do_test (0, len);
107       do_test (bufsize - len, len);
108     }
109
110   for (i = 0; i < 32; ++i)
111     for (j = 0; j < 32; ++j)
112       do_test (j, i);
113
114   for (i = 3; i < 32; ++i)
115     {
116       if ((i & (i - 1)) == 0)
117         continue;
118       do_test (0, 16 * i);
119       do_test (i, 16 * i);
120     }
121
122   return fail;
123 }