S390: Move utf8-utf32-z9.c to multiarch folder and use s390_libc_ifunc_expr macro.
[glibc.git] / string / test-string.h
blob3f728764542466af5c4c0e32903b1933ec6fd38b
1 /* Test and measure string and memory functions.
2 Copyright (C) 1999-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <sys/cdefs.h>
22 typedef struct
24 const char *name;
25 void (*fn) (void);
26 long test;
27 } impl_t;
28 extern impl_t __start_impls[], __stop_impls[];
30 #define IMPL(name, test) \
31 impl_t tst_ ## name \
32 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
33 = { __STRING (name), (void (*) (void))name, test };
35 #ifdef TEST_MAIN
37 #ifndef _GNU_SOURCE
38 #define _GNU_SOURCE
39 #endif
41 #undef __USE_STRING_INLINES
43 #include <getopt.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/mman.h>
48 #include <sys/param.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <error.h>
52 #include <errno.h>
53 #include <time.h>
54 #include <ifunc-impl-list.h>
55 #define GL(x) _##x
56 #define GLRO(x) _##x
59 # define TEST_FUNCTION test_main
60 # define TIMEOUT (4 * 60)
61 # define OPT_ITERATIONS 10000
62 # define OPT_RANDOM 10001
63 # define OPT_SEED 10002
65 unsigned char *buf1, *buf2;
66 int ret, do_srandom;
67 unsigned int seed;
68 size_t page_size;
70 # ifndef ITERATIONS
71 size_t iterations = 100000;
72 # define ITERATIONS_OPTIONS \
73 { "iterations", required_argument, NULL, OPT_ITERATIONS },
74 # define ITERATIONS_PROCESS \
75 case OPT_ITERATIONS: \
76 iterations = strtoul (optarg, NULL, 0); \
77 break;
78 # define ITERATIONS iterations
79 # else
80 # define ITERATIONS_OPTIONS
81 # define ITERATIONS_PROCESS
82 # endif
84 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
85 { "random", no_argument, NULL, OPT_RANDOM }, \
86 { "seed", required_argument, NULL, OPT_SEED },
88 static void __attribute__ ((used))
89 cmdline_process_function (int c)
91 switch (c)
93 ITERATIONS_PROCESS
94 case OPT_RANDOM:
96 int fdr = open ("/dev/urandom", O_RDONLY);
97 if (fdr < 0 || read (fdr, &seed, sizeof (seed)) != sizeof (seed))
98 seed = time (NULL);
99 if (fdr >= 0)
100 close (fdr);
101 do_srandom = 1;
102 break;
105 case OPT_SEED:
106 seed = strtoul (optarg, NULL, 0);
107 do_srandom = 1;
108 break;
111 # define CMDLINE_PROCESS cmdline_process_function
113 #define CALL(impl, ...) \
114 (* (proto_t) (impl)->fn) (__VA_ARGS__)
116 #ifdef TEST_NAME
117 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
118 static struct libc_ifunc_impl func_list[32];
119 static int func_count;
120 static int impl_count = -1;
121 static impl_t *impl_array;
123 # define FOR_EACH_IMPL(impl, notall) \
124 impl_t *impl; \
125 int count; \
126 if (impl_count == -1) \
128 impl_count = 0; \
129 if (func_count != 0) \
131 int f; \
132 impl_t *skip = NULL, *a; \
133 for (impl = __start_impls; impl < __stop_impls; ++impl) \
134 if (strcmp (impl->name, TEST_NAME) == 0) \
135 skip = impl; \
136 else \
137 impl_count++; \
138 a = impl_array = malloc ((impl_count + func_count) * \
139 sizeof (impl_t)); \
140 for (impl = __start_impls; impl < __stop_impls; ++impl) \
141 if (impl != skip) \
142 *a++ = *impl; \
143 for (f = 0; f < func_count; f++) \
144 if (func_list[f].usable) \
146 a->name = func_list[f].name; \
147 a->fn = func_list[f].fn; \
148 a->test = 1; \
149 a++; \
151 impl_count = a - impl_array; \
153 else \
155 impl_count = __stop_impls - __start_impls; \
156 impl_array = __start_impls; \
159 impl = impl_array; \
160 for (count = 0; count < impl_count; ++count, ++impl) \
161 if (!notall || impl->test)
162 #else
163 # define FOR_EACH_IMPL(impl, notall) \
164 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
165 if (!notall || impl->test)
166 #endif
168 #ifndef BUF1PAGES
169 # define BUF1PAGES 1
170 #endif
172 static void
173 test_init (void)
175 #ifdef TEST_NAME
176 func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
177 (sizeof func_list
178 / sizeof func_list[0]));
179 #endif
181 page_size = 2 * getpagesize ();
182 #ifdef MIN_PAGE_SIZE
183 if (page_size < MIN_PAGE_SIZE)
184 page_size = MIN_PAGE_SIZE;
185 #endif
186 buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
187 MAP_PRIVATE | MAP_ANON, -1, 0);
188 if (buf1 == MAP_FAILED)
189 error (EXIT_FAILURE, errno, "mmap failed");
190 if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE))
191 error (EXIT_FAILURE, errno, "mprotect failed");
192 buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
193 MAP_PRIVATE | MAP_ANON, -1, 0);
194 if (buf2 == MAP_FAILED)
195 error (EXIT_FAILURE, errno, "mmap failed");
196 if (mprotect (buf2 + page_size, page_size, PROT_NONE))
197 error (EXIT_FAILURE, errno, "mprotect failed");
198 if (do_srandom)
200 printf ("Setting seed to 0x%x\n", seed);
201 srandom (seed);
204 memset (buf1, 0xa5, BUF1PAGES * page_size);
205 memset (buf2, 0x5a, page_size);
208 #endif