Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/container/new_allocator.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 {  BOOST_STATIC_CONSTEXPR 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    inline new_allocator() BOOST_NOEXCEPT_OR_NOTHROW
0133    {}
0134 
0135    //!Constructor from other new_allocator.
0136    //!Never throws
0137    inline new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0138    {}
0139 
0140    //!Copy assignment operator from other new_allocator.
0141    //!Never throws
0142    inline new_allocator& operator=(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0143    {  return *this;  }
0144 
0145    //!Constructor from related new_allocator.
0146    //!Never throws
0147    template<class T2>
0148    inline new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
0149    {}
0150 
0151    //!Allocates memory for an array of count elements.
0152    //!Throws bad_alloc if there is no enough memory
0153    pointer allocate(size_type count)
0154    {
0155       const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));
0156       if(BOOST_UNLIKELY(count > max_count))
0157          throw_bad_alloc();
0158       return static_cast<T*>(::operator new(count*sizeof(T)));
0159    }
0160 
0161    //!Deallocates previously allocated memory.
0162    //!Never throws
0163    void deallocate(pointer ptr, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
0164    {
0165       (void)n;
0166       # if __cpp_sized_deallocation
0167       ::operator delete((void*)ptr, n * sizeof(T));
0168       #else
0169       ::operator delete((void*)ptr);
0170       # endif
0171    }
0172 
0173    //!Returns the maximum number of elements that could be allocated.
0174    //!Never throws
0175    inline size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
0176    {  return std::size_t(-1)/(2*sizeof(T));   }
0177 
0178    //!Swaps two allocators, does nothing
0179    //!because this new_allocator is stateless
0180    inline friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0181    {}
0182 
0183    //!An new_allocator always compares to true, as memory allocated with one
0184    //!instance can be deallocated by another instance
0185    inline friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0186    {  return true;   }
0187 
0188    //!An new_allocator always compares to false, as memory allocated with one
0189    //!instance can be deallocated by another instance
0190    inline friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0191    {  return false;   }
0192 };
0193 
0194 }  //namespace container {
0195 }  //namespace boost {
0196 
0197 #include <boost/container/detail/config_end.hpp>
0198 
0199 #endif   //BOOST_CONTAINER_NEW_ALLOCATOR_HPP