Add sysdeps/init_array to produce empty crt[in].o and use .preinit_array for gcrt1.o
[glibc.git] / string / test-string.h
blob47659d080472c476034f6986eab1d330cd353821
1 /* Test and measure string and memory functions.
2 Copyright (C) 1999-2013 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 <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sys/mman.h>
47 #include <sys/param.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <error.h>
51 #include <errno.h>
52 #include <time.h>
53 #include <ifunc-impl-list.h>
54 #define GL(x) _##x
55 #define GLRO(x) _##x
56 #include <hp-timing.h>
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 hp_timing_t _dl_hp_timing_overhead;
72 # ifndef ITERATIONS
73 size_t iterations = 100000;
74 # define ITERATIONS_OPTIONS \
75 { "iterations", required_argument, NULL, OPT_ITERATIONS },
76 # define ITERATIONS_PROCESS \
77 case OPT_ITERATIONS: \
78 iterations = strtoul (optarg, NULL, 0); \
79 break;
80 # define ITERATIONS iterations
81 # else
82 # define ITERATIONS_OPTIONS
83 # define ITERATIONS_PROCESS
84 # endif
86 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
87 { "random", no_argument, NULL, OPT_RANDOM }, \
88 { "seed", required_argument, NULL, OPT_SEED },
89 # define CMDLINE_PROCESS ITERATIONS_PROCESS \
90 case OPT_RANDOM: \
91 { \
92 int fdr = open ("/dev/urandom", O_RDONLY); \
94 if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed)) \
95 seed = time (NULL); \
96 if (fdr >= 0) \
97 close (fdr); \
98 do_srandom = 1; \
99 break; \
102 case OPT_SEED: \
103 seed = strtoul (optarg, NULL, 0); \
104 do_srandom = 1; \
105 break;
107 #define CALL(impl, ...) \
108 (* (proto_t) (impl)->fn) (__VA_ARGS__)
110 #if defined TEST_IFUNC && defined TEST_NAME
111 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
112 static struct libc_ifunc_impl func_list[32];
113 static int func_count;
114 static int impl_count = -1;
115 static impl_t *impl_array;
117 # define FOR_EACH_IMPL(impl, notall) \
118 impl_t *impl; \
119 int count; \
120 if (impl_count == -1) \
122 impl_count = 0; \
123 if (func_count != 0) \
125 int f; \
126 impl_t *skip = NULL, *a; \
127 for (impl = __start_impls; impl < __stop_impls; ++impl) \
128 if (strcmp (impl->name, TEST_NAME) == 0) \
129 skip = impl; \
130 else \
131 impl_count++; \
132 a = impl_array = malloc ((impl_count + func_count) * \
133 sizeof (impl_t)); \
134 for (impl = __start_impls; impl < __stop_impls; ++impl) \
135 if (impl != skip) \
136 *a++ = *impl; \
137 for (f = 0; f < func_count; f++) \
138 if (func_list[f].usable) \
140 a->name = func_list[f].name; \
141 a->fn = func_list[f].fn; \
142 a->test = 1; \
143 a++; \
145 impl_count = a - impl_array; \
147 else \
149 impl_count = __stop_impls - __start_impls; \
150 impl_array = __start_impls; \
153 impl = impl_array; \
154 for (count = 0; count < impl_count; ++count, ++impl) \
155 if (!notall || impl->test)
156 #else
157 # define FOR_EACH_IMPL(impl, notall) \
158 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
159 if (!notall || impl->test)
160 #endif
162 #define HP_TIMING_BEST(best_time, start, end) \
163 do \
165 hp_timing_t tmptime; \
166 HP_TIMING_DIFF (tmptime, start + _dl_hp_timing_overhead, end); \
167 if (best_time > tmptime) \
168 best_time = tmptime; \
170 while (0)
172 #ifndef BUF1PAGES
173 # define BUF1PAGES 1
174 #endif
176 static void
177 test_init (void)
179 #if defined TEST_IFUNC && defined TEST_NAME
180 func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
181 (sizeof func_list
182 / sizeof func_list[0]));
183 #endif
185 page_size = 2 * getpagesize ();
186 #ifdef MIN_PAGE_SIZE
187 if (page_size < MIN_PAGE_SIZE)
188 page_size = MIN_PAGE_SIZE;
189 #endif
190 buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
191 MAP_PRIVATE | MAP_ANON, -1, 0);
192 if (buf1 == MAP_FAILED)
193 error (EXIT_FAILURE, errno, "mmap failed");
194 if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE))
195 error (EXIT_FAILURE, errno, "mprotect failed");
196 buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
197 MAP_PRIVATE | MAP_ANON, -1, 0);
198 if (buf2 == MAP_FAILED)
199 error (EXIT_FAILURE, errno, "mmap failed");
200 if (mprotect (buf2 + page_size, page_size, PROT_NONE))
201 error (EXIT_FAILURE, errno, "mprotect failed");
202 HP_TIMING_DIFF_INIT ();
203 if (do_srandom)
205 printf ("Setting seed to 0x%x\n", seed);
206 srandom (seed);
209 memset (buf1, 0xa5, BUF1PAGES * page_size);
210 memset (buf2, 0x5a, page_size);
213 #endif