]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/util/testsuite_hooks.cc
3 // Utility subroutines for the C++ library testsuite.
5 // Copyright (C) 2002-2016 Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
23 #include <testsuite_hooks.h>
25 #ifdef _GLIBCXX_RES_LIMITS
28 #include <sys/resource.h>
39 // If we have <sys/types.h>, <sys/ipc.h>, and <sys/sem.h>, then assume
40 // that System V semaphores are available.
41 #if defined(_GLIBCXX_HAVE_SYS_TYPES_H) \
42 && defined(_GLIBCXX_HAVE_SYS_IPC_H) \
43 && defined(_GLIBCXX_HAVE_SYS_SEM_H)
44 #define _GLIBCXX_SYSV_SEM
47 #ifdef _GLIBCXX_SYSV_SEM
48 #include <sys/types.h>
55 #ifdef _GLIBCXX_RES_LIMITS
57 set_memory_limits(float size
)
60 // Cater to the absence of rlim_t.
61 __typeof__ (r
.rlim_cur
) limit
= (__typeof__ (r
.rlim_cur
))(size
* 1048576);
63 // Heap size, seems to be common.
64 #if _GLIBCXX_HAVE_LIMIT_DATA
65 getrlimit(RLIMIT_DATA
, &r
);
67 setrlimit(RLIMIT_DATA
, &r
);
71 #if _GLIBCXX_HAVE_LIMIT_RSS
72 getrlimit(RLIMIT_RSS
, &r
);
74 setrlimit(RLIMIT_RSS
, &r
);
77 // Mapped memory (brk + mmap).
78 #if _GLIBCXX_HAVE_LIMIT_VMEM
79 getrlimit(RLIMIT_VMEM
, &r
);
81 setrlimit(RLIMIT_VMEM
, &r
);
84 // Virtual memory. On x86_64-linux, the default is -z
85 // max-page-size=0x200000 which means up to 2MB of address space
86 // are accounted for PROT_NONE mappings between text and data
87 // segments of each shared library. There are 4 shared libs
88 // involved in addition to the dynamic linker, maybe 5 if libgomp
89 // is being used as well. Use at least 20MB address space limit.
90 #if defined(__x86_64__) && defined(__linux__)
95 // On HP-UX 11.23, a trivial C++ program that sets RLIMIT_AS to
96 // anything less than 128MB cannot "malloc" even 1K of memory.
97 // Therefore, we skip RLIMIT_AS on HP-UX.
98 #if _GLIBCXX_HAVE_LIMIT_AS && !defined(__hpux__)
99 getrlimit(RLIMIT_AS
, &r
);
101 setrlimit(RLIMIT_AS
, &r
);
107 set_memory_limits(float) { }
110 #ifdef _GLIBCXX_RES_LIMITS
112 set_file_limit(unsigned long size
)
114 #if _GLIBCXX_HAVE_LIMIT_FSIZE
116 // Cater to the absence of rlim_t.
117 __typeof__ (r
.rlim_cur
) limit
= (__typeof__ (r
.rlim_cur
))(size
);
119 getrlimit(RLIMIT_FSIZE
, &r
);
121 setrlimit(RLIMIT_FSIZE
, &r
);
127 set_file_limit(unsigned long) { }
131 verify_demangle(const char* mangled
, const char* wanted
)
134 const char* s
= abi::__cxa_demangle(mangled
, 0, 0, &status
);
140 s
= "error code = 0: success";
143 s
= "error code = -1: memory allocation failure";
146 s
= "error code = -2: invalid mangled name";
149 s
= "error code = -3: invalid arguments";
152 s
= "error code unknown - who knows what happened";
156 std::string
w(wanted
);
158 std::__throw_runtime_error(s
);
162 run_tests_wrapped_locale(const char* name
, const func_callback
& l
)
166 // Set the global locale.
167 locale loc_name
= locale(name
);
168 locale orig
= locale::global(loc_name
);
170 const char* res
= setlocale(LC_ALL
, name
);
173 string preLC_ALL
= res
;
174 const func_callback::test_type
* tests
= l
.tests();
175 for (int i
= 0; i
< l
.size(); ++i
)
177 string postLC_ALL
= setlocale(LC_ALL
, 0);
178 VERIFY( preLC_ALL
== postLC_ALL
);
182 string
s("LC_ALL for ");
184 __throw_runtime_error(s
.c_str());
189 run_tests_wrapped_env(const char* name
, const char* env
,
190 const func_callback
& l
)
194 #ifdef _GLIBCXX_HAVE_SETENV
195 // Set the global locale.
196 locale loc_name
= locale(name
);
197 locale orig
= locale::global(loc_name
);
199 // Set environment variable env to value in name.
200 const char* oldENV
= getenv(env
);
201 if (!setenv(env
, name
, 1))
203 const func_callback::test_type
* tests
= l
.tests();
204 for (int i
= 0; i
< l
.size(); ++i
)
206 setenv(env
, oldENV
? oldENV
: "", 1);
213 __throw_runtime_error(s
.c_str());
218 object_counter::size_type
object_counter::count
= 0;
219 unsigned int copy_constructor::count_
= 0;
220 unsigned int copy_constructor::throw_on_
= 0;
221 unsigned int assignment_operator::count_
= 0;
222 unsigned int assignment_operator::throw_on_
= 0;
223 unsigned int destructor::_M_count
= 0;
224 int copy_tracker::next_id_
= 0;
226 #ifdef _GLIBCXX_SYSV_SEM
227 // This union is not declared in system headers. Instead, it must
228 // be defined by user programs.
232 struct semid_ds
*buf
;
233 unsigned short *array
;
237 semaphore::semaphore()
239 #ifdef _GLIBCXX_SYSV_SEM
240 // Remember the PID for the process that created the semaphore set
241 // so that only one process will destroy the set.
244 // GLIBC does not define SEM_R and SEM_A.
253 // Get a semaphore set with one semaphore.
254 sem_set_
= semget(IPC_PRIVATE
, 1, SEM_R
| SEM_A
);
256 std::__throw_runtime_error("could not obtain semaphore set");
258 // Initialize the semaphore.
261 if (semctl(sem_set_
, 0, SETVAL
, val
) == -1)
262 std::__throw_runtime_error("could not initialize semaphore");
264 // There are no semaphores on this system. We have no way to mark
265 // a test as "unsupported" at runtime, so we just exit, pretending
266 // that the test passed.
271 semaphore::~semaphore()
273 #ifdef _GLIBCXX_SYSV_SEM
275 val
.val
= 0; // Avoid uninitialized variable warning.
276 // Destroy the semaphore set only in the process that created it.
277 if (pid_
== getpid())
278 semctl(sem_set_
, 0, IPC_RMID
, val
);
285 #ifdef _GLIBCXX_SYSV_SEM
286 struct sembuf op
[1] =
290 if (semop(sem_set_
, op
, 1) == -1)
291 std::__throw_runtime_error("could not signal semaphore");
298 #ifdef _GLIBCXX_SYSV_SEM
299 struct sembuf op
[1] =
303 if (semop(sem_set_
, op
, 1) == -1)
304 std::__throw_runtime_error("could not wait for semaphore");
308 // For use in 22_locale/time_get and time_put.
310 test_tm(int sec
, int min
, int hour
, int mday
, int mon
,
311 int year
, int wday
, int yday
, int isdst
)
322 tmp
.tm_isdst
= isdst
;
325 } // namespace __gnu_test