stack-limit.c 2.0 KB

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