stack.qbk 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. [/
  2. Copyright Oliver Kowalke 2009.
  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. A __coro__ uses internally a __ctx__ which manages a set of registers and a stack.
  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.allocate( sctx, size)`]
  19. [`void`]
  20. [creates a stack of at least `size` bytes and stores its pointer and
  21. length in `sctx`]
  22. ]
  23. [
  24. [`a.deallocate( sctx)`]
  25. [`void`]
  26. [deallocates the stack created by `a.allocate()`]
  27. ]
  28. ]
  29. [important The implementation of `allocate()` might include logic to protect
  30. against exceeding the context's available stack size rather than leaving it as
  31. undefined behaviour.]
  32. [important Calling `deallocate()` with a `stack_context` not set by `allocate()`
  33. results in undefined behaviour.]
  34. [note The stack is not required to be aligned; alignment takes place inside
  35. __coro__.]
  36. [note Depending on the architecture `allocate()` stores an address from the
  37. top of the stack (growing downwards) or the bottom of the stack (growing
  38. upwards).]
  39. class __coro_allocator__ is a typedef of __standard_allocator__.
  40. [section:protected_stack_allocator Class ['protected_stack_allocator]]
  41. __boost_coroutine__ provides the class __protected_allocator__ which models
  42. the __stack_allocator_concept__.
  43. It appends a guard page at the end of each stack to protect against exceeding
  44. the stack. If the guard page is accessed (read or write operation) a
  45. segmentation fault/access violation is generated by the operating system.
  46. [important Using __protected_allocator__ is expensive. That is, launching a
  47. new coroutine with a new stack is expensive; the allocated stack is just as
  48. efficient to use as any other stack.]
  49. [note The appended `guard page` is [*not] mapped to physical memory, only
  50. virtual addresses are used.]
  51. #include <boost/coroutine/protected_stack_allocator.hpp>
  52. template< typename traitsT >
  53. struct basic_protected_stack_allocator
  54. {
  55. typedef traitT traits_type;
  56. void allocate( stack_context &, std::size_t size);
  57. void deallocate( stack_context &);
  58. }
  59. typedef basic_protected_stack_allocator< stack_traits > protected_stack_allocator
  60. [heading `void allocate( stack_context & sctx, std::size_t size)`]
  61. [variablelist
  62. [[Preconditions:] [`traits_type::minimum_size() <= size` and
  63. `! traits_type::is_unbounded() && ( traits_type::maximum_size() >= size)`.]]
  64. [[Effects:] [Allocates memory of at least `size` bytes and stores a pointer
  65. to the stack and its actual size in `sctx`. Depending
  66. on the architecture (the stack grows downwards/upwards) the stored address is
  67. the highest/lowest address of the stack.]]
  68. ]
  69. [heading `void deallocate( stack_context & sctx)`]
  70. [variablelist
  71. [[Preconditions:] [`sctx.sp` is valid, `traits_type::minimum_size() <= sctx.size` and
  72. `! traits_type::is_unbounded() && ( traits_type::maximum_size() >= sctx.size)`.]]
  73. [[Effects:] [Deallocates the stack space.]]
  74. ]
  75. [endsect]
  76. [section:standard_stack_allocator Class ['standard_stack_allocator]]
  77. __boost_coroutine__ provides the class __standard_allocator__ which models
  78. the __stack_allocator_concept__.
  79. In contrast to __protected_allocator__ it does not append a guard page at the
  80. end of each stack. The memory is simply managed by `std::malloc()` and
  81. `std::free()`.
  82. [note The __standard_allocator__ is the default stack allocator.]
  83. #include <boost/coroutine/standard_stack_allocator.hpp>
  84. template< typename traitsT >
  85. struct standard_stack_allocator
  86. {
  87. typedef traitT traits_type;
  88. void allocate( stack_context &, std::size_t size);
  89. void deallocate( stack_context &);
  90. }
  91. typedef basic_standard_stack_allocator< stack_traits > standard_stack_allocator
  92. [heading `void allocate( stack_context & sctx, std::size_t size)`]
  93. [variablelist
  94. [[Preconditions:] [`traits_type::minimum_size() <= size` and
  95. `! traits_type::is_unbounded() && ( traits_type::maximum_size() >= size)`.]]
  96. [[Effects:] [Allocates memory of at least `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.]]
  100. ]
  101. [heading `void deallocate( stack_context & sctx)`]
  102. [variablelist
  103. [[Preconditions:] [`sctx.sp` is valid, `traits_type::minimum_size() <= sctx.size` and
  104. `! traits_type::is_unbounded() && ( traits_type::maximum_size() >= sctx.size)`.]]
  105. [[Effects:] [Deallocates the stack space.]]
  106. ]
  107. [endsect]
  108. [section:segmented_stack_allocator Class ['segmented_stack_allocator]]
  109. __boost_coroutine__ supports usage of a __segmented_stack__, e. g. the size of
  110. the stack grows on demand. The coroutine is created with a minimal stack size
  111. and will be increased as required.
  112. Class __segmented_allocator__ models the __stack_allocator_concept__.
  113. In contrast to __protected_allocator__ and __standard_allocator__ it creates a
  114. stack which grows on demand.
  115. [note Segmented stacks are currently only supported by [*gcc] from version
  116. [*4.7] and [*clang] from version [*3.4] onwards. In order to use a
  117. __segmented_stack__ __boost_coroutine__ must be built with
  118. [*toolset=gcc segmented-stacks=on] at b2/bjam command-line. Applications
  119. must be compiled with compiler-flags
  120. [*-fsplit-stack -DBOOST_USE_SEGMENTED_STACKS].]
  121. #include <boost/coroutine/segmented_stack_allocator.hpp>
  122. template< typename traitsT >
  123. struct basic_segmented_stack_allocator
  124. {
  125. typedef traitT traits_type;
  126. void allocate( stack_context &, std::size_t size);
  127. void deallocate( stack_context &);
  128. }
  129. typedef basic_segmented_stack_allocator< stack_traits > segmented_stack_allocator;
  130. [heading `void allocate( stack_context & sctx, std::size_t size)`]
  131. [variablelist
  132. [[Preconditions:] [`traits_type::minimum_size() <= size` and
  133. `! traits_type::is_unbounded() && ( traits_type::maximum_size() >= size)`.]]
  134. [[Effects:] [Allocates memory of at least `size` bytes and stores a pointer to
  135. the stack and its actual size in `sctx`. Depending on the architecture (the
  136. stack grows downwards/upwards) the stored address is the highest/lowest
  137. address of the stack.]]
  138. ]
  139. [heading `void deallocate( stack_context & sctx)`]
  140. [variablelist
  141. [[Preconditions:] [`sctx.sp` is valid, `traits_type::minimum_size() <= sctx.size` and
  142. `! traits_type::is_unbounded() && ( traits_type::maximum_size() >= sctx.size)`.]]
  143. [[Effects:] [Deallocates the stack space.]]
  144. ]
  145. [endsect]
  146. [section:stack_traits Class ['stack_traits]]
  147. ['stack_traits] models a __stack_traits__ providing a way to access certain
  148. properites defined by the enironment. Stack allocators use __stack_traits__ to
  149. allocate stacks.
  150. #include <boost/coroutine/stack_traits.hpp>
  151. struct stack_traits
  152. {
  153. static bool is_unbounded() noexcept;
  154. static std::size_t page_size() noexcept;
  155. static std::size_t default_size() noexcept;
  156. static std::size_t minimum_size() noexcept;
  157. static std::size_t maximum_size() noexcept;
  158. }
  159. [heading `static bool is_unbounded()`]
  160. [variablelist
  161. [[Returns:] [Returns `true` if the environment defines no limit for the size of
  162. a stack.]]
  163. [[Throws:] [Nothing.]]
  164. ]
  165. [heading `static std::size_t page_size()`]
  166. [variablelist
  167. [[Returns:] [Returns the page size in bytes.]]
  168. [[Throws:] [Nothing.]]
  169. ]
  170. [heading `static std::size_t default_size()`]
  171. [variablelist
  172. [[Returns:] [Returns a default stack size, which may be platform specific.
  173. If the stack is unbounded then the present implementation returns the maximum of
  174. `64 kB` and `minimum_size()`.]]
  175. [[Throws:] [Nothing.]]
  176. ]
  177. [heading `static std::size_t minimum_size()`]
  178. [variablelist
  179. [[Returns:] [Returns the minimum size in bytes of stack defined by the
  180. environment (Win32 4kB/Win64 8kB, defined by rlimit on POSIX).]]
  181. [[Throws:] [Nothing.]]
  182. ]
  183. [heading `static std::size_t maximum_size()`]
  184. [variablelist
  185. [[Preconditions:] [`is_unbounded()` returns `false`.]]
  186. [[Returns:] [Returns the maximum size in bytes of stack defined by the
  187. environment.]]
  188. [[Throws:] [Nothing.]]
  189. ]
  190. [endsect]
  191. [section:stack_context Class ['stack_context]]
  192. __boost_coroutine__ provides the class __stack_context__ which will contain
  193. the stack pointer and the size of the stack.
  194. In case of a __segmented_stack__, __stack_context__ contains some extra control
  195. structures.
  196. struct stack_context
  197. {
  198. void * sp;
  199. std::size_t size;
  200. // might contain additional control structures
  201. // for instance for segmented stacks
  202. }
  203. [heading `void * sp`]
  204. [variablelist
  205. [[Value:] [Pointer to the beginning of the stack.]]
  206. ]
  207. [heading `std::size_t size`]
  208. [variablelist
  209. [[Value:] [Actual size of the stack.]]
  210. ]
  211. [endsect]
  212. [section:valgrind Support for valgrind]
  213. Running programs that switch stacks under valgrind causes problems.
  214. Property (b2 command-line) `valgrind=on` let valgrind treat the memory regions
  215. as stack space which suppresses the errors.
  216. [endsect]
  217. [endsect]