sl@0: // Copyright (C) 2000, 2001 Stephen Cleary sl@0: // sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: // sl@0: // See http://www.boost.org for updates, documentation, and revision history. sl@0: sl@0: #ifndef BOOST_POOL_ALLOC_HPP sl@0: #define BOOST_POOL_ALLOC_HPP sl@0: sl@0: // std::numeric_limits sl@0: #include sl@0: // new, std::bad_alloc sl@0: #include sl@0: sl@0: #include sl@0: sl@0: // boost::singleton_pool sl@0: #include sl@0: sl@0: #include sl@0: sl@0: // The following code will be put into Boost.Config in a later revision sl@0: #if defined(_RWSTD_VER) || defined(__SGI_STL_PORT) || \ sl@0: BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) sl@0: #define BOOST_NO_PROPER_STL_DEALLOCATE sl@0: #endif sl@0: sl@0: namespace boost { sl@0: sl@0: struct pool_allocator_tag { }; sl@0: sl@0: template sl@0: class pool_allocator sl@0: { sl@0: public: sl@0: typedef T value_type; sl@0: typedef UserAllocator user_allocator; sl@0: typedef Mutex mutex; sl@0: BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize); sl@0: sl@0: typedef value_type * pointer; sl@0: typedef const value_type * const_pointer; sl@0: typedef value_type & reference; sl@0: typedef const value_type & const_reference; sl@0: typedef typename pool::size_type size_type; sl@0: typedef typename pool::difference_type difference_type; sl@0: sl@0: template sl@0: struct rebind sl@0: { sl@0: typedef pool_allocator other; sl@0: }; sl@0: sl@0: public: sl@0: pool_allocator() { } sl@0: sl@0: // default copy constructor sl@0: sl@0: // default assignment operator sl@0: sl@0: // not explicit, mimicking std::allocator [20.4.1] sl@0: template sl@0: pool_allocator(const pool_allocator &) sl@0: { } sl@0: sl@0: // default destructor sl@0: sl@0: static pointer address(reference r) sl@0: { return &r; } sl@0: static const_pointer address(const_reference s) sl@0: { return &s; } sl@0: static size_type max_size() sl@0: { return (std::numeric_limits::max)(); } sl@0: static void construct(const pointer ptr, const value_type & t) sl@0: { new (ptr) T(t); } sl@0: static void destroy(const pointer ptr) sl@0: { sl@0: ptr->~T(); sl@0: (void) ptr; // avoid unused variable warning sl@0: } sl@0: sl@0: bool operator==(const pool_allocator &) const sl@0: { return true; } sl@0: bool operator!=(const pool_allocator &) const sl@0: { return false; } sl@0: sl@0: static pointer allocate(const size_type n) sl@0: { sl@0: const pointer ret = static_cast( sl@0: singleton_pool::ordered_malloc(n) ); sl@0: if (ret == 0) sl@0: throw std::bad_alloc(); sl@0: return ret; sl@0: } sl@0: static pointer allocate(const size_type n, const void * const) sl@0: { return allocate(n); } sl@0: static void deallocate(const pointer ptr, const size_type n) sl@0: { sl@0: #ifdef BOOST_NO_PROPER_STL_DEALLOCATE sl@0: if (ptr == 0 || n == 0) sl@0: return; sl@0: #endif sl@0: singleton_pool::ordered_free(ptr, n); sl@0: } sl@0: }; sl@0: sl@0: struct fast_pool_allocator_tag { }; sl@0: sl@0: template sl@0: class fast_pool_allocator sl@0: { sl@0: public: sl@0: typedef T value_type; sl@0: typedef UserAllocator user_allocator; sl@0: typedef Mutex mutex; sl@0: BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize); sl@0: sl@0: typedef value_type * pointer; sl@0: typedef const value_type * const_pointer; sl@0: typedef value_type & reference; sl@0: typedef const value_type & const_reference; sl@0: typedef typename pool::size_type size_type; sl@0: typedef typename pool::difference_type difference_type; sl@0: sl@0: template sl@0: struct rebind sl@0: { sl@0: typedef fast_pool_allocator other; sl@0: }; sl@0: sl@0: public: sl@0: fast_pool_allocator() { } sl@0: sl@0: // default copy constructor sl@0: sl@0: // default assignment operator sl@0: sl@0: // not explicit, mimicking std::allocator [20.4.1] sl@0: template sl@0: fast_pool_allocator( sl@0: const fast_pool_allocator &) sl@0: { } sl@0: sl@0: // default destructor sl@0: sl@0: static pointer address(reference r) sl@0: { return &r; } sl@0: static const_pointer address(const_reference s) sl@0: { return &s; } sl@0: static size_type max_size() sl@0: { return (std::numeric_limits::max)(); } sl@0: void construct(const pointer ptr, const value_type & t) sl@0: { new (ptr) T(t); } sl@0: void destroy(const pointer ptr) sl@0: { sl@0: ptr->~T(); sl@0: (void) ptr; // avoid unused variable warning sl@0: } sl@0: sl@0: bool operator==(const fast_pool_allocator &) const sl@0: { return true; } sl@0: bool operator!=(const fast_pool_allocator &) const sl@0: { return false; } sl@0: sl@0: static pointer allocate(const size_type n) sl@0: { sl@0: const pointer ret = (n == 1) ? sl@0: static_cast( sl@0: singleton_pool::malloc() ) : sl@0: static_cast( sl@0: singleton_pool::ordered_malloc(n) ); sl@0: if (ret == 0) sl@0: throw std::bad_alloc(); sl@0: return ret; sl@0: } sl@0: static pointer allocate(const size_type n, const void * const) sl@0: { return allocate(n); } sl@0: static pointer allocate() sl@0: { sl@0: const pointer ret = static_cast( sl@0: singleton_pool::malloc() ); sl@0: if (ret == 0) sl@0: throw std::bad_alloc(); sl@0: return ret; sl@0: } sl@0: static void deallocate(const pointer ptr, const size_type n) sl@0: { sl@0: #ifdef BOOST_NO_PROPER_STL_DEALLOCATE sl@0: if (ptr == 0 || n == 0) sl@0: return; sl@0: #endif sl@0: if (n == 1) sl@0: singleton_pool::free(ptr); sl@0: else sl@0: singleton_pool::free(ptr, n); sl@0: } sl@0: static void deallocate(const pointer ptr) sl@0: { sl@0: singleton_pool::free(ptr); sl@0: } sl@0: }; sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif