2003-01-30 14:20:22 +00:00
|
|
|
#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 <boost/config.hpp>
|
|
|
|
|
|
|
|
#include <boost/detail/lightweight_mutex.hpp>
|
|
|
|
#include <boost/type_traits/type_with_alignment.hpp>
|
|
|
|
#include <boost/type_traits/alignment_of.hpp>
|
|
|
|
|
|
|
|
#include <new> // ::operator new, ::operator delete
|
|
|
|
#include <cstddef> // std::size_t
|
|
|
|
|
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
2003-02-22 13:40:23 +00:00
|
|
|
template<unsigned size, unsigned align_> union freeblock
|
2003-01-30 14:20:22 +00:00
|
|
|
{
|
2003-02-22 13:40:23 +00:00
|
|
|
typedef typename boost::type_with_alignment<align_>::type aligner_type;
|
2003-01-30 14:20:22 +00:00
|
|
|
aligner_type aligner;
|
|
|
|
char bytes[size];
|
|
|
|
freeblock * next;
|
|
|
|
};
|
|
|
|
|
2003-02-22 13:40:23 +00:00
|
|
|
template<unsigned size, unsigned align_> struct allocator_impl
|
2003-01-30 14:20:22 +00:00
|
|
|
{
|
2003-02-22 13:40:23 +00:00
|
|
|
typedef freeblock<size, align_> block;
|
2003-01-30 14:20:22 +00:00
|
|
|
|
2003-02-07 15:08:52 +00:00
|
|
|
// It may seem odd to use such small pages.
|
|
|
|
//
|
|
|
|
// However, on a typical Windows implementation that uses
|
|
|
|
// the OS allocator, "normal size" pages interact with the
|
|
|
|
// "ordinary" operator new, slowing it down dramatically.
|
|
|
|
//
|
|
|
|
// 512 byte pages are handled by the small object allocator,
|
|
|
|
// and don't interfere with ::new.
|
|
|
|
//
|
|
|
|
// The other alternative is to use much bigger pages (1M.)
|
2003-02-07 19:06:28 +00:00
|
|
|
//
|
|
|
|
// It is surprisingly easy to hit pathological behavior by
|
|
|
|
// varying the page size. g++ 2.96 on Red Hat Linux 7.2,
|
|
|
|
// for example, passionately dislikes 496. 512 seems OK.
|
2003-02-07 15:08:52 +00:00
|
|
|
|
2003-02-07 18:43:48 +00:00
|
|
|
#if defined(BOOST_QA_PAGE_SIZE)
|
|
|
|
|
|
|
|
enum { items_per_page = BOOST_QA_PAGE_SIZE / size };
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2003-02-07 19:06:28 +00:00
|
|
|
enum { items_per_page = 512 / size }; // 1048560 / size
|
2003-02-07 15:08:52 +00:00
|
|
|
|
2003-02-07 18:43:48 +00:00
|
|
|
#endif
|
|
|
|
|
2003-02-07 15:08:52 +00:00
|
|
|
#ifdef BOOST_HAS_THREADS
|
2003-01-30 14:20:22 +00:00
|
|
|
static lightweight_mutex mutex;
|
2003-02-07 15:08:52 +00:00
|
|
|
#endif
|
|
|
|
|
2003-01-30 14:20:22 +00:00
|
|
|
static block * free;
|
2003-02-07 15:08:52 +00:00
|
|
|
static block * page;
|
|
|
|
static unsigned last;
|
|
|
|
|
2003-01-30 14:20:22 +00:00
|
|
|
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
|
|
|
|
{
|
2003-02-07 15:08:52 +00:00
|
|
|
if(last == items_per_page)
|
|
|
|
{
|
|
|
|
// "Listen to me carefully: there is no memory leak"
|
|
|
|
// -- Scott Meyers, Eff C++ 2nd Ed Item 10
|
|
|
|
page = ::new block[items_per_page];
|
|
|
|
last = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &page[last++];
|
2003-01-30 14:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2003-02-07 15:08:52 +00:00
|
|
|
if(last == items_per_page)
|
|
|
|
{
|
|
|
|
page = ::new block[items_per_page];
|
|
|
|
last = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &page[last++];
|
2003-01-30 14:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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<block *>(pv);
|
|
|
|
pb->next = free;
|
|
|
|
free = pb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void dealloc(void * pv, std::size_t n)
|
|
|
|
{
|
2003-02-07 15:08:52 +00:00
|
|
|
if(n != size) // class-specific delete called for a derived object
|
|
|
|
{
|
|
|
|
::operator delete(pv);
|
|
|
|
}
|
|
|
|
else if(pv != 0) // 18.4.1.1/13
|
2003-01-30 14:20:22 +00:00
|
|
|
{
|
|
|
|
#ifdef BOOST_HAS_THREADS
|
2003-02-07 15:08:52 +00:00
|
|
|
lightweight_mutex::scoped_lock lock(mutex);
|
2003-01-30 14:20:22 +00:00
|
|
|
#endif
|
2003-02-07 15:08:52 +00:00
|
|
|
block * pb = static_cast<block *>(pv);
|
|
|
|
pb->next = free;
|
|
|
|
free = pb;
|
2003-01-30 14:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-02-07 15:08:52 +00:00
|
|
|
#ifdef BOOST_HAS_THREADS
|
2003-02-22 13:40:23 +00:00
|
|
|
template<unsigned size, unsigned align_>
|
|
|
|
lightweight_mutex allocator_impl<size, align_>::mutex;
|
2003-02-07 15:08:52 +00:00
|
|
|
#endif
|
2003-01-30 14:20:22 +00:00
|
|
|
|
2003-02-22 13:40:23 +00:00
|
|
|
template<unsigned size, unsigned align_>
|
|
|
|
freeblock<size, align_> * allocator_impl<size, align_>::free = 0;
|
2003-01-30 14:20:22 +00:00
|
|
|
|
2003-02-22 13:40:23 +00:00
|
|
|
template<unsigned size, unsigned align_>
|
|
|
|
freeblock<size, align_> * allocator_impl<size, align_>::page = 0;
|
2003-02-07 15:08:52 +00:00
|
|
|
|
2003-02-22 13:40:23 +00:00
|
|
|
template<unsigned size, unsigned align_>
|
|
|
|
unsigned allocator_impl<size, align_>::last = allocator_impl<size, align_>::items_per_page;
|
2003-01-30 14:20:22 +00:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct quick_allocator: public allocator_impl< sizeof(T), boost::alignment_of<T>::value >
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
} // namespace boost
|
|
|
|
|
|
|
|
#endif // #ifndef BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
|