• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/container for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef BOOST_CONTAINER_PMR_MEMORY_RESOURCE_HPP
12 #define BOOST_CONTAINER_PMR_MEMORY_RESOURCE_HPP
13 
14 #if defined (_MSC_VER)
15 #  pragma once
16 #endif
17 
18 #include <boost/container/detail/config_begin.hpp>
19 #include <boost/container/detail/workaround.hpp>
20 #include <boost/container/container_fwd.hpp>
21 #include <boost/move/detail/type_traits.hpp>
22 #include <cstddef>
23 
24 namespace boost {
25 namespace container {
26 namespace pmr {
27 
28 //! The memory_resource class is an abstract interface to an
29 //! unbounded set of classes encapsulating memory resources.
30 class BOOST_CONTAINER_DECL memory_resource
31 {
32    public:
33    // For exposition only
34    static BOOST_CONSTEXPR_OR_CONST std::size_t max_align =
35       boost::move_detail::alignment_of<boost::move_detail::max_align_t>::value;
36 
37    //! <b>Effects</b>: Destroys
38    //! this memory_resource.
~memory_resource()39    virtual ~memory_resource(){}
40 
41    //! <b>Effects</b>: Equivalent to
42    //! `return do_allocate(bytes, alignment);`
allocate(std::size_t bytes,std::size_t alignment=max_align)43    void* allocate(std::size_t bytes, std::size_t alignment = max_align)
44    {  return this->do_allocate(bytes, alignment);  }
45 
46    //! <b>Effects</b>: Equivalent to
47    //! `return do_deallocate(bytes, alignment);`
deallocate(void * p,std::size_t bytes,std::size_t alignment=max_align)48    void  deallocate(void* p, std::size_t bytes, std::size_t alignment = max_align)
49    {  return this->do_deallocate(p, bytes, alignment);  }
50 
51    //! <b>Effects</b>: Equivalent to
52    //! `return return do_is_equal(other);`
is_equal(const memory_resource & other) const53    bool is_equal(const memory_resource& other) const BOOST_NOEXCEPT
54    {  return this->do_is_equal(other);  }
55 
56    #if !defined(BOOST_EMBTC)
57 
58    //! <b>Returns</b>:
59    //!   `&a == &b || a.is_equal(b)`.
operator ==(const memory_resource & a,const memory_resource & b)60    friend bool operator==(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
61    {  return &a == &b || a.is_equal(b);   }
62 
63    //! <b>Returns</b>:
64    //!   !(a == b).
operator !=(const memory_resource & a,const memory_resource & b)65    friend bool operator!=(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
66    {  return !(a == b); }
67 
68    #else
69 
70    //! <b>Returns</b>:
71    //!   `&a == &b || a.is_equal(b)`.
72    friend bool operator==(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT;
73 
74    //! <b>Returns</b>:
75    //!   !(a == b).
76    friend bool operator!=(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT;
77 
78    #endif
79 
80    protected:
81    //! <b>Requires</b>: Alignment shall be a power of two.
82    //!
83    //! <b>Returns</b>: A derived class shall implement this function to return a pointer
84    //!   to allocated storage with a size of at least bytes. The returned storage is
85    //!   aligned to the specified alignment, if such alignment is supported; otherwise
86    //!   it is aligned to max_align.
87    //!
88    //! <b>Throws</b>: A derived class implementation shall throw an appropriate exception if
89    //!   it is unable to allocate memory with the requested size and alignment.
90    virtual void* do_allocate(std::size_t bytes, std::size_t alignment) = 0;
91 
92    //! <b>Requires</b>: p shall have been returned from a prior call to
93    //!   `allocate(bytes, alignment)` on a memory resource equal to *this, and the storage
94    //!   at p shall not yet have been deallocated.
95    //!
96    //! <b>Effects</b>: A derived class shall implement this function to dispose of allocated storage.
97    //!
98    //! <b>Throws</b>: Nothing.
99    virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) = 0;
100 
101    //! <b>Returns</b>: A derived class shall implement this function to return true if memory
102    //!   allocated from this can be deallocated from other and vice-versa; otherwise it shall
103    //!   return false. <i>[Note: The most-derived type of other might not match the type of this.
104    //!   For a derived class, D, a typical implementation of this function will compute
105    //!   `dynamic_cast<const D*>(&other)` and go no further (i.e., return false)
106    //!   if it returns nullptr. - end note]</i>.
107    virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT = 0;
108 };
109 
110 #if defined(BOOST_EMBTC)
111 
112 //! <b>Returns</b>:
113 //!   `&a == &b || a.is_equal(b)`.
operator ==(const memory_resource & a,const memory_resource & b)114 inline bool operator==(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
115 {  return &a == &b || a.is_equal(b);   }
116 
117 //! <b>Returns</b>:
118 //!   !(a == b).
operator !=(const memory_resource & a,const memory_resource & b)119 inline bool operator!=(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
120 {  return !(a == b); }
121 
122 #endif
123 
124 }  //namespace pmr {
125 }  //namespace container {
126 }  //namespace boost {
127 
128 #include <boost/container/detail/config_end.hpp>
129 
130 #endif   //BOOST_CONTAINER_PMR_MEMORY_RESOURCE_HPP
131