From 8f7107433955df7bedb10cc4859249d41d1edac9 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 30 Jan 2003 14:20:22 +0000 Subject: [PATCH] Dave's quick_allocator added, #define BOOST_SP_USE_QUICK_ALLOCATOR to make shared_ptr use it. [SVN r17087] --- include/boost/detail/quick_allocator.hpp | 145 +++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 include/boost/detail/quick_allocator.hpp diff --git a/include/boost/detail/quick_allocator.hpp b/include/boost/detail/quick_allocator.hpp new file mode 100644 index 0000000..66ba3d8 --- /dev/null +++ b/include/boost/detail/quick_allocator.hpp @@ -0,0 +1,145 @@ +#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