BVB Source Codes

mars Show pooled_fixedsize_stack.hpp Source code

Return Download mars: download pooled_fixedsize_stack.hpp Source code - Download mars Source code - Type:.hpp
  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. #ifndef BOOST_CONTEXT_POOLED_pooled_fixedsize_H
  8. #define BOOST_CONTEXT_POOLED_pooled_fixedsize_H
  9.  
  10. #include <atomic>
  11. #include <cstddef>
  12. #include <cstdlib>
  13. #include <new>
  14.  
  15. #include <boost/assert.hpp>
  16. #include <boost/config.hpp>
  17. #include <boost/pool/pool.hpp>
  18.  
  19. #include <boost/context/detail/config.hpp>
  20. #include <boost/context/stack_context.hpp>
  21. #include <boost/context/stack_traits.hpp>
  22.  
  23. #if defined(BOOST_USE_VALGRIND)
  24. #include <valgrind/valgrind.h>
  25. #endif
  26.  
  27. #ifdef BOOST_HAS_ABI_HEADERS
  28. #  include BOOST_ABI_PREFIX
  29. #endif
  30.  
  31. namespace mars_boost {} namespace boost = mars_boost; namespace mars_boost {
  32. namespace context {
  33.  
  34. template< typename traitsT >
  35. class basic_pooled_fixedsize_stack {
  36. private:
  37.     class storage {
  38.     private:
  39.         std::atomic< std::size_t >                                  use_count_;
  40.         std::size_t                                                 stack_size_;
  41.         mars_boost::pool< mars_boost::default_user_allocator_malloc_free >    storage_;
  42.  
  43.     public:
  44.         storage( std::size_t stack_size, std::size_t next_size, std::size_t max_size) :
  45.                 use_count_( 0),
  46.                 stack_size_( stack_size),
  47.                 storage_( stack_size, next_size, max_size) {
  48.             BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= stack_size_) );
  49.         }
  50.  
  51.         stack_context allocate() {
  52.             void * vp = storage_.malloc();
  53.             if ( ! vp) {
  54.                 throw std::bad_alloc();
  55.             }
  56.             stack_context sctx;
  57.             sctx.size = stack_size_;
  58.             sctx.sp = static_cast< char * >( vp) + sctx.size;
  59. #if defined(BOOST_USE_VALGRIND)
  60.             sctx.valgrind_stack_id = VALGRIND_STACK_REGISTER( sctx.sp, vp);
  61. #endif
  62.             return sctx;
  63.         }
  64.  
  65.         void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
  66.             BOOST_ASSERT( sctx.sp);
  67.             BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= sctx.size) );
  68.  
  69. #if defined(BOOST_USE_VALGRIND)
  70.             VALGRIND_STACK_DEREGISTER( sctx.valgrind_stack_id);
  71. #endif
  72.             void * vp = static_cast< char * >( sctx.sp) - sctx.size;
  73.             storage_.free( vp);
  74.         }
  75.  
  76.         friend void intrusive_ptr_add_ref( storage * s) noexcept {
  77.             ++s->use_count_;
  78.         }
  79.  
  80.         friend void intrusive_ptr_release( storage * s) noexcept {
  81.             if ( 0 == --s->use_count_) {
  82.                 delete s;
  83.             }
  84.         }
  85.     };
  86.  
  87.     intrusive_ptr< storage >    storage_;
  88.  
  89. public:
  90.     typedef traitsT traits_type;
  91.  
  92.     basic_pooled_fixedsize_stack( std::size_t stack_size = traits_type::default_size(),
  93.                            std::size_t next_size = 32,
  94.                            std::size_t max_size = 0) BOOST_NOEXCEPT_OR_NOTHROW :
  95.         storage_( new storage( stack_size, next_size, max_size) ) {
  96.     }
  97.  
  98.     stack_context allocate() {
  99.         return storage_->allocate();
  100.     }
  101.  
  102.     void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
  103.         storage_->deallocate( sctx);
  104.     }
  105. };
  106.  
  107. typedef basic_pooled_fixedsize_stack< stack_traits >  pooled_fixedsize_stack;
  108.  
  109. }}
  110.  
  111. #ifdef BOOST_HAS_ABI_HEADERS
  112. #  include BOOST_ABI_SUFFIX
  113. #endif
  114.  
  115. #endif // BOOST_CONTEXT_POOLED_pooled_fixedsize_H
  116.  
downloadpooled_fixedsize_stack.hpp Source code - Download mars Source code
Related Source Codes/Software:
Hero - Elegant transition library for iOS & tvOS 2017-06-09
deep-photo-styletransfer - Code and data for paper "Deep Photo Style Transfer... 2017-06-09
mastodon - A GNU Social-compatible microblogging server ... 2017-06-09
plyr - A simple HTML5, YouTube and Vimeo player ... 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
Public-APIs - 2017-06-09
lottie-ios - An iOS library to natively render After Effects ve... 2017-06-09
Awesome-Hacking - A collection of various awesome lists for hackers,... 2017-06-09
algorithms - Minimal examples of data structures and algorithms... 2017-06-10
lectures - Oxford Deep NLP 2017 course 2017-06-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top