stack.qbk 11 KB

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