123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- #ifndef BOOST_POOL_ALLOC_HPP
- #define BOOST_POOL_ALLOC_HPP
- #include <boost/limits.hpp>
- #include <new>
- #include <boost/throw_exception.hpp>
- #include <boost/pool/poolfwd.hpp>
- #include <boost/pool/singleton_pool.hpp>
- #include <boost/detail/workaround.hpp>
- #include <boost/config.hpp>
- #ifdef BOOST_HAS_VARIADIC_TMPL
- #include <utility>
- #endif
- #ifdef BOOST_POOL_INSTRUMENT
- #include <iostream>
- #include <iomanip>
- #endif
- #if defined(_RWSTD_VER) || defined(__SGI_STL_PORT) || \
- BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x582))
- #define BOOST_NO_PROPER_STL_DEALLOCATE
- #endif
- namespace boost {
- #ifdef BOOST_POOL_INSTRUMENT
- template <bool b>
- struct debug_info
- {
- static unsigned allocated;
- };
- template <bool b>
- unsigned debug_info<b>::allocated = 0;
- #endif
-
-
- struct pool_allocator_tag
- {
- };
- template <typename T,
- typename UserAllocator,
- typename Mutex,
- unsigned NextSize,
- unsigned MaxSize >
- class pool_allocator
- {
- public:
- typedef T value_type;
- typedef UserAllocator user_allocator;
- typedef Mutex mutex;
- BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
- typedef value_type * pointer;
- typedef const value_type * const_pointer;
- typedef value_type & reference;
- typedef const value_type & const_reference;
- typedef typename pool<UserAllocator>::size_type size_type;
- typedef typename pool<UserAllocator>::difference_type difference_type;
-
-
-
-
-
-
- template <typename U>
- struct rebind
- {
- typedef pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
- };
- public:
- pool_allocator()
- {
- singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
- NextSize, MaxSize>::is_from(0);
- }
-
-
-
- template <typename U>
- pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
- {
- singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
- NextSize, MaxSize>::is_from(0);
- }
-
- static pointer address(reference r)
- { return &r; }
- static const_pointer address(const_reference s)
- { return &s; }
- static size_type max_size()
- { return (std::numeric_limits<size_type>::max)(); }
- #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
- template <typename U, typename... Args>
- static void construct(U* ptr, Args&&... args)
- { new (ptr) U(std::forward<Args>(args)...); }
- #else
- static void construct(const pointer ptr, const value_type & t)
- { new (ptr) T(t); }
- #endif
- static void destroy(const pointer ptr)
- {
- ptr->~T();
- (void) ptr;
- }
- bool operator==(const pool_allocator &) const
- { return true; }
- bool operator!=(const pool_allocator &) const
- { return false; }
- static pointer allocate(const size_type n)
- {
- #ifdef BOOST_POOL_INSTRUMENT
- debug_info<true>::allocated += n * sizeof(T);
- std::cout << "Allocating " << n << " * " << sizeof(T) << " bytes...\n"
- "Total allocated is now " << debug_info<true>::allocated << std::endl;
- #endif
- const pointer ret = static_cast<pointer>(
- singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
- NextSize, MaxSize>::ordered_malloc(n) );
- if ((ret == 0) && n)
- boost::throw_exception(std::bad_alloc());
- return ret;
- }
- static pointer allocate(const size_type n, const void * const)
- {
-
-
- return allocate(n);
- }
- static void deallocate(const pointer ptr, const size_type n)
- {
-
-
- #ifdef BOOST_POOL_INSTRUMENT
- debug_info<true>::allocated -= n * sizeof(T);
- std::cout << "Deallocating " << n << " * " << sizeof(T) << " bytes...\n"
- "Total allocated is now " << debug_info<true>::allocated << std::endl;
- #endif
- #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
- if (ptr == 0 || n == 0)
- return;
- #endif
- singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
- NextSize, MaxSize>::ordered_free(ptr, n);
- }
- };
- template<
- typename UserAllocator,
- typename Mutex,
- unsigned NextSize,
- unsigned MaxSize>
- class pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>
- {
- public:
- typedef void* pointer;
- typedef const void* const_pointer;
- typedef void value_type;
-
-
-
-
-
-
- template <class U>
- struct rebind
- {
- typedef pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
- };
- };
- struct fast_pool_allocator_tag
- {
- };
-
- template <typename T,
- typename UserAllocator,
- typename Mutex,
- unsigned NextSize,
- unsigned MaxSize >
- class fast_pool_allocator
- {
- public:
- typedef T value_type;
- typedef UserAllocator user_allocator;
- typedef Mutex mutex;
- BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
- typedef value_type * pointer;
- typedef const value_type * const_pointer;
- typedef value_type & reference;
- typedef const value_type & const_reference;
- typedef typename pool<UserAllocator>::size_type size_type;
- typedef typename pool<UserAllocator>::difference_type difference_type;
-
-
-
-
-
-
- template <typename U>
- struct rebind
- {
- typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
- };
- public:
- fast_pool_allocator()
- {
-
-
-
-
- singleton_pool<fast_pool_allocator_tag, sizeof(T),
- UserAllocator, Mutex, NextSize, MaxSize>::is_from(0);
- }
-
-
-
- template <typename U>
- fast_pool_allocator(
- const fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
- {
-
-
-
-
- singleton_pool<fast_pool_allocator_tag, sizeof(T),
- UserAllocator, Mutex, NextSize, MaxSize>::is_from(0);
- }
-
- static pointer address(reference r)
- {
- return &r;
- }
- static const_pointer address(const_reference s)
- { return &s; }
- static size_type max_size()
- { return (std::numeric_limits<size_type>::max)(); }
- #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS)
- template <typename U, typename... Args>
- void construct(U* ptr, Args&&... args)
- { new (ptr) U(std::forward<Args>(args)...); }
- #else
- void construct(const pointer ptr, const value_type & t)
- { new (ptr) T(t); }
- #endif
- void destroy(const pointer ptr)
- {
- ptr->~T();
- (void) ptr;
- }
- bool operator==(const fast_pool_allocator &) const
- { return true; }
- bool operator!=(const fast_pool_allocator &) const
- { return false; }
- static pointer allocate(const size_type n)
- {
- const pointer ret = (n == 1) ?
- static_cast<pointer>(
- (singleton_pool<fast_pool_allocator_tag, sizeof(T),
- UserAllocator, Mutex, NextSize, MaxSize>::malloc)() ) :
- static_cast<pointer>(
- singleton_pool<fast_pool_allocator_tag, sizeof(T),
- UserAllocator, Mutex, NextSize, MaxSize>::ordered_malloc(n) );
- if (ret == 0)
- boost::throw_exception(std::bad_alloc());
- return ret;
- }
- static pointer allocate(const size_type n, const void * const)
- {
- return allocate(n);
- }
- static pointer allocate()
- {
- const pointer ret = static_cast<pointer>(
- (singleton_pool<fast_pool_allocator_tag, sizeof(T),
- UserAllocator, Mutex, NextSize, MaxSize>::malloc)() );
- if (ret == 0)
- boost::throw_exception(std::bad_alloc());
- return ret;
- }
- static void deallocate(const pointer ptr, const size_type n)
- {
- #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
- if (ptr == 0 || n == 0)
- return;
- #endif
- if (n == 1)
- (singleton_pool<fast_pool_allocator_tag, sizeof(T),
- UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr);
- else
- (singleton_pool<fast_pool_allocator_tag, sizeof(T),
- UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr, n);
- }
- static void deallocate(const pointer ptr)
- {
- (singleton_pool<fast_pool_allocator_tag, sizeof(T),
- UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr);
- }
- };
- template<
- typename UserAllocator,
- typename Mutex,
- unsigned NextSize,
- unsigned MaxSize >
- class fast_pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>
- {
- public:
- typedef void* pointer;
- typedef const void* const_pointer;
- typedef void value_type;
-
-
-
-
-
-
- template <class U> struct rebind
- {
- typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
- };
- };
- }
- #endif
|