Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / context / pooled_fixedsize_stack.hpp
index 9c417fd..c115c86 100644 (file)
 #include <boost/context/stack_context.hpp>
 #include <boost/context/stack_traits.hpp>
 
+#if defined(BOOST_CONTEXT_USE_MAP_STACK)
+extern "C" {
+#include <sys/mman.h>
+#include <stdlib.h>
+}
+#endif
+
 #if defined(BOOST_USE_VALGRIND)
 #include <valgrind/valgrind.h>
 #endif
 namespace boost {
 namespace context {
 
+#if defined(BOOST_CONTEXT_USE_MAP_STACK)
+namespace detail {
+template< typename traitsT >
+struct map_stack_allocator {
+    typedef std::size_t size_type;
+    typedef std::ptrdiff_t difference_type;
+
+    static char * malloc( const size_type bytes) {
+        void * block;
+        if ( ::posix_memalign( &block, traitsT::page_size(), bytes) != 0) {
+            return 0;
+        }
+        if ( mmap( block, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_STACK, -1, 0) == MAP_FAILED) {
+            std::free( block);
+            return 0;
+        }
+        return reinterpret_cast< char * >( block);
+    }
+    static void free( char * const block) {
+        std::free( block);
+    }
+};
+}
+#endif
+
 template< typename traitsT >
 class basic_pooled_fixedsize_stack {
 private:
@@ -39,7 +71,11 @@ private:
     private:
         std::atomic< std::size_t >                                  use_count_;
         std::size_t                                                 stack_size_;
+#if defined(BOOST_CONTEXT_USE_MAP_STACK)
+        boost::pool< detail::map_stack_allocator< traitsT > >       storage_;
+#else
         boost::pool< boost::default_user_allocator_malloc_free >    storage_;
+#endif
 
     public:
         storage( std::size_t stack_size, std::size_t next_size, std::size_t max_size) :