xref: /netbsd-src/external/gpl3/gcc/dist/libiberty/stack-limit.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
148fb7bfaSmrg /* Increase stack size limit if possible.
2*b1e83836Smrg    Copyright (C) 2011-2022 Free Software Foundation, Inc.
348fb7bfaSmrg 
448fb7bfaSmrg This file is part of the libiberty library.  This library is free
548fb7bfaSmrg software; you can redistribute it and/or modify it under the
648fb7bfaSmrg terms of the GNU General Public License as published by the
748fb7bfaSmrg Free Software Foundation; either version 2, or (at your option)
848fb7bfaSmrg any later version.
948fb7bfaSmrg 
1048fb7bfaSmrg This library is distributed in the hope that it will be useful,
1148fb7bfaSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
1248fb7bfaSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1348fb7bfaSmrg GNU General Public License for more details.
1448fb7bfaSmrg 
1548fb7bfaSmrg You should have received a copy of the GNU General Public License
1648fb7bfaSmrg along with GNU CC; see the file COPYING.  If not, write to
1748fb7bfaSmrg the Free Software Foundation, 51 Franklin Street - Fifth Floor,
1848fb7bfaSmrg Boston, MA 02110-1301, USA.
1948fb7bfaSmrg 
2048fb7bfaSmrg As a special exception, if you link this library with files
2148fb7bfaSmrg compiled with a GNU compiler to produce an executable, this does not cause
2248fb7bfaSmrg the resulting executable to be covered by the GNU General Public License.
2348fb7bfaSmrg This exception does not however invalidate any other reasons why
2448fb7bfaSmrg the executable file might be covered by the GNU General Public License. */
2548fb7bfaSmrg 
2648fb7bfaSmrg /*
2748fb7bfaSmrg 
2848fb7bfaSmrg @deftypefn Extension void stack_limit_increase (unsigned long @var{pref})
2948fb7bfaSmrg 
3048fb7bfaSmrg Attempt to increase stack size limit to @var{pref} bytes if possible.
3148fb7bfaSmrg 
3248fb7bfaSmrg @end deftypefn
3348fb7bfaSmrg 
3448fb7bfaSmrg */
3548fb7bfaSmrg 
3648fb7bfaSmrg #include "config.h"
3748fb7bfaSmrg #include "ansidecl.h"
3848fb7bfaSmrg 
3948fb7bfaSmrg #ifdef HAVE_STDINT_H
4048fb7bfaSmrg #include <stdint.h>
4148fb7bfaSmrg #endif
4248fb7bfaSmrg #ifdef HAVE_SYS_RESOURCE_H
4348fb7bfaSmrg #include <sys/resource.h>
4448fb7bfaSmrg #endif
4548fb7bfaSmrg 
4648fb7bfaSmrg void
stack_limit_increase(unsigned long pref ATTRIBUTE_UNUSED)4748fb7bfaSmrg stack_limit_increase (unsigned long pref ATTRIBUTE_UNUSED)
4848fb7bfaSmrg {
4948fb7bfaSmrg #if defined(HAVE_SETRLIMIT) && defined(HAVE_GETRLIMIT) \
5048fb7bfaSmrg     && defined(RLIMIT_STACK) && defined(RLIM_INFINITY)
5148fb7bfaSmrg   struct rlimit rlim;
5248fb7bfaSmrg   if (getrlimit (RLIMIT_STACK, &rlim) == 0
5348fb7bfaSmrg       && rlim.rlim_cur != RLIM_INFINITY
5448fb7bfaSmrg       && rlim.rlim_cur < pref
5548fb7bfaSmrg       && (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_cur < rlim.rlim_max))
5648fb7bfaSmrg     {
5748fb7bfaSmrg       rlim.rlim_cur = pref;
5848fb7bfaSmrg       if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_cur > rlim.rlim_max)
5948fb7bfaSmrg 	rlim.rlim_cur = rlim.rlim_max;
6048fb7bfaSmrg       setrlimit (RLIMIT_STACK, &rlim);
6148fb7bfaSmrg     }
6248fb7bfaSmrg #endif
6348fb7bfaSmrg }
64