3 // Utility subroutines for the C++ library testsuite.
5 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
6 // Free Software Foundation, Inc.
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
24 #include <testsuite_hooks.h>
26 #ifdef _GLIBCXX_RES_LIMITS
29 #include <sys/resource.h>
40 // If we have <sys/types.h>, <sys/ipc.h>, and <sys/sem.h>, then assume
41 // that System V semaphores are available.
42 #if defined(_GLIBCXX_HAVE_SYS_TYPES_H) \
43 && defined(_GLIBCXX_HAVE_SYS_IPC_H) \
44 && defined(_GLIBCXX_HAVE_SYS_SEM_H)
45 #define _GLIBCXX_SYSV_SEM
48 #ifdef _GLIBCXX_SYSV_SEM
49 #include <sys/types.h>
56 #ifdef _GLIBCXX_RES_LIMITS
58 set_memory_limits(float size
)
61 // Cater to the absence of rlim_t.
62 __typeof__ (r
.rlim_cur
) limit
= (__typeof__ (r
.rlim_cur
))(size
* 1048576);
64 // Heap size, seems to be common.
65 #if _GLIBCXX_HAVE_LIMIT_DATA
66 getrlimit(RLIMIT_DATA
, &r
);
68 setrlimit(RLIMIT_DATA
, &r
);
72 #if _GLIBCXX_HAVE_LIMIT_RSS
73 getrlimit(RLIMIT_RSS
, &r
);
75 setrlimit(RLIMIT_RSS
, &r
);
78 // Mapped memory (brk + mmap).
79 #if _GLIBCXX_HAVE_LIMIT_VMEM
80 getrlimit(RLIMIT_VMEM
, &r
);
82 setrlimit(RLIMIT_VMEM
, &r
);
85 // Virtual memory. On x86_64-linux, the default is -z
86 // max-page-size=0x200000 which means up to 2MB of address space
87 // are accounted for PROT_NONE mappings between text and data
88 // segments of each shared library. There are 4 shared libs
89 // involved in addition to the dynamic linker, maybe 5 if libgomp
90 // is being used as well. Use at least 20MB address space limit.
91 #if defined(__x86_64__) && defined(__linux__)
96 // On HP-UX 11.23, a trivial C++ program that sets RLIMIT_AS to
97 // anything less than 128MB cannot "malloc" even 1K of memory.
98 // Therefore, we skip RLIMIT_AS on HP-UX.
99 #if _GLIBCXX_HAVE_LIMIT_AS && !defined(__hpux__)
100 getrlimit(RLIMIT_AS
, &r
);
102 setrlimit(RLIMIT_AS
, &r
);
108 set_memory_limits(float) { }
111 #ifdef _GLIBCXX_RES_LIMITS
113 set_file_limit(unsigned long size
)
115 #if _GLIBCXX_HAVE_LIMIT_FSIZE
117 // Cater to the absence of rlim_t.
118 __typeof__ (r
.rlim_cur
) limit
= (__typeof__ (r
.rlim_cur
))(size
);
120 getrlimit(RLIMIT_FSIZE
, &r
);
122 setrlimit(RLIMIT_FSIZE
, &r
);
128 set_file_limit(unsigned long) { }
132 verify_demangle(const char* mangled
, const char* wanted
)
135 const char* s
= abi::__cxa_demangle(mangled
, 0, 0, &status
);
141 s
= "error code = 0: success";
144 s
= "error code = -1: memory allocation failure";
147 s
= "error code = -2: invalid mangled name";
150 s
= "error code = -3: invalid arguments";
153 s
= "error code unknown - who knows what happened";
157 std::string
w(wanted
);
159 std::__throw_runtime_error(s
);
163 run_tests_wrapped_locale(const char* name
, const func_callback
& l
)
167 // Set the global locale.
168 locale loc_name
= locale(name
);
169 locale orig
= locale::global(loc_name
);
171 const char* res
= setlocale(LC_ALL
, name
);
174 string preLC_ALL
= res
;
175 const func_callback::test_type
* tests
= l
.tests();
176 for (int i
= 0; i
< l
.size(); ++i
)
178 string postLC_ALL
= setlocale(LC_ALL
, 0);
179 VERIFY( preLC_ALL
== postLC_ALL
);
183 string
s("LC_ALL for ");
185 __throw_runtime_error(s
.c_str());
190 run_tests_wrapped_env(const char* name
, const char* env
,
191 const func_callback
& l
)
195 #ifdef _GLIBCXX_HAVE_SETENV
196 // Set the global locale.
197 locale loc_name
= locale(name
);
198 locale orig
= locale::global(loc_name
);
200 // Set environment variable env to value in name.
201 const char* oldENV
= getenv(env
);
202 if (!setenv(env
, name
, 1))
204 const func_callback::test_type
* tests
= l
.tests();
205 for (int i
= 0; i
< l
.size(); ++i
)
207 setenv(env
, oldENV
? oldENV
: "", 1);
214 __throw_runtime_error(s
.c_str());
219 object_counter::size_type
object_counter::count
= 0;
220 unsigned int copy_constructor::count_
= 0;
221 unsigned int copy_constructor::throw_on_
= 0;
222 unsigned int assignment_operator::count_
= 0;
223 unsigned int assignment_operator::throw_on_
= 0;
224 unsigned int destructor::_M_count
= 0;
225 int copy_tracker::next_id_
= 0;
227 #ifdef _GLIBCXX_SYSV_SEM
228 // This union is not declared in system headers. Instead, it must
229 // be defined by user programs.
233 struct semid_ds
*buf
;
234 unsigned short *array
;
238 semaphore::semaphore()
240 #ifdef _GLIBCXX_SYSV_SEM
241 // Remeber the PID for the process that created the semaphore set
242 // so that only one process will destroy the set.
245 // GLIBC does not define SEM_R and SEM_A.
254 // Get a semaphore set with one semaphore.
255 sem_set_
= semget(IPC_PRIVATE
, 1, SEM_R
| SEM_A
);
257 std::__throw_runtime_error("could not obtain semaphore set");
259 // Initialize the semaphore.
262 if (semctl(sem_set_
, 0, SETVAL
, val
) == -1)
263 std::__throw_runtime_error("could not initialize semaphore");
265 // There are no semaphores on this system. We have no way to mark
266 // a test as "unsupported" at runtime, so we just exit, pretending
267 // that the test passed.
272 semaphore::~semaphore()
274 #ifdef _GLIBCXX_SYSV_SEM
276 val
.val
= 0; // Avoid uninitialized variable warning.
277 // Destroy the semaphore set only in the process that created it.
278 if (pid_
== getpid())
279 semctl(sem_set_
, 0, IPC_RMID
, val
);
286 #ifdef _GLIBCXX_SYSV_SEM
287 struct sembuf op
[1] =
291 if (semop(sem_set_
, op
, 1) == -1)
292 std::__throw_runtime_error("could not signal semaphore");
299 #ifdef _GLIBCXX_SYSV_SEM
300 struct sembuf op
[1] =
304 if (semop(sem_set_
, op
, 1) == -1)
305 std::__throw_runtime_error("could not wait for semaphore");
309 // For use in 22_locale/time_get and time_put.
311 test_tm(int sec
, int min
, int hour
, int mday
, int mon
,
312 int year
, int wday
, int yday
, int isdst
)
323 tmp
.tm_isdst
= isdst
;
326 } // namespace __gnu_test