11debfc3dSmrg /* Increase stack size limit if possible.
2*8feb0f0bSmrg Copyright (C) 2011-2020 Free Software Foundation, Inc.
31debfc3dSmrg
41debfc3dSmrg This file is part of the libiberty library. This library is free
51debfc3dSmrg software; you can redistribute it and/or modify it under the
61debfc3dSmrg terms of the GNU General Public License as published by the
71debfc3dSmrg Free Software Foundation; either version 2, or (at your option)
81debfc3dSmrg any later version.
91debfc3dSmrg
101debfc3dSmrg This library is distributed in the hope that it will be useful,
111debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
121debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131debfc3dSmrg GNU General Public License for more details.
141debfc3dSmrg
151debfc3dSmrg You should have received a copy of the GNU General Public License
161debfc3dSmrg along with GNU CC; see the file COPYING. If not, write to
171debfc3dSmrg the Free Software Foundation, 51 Franklin Street - Fifth Floor,
181debfc3dSmrg Boston, MA 02110-1301, USA.
191debfc3dSmrg
201debfc3dSmrg As a special exception, if you link this library with files
211debfc3dSmrg compiled with a GNU compiler to produce an executable, this does not cause
221debfc3dSmrg the resulting executable to be covered by the GNU General Public License.
231debfc3dSmrg This exception does not however invalidate any other reasons why
241debfc3dSmrg the executable file might be covered by the GNU General Public License. */
251debfc3dSmrg
261debfc3dSmrg /*
271debfc3dSmrg
281debfc3dSmrg @deftypefn Extension void stack_limit_increase (unsigned long @var{pref})
291debfc3dSmrg
301debfc3dSmrg Attempt to increase stack size limit to @var{pref} bytes if possible.
311debfc3dSmrg
321debfc3dSmrg @end deftypefn
331debfc3dSmrg
341debfc3dSmrg */
351debfc3dSmrg
361debfc3dSmrg #include "config.h"
371debfc3dSmrg #include "ansidecl.h"
381debfc3dSmrg
391debfc3dSmrg #ifdef HAVE_STDINT_H
401debfc3dSmrg #include <stdint.h>
411debfc3dSmrg #endif
421debfc3dSmrg #ifdef HAVE_SYS_RESOURCE_H
431debfc3dSmrg #include <sys/resource.h>
441debfc3dSmrg #endif
451debfc3dSmrg
461debfc3dSmrg void
stack_limit_increase(unsigned long pref ATTRIBUTE_UNUSED)471debfc3dSmrg stack_limit_increase (unsigned long pref ATTRIBUTE_UNUSED)
481debfc3dSmrg {
491debfc3dSmrg #if defined(HAVE_SETRLIMIT) && defined(HAVE_GETRLIMIT) \
501debfc3dSmrg && defined(RLIMIT_STACK) && defined(RLIM_INFINITY)
511debfc3dSmrg struct rlimit rlim;
521debfc3dSmrg if (getrlimit (RLIMIT_STACK, &rlim) == 0
531debfc3dSmrg && rlim.rlim_cur != RLIM_INFINITY
541debfc3dSmrg && rlim.rlim_cur < pref
551debfc3dSmrg && (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_cur < rlim.rlim_max))
561debfc3dSmrg {
571debfc3dSmrg rlim.rlim_cur = pref;
581debfc3dSmrg if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_cur > rlim.rlim_max)
591debfc3dSmrg rlim.rlim_cur = rlim.rlim_max;
601debfc3dSmrg setrlimit (RLIMIT_STACK, &rlim);
611debfc3dSmrg }
621debfc3dSmrg #endif
631debfc3dSmrg }
64