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