Files
2025-10-25 03:02:53 +03:00

6.3 KiB

[mem.res.class]

20 Memory management library [mem]

20.5 Memory resources [mem.res]

20.5.2 Class memory_resource [mem.res.class]

20.5.2.1 General [mem.res.class.general]

1

#

The memory_resource class is an abstract interface to an unbounded set of classes encapsulating memory resources.

🔗

namespace std::pmr {class memory_resource {static constexpr size_t max_align = alignof(max_align_t); // exposition onlypublic: 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 Public member functions [mem.res.public]

🔗

~memory_resource();

1

#

Effects: Destroys this memory_resource.

🔗

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

2

#

Effects: Allocates storage by calling do_allocate(bytes, alignment) and implicitly creates objects within the allocated region of storage.

3

#

Returns: A pointer to a suitable created object ([intro.object]) in the allocated region of storage.

4

#

Throws: What and when the call to do_allocate throws.

🔗

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

5

#

Effects: Equivalent to do_deallocate(p, bytes, alignment).

🔗

bool is_equal(const memory_resource& other) const noexcept;

6

#

Effects: Equivalent to: return do_is_equal(other);

20.5.2.3 Private virtual member functions [mem.res.private]

🔗

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

1

#

Preconditions: alignment is a power of two.

2

#

Returns: A derived class shall implement this function to return a pointer to allocated storage ([basic.stc.dynamic.allocation]) with a size of at least bytes, aligned to the specified alignment.

3

#

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, size_t bytes, size_t alignment) = 0;

4

#

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.

5

#

Effects: A derived class shall implement this function to dispose of allocated storage.

6

#

Throws: Nothing.

🔗

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

7

#

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.

[Note 1:

It is possible that the most-derived type of other does not match the type of *this.

For a derived class D, an implementation of this function can immediately return false if dynamic_cast<const D*>(&other) == nullptr.

— end note]

20.5.2.4 Equality [mem.res.eq]

🔗

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

1

#

Returns: &a == &b || a.is_equal(b).