blob: 096879f895b17aee3b86b0b944f8caadd699e65c [file] [log] [blame]
H. Peter Anvin (Intel)5b4de522020-06-01 13:10:46 -07001 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2020 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34#include "compiler.h"
35#include "nasmlib.h"
36
37#ifdef HAVE_SYS_RESOURCE_H
38# include <sys/resource.h>
39#endif
40
41#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_STACK)
42
43size_t nasm_get_stack_size_limit(void)
44{
45 struct rlimit rl;
46
47 if (getrlimit(RLIMIT_STACK, &rl))
48 return SIZE_MAX;
49
50# ifdef RLIM_SAVED_MAX
51 if (rl.rlim_cur == RLIM_SAVED_MAX)
52 rl.rlim_cur = rl.rlim_max;
53# endif
54
55 if (
56# ifdef RLIM_INFINITY
57 rl.rlim_cur >= RLIM_INFINITY ||
58# endif
59# ifdef RLIM_SAVED_CUR
60 rl.rlim_cur == RLIM_SAVED_CUR ||
61# endif
62# ifdef RLIM_SAVED_MAX
63 rl.rlim_cur == RLIM_SAVED_MAX ||
64# endif
65 (size_t)rl.rlim_cur != rl.rlim_cur)
66 return SIZE_MAX;
67
68 return rl.rlim_cur;
69}
70
71#else
72
73size_t nasm_get_stack_size_limit(void)
74{
75 return SIZE_MAX;
76}
77
78#endif