libcopp  1.1.0
stack_traits_posix.cpp
Go to the documentation of this file.
1 #if !defined(_MSC_VER)
2 
4 
5 extern "C" {
6 #include <signal.h>
7 #include <sys/resource.h>
8 #include <sys/time.h>
9 #include <unistd.h>
10 #include <assert.h>
11 }
12 
13 //#if _POSIX_C_SOURCE >= 200112L
14 
15 #include <algorithm>
16 #include <cmath>
17 #include <limits>
18 
19 #if !defined (SIGSTKSZ)
20 # define SIGSTKSZ (8 * 1024)
21 # define UDEF_SIGSTKSZ
22 #endif
23 
24 #ifdef COPP_HAS_ABI_HEADERS
25 # include COPP_ABI_PREFIX
26 #endif
27 
28 
29 namespace copp {
30 
31  namespace detail {
32  static std::size_t pagesize() {
33  std::size_t size = ::sysconf(_SC_PAGESIZE);
34  return size;
35  }
36 
37  static rlimit stacksize_limit_() {
38  rlimit limit;
39  // conforming to POSIX.1-2001
40  ::getrlimit( RLIMIT_STACK, & limit);
41  return limit;
42  }
43 
44  static rlimit stacksize_limit() {
45  static rlimit limit = stacksize_limit_();
46  return limit;
47  }
48  }
49 
51  return RLIM_INFINITY == detail::stacksize_limit().rlim_max;
52  }
53 
55  return detail::pagesize();
56  }
57 
59  std::size_t size = 8 * minimum_size(); // 64 KB
60  if (is_unbounded())
61  return size;
62 
63  assert(maximum_size() >= minimum_size());
64  return maximum_size() == size
65  ? size
66  : (std::min)(size, maximum_size());
67  }
68 
70  return SIGSTKSZ;
71  }
72 
74  if(is_unbounded())
75  return std::numeric_limits<std::size_t>::max();
76  return detail::stacksize_limit().rlim_max;
77  }
78 
79  std::size_t stack_traits::round_to_page_size(std::size_t stacksize) COPP_MACRO_NOEXCEPT {
80  // page size must be 2^N
81  return static_cast<std::size_t>((stacksize + detail::pagesize() - 1) & (~(detail::pagesize() - 1)));
82  }
83 }
84 
85 #ifdef COPP_HAS_ABI_HEADERS
86 # include COPP_ABI_SUFFIX
87 #endif
88 
89 #ifdef UDEF_SIGSTKSZ
90 # undef SIGSTKSZ
91 #endif
92 
93 #endif
static std::size_t default_size() COPP_MACRO_NOEXCEPT
static std::size_t page_size() COPP_MACRO_NOEXCEPT
#define SIGSTKSZ
static bool is_unbounded() COPP_MACRO_NOEXCEPT
#define COPP_MACRO_NOEXCEPT
Definition: features.h:71
static std::size_t pagesize()
static rlimit stacksize_limit()
static std::size_t maximum_size() COPP_MACRO_NOEXCEPT
static std::size_t minimum_size() COPP_MACRO_NOEXCEPT
static std::size_t round_to_page_size(std::size_t stacksize) COPP_MACRO_NOEXCEPT
static rlimit stacksize_limit_()