Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:18

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2014-2015. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/container for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_CONTAINER_NEW_ALLOCATOR_HPP
0012 #define BOOST_CONTAINER_NEW_ALLOCATOR_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #include <boost/container/detail/config_begin.hpp>
0023 #include <boost/container/detail/workaround.hpp>
0024 #include <boost/container/throw_exception.hpp>
0025 #include <cstddef>
0026 
0027 //!\file
0028 
0029 namespace boost {
0030 namespace container {
0031 
0032 /// @cond
0033 
0034 template<bool Value>
0035 struct new_allocator_bool
0036 {  static const bool value = Value;  };
0037 
0038 template<class T>
0039 class new_allocator;
0040 
0041 /// @endcond
0042 
0043 //! Specialization of new_allocator for void types
0044 template<>
0045 class new_allocator<void>
0046 {
0047    public:
0048    typedef void                                 value_type;
0049    typedef void *                               pointer;
0050    typedef const void*                          const_pointer;
0051    //!A integral constant of type bool with value true
0052    typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) propagate_on_container_move_assignment;
0053    //!A integral constant of type bool with value true
0054    typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) is_always_equal;
0055    // reference-to-void members are impossible
0056 
0057    //!Obtains an new_allocator that allocates
0058    //!objects of type T2
0059    template<class T2>
0060    struct rebind
0061    {
0062       typedef new_allocator< T2> other;
0063    };
0064 
0065    //!Default constructor
0066    //!Never throws
0067    new_allocator() BOOST_NOEXCEPT_OR_NOTHROW
0068    {}
0069 
0070    //!Constructor from other new_allocator.
0071    //!Never throws
0072    new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0073    {}
0074 
0075    //!Copy assignment operator from other new_allocator.
0076    //!Never throws
0077    new_allocator& operator=(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0078    {
0079        return *this;
0080    }
0081 
0082    //!Constructor from related new_allocator.
0083    //!Never throws
0084    template<class T2>
0085    new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
0086    {}
0087 
0088    //!Swaps two allocators, does nothing
0089    //!because this new_allocator is stateless
0090    friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0091    {}
0092 
0093    //!An new_allocator always compares to true, as memory allocated with one
0094    //!instance can be deallocated by another instance
0095    friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0096    {  return true;   }
0097 
0098    //!An new_allocator always compares to false, as memory allocated with one
0099    //!instance can be deallocated by another instance
0100    friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0101    {  return false;   }
0102 };
0103 
0104 
0105 //! This class is a reduced STL-compatible allocator that allocates memory using operator new
0106 template<class T>
0107 class new_allocator
0108 {
0109    public:
0110    typedef T                                    value_type;
0111    typedef T *                                  pointer;
0112    typedef const T *                            const_pointer;
0113    typedef T &                                  reference;
0114    typedef const T &                            const_reference;
0115    typedef std::size_t                          size_type;
0116    typedef std::ptrdiff_t                       difference_type;
0117    //!A integral constant of type bool with value true
0118    typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) propagate_on_container_move_assignment;
0119    //!A integral constant of type bool with value true
0120    typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) is_always_equal;
0121 
0122    //!Obtains an new_allocator that allocates
0123    //!objects of type T2
0124    template<class T2>
0125    struct rebind
0126    {
0127       typedef new_allocator<T2> other;
0128    };
0129 
0130    //!Default constructor
0131    //!Never throws
0132    new_allocator() BOOST_NOEXCEPT_OR_NOTHROW
0133    {}
0134 
0135    //!Constructor from other new_allocator.
0136    //!Never throws
0137    new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0138    {}
0139 
0140    //!Copy assignment operator from other new_allocator.
0141    //!Never throws
0142    new_allocator& operator=(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0143    {
0144        return *this;
0145    }
0146 
0147    //!Constructor from related new_allocator.
0148    //!Never throws
0149    template<class T2>
0150    new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
0151    {}
0152 
0153    //!Allocates memory for an array of count elements.
0154    //!Throws bad_alloc if there is no enough memory
0155    pointer allocate(size_type count)
0156    {
0157       const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));
0158       if(BOOST_UNLIKELY(count > max_count))
0159          throw_bad_alloc();
0160       return static_cast<T*>(::operator new(count*sizeof(T)));
0161    }
0162 
0163    //!Deallocates previously allocated memory.
0164    //!Never throws
0165    void deallocate(pointer ptr, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
0166    {
0167       (void)n;
0168       # if __cpp_sized_deallocation
0169       ::operator delete((void*)ptr, n * sizeof(T));
0170       #else
0171       ::operator delete((void*)ptr);
0172       # endif
0173    }
0174 
0175    //!Returns the maximum number of elements that could be allocated.
0176    //!Never throws
0177    size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
0178    {  return std::size_t(-1)/(2*sizeof(T));   }
0179 
0180    //!Swaps two allocators, does nothing
0181    //!because this new_allocator is stateless
0182    friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0183    {}
0184 
0185    //!An new_allocator always compares to true, as memory allocated with one
0186    //!instance can be deallocated by another instance
0187    friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0188    {  return true;   }
0189 
0190    //!An new_allocator always compares to false, as memory allocated with one
0191    //!instance can be deallocated by another instance
0192    friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0193    {  return false;   }
0194 };
0195 
0196 }  //namespace container {
0197 }  //namespace boost {
0198 
0199 #include <boost/container/detail/config_end.hpp>
0200 
0201 #endif   //BOOST_CONTAINER_NEW_ALLOCATOR_HPP