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