xref: /netbsd-src/external/gpl3/binutils/dist/libiberty/stack-limit.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1883529b6Schristos /* Increase stack size limit if possible.
2*cb63e24eSchristos    Copyright (C) 2011-2024 Free Software Foundation, Inc.
3883529b6Schristos 
4883529b6Schristos This file is part of the libiberty library.  This library is free
5883529b6Schristos software; you can redistribute it and/or modify it under the
6883529b6Schristos terms of the GNU General Public License as published by the
7883529b6Schristos Free Software Foundation; either version 2, or (at your option)
8883529b6Schristos any later version.
9883529b6Schristos 
10883529b6Schristos This library is distributed in the hope that it will be useful,
11883529b6Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12883529b6Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13883529b6Schristos GNU General Public License for more details.
14883529b6Schristos 
15883529b6Schristos You should have received a copy of the GNU General Public License
16883529b6Schristos along with GNU CC; see the file COPYING.  If not, write to
17883529b6Schristos the Free Software Foundation, 51 Franklin Street - Fifth Floor,
18883529b6Schristos Boston, MA 02110-1301, USA.
19883529b6Schristos 
20883529b6Schristos As a special exception, if you link this library with files
21883529b6Schristos compiled with a GNU compiler to produce an executable, this does not cause
22883529b6Schristos the resulting executable to be covered by the GNU General Public License.
23883529b6Schristos This exception does not however invalidate any other reasons why
24883529b6Schristos the executable file might be covered by the GNU General Public License. */
25883529b6Schristos 
26883529b6Schristos /*
27883529b6Schristos 
28883529b6Schristos @deftypefn Extension void stack_limit_increase (unsigned long @var{pref})
29883529b6Schristos 
30883529b6Schristos Attempt to increase stack size limit to @var{pref} bytes if possible.
31883529b6Schristos 
32883529b6Schristos @end deftypefn
33883529b6Schristos 
34883529b6Schristos */
35883529b6Schristos 
36883529b6Schristos #include "config.h"
37883529b6Schristos #include "ansidecl.h"
38883529b6Schristos 
39883529b6Schristos #ifdef HAVE_STDINT_H
40883529b6Schristos #include <stdint.h>
41883529b6Schristos #endif
42883529b6Schristos #ifdef HAVE_SYS_RESOURCE_H
43883529b6Schristos #include <sys/resource.h>
44883529b6Schristos #endif
45883529b6Schristos 
46883529b6Schristos void
stack_limit_increase(unsigned long pref ATTRIBUTE_UNUSED)47883529b6Schristos stack_limit_increase (unsigned long pref ATTRIBUTE_UNUSED)
48883529b6Schristos {
49883529b6Schristos #if defined(HAVE_SETRLIMIT) && defined(HAVE_GETRLIMIT) \
50883529b6Schristos     && defined(RLIMIT_STACK) && defined(RLIM_INFINITY)
51883529b6Schristos   struct rlimit rlim;
52883529b6Schristos   if (getrlimit (RLIMIT_STACK, &rlim) == 0
53883529b6Schristos       && rlim.rlim_cur != RLIM_INFINITY
54883529b6Schristos       && rlim.rlim_cur < pref
55883529b6Schristos       && (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_cur < rlim.rlim_max))
56883529b6Schristos     {
57883529b6Schristos       rlim.rlim_cur = pref;
58883529b6Schristos       if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_cur > rlim.rlim_max)
59883529b6Schristos 	rlim.rlim_cur = rlim.rlim_max;
60883529b6Schristos       setrlimit (RLIMIT_STACK, &rlim);
61883529b6Schristos     }
62883529b6Schristos #endif
63883529b6Schristos }
64