[/
          Copyright Oliver Kowalke 2009.
 Distributed under the Boost Software License, Version 1.0.
    (See accompanying file LICENSE_1_0.txt or copy at
          http://www.boost.org/LICENSE_1_0.txt
]

[section:stack Stack allocation]

A __coro__ uses internally a __ctx__ which manages a set of registers and a stack.
The memory used by the stack is allocated/deallocated via a __stack_allocator__
which is required to model a __stack_allocator_concept__.


[heading __stack_allocator_concept__]
A __stack_allocator__ must satisfy the __stack_allocator_concept__ requirements
shown in the following table, in which `a` is an object of a
__stack_allocator__ type, `sctx` is a `stack_context`, and `size` is a `std::size_t`:

[table
    [[expression][return type][notes]]
    [
        [`a.allocate( sctx, size)`]
        [`void`]
        [creates a stack of at least `size` bytes and stores both values in `sctx`]
    ]
    [
        [`a.deallocate( sctx)`]
        [`void`]
        [deallocates the stack created by `a.allocate()`]
    ]
]

[important The implementation of `allocate()` might include logic to protect
against exceeding the context's available stack size rather than leaving it as
undefined behaviour.]

[important Calling `deallocate()` with a pointer not returned by `allocate()`
results in undefined behaviour.]

[note The stack is not required to be aligned; alignment takes place inside
__coro__.]

[note Depending on the architecture `allocate()` returns an address from the
top of the stack (growing downwards) or the bottom of the stack (growing
upwards).]


[section:stack_allocator Class ['stack_allocator]]

__boost_coroutine__ provides the class __coro_allocator__ which models
the __stack_allocator_concept__.
It appends a guard page at the end of each stack to protect against exceeding
the stack. If the guard page is accessed (read or write operation) a
segmentation fault/access violation is generated by the operating system.

[note The appended `guard page` is [*not] mapped to physical memory, only
virtual addresses are used.]

        class stack_allocator
        {
            static bool is_stack_unbound();

            static std::size_t maximum_stacksize();

            static std::size_t default_stacksize();

            static std::size_t minimum_stacksize();

            void allocate( stack_context &, std::size_t size);

            void deallocate( stack_context &);
        }

[heading `static bool is_stack_unbound()`]
[variablelist
[[Returns:] [Returns `true` if the environment defines no limit for the size of a stack.]]
]

[heading `static std::size_t maximum_stacksize()`]
[variablelist
[[Preconditions:] [`is_stack_unbound()` returns `false`.]]
[[Returns:] [Returns the maximum size in bytes of stack defined by the environment.]]
]

[heading `static std::size_t default_stacksize()`]
[variablelist
[[Returns:] [Returns a default stack size, which may be platform specific.
If the stack is unbound then the present implementation returns the maximum of
`64 kB` and `minimum_stacksize()`.]]
]

[heading `static std::size_t minimum_stacksize()`]
[variablelist
[[Returns:] [Returns the minimum size in bytes of stack defined by the
environment (Win32 4kB/Win64 8kB, defined by rlimit on POSIX).]]
]

[heading `void allocate( stack_context & sctx, std::size_t size)`]
[variablelist
[[Preconditions:] [`minimum_stacksize() > size` and
`! is_stack_unbound() && ( maximum_stacksize() < size)`.]]
[[Effects:] [Allocates memory of at least `size` Bytes and stores a pointer
to the stack and its actual size in `sctx`.]]
[[Returns:] [Returns pointer to the start address of the new stack. Depending
on the architecture the stack grows downwards/upwards the returned address is
the highest/lowest address of the stack.]]
]

[heading `void deallocate( stack_context & sctx)`]
[variablelist
[[Preconditions:] [`sctx.sp` is valid, `minimum_stacksize() > sctx.size` and
`! is_stack_unbound() && ( maximum_stacksize() < size)`.]]
[[Effects:] [Deallocates the stack space.]]
]

[endsect]


[section:stack_context Class ['stack_context]]

__boost_coroutine__ provides the class __stack_context__ which will contain
the stack pointer and the size of the stack.
In case of a __segmented_stack__ __stack_context__ contains some extra controll
structures.

        struct stack_context
        {
            void    *   sp;
            std::size_t size;

            // might contain addition controll structures
            // for instance for segmented stacks
        }

[heading `void * sp`]
[variablelist
[[Value:] [Pointer to the beginning of the stack.]]
]

[heading `std::size_t size`]
[variablelist
[[Value:] [Actual size of the stack.]]
]

[endsect]


[section:segmented_stack Segmented stacks]

__boost_coroutine__ supports usage of a __segmented_stack__, e. g. the size of the
stack of a coroutine grows on demand. The coroutine is created with a stack with
an minimal stack and if the coroutine is execute the stack size is increased as
required.

Segmented stack are currently only supported by [*gcc] from version [*4.7] onwards.
n order to use __segmented_stack__ compile __boost_coroutine__ with
[*toolset=gcc segmented-stacks=on] at b2/bjam command-line. Application using __boost_coroutine__
with enabled __segmented-stack__ must be compiled with compiler-flags
[*-fsplit-stack -DBOOST_USE_SEGMENTED_STACKS].

[endsect]

[endsect]
