tmp/tmp_lbji8vi/{from.md → to.md}
RENAMED
|
@@ -2,80 +2,84 @@
|
|
| 2 |
|
| 3 |
The `memory_resource` class is an abstract interface to an unbounded set
|
| 4 |
of classes encapsulating memory resources.
|
| 5 |
|
| 6 |
``` cpp
|
|
|
|
| 7 |
class memory_resource {
|
| 8 |
static constexpr size_t max_align = alignof(max_align_t); // exposition only
|
| 9 |
|
| 10 |
public:
|
|
|
|
|
|
|
| 11 |
virtual ~memory_resource();
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
void deallocate(void* p, size_t bytes, size_t alignment = max_align);
|
| 15 |
|
| 16 |
bool is_equal(const memory_resource& other) const noexcept;
|
| 17 |
|
| 18 |
private:
|
| 19 |
virtual void* do_allocate(size_t bytes, size_t alignment) = 0;
|
| 20 |
virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0;
|
| 21 |
|
| 22 |
virtual bool do_is_equal(const memory_resource& other) const noexcept = 0;
|
| 23 |
};
|
|
|
|
| 24 |
```
|
| 25 |
|
| 26 |
-
####
|
| 27 |
|
| 28 |
``` cpp
|
| 29 |
~memory_resource();
|
| 30 |
```
|
| 31 |
|
| 32 |
*Effects:* Destroys this `memory_resource`.
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
-
void* allocate(size_t bytes, size_t alignment = max_align);
|
| 36 |
```
|
| 37 |
|
| 38 |
*Effects:* Equivalent to: `return do_allocate(bytes, alignment);`
|
| 39 |
|
| 40 |
``` cpp
|
| 41 |
void deallocate(void* p, size_t bytes, size_t alignment = max_align);
|
| 42 |
```
|
| 43 |
|
| 44 |
-
*Effects:* Equivalent to
|
| 45 |
|
| 46 |
``` cpp
|
| 47 |
bool is_equal(const memory_resource& other) const noexcept;
|
| 48 |
```
|
| 49 |
|
| 50 |
*Effects:* Equivalent to: `return do_is_equal(other);`
|
| 51 |
|
| 52 |
-
####
|
| 53 |
|
| 54 |
``` cpp
|
| 55 |
virtual void* do_allocate(size_t bytes, size_t alignment) = 0;
|
| 56 |
```
|
| 57 |
|
| 58 |
-
*
|
| 59 |
|
| 60 |
*Returns:* A derived class shall implement this function to return a
|
| 61 |
-
pointer to allocated storage
|
| 62 |
-
|
| 63 |
-
specified alignment, if such alignment is supported ([[basic.align]]);
|
| 64 |
-
otherwise it is aligned to `max_align`.
|
| 65 |
|
| 66 |
*Throws:* A derived class implementation shall throw an appropriate
|
| 67 |
exception if it is unable to allocate memory with the requested size and
|
| 68 |
alignment.
|
| 69 |
|
| 70 |
``` cpp
|
| 71 |
virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0;
|
| 72 |
```
|
| 73 |
|
| 74 |
-
*
|
| 75 |
`allocate(bytes, alignment)` on a memory resource equal to `*this`, and
|
| 76 |
-
the storage at `p`
|
| 77 |
|
| 78 |
*Effects:* A derived class shall implement this function to dispose of
|
| 79 |
allocated storage.
|
| 80 |
|
| 81 |
*Throws:* Nothing.
|
|
@@ -87,23 +91,17 @@ virtual bool do_is_equal(const memory_resource& other) const noexcept = 0;
|
|
| 87 |
*Returns:* A derived class shall implement this function to return
|
| 88 |
`true` if memory allocated from `this` can be deallocated from `other`
|
| 89 |
and vice-versa, otherwise `false`.
|
| 90 |
|
| 91 |
[*Note 1*: The most-derived type of `other` might not match the type of
|
| 92 |
-
`this`. For a derived class `D`,
|
| 93 |
-
|
| 94 |
`dynamic_cast<const D*>(&other) == nullptr`. — *end note*]
|
| 95 |
|
| 96 |
-
####
|
| 97 |
|
| 98 |
``` cpp
|
| 99 |
bool operator==(const memory_resource& a, const memory_resource& b) noexcept;
|
| 100 |
```
|
| 101 |
|
| 102 |
*Returns:* `&a == &b || a.is_equal(b)`.
|
| 103 |
|
| 104 |
-
``` cpp
|
| 105 |
-
bool operator!=(const memory_resource& a, const memory_resource& b) noexcept;
|
| 106 |
-
```
|
| 107 |
-
|
| 108 |
-
*Returns:* `!(a == b)`.
|
| 109 |
-
|
|
|
|
| 2 |
|
| 3 |
The `memory_resource` class is an abstract interface to an unbounded set
|
| 4 |
of classes encapsulating memory resources.
|
| 5 |
|
| 6 |
``` cpp
|
| 7 |
+
namespace std::pmr {
|
| 8 |
class memory_resource {
|
| 9 |
static constexpr size_t max_align = alignof(max_align_t); // exposition only
|
| 10 |
|
| 11 |
public:
|
| 12 |
+
memory_resource() = default;
|
| 13 |
+
memory_resource(const memory_resource&) = default;
|
| 14 |
virtual ~memory_resource();
|
| 15 |
|
| 16 |
+
memory_resource& operator=(const memory_resource&) = default;
|
| 17 |
+
|
| 18 |
+
[[nodiscard]] void* allocate(size_t bytes, size_t alignment = max_align);
|
| 19 |
void deallocate(void* p, size_t bytes, size_t alignment = max_align);
|
| 20 |
|
| 21 |
bool is_equal(const memory_resource& other) const noexcept;
|
| 22 |
|
| 23 |
private:
|
| 24 |
virtual void* do_allocate(size_t bytes, size_t alignment) = 0;
|
| 25 |
virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0;
|
| 26 |
|
| 27 |
virtual bool do_is_equal(const memory_resource& other) const noexcept = 0;
|
| 28 |
};
|
| 29 |
+
}
|
| 30 |
```
|
| 31 |
|
| 32 |
+
#### Public member functions <a id="mem.res.public">[[mem.res.public]]</a>
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
~memory_resource();
|
| 36 |
```
|
| 37 |
|
| 38 |
*Effects:* Destroys this `memory_resource`.
|
| 39 |
|
| 40 |
``` cpp
|
| 41 |
+
[[nodiscard]] void* allocate(size_t bytes, size_t alignment = max_align);
|
| 42 |
```
|
| 43 |
|
| 44 |
*Effects:* Equivalent to: `return do_allocate(bytes, alignment);`
|
| 45 |
|
| 46 |
``` cpp
|
| 47 |
void deallocate(void* p, size_t bytes, size_t alignment = max_align);
|
| 48 |
```
|
| 49 |
|
| 50 |
+
*Effects:* Equivalent to `do_deallocate(p, bytes, alignment)`.
|
| 51 |
|
| 52 |
``` cpp
|
| 53 |
bool is_equal(const memory_resource& other) const noexcept;
|
| 54 |
```
|
| 55 |
|
| 56 |
*Effects:* Equivalent to: `return do_is_equal(other);`
|
| 57 |
|
| 58 |
+
#### Private virtual member functions <a id="mem.res.private">[[mem.res.private]]</a>
|
| 59 |
|
| 60 |
``` cpp
|
| 61 |
virtual void* do_allocate(size_t bytes, size_t alignment) = 0;
|
| 62 |
```
|
| 63 |
|
| 64 |
+
*Preconditions:* `alignment` is a power of two.
|
| 65 |
|
| 66 |
*Returns:* A derived class shall implement this function to return a
|
| 67 |
+
pointer to allocated storage [[basic.stc.dynamic.allocation]] with a
|
| 68 |
+
size of at least `bytes`, aligned to the specified `alignment`.
|
|
|
|
|
|
|
| 69 |
|
| 70 |
*Throws:* A derived class implementation shall throw an appropriate
|
| 71 |
exception if it is unable to allocate memory with the requested size and
|
| 72 |
alignment.
|
| 73 |
|
| 74 |
``` cpp
|
| 75 |
virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0;
|
| 76 |
```
|
| 77 |
|
| 78 |
+
*Preconditions:* `p` was returned from a prior call to
|
| 79 |
`allocate(bytes, alignment)` on a memory resource equal to `*this`, and
|
| 80 |
+
the storage at `p` has not yet been deallocated.
|
| 81 |
|
| 82 |
*Effects:* A derived class shall implement this function to dispose of
|
| 83 |
allocated storage.
|
| 84 |
|
| 85 |
*Throws:* Nothing.
|
|
|
|
| 91 |
*Returns:* A derived class shall implement this function to return
|
| 92 |
`true` if memory allocated from `this` can be deallocated from `other`
|
| 93 |
and vice-versa, otherwise `false`.
|
| 94 |
|
| 95 |
[*Note 1*: The most-derived type of `other` might not match the type of
|
| 96 |
+
`this`. For a derived class `D`, an implementation of this function
|
| 97 |
+
could immediately return `false` if
|
| 98 |
`dynamic_cast<const D*>(&other) == nullptr`. — *end note*]
|
| 99 |
|
| 100 |
+
#### Equality <a id="mem.res.eq">[[mem.res.eq]]</a>
|
| 101 |
|
| 102 |
``` cpp
|
| 103 |
bool operator==(const memory_resource& a, const memory_resource& b) noexcept;
|
| 104 |
```
|
| 105 |
|
| 106 |
*Returns:* `&a == &b || a.is_equal(b)`.
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|