stack.qbk 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. [/
  2. Copyright Oliver Kowalke 2009-2013.
  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. A __fiber__ uses internally an __econtext__ which manages a set of registers and a stack.
  10. The memory used by the stack is allocated/deallocated via a __stack_allocator__
  11. which is required to model a __stack_allocator_concept__.
  12. A __stack_allocator__ can be passed to [link fiber_fiber `fiber::fiber()`] or
  13. to [ns_function_link fibers..async].
  14. [#stack_allocator_concept]
  15. [heading stack-allocator concept]
  16. A __stack_allocator__ must satisfy the ['stack-allocator concept] requirements
  17. shown in the following table, in which `a` is an object of a
  18. __stack_allocator__ type, `sctx` is a __stack_context__, and `size` is a `std::size_t`:
  19. [table
  20. [[expression][return type][notes]]
  21. [
  22. [`a(size)`]
  23. []
  24. [creates a stack allocator]
  25. ]
  26. [
  27. [`a.allocate()`]
  28. [__stack_context__]
  29. [creates a stack]
  30. ]
  31. [
  32. [`a.deallocate( sctx)`]
  33. [`void`]
  34. [deallocates the stack created by `a.allocate()`]
  35. ]
  36. ]
  37. [important The implementation of `allocate()` might include logic to protect
  38. against exceeding the context's available stack size rather than leaving it as
  39. undefined behaviour.]
  40. [important Calling `deallocate()` with a __stack_context__ not obtained from
  41. `allocate()` results in undefined behaviour.]
  42. [note The memory for the stack is not required to be aligned; alignment takes
  43. place inside __econtext__.]
  44. See also [@http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack.html Boost.Context stack allocation].
  45. In particular, `traits_type` methods are as described for
  46. [@http://www.boost.org/doc/libs/release/libs/context/doc/html/context/stack/stack_traits.html
  47. `boost::context::stack_traits`].
  48. [class_heading protected_fixedsize_stack]
  49. __boost_fiber__ provides the class __pfixedsize_stack__ which models
  50. the __stack_allocator_concept__.
  51. It appends a guard page at the end of each stack to protect against exceeding
  52. the stack. If the guard page is accessed (read or write operation) a
  53. segmentation fault/access violation is generated by the operating system.
  54. [important Using __pfixedsize_stack__ is expensive. Launching a new fiber with
  55. a stack of this type incurs the overhead of setting the memory protection;
  56. once allocated, this stack is just as efficient to use as __fixedsize_stack__.]
  57. [note The appended `guard page` is [*not] mapped to physical memory, only
  58. virtual addresses are used.]
  59. #include <boost/fiber/protected_fixedsize.hpp>
  60. namespace boost {
  61. namespace fibers {
  62. struct protected_fixedsize {
  63. protected_fixesize(std::size_t size = traits_type::default_size());
  64. stack_context allocate();
  65. void deallocate( stack_context &);
  66. }
  67. }}
  68. [member_heading protected_fixedsize..allocate]
  69. stack_context allocate();
  70. [variablelist
  71. [[Preconditions:] [`traits_type::minimum_size() <= size` and
  72. `traits_type::is_unbounded() || ( size <= traits_type::maximum_size() )`.]]
  73. [[Effects:] [Allocates memory of at least `size` bytes and stores a pointer
  74. to the stack and its actual size in `sctx`. Depending
  75. on the architecture (the stack grows downwards/upwards) the stored address is
  76. the highest/lowest address of the stack.]]
  77. ]
  78. [member_heading protected_fixesize..deallocate]
  79. void deallocate( stack_context & sctx);
  80. [variablelist
  81. [[Preconditions:] [`sctx.sp` is valid, `traits_type::minimum_size() <= sctx.size` and
  82. `traits_type::is_unbounded() || ( sctx.size <= traits_type::maximum_size() )`.]]
  83. [[Effects:] [Deallocates the stack space.]]
  84. ]
  85. [class_heading pooled_fixedsize_stack]
  86. __boost_fiber__ provides the class __ofixedsize_stack__ which models
  87. the __stack_allocator_concept__.
  88. In contrast to __pfixedsize_stack__ it does not append a guard page at the
  89. end of each stack. The memory is managed internally by
  90. [@http://www.boost.org/doc/libs/release/libs/pool/doc/html/boost/pool.html `boost::pool<>`].
  91. #include <boost/fiber/pooled_fixedsize_stack.hpp>
  92. namespace boost {
  93. namespace fibers {
  94. struct pooled_fixedsize_stack {
  95. pooled_fixedsize_stack(std::size_t stack_size = traits_type::default_size(), std::size_t next_size = 32, std::size_t max_size = 0);
  96. stack_context allocate();
  97. void deallocate( stack_context &);
  98. }
  99. }}
  100. [hding pooled_fixedsize..Constructor]
  101. pooled_fixedsize_stack(std::size_t stack_size, std::size_t next_size, std::size_t max_size);
  102. [variablelist
  103. [[Preconditions:] [`traits_type::is_unbounded() || ( traits_type::maximum_size() >= stack_size)`
  104. and `0 < next_size`.]]
  105. [[Effects:] [Allocates memory of at least `stack_size` bytes and stores a pointer to
  106. the stack and its actual size in `sctx`. Depending on the architecture (the
  107. stack grows downwards/upwards) the stored address is the highest/lowest
  108. address of the stack. Argument `next_size` determines the number of stacks to
  109. request from the system the first time that `*this` needs to allocate system
  110. memory. The third argument `max_size` controls how much memory might be
  111. allocated for stacks [mdash] a value of zero means no upper limit.]]
  112. ]
  113. [member_heading pooled_fixedsize..allocate]
  114. stack_context allocate();
  115. [variablelist
  116. [[Preconditions:] [`traits_type::is_unbounded() || ( traits_type::maximum_size() >= stack_size)`.]]
  117. [[Effects:] [Allocates memory of at least `stack_size` bytes and stores a pointer to
  118. the stack and its actual size in `sctx`. Depending on the architecture (the
  119. stack grows downwards/upwards) the stored address is the highest/lowest
  120. address of the stack.]]
  121. ]
  122. [member_heading pooled_fixesize..deallocate]
  123. void deallocate( stack_context & sctx);
  124. [variablelist
  125. [[Preconditions:] [`sctx.sp` is valid,
  126. `traits_type::is_unbounded() || ( traits_type::maximum_size() >= sctx.size)`.]]
  127. [[Effects:] [Deallocates the stack space.]]
  128. ]
  129. [note This stack allocator is not thread safe.]
  130. [class_heading fixedsize_stack]
  131. __boost_fiber__ provides the class __fixedsize_stack__ which models
  132. the __stack_allocator_concept__.
  133. In contrast to __pfixedsize_stack__ it does not append a guard page at the
  134. end of each stack. The memory is simply managed by `std::malloc()` and
  135. `std::free()`.
  136. #include <boost/context/fixedsize_stack.hpp>
  137. namespace boost {
  138. namespace fibers {
  139. struct fixedsize_stack {
  140. fixedsize_stack(std::size_t size = traits_type::default_size());
  141. stack_context allocate();
  142. void deallocate( stack_context &);
  143. }
  144. }}
  145. [member_heading fixedsize..allocate]
  146. stack_context allocate();
  147. [variablelist
  148. [[Preconditions:] [`traits_type::minimum_size() <= size` and
  149. `traits_type::is_unbounded() || ( traits_type::maximum_size() >= size)`.]]
  150. [[Effects:] [Allocates memory of at least `size` bytes and stores a pointer to
  151. the stack and its actual size in `sctx`. Depending on the architecture (the
  152. stack grows downwards/upwards) the stored address is the highest/lowest
  153. address of the stack.]]
  154. ]
  155. [member_heading fixesize..deallocate]
  156. void deallocate( stack_context & sctx);
  157. [variablelist
  158. [[Preconditions:] [`sctx.sp` is valid, `traits_type::minimum_size() <= sctx.size` and
  159. `traits_type::is_unbounded() || ( traits_type::maximum_size() >= sctx.size)`.]]
  160. [[Effects:] [Deallocates the stack space.]]
  161. ]
  162. [#segmented]
  163. [class_heading segmented_stack]
  164. __boost_fiber__ supports usage of a __segmented_stack__, i.e.
  165. the stack grows on demand. The fiber is created with a minimal stack size
  166. which will be increased as required.
  167. Class __segmented_stack__ models the __stack_allocator_concept__.
  168. In contrast to __pfixedsize_stack__ and __fixedsize_stack__ it creates a
  169. stack which grows on demand.
  170. [note Segmented stacks are currently only supported by [*gcc] from version
  171. [*4.7] and [*clang] from version [*3.4] onwards. In order to use a
  172. __segmented_stack__ __boost_fiber__ must be built with
  173. property `segmented-stacks`, e.g. [*toolset=gcc segmented-stacks=on]
  174. and applying BOOST_USE_SEGMENTED_STACKS at b2/bjam command line.]
  175. [note Segmented stacks can only be used with callcc() using property
  176. `context-impl=ucontext`.]
  177. #include <boost/fiber/segmented_stack.hpp>
  178. namespace boost {
  179. namespace fibers {
  180. struct segmented_stack {
  181. segmented_stack(std::size_t stack_size = traits_type::default_size());
  182. stack_context allocate();
  183. void deallocate( stack_context &);
  184. }
  185. }}
  186. [member_heading segmented..allocate]
  187. stack_context allocate();
  188. [variablelist
  189. [[Preconditions:] [`traits_type::minimum_size() <= size` and
  190. `traits_type::is_unbounded() || ( traits_type::maximum_size() >= size)`.]]
  191. [[Effects:] [Allocates memory of at least `size` bytes and stores a pointer to
  192. the stack and its actual size in `sctx`. Depending on the architecture (the
  193. stack grows downwards/upwards) the stored address is the highest/lowest
  194. address of the stack.]]
  195. ]
  196. [member_heading segmented..deallocate]
  197. void deallocate( stack_context & sctx);
  198. [variablelist
  199. [[Preconditions:] [`sctx.sp` is valid, `traits_type::minimum_size() <= sctx.size` and
  200. `traits_type::is_unbounded() || ( traits_type::maximum_size() >= sctx.size)`.]]
  201. [[Effects:] [Deallocates the stack space.]]
  202. ]
  203. [note If the library is compiled for segmented stacks, __segmented_stack__ is the only
  204. available stack allocator.]
  205. [section:valgrind Support for valgrind]
  206. Running programs that switch stacks under valgrind causes problems.
  207. Property (b2 command-line) `valgrind=on` let valgrind treat the memory regions
  208. as stack space which suppresses the errors.
  209. [endsect]
  210. [section:sanitizers Support for sanitizers]
  211. Sanitizers (GCC/Clang) are confused by the stack switches.
  212. The library (and Boost.Context too) is required to be compiled with property (b2 command-line)
  213. `context-impl=ucontext` and compilers santizer options.
  214. Users must define `BOOST_USE_ASAN` before including any Boost.Context headers
  215. when linking against Boost binaries.
  216. [endsect]
  217. [endsect]