stack.qbk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. [/
  2. Copyright Oliver Kowalke 2014.
  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. [#stack]
  8. [section:stack Stack allocation]
  9. The memory used by the stack is allocated/deallocated via a __stack_allocator__
  10. which is required to model a __stack_allocator_concept__.
  11. [heading __stack_allocator_concept__]
  12. A __stack_allocator__ must satisfy the __stack_allocator_concept__ requirements
  13. shown in the following table, in which `a` is an object of a
  14. __stack_allocator__ type, `sctx` is a `stack_context`, and `size` is a `std::size_t`:
  15. [table
  16. [[expression][return type][notes]]
  17. [
  18. [`a(size)`]
  19. []
  20. [creates a stack allocator]
  21. ]
  22. [
  23. [`a.allocate()`]
  24. [`stack_context`]
  25. [creates a stack]
  26. ]
  27. [
  28. [`a.deallocate( sctx)`]
  29. [`void`]
  30. [deallocates the stack created by `a.allocate()`]
  31. ]
  32. ]
  33. [important The implementation of `allocate()` might include logic to protect
  34. against exceeding the context's available stack size rather than leaving it as
  35. undefined behaviour.]
  36. [important Calling `deallocate()` with a `stack_context` not set by `allocate()`
  37. results in undefined behaviour.]
  38. [note Depending on the architecture `allocate()` stores an address from the
  39. top of the stack (growing downwards) or the bottom of the stack (growing
  40. upwards).]
  41. [section:protected_fixedsize Class ['protected_fixedsize]]
  42. __boost_context__ provides the class __protected_fixedsize__ which models
  43. the __stack_allocator_concept__.
  44. It appends a guard page at the end of each stack to protect against exceeding
  45. the stack. If the guard page is accessed (read or write operation) a
  46. segmentation fault/access violation is generated by the operating system.
  47. [important Using __protected_fixedsize__ is expensive. That is, launching a
  48. new coroutine with a new stack is expensive; the allocated stack is just as
  49. efficient to use as any other stack.]
  50. [note The appended `guard page` is [*not] mapped to physical memory, only
  51. virtual addresses are used.]
  52. #include <boost/context/protected_fixedsize.hpp>
  53. template< typename traitsT >
  54. struct basic_protected_fixedsize {
  55. typedef traitT traits_type;
  56. basic_protected_fixesize(std::size_t size = traits_type::default_size());
  57. stack_context allocate();
  58. void deallocate( stack_context &);
  59. }
  60. typedef basic_protected_fixedsize< stack_traits > protected_fixedsize
  61. [heading `stack_context allocate()`]
  62. [variablelist
  63. [[Preconditions:] [`traits_type::minimum:size() <= size` and
  64. `! traits_type::is_unbounded() && ( traits_type::maximum:size() >= size)`.]]
  65. [[Effects:] [Allocates memory of at least `size` Bytes and stores a pointer
  66. to the stack and its actual size in `sctx`. Depending
  67. on the architecture (the stack grows downwards/upwards) the stored address is
  68. the highest/lowest address of the stack.]]
  69. ]
  70. [heading `void deallocate( stack_context & sctx)`]
  71. [variablelist
  72. [[Preconditions:] [`sctx.sp` is valid, `traits_type::minimum:size() <= sctx.size` and
  73. `! traits_type::is_unbounded() && ( traits_type::maximum:size() >= sctx.size)`.]]
  74. [[Effects:] [Deallocates the stack space.]]
  75. ]
  76. [endsect]
  77. [section:pooled_fixedsize Class ['pooled_fixedsize_stack]]
  78. __boost_context__ provides the class __pooled_fixedsize__ which models
  79. the __stack_allocator_concept__.
  80. In contrast to __protected_fixedsize__ it does not append a guard page at the
  81. end of each stack. The memory is managed internally by
  82. [@http://www.boost.org/doc/libs/release/libs/pool/doc/html/boost/pool.html `boost::pool<>`].
  83. #include <boost/context/pooled_fixedsize_stack.hpp>
  84. template< typename traitsT >
  85. struct basic_pooled_fixedsize_stack {
  86. typedef traitT traits_type;
  87. basic_pooled_fixedsize_stack(std::size_t stack_size = traits_type::default_size(), std::size_t next_size = 32, std::size_t max_size = 0);
  88. stack_context allocate();
  89. void deallocate( stack_context &);
  90. }
  91. typedef basic_pooled_fixedsize_stack< stack_traits > pooled_fixedsize_stack;
  92. [heading `basic_pooled_fixedsize_stack(std::size_t stack_size, std::size_t next_size, std::size_t max_size)`]
  93. [variablelist
  94. [[Preconditions:] [`! traits_type::is_unbounded() && ( traits_type::maximum:size() >= stack_size)`
  95. and `0 < nest_size`.]]
  96. [[Effects:] [Allocates memory of at least `stack_size` Bytes and stores a pointer to
  97. the stack and its actual size in `sctx`. Depending on the architecture (the
  98. stack grows downwards/upwards) the stored address is the highest/lowest
  99. address of the stack. Argument `next_size` determines the number of stacks to
  100. request from the system the first time that `*this` needs to allocate system
  101. memory. The third argument `max_size` controls how many memory might be
  102. allocated for stacks - a value of zero means no uper limit.]]
  103. ]
  104. [heading `stack_context allocate()`]
  105. [variablelist
  106. [[Preconditions:] [`! traits_type::is_unbounded() && ( traits_type::maximum:size() >= stack_size)`.]]
  107. [[Effects:] [Allocates memory of at least `stack_size` Bytes and stores a pointer to
  108. the stack and its actual size in `sctx`. Depending on the architecture (the
  109. stack grows downwards/upwards) the stored address is the highest/lowest
  110. address of the stack.]]
  111. ]
  112. [heading `void deallocate( stack_context & sctx)`]
  113. [variablelist
  114. [[Preconditions:] [`sctx.sp` is valid,
  115. `! traits_type::is_unbounded() && ( traits_type::maximum:size() >= sctx.size)`.]]
  116. [[Effects:] [Deallocates the stack space.]]
  117. ]
  118. [endsect]
  119. [section:fixedsize Class ['fixedsize_stack]]
  120. __boost_context__ provides the class __fixedsize__ which models
  121. the __stack_allocator_concept__.
  122. In contrast to __protected_fixedsize__ it does not append a guard page at the
  123. end of each stack. The memory is simply managed by `std::malloc()` and
  124. `std::free()`.
  125. #include <boost/context/fixedsize_stack.hpp>
  126. template< typename traitsT >
  127. struct basic_fixedsize_stack {
  128. typedef traitT traits_type;
  129. basic_fixesize_stack(std::size_t size = traits_type::default_size());
  130. stack_context allocate();
  131. void deallocate( stack_context &);
  132. }
  133. typedef basic_fixedsize_stack< stack_traits > fixedsize_stack;
  134. [heading `stack_context allocate()`]
  135. [variablelist
  136. [[Preconditions:] [`traits_type::minimum:size() <= size` and
  137. `! traits_type::is_unbounded() && ( traits_type::maximum:size() >= size)`.]]
  138. [[Effects:] [Allocates memory of at least `size` Bytes and stores a pointer to
  139. the stack and its actual size in `sctx`. Depending on the architecture (the
  140. stack grows downwards/upwards) the stored address is the highest/lowest
  141. address of the stack.]]
  142. ]
  143. [heading `void deallocate( stack_context & sctx)`]
  144. [variablelist
  145. [[Preconditions:] [`sctx.sp` is valid, `traits_type::minimum:size() <= sctx.size` and
  146. `! traits_type::is_unbounded() && ( traits_type::maximum:size() >= sctx.size)`.]]
  147. [[Effects:] [Deallocates the stack space.]]
  148. ]
  149. [endsect]
  150. [#segmented]
  151. [section:segmented Class ['segmented_stack]]
  152. __boost_context__ supports usage of a __segmented__, e. g. the size of
  153. the stack grows on demand. The coroutine is created with a minimal stack size
  154. and will be increased as required.
  155. Class __segmented__ models the __stack_allocator_concept__.
  156. In contrast to __protected_fixedsize__ and __fixedsize__ it creates a
  157. stack which grows on demand.
  158. [note Segmented stacks are currently only supported by [*gcc] from version
  159. [*4.7] [*clang] from version [*3.4] onwards. In order to use a
  160. __segmented_stack__ __boost_context__ must be built with
  161. property `segmented-stacks`, e.g. [*toolset=gcc segmented-stacks=on] and
  162. applying `BOOST_USE_SEGMENTED_STACKS` at b2/bjam command line.]
  163. [note Segmented stacks can only be used with __cc__ (using
  164. [link implementation __ucontext__])].
  165. #include <boost/context/segmented_stack.hpp>
  166. template< typename traitsT >
  167. struct basic_segmented_stack {
  168. typedef traitT traits_type;
  169. basic_segmented_stack(std::size_t size = traits_type::default_size());
  170. stack_context allocate();
  171. void deallocate( stack_context &);
  172. }
  173. typedef basic_segmented_stack< stack_traits > segmented_stack;
  174. [heading `stack_context allocate()`]
  175. [variablelist
  176. [[Preconditions:] [`traits_type::minimum:size() <= size` and
  177. `! traits_type::is_unbounded() && ( traits_type::maximum:size() >= size)`.]]
  178. [[Effects:] [Allocates memory of at least `size` Bytes and stores a pointer to
  179. the stack and its actual size in `sctx`. Depending on the architecture (the
  180. stack grows downwards/upwards) the stored address is the highest/lowest
  181. address of the stack.]]
  182. ]
  183. [heading `void deallocate( stack_context & sctx)`]
  184. [variablelist
  185. [[Preconditions:] [`sctx.sp` is valid, `traits_type::minimum:size() <= sctx.size` and
  186. `! traits_type::is_unbounded() && ( traits_type::maximum:size() >= sctx.size)`.]]
  187. [[Effects:] [Deallocates the stack space.]]
  188. ]
  189. [note If the library is compiled for segmented stacks, __segmented_stack__ is the only
  190. available stack allocator.]
  191. [endsect]
  192. [section:stack_traits Class ['stack_traits]]
  193. ['stack_traits] models a __stack_traits__ providing a way to access certain
  194. properites defined by the enironment. Stack allocators use __stack_traits__ to
  195. allocate stacks.
  196. #include <boost/context/stack_traits.hpp>
  197. struct stack_traits {
  198. static bool is_unbounded() noexcept;
  199. static std::size_t page_size() noexcept;
  200. static std::size_t default_size() noexcept;
  201. static std::size_t minimum_size() noexcept;
  202. static std::size_t maximum_size() noexcept;
  203. }
  204. [heading `static bool is_unbounded()`]
  205. [variablelist
  206. [[Returns:] [Returns `true` if the environment defines no limit for the size of
  207. a stack.]]
  208. [[Throws:] [Nothing.]]
  209. ]
  210. [heading `static std::size_t page_size()`]
  211. [variablelist
  212. [[Returns:] [Returns the page size in bytes.]]
  213. [[Throws:] [Nothing.]]
  214. ]
  215. [heading `static std::size_t default_size()`]
  216. [variablelist
  217. [[Returns:] [Returns a default stack size, which may be platform specific.
  218. If the stack is unbounded then the present implementation returns the maximum of
  219. `64 kB` and `minimum_size()`.]]
  220. [[Throws:] [Nothing.]]
  221. ]
  222. [heading `static std::size_t minimum_size()`]
  223. [variablelist
  224. [[Returns:] [Returns the minimum size in bytes of stack defined by the
  225. environment (Win32 4kB/Win64 8kB, defined by rlimit on POSIX).]]
  226. [[Throws:] [Nothing.]]
  227. ]
  228. [heading `static std::size_t maximum_size()`]
  229. [variablelist
  230. [[Preconditions:] [`is_unbounded()` returns `false`.]]
  231. [[Returns:] [Returns the maximum size in bytes of stack defined by the
  232. environment.]]
  233. [[Throws:] [Nothing.]]
  234. ]
  235. [endsect]
  236. [section:stack_context Class ['stack_context]]
  237. __boost_context__ provides the class __stack_context__ which will contain
  238. the stack pointer and the size of the stack.
  239. In case of a __segmented__, __stack_context__ contains some extra control
  240. structures.
  241. struct stack_context {
  242. void * sp;
  243. std::size_t size;
  244. // might contain additional control structures
  245. // for segmented stacks
  246. }
  247. [heading `void * sp`]
  248. [variablelist
  249. [[Value:] [Pointer to the beginning of the stack.]]
  250. ]
  251. [heading `std::size_t size`]
  252. [variablelist
  253. [[Value:] [Actual size of the stack.]]
  254. ]
  255. [endsect]
  256. [section:valgrind Support for valgrind]
  257. Running programs that switch stacks under valgrind causes problems.
  258. Property (b2 command-line) `valgrind=on` let valgrind treat the memory regions
  259. as stack space which suppresses the errors. Users must define `BOOST_USE_VALGRIND`
  260. before including any Boost.Context headers when linking against Boost binaries
  261. compiled with `valgrind=on`.
  262. [endsect]
  263. [section:sanitizers Support for sanitizers]
  264. Sanitizers (GCC/Clang) are confused by the stack switches.
  265. The library is required to be compiled with property (b2 command-line)
  266. `context-impl=ucontext` and compilers santizer options.
  267. Users must define `BOOST_USE_ASAN` before including any Boost.Context headers
  268. when linking against Boost binaries.
  269. [endsect]
  270. [endsect]