libcopp  1.1.0
stack_traits_windows.cpp
Go to the documentation of this file.
1 extern "C" {
2 #include <windows.h>
3 #include <assert.h>
4 }
5 
7 
8 # if defined(COPP_MACRO_COMPILER_MSVC)
9 # pragma warning(push)
10 # pragma warning(disable:4244 4267)
11 # endif
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <cstddef>
16 #include <cstring>
17 #include <stdexcept>
18 
19 #if __cplusplus >= 201103L
20 #include <thread>
21 #include <mutex>
22 #endif
23 
25 
26 // x86_64
27 // test x86_64 before i386 because icc might
28 // define __i686__ for x86_64 too
29 #if defined(__x86_64__) || defined(__x86_64) \
30  || defined(__amd64__) || defined(__amd64) \
31  || defined(_M_X64) || defined(_M_AMD64)
32 
33 // Windows seams not to provide a constant or function
34 // telling the minimal stacksize
35 # define MIN_STACKSIZE 8 * 1024
36 #else
37 # define MIN_STACKSIZE 4 * 1024
38 #endif
39 
40 #ifdef COPP_HAS_ABI_HEADERS
41 # include COPP_ABI_PREFIX
42 #endif
43 
44 namespace copp {
45 
46  namespace detail {
47 #if __cplusplus < 201103L
48  static void system_info_( SYSTEM_INFO * si) { ::GetSystemInfo( si); }
49 
50  static SYSTEM_INFO system_info() {
51  static SYSTEM_INFO si;
52  static bool inited = false;
53  if (inited) {
54  return si;
55  }
56 
57  system_info_(&si);
58  inited = true;
59  return si;
60  }
61 #else
62 
63  static SYSTEM_INFO system_info() {
64  static SYSTEM_INFO si;
65  static std::once_flag flag;
66  std::call_once(flag, []() { ::GetSystemInfo(&si); });
67  return si;
68  }
69 
70 #endif
71 
72  static std::size_t pagesize() { return static_cast< std::size_t >( system_info().dwPageSize); }
73 
81  }
82 
83  // Windows seams not to provide a limit for the stacksize
84  // libcoco uses 32k+4k bytes as minimum
86  return true;
87  }
88 
90  return detail::pagesize();
91  }
92 
94  std::size_t size = 64 * 1024; // 64 KB
95  if (is_unbounded())
96  return (std::max)(size, minimum_size() );
97 
98  assert(is_unbounded() || maximum_size() >= minimum_size());
99  return is_unbounded() ?
100  size
101  : (std::min)(size, maximum_size());
102  }
103 
104  // because Windows seams not to provide a limit for minimum stacksize
106  return MIN_STACKSIZE;
107  }
108 
109  // because Windows seams not to provide a limit for maximum stacksize
110  // maximum_size() can never be called (pre-condition ! is_unbounded() )
112  assert(is_unbounded());
113  return SIZE_MAX;
114  }
115 
116  std::size_t stack_traits::round_to_page_size(std::size_t stacksize) COPP_MACRO_NOEXCEPT {
117  // page size must be 2^N
118  return static_cast<std::size_t>((stacksize + stack_traits::page_size() - 1) & (~(stack_traits::page_size() - 1)));
119  }
120 }
121 
122 #ifdef COPP_HAS_ABI_HEADERS
123 # include COPP_ABI_SUFFIX
124 #endif
static std::size_t default_size() COPP_MACRO_NOEXCEPT
static std::size_t page_size() COPP_MACRO_NOEXCEPT
static bool is_unbounded() COPP_MACRO_NOEXCEPT
#define COPP_MACRO_NOEXCEPT
Definition: features.h:71
static std::size_t pagesize()
#define MIN_STACKSIZE
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 void system_info_(SYSTEM_INFO *si)
static SYSTEM_INFO system_info()