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