[mem.res.class]

# 20 Memory management library [[mem]](./#mem)

## 20.5 Memory resources [[mem.res]](mem.res#class)

### 20.5.2 Class memory_resource [mem.res.class]

#### [20.5.2.1](#general) General [[mem.res.class.general]](mem.res.class.general)

[1](#general-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7484)

The memory_resource class is an abstract interface to an unbounded set of classes encapsulating memory resources[.](#general-1.sentence-1)

[ð](#lib:memory_resource)

namespace std::pmr {class memory_resource {static constexpr size_t max_align = alignof(max_align_t); // *exposition only*public: memory_resource() = default;
 memory_resource(const memory_resource&) = default; virtual ~memory_resource();

 memory_resource& operator=(const memory_resource&) = default; void* allocate(size_t bytes, size_t alignment = max_align); void deallocate(void* p, size_t bytes, size_t alignment = max_align); bool is_equal(const memory_resource& other) const noexcept; private:virtual void* do_allocate(size_t bytes, size_t alignment) = 0; virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0; virtual bool do_is_equal(const memory_resource& other) const noexcept = 0; };}

#### [20.5.2.2](#mem.res.public) Public member functions [[mem.res.public]](mem.res.public)

[ð](#lib:memory_resource,destructor)

`~memory_resource();
`

[1](#mem.res.public-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7524)

*Effects*: Destroys this memory_resource[.](#mem.res.public-1.sentence-1)

[ð](#lib:allocate,memory_resource)

`void* allocate(size_t bytes, size_t alignment = max_align);
`

[2](#mem.res.public-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7535)

*Effects*: Allocates storage by calling do_allocate(bytes, alignment) and
implicitly creates objects within the allocated region of storage[.](#mem.res.public-2.sentence-1)

[3](#mem.res.public-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7540)

*Returns*: A pointer to a suitable created object ([[intro.object]](intro.object "6.8.2 Object model"))
in the allocated region of storage[.](#mem.res.public-3.sentence-1)

[4](#mem.res.public-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7545)

*Throws*: What and when the call to do_allocate throws[.](#mem.res.public-4.sentence-1)

[ð](#lib:deallocate,memory_resource)

`void deallocate(void* p, size_t bytes, size_t alignment = max_align);
`

[5](#mem.res.public-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7556)

*Effects*: Equivalent to do_deallocate(p, bytes, alignment)[.](#mem.res.public-5.sentence-1)

[ð](#lib:is_equal,memory_resource)

`bool is_equal(const memory_resource& other) const noexcept;
`

[6](#mem.res.public-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7567)

*Effects*: Equivalent to: return do_is_equal(other);

#### [20.5.2.3](#mem.res.private) Private virtual member functions [[mem.res.private]](mem.res.private)

[ð](#lib:do_allocate,memory_resource)

`virtual void* do_allocate(size_t bytes, size_t alignment) = 0;
`

[1](#mem.res.private-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7581)

*Preconditions*: alignment is a power of two[.](#mem.res.private-1.sentence-1)

[2](#mem.res.private-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7585)

*Returns*: A derived class shall implement this function to
return a pointer to allocated storage ([[basic.stc.dynamic.allocation]](basic.stc.dynamic.allocation "6.8.6.5.2 Allocation functions"))
with a size of at least bytes,
aligned to the specified alignment[.](#mem.res.private-2.sentence-1)

[3](#mem.res.private-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7592)

*Throws*: A derived class implementation shall throw an appropriate exception if it is unable to allocate memory with the requested size and alignment[.](#mem.res.private-3.sentence-1)

[ð](#lib:do_deallocate,memory_resource)

`virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0;
`

[4](#mem.res.private-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7603)

*Preconditions*: p was returned from a prior call to allocate(bytes, alignment) on a memory resource equal to *this,
and the storage at p has not yet been deallocated[.](#mem.res.private-4.sentence-1)

[5](#mem.res.private-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7609)

*Effects*: A derived class shall implement this function to dispose of allocated storage[.](#mem.res.private-5.sentence-1)

[6](#mem.res.private-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7613)

*Throws*: Nothing[.](#mem.res.private-6.sentence-1)

[ð](#lib:do_is_equal,memory_resource)

`virtual bool do_is_equal(const memory_resource& other) const noexcept = 0;
`

[7](#mem.res.private-7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7624)

*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 false[.](#mem.res.private-7.sentence-1)

[*Note [1](#mem.res.private-note-1)*:

It is possible that the most-derived type of other does not match the type of *this[.](#mem.res.private-7.sentence-2)

For a derived class D, an implementation of this function
can immediately return false if dynamic_cast<const D*>(&other) == nullptr[.](#mem.res.private-7.sentence-3)

â *end note*]

#### [20.5.2.4](#mem.res.eq) Equality [[mem.res.eq]](mem.res.eq)

[ð](#lib:operator==,memory_resource)

`bool operator==(const memory_resource& a, const memory_resource& b) noexcept;
`

[1](#mem.res.eq-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7644)

*Returns*: &a == &b || a.is_equal(b)[.](#mem.res.eq-1.sentence-1)
