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