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/move/detail/type_traits.hpp> 21 #include <cstddef> 22 23 namespace boost { 24 namespace container { 25 namespace pmr { 26 27 //! The memory_resource class is an abstract interface to an 28 //! unbounded set of classes encapsulating memory resources. 29 class memory_resource 30 { 31 public: 32 // For exposition only 33 static BOOST_CONSTEXPR_OR_CONST std::size_t max_align = 34 boost::move_detail::alignment_of<boost::move_detail::max_align_t>::value; 35 36 //! <b>Effects</b>: Destroys 37 //! this memory_resource. ~memory_resource()38 virtual ~memory_resource(){} 39 40 //! <b>Effects</b>: Equivalent to 41 //! `return do_allocate(bytes, alignment);` allocate(std::size_t bytes,std::size_t alignment=max_align)42 void* allocate(std::size_t bytes, std::size_t alignment = max_align) 43 { return this->do_allocate(bytes, alignment); } 44 45 //! <b>Effects</b>: Equivalent to 46 //! `return do_deallocate(bytes, alignment);` deallocate(void * p,std::size_t bytes,std::size_t alignment=max_align)47 void deallocate(void* p, std::size_t bytes, std::size_t alignment = max_align) 48 { return this->do_deallocate(p, bytes, alignment); } 49 50 //! <b>Effects</b>: Equivalent to 51 //! `return return do_is_equal(other);` is_equal(const memory_resource & other) const52 bool is_equal(const memory_resource& other) const BOOST_NOEXCEPT 53 { return this->do_is_equal(other); } 54 55 //! <b>Returns</b>: 56 //! `&a == &b || a.is_equal(b)`. operator ==(const memory_resource & a,const memory_resource & b)57 friend bool operator==(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT 58 { return &a == &b || a.is_equal(b); } 59 60 //! <b>Returns</b>: 61 //! !(a == b). operator !=(const memory_resource & a,const memory_resource & b)62 friend bool operator!=(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT 63 { return !(a == b); } 64 65 protected: 66 //! <b>Requires</b>: Alignment shall be a power of two. 67 //! 68 //! <b>Returns</b>: A derived class shall implement this function to return a pointer 69 //! to allocated storage with a size of at least bytes. The returned storage is 70 //! aligned to the specified alignment, if such alignment is supported; otherwise 71 //! it is aligned to max_align. 72 //! 73 //! <b>Throws</b>: A derived class implementation shall throw an appropriate exception if 74 //! it is unable to allocate memory with the requested size and alignment. 75 virtual void* do_allocate(std::size_t bytes, std::size_t alignment) = 0; 76 77 //! <b>Requires</b>: p shall have been returned from a prior call to 78 //! `allocate(bytes, alignment)` on a memory resource equal to *this, and the storage 79 //! at p shall not yet have been deallocated. 80 //! 81 //! <b>Effects</b>: A derived class shall implement this function to dispose of allocated storage. 82 //! 83 //! <b>Throws</b>: Nothing. 84 virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) = 0; 85 86 //! <b>Returns</b>: A derived class shall implement this function to return true if memory 87 //! allocated from this can be deallocated from other and vice-versa; otherwise it shall 88 //! return false. <i>[Note: The most-derived type of other might not match the type of this. 89 //! For a derived class, D, a typical implementation of this function will compute 90 //! `dynamic_cast<const D*>(&other)` and go no further (i.e., return false) 91 //! if it returns nullptr. - end note]</i>. 92 virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT = 0; 93 }; 94 95 } //namespace pmr { 96 } //namespace container { 97 } //namespace boost { 98 99 #include <boost/container/detail/config_end.hpp> 100 101 #endif //BOOST_CONTAINER_PMR_MEMORY_RESOURCE_HPP 102