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