stack-limit.c revision 1.10 1 1.1 christos /* Increase stack size limit if possible.
2 1.10 christos Copyright (C) 2011-2024 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos This file is part of the libiberty library. This library is free
5 1.1 christos software; you can redistribute it and/or modify it under the
6 1.1 christos terms of the GNU General Public License as published by the
7 1.1 christos Free Software Foundation; either version 2, or (at your option)
8 1.1 christos any later version.
9 1.1 christos
10 1.1 christos This library is distributed in the hope that it will be useful,
11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 1.1 christos GNU General Public License for more details.
14 1.1 christos
15 1.1 christos You should have received a copy of the GNU General Public License
16 1.1 christos along with GNU CC; see the file COPYING. If not, write to
17 1.1 christos the Free Software Foundation, 51 Franklin Street - Fifth Floor,
18 1.1 christos Boston, MA 02110-1301, USA.
19 1.1 christos
20 1.1 christos As a special exception, if you link this library with files
21 1.1 christos compiled with a GNU compiler to produce an executable, this does not cause
22 1.1 christos the resulting executable to be covered by the GNU General Public License.
23 1.1 christos This exception does not however invalidate any other reasons why
24 1.1 christos the executable file might be covered by the GNU General Public License. */
25 1.1 christos
26 1.1 christos /*
27 1.1 christos
28 1.1 christos @deftypefn Extension void stack_limit_increase (unsigned long @var{pref})
29 1.1 christos
30 1.1 christos Attempt to increase stack size limit to @var{pref} bytes if possible.
31 1.1 christos
32 1.1 christos @end deftypefn
33 1.1 christos
34 1.1 christos */
35 1.1 christos
36 1.1 christos #include "config.h"
37 1.1 christos #include "ansidecl.h"
38 1.1 christos
39 1.1 christos #ifdef HAVE_STDINT_H
40 1.1 christos #include <stdint.h>
41 1.1 christos #endif
42 1.1 christos #ifdef HAVE_SYS_RESOURCE_H
43 1.1 christos #include <sys/resource.h>
44 1.1 christos #endif
45 1.1 christos
46 1.1 christos void
47 1.1 christos stack_limit_increase (unsigned long pref ATTRIBUTE_UNUSED)
48 1.1 christos {
49 1.1 christos #if defined(HAVE_SETRLIMIT) && defined(HAVE_GETRLIMIT) \
50 1.1 christos && defined(RLIMIT_STACK) && defined(RLIM_INFINITY)
51 1.1 christos struct rlimit rlim;
52 1.1 christos if (getrlimit (RLIMIT_STACK, &rlim) == 0
53 1.1 christos && rlim.rlim_cur != RLIM_INFINITY
54 1.1 christos && rlim.rlim_cur < pref
55 1.1 christos && (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_cur < rlim.rlim_max))
56 1.1 christos {
57 1.1 christos rlim.rlim_cur = pref;
58 1.1 christos if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_cur > rlim.rlim_max)
59 1.1 christos rlim.rlim_cur = rlim.rlim_max;
60 1.1 christos setrlimit (RLIMIT_STACK, &rlim);
61 1.1 christos }
62 1.1 christos #endif
63 1.1 christos }
64