| Home | Libraries | People | FAQ | More

### Class protected_stack_allocator

Boost.Coroutine provides the class protected_stack_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.

| Important
Using protected_stack_allocator is expensive. That is, launching a new coroutine with a new stack is expensive; the allocated stack is just as efficient to use as any other stack.

| Note
The appended `guard page` is not mapped to physical memory, only virtual addresses are used.

    #include <boost/coroutine/protected_stack_allocator.hpp>

    template< typename traitsT >
    struct basic_protected_stack_allocator
    {
        typedef traitT  traits_type;

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

        void deallocate( stack_context &);
    }

    typedef basic_protected_stack_allocator< stack_traits > protected_stack_allocator

##### `void allocate( stack_context & sctx, std::size_t size)`

Preconditions:

  `traits_type::minimum:size() <= size` and `! traits_type::is_unbounded() && ( traits_type::maximum:size() >= size)`.

Effects:

  Allocates memory of at least `size` Bytes and stores a pointer to the stack and its actual size in `sctx`. Depending on the architecture (the stack grows downwards/upwards) the stored address is the highest/lowest address of the stack.

##### `void deallocate( stack_context & sctx)`

Preconditions:

  `sctx.sp` is valid, `traits_type::minimum:size() <= sctx.size` and `! traits_type::is_unbounded() && ( traits_type::maximum:size() >= sctx.size)`.

Effects:

  Deallocates the stack space.