1
2// Copyright Oliver Kowalke 2009.
3// Distributed under the Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#include "boost/coroutine/stack_traits.hpp"
8
9extern "C" {
10#include <signal.h>
11#include <sys/resource.h>
12#include <sys/time.h>
13#include <unistd.h>
14}
15
16//#if _POSIX_C_SOURCE >= 200112L
17
18#include <algorithm>
19#include <cmath>
20
21#include <boost/assert.hpp>
22#include <boost/config.hpp>
23
24#if !defined (SIGSTKSZ)
25# define SIGSTKSZ (8 * 1024)
26# define UDEF_SIGSTKSZ
27#endif
28
29#ifdef BOOST_HAS_ABI_HEADERS
30# include BOOST_ABI_PREFIX
31#endif
32
33namespace {
34
35std::size_t pagesize()
36{
37 // conform to POSIX.1-2001
38 return ::sysconf( _SC_PAGESIZE);
39}
40
41rlim_t stacksize_limit_()
42{
43 rlimit limit;
44 // conforming to POSIX.1-2001
45#if defined(BOOST_DISABLE_ASSERTS) || defined(NDEBUG)
46 ::getrlimit( RLIMIT_STACK, rlimits: & limit);
47#else
48 const int result = ::getrlimit( RLIMIT_STACK, & limit);
49 BOOST_ASSERT( 0 == result);
50#endif
51 return limit.rlim_max;
52}
53
54rlim_t stacksize_limit() BOOST_NOEXCEPT_OR_NOTHROW {
55 static rlim_t limit = stacksize_limit_();
56 return limit;
57}
58
59}
60
61namespace boost {
62namespace coroutines {
63
64bool
65stack_traits::is_unbounded() BOOST_NOEXCEPT
66{ return RLIM_INFINITY == stacksize_limit(); }
67
68std::size_t
69stack_traits::page_size() BOOST_NOEXCEPT
70{
71 static std::size_t size = pagesize();
72 return size;
73}
74
75std::size_t
76stack_traits::default_size() BOOST_NOEXCEPT
77{
78 std::size_t size = 8 * minimum_size();
79 if ( is_unbounded() ) return size;
80
81 BOOST_ASSERT( maximum_size() >= minimum_size() );
82 return maximum_size() == size
83 ? size
84 : (std::min)( a: size, b: maximum_size() );
85}
86
87std::size_t
88stack_traits::minimum_size() BOOST_NOEXCEPT
89{ return SIGSTKSZ; }
90
91std::size_t
92stack_traits::maximum_size() BOOST_NOEXCEPT
93{
94 BOOST_ASSERT( ! is_unbounded() );
95 return static_cast< std::size_t >( stacksize_limit() );
96}
97
98}}
99
100#ifdef BOOST_HAS_ABI_HEADERS
101# include BOOST_ABI_SUFFIX
102#endif
103
104#ifdef UDEF_SIGSTKSZ
105# undef SIGSTKSZ
106#endif
107

source code of boost/libs/coroutine/src/posix/stack_traits.cpp