...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::container::pmr::memory_resource
// In header: <boost/container/pmr/memory_resource.hpp> class memory_resource { public: // construct/copy/destruct ~memory_resource(); // public member functions void * allocate(std::size_t, std::size_t = max_align); void deallocate(void *, std::size_t, std::size_t = max_align); bool is_equal(const memory_resource &) const noexcept; // friend functions friend bool operator==(const memory_resource &, const memory_resource &) noexcept; friend bool operator!=(const memory_resource &, const memory_resource &) noexcept; // protected member functions virtual void * do_allocate(std::size_t, std::size_t) = 0; virtual void do_deallocate(void *, std::size_t, std::size_t) = 0; virtual bool do_is_equal(const memory_resource &) const = 0; // public data members static constexpr std::size_t max_align; };
The memory_resource class is an abstract interface to an unbounded set of classes encapsulating memory resources.
memory_resource
public
construct/copy/destruct~memory_resource();
Effects: Destroys this memory_resource
.
memory_resource
public member functionsvoid * allocate(std::size_t bytes, std::size_t alignment = max_align);
Effects: Equivalent to `return do_allocate(bytes, alignment);`
void deallocate(void * p, std::size_t bytes, std::size_t alignment = max_align);
Effects: Equivalent to `return do_deallocate(bytes, alignment);`
bool is_equal(const memory_resource & other) const noexcept;
Effects: Equivalent to `return return do_is_equal(other);`
memory_resource
friend functionsfriend bool operator==(const memory_resource & a, const memory_resource & b) noexcept;
Returns: `&a == &b || a.is_equal(b)`.
friend bool operator!=(const memory_resource & a, const memory_resource & b) noexcept;
Returns: !(a == b).
memory_resource
protected member functionsvirtual void * do_allocate(std::size_t bytes, std::size_t alignment) = 0;
Requires: Alignment shall be a power of two.
Returns: A derived class shall implement this function to return a pointer to allocated storage with a size of at least bytes. The returned storage is aligned to the specified alignment, if such alignment is supported; otherwise it is aligned to max_align.
Throws: A derived class implementation shall throw an appropriate exception if it is unable to allocate memory with the requested size and alignment.
virtual void do_deallocate(void * p, std::size_t bytes, std::size_t alignment) = 0;
Requires: p shall have been returned from a prior call to `allocate(bytes, alignment)` on a memory resource equal to *this, and the storage at p shall not yet have been deallocated.
Effects: A derived class shall implement this function to dispose of allocated storage.
Throws: Nothing.
virtual bool do_is_equal(const memory_resource & other) const = 0;
Returns: A derived class shall implement this function to return true if memory allocated from this can be deallocated from other and vice-versa; otherwise it shall return false. [Note: The most-derived type of other might not match the type of this. For a derived class, D, a typical implementation of this function will compute `dynamic_cast<const D*>(&other)` and go no further (i.e., return false) if it returns nullptr. - end note].