* elf32-spu.c (build_stub): Fix malloc under-allocation.
[binutils.git] / libiberty / stack-limit.c
blobe64cac28d1df23c5285cc579610ee5b089f54d8c
1 /* Increase stack size limit if possible.
2 Copyright (C) 2011 Free Software Foundation, Inc.
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.
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.
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.
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. */
28 @deftypefn Extension void stack_limit_increase (unsigned long @var{pref})
30 Attempt to increase stack size limit to @var{pref} bytes if possible.
32 @end deftypefn
36 #include "config.h"
38 #ifdef HAVE_STDINT_H
39 #include <stdint.h>
40 #endif
41 #ifdef HAVE_SYS_RESOURCE_H
42 #include <sys/resource.h>
43 #endif
45 void
46 stack_limit_increase (unsigned long pref)
48 #if defined(HAVE_SETRLIMIT) && defined(HAVE_GETRLIMIT) \
49 && defined(RLIMIT_STACK) && defined(RLIM_INFINITY)
50 struct rlimit rlim;
51 if (getrlimit (RLIMIT_STACK, &rlim) == 0
52 && rlim.rlim_cur != RLIM_INFINITY
53 && rlim.rlim_cur < pref
54 && (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_cur < rlim.rlim_max))
56 rlim.rlim_cur = pref;
57 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_cur > rlim.rlim_max)
58 rlim.rlim_cur = rlim.rlim_max;
59 setrlimit (RLIMIT_STACK, &rlim);
61 #endif