#ifndef BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED #define BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED #if _MSC_VER >= 1020 #pragma once #endif // // detail/quick_allocator.hpp // // Copyright (c) 2003 David Abrahams // Copyright (c) 2003 Peter Dimov // // Permission to copy, use, modify, sell and distribute this software // is granted provided this copyright notice appears in all copies. // This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. // #include #include #include #include #include #include // ::operator new, ::operator delete #include // std::size_t namespace boost { namespace detail { template union freeblock { typedef typename boost::type_with_alignment::type aligner_type; aligner_type aligner; char bytes[size]; freeblock * next; }; template struct allocator_impl { typedef freeblock block; static lightweight_mutex mutex; static std::deque store; static block * free; static inline void * alloc() { #ifdef BOOST_HAS_THREADS lightweight_mutex::scoped_lock lock(mutex); #endif if(block * x = free) { free = x->next; return x; } else { store.resize(store.size() + 1); return &store.back(); } } static inline void * alloc(std::size_t n) { if(n != size) // class-specific new called for a derived object { return ::operator new(n); } else { #ifdef BOOST_HAS_THREADS lightweight_mutex::scoped_lock lock(mutex); #endif if(block * x = free) { free = x->next; return x; } else { store.resize(store.size() + 1); return &store.back(); } } } static inline void dealloc(void * pv) { if(pv != 0) // 18.4.1.1/13 { #ifdef BOOST_HAS_THREADS lightweight_mutex::scoped_lock lock(mutex); #endif block * pb = static_cast(pv); pb->next = free; free = pb; } } static inline void dealloc(void * pv, std::size_t n) { if(pv != 0) // 18.4.1.1/13 { if(n != size) // class-specific delete called for a derived object { ::operator delete(pv); } else { #ifdef BOOST_HAS_THREADS lightweight_mutex::scoped_lock lock(mutex); #endif block * pb = static_cast(pv); pb->next = free; free = pb; } } } }; template lightweight_mutex allocator_impl::mutex; template std::deque< freeblock > allocator_impl::store; template freeblock * allocator_impl::free = 0; template struct quick_allocator: public allocator_impl< sizeof(T), boost::alignment_of::value > { }; } // namespace detail } // namespace boost #endif // #ifndef BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED