From Jason Turner

[mem.res.monotonic.buffer]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpagwmjm7n/{from.md → to.md} +148 -0
tmp/tmpagwmjm7n/{from.md → to.md} RENAMED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Class `monotonic_buffer_resource` <a id="mem.res.monotonic.buffer">[[mem.res.monotonic.buffer]]</a>
2
+
3
+ A `monotonic_buffer_resource` is a special-purpose memory resource
4
+ intended for very fast memory allocations in situations where memory is
5
+ used to build up a few objects and then is released all at once when the
6
+ memory resource object is destroyed. It has the following qualities:
7
+
8
+ - A call to `deallocate` has no effect, thus the amount of memory
9
+ consumed increases monotonically until the resource is destroyed.
10
+ - The program can supply an initial buffer, which the allocator uses to
11
+ satisfy memory requests.
12
+ - When the initial buffer (if any) is exhausted, it obtains additional
13
+ buffers from an *upstream* memory resource supplied at construction.
14
+ Each additional buffer is larger than the previous one, following a
15
+ geometric progression.
16
+ - It is intended for access from one thread of control at a time.
17
+ Specifically, calls to `allocate` and `deallocate` do not synchronize
18
+ with one another.
19
+ - It frees the allocated memory on destruction, even if `deallocate` has
20
+ not been called for some of the allocated blocks.
21
+
22
+ ``` cpp
23
+ class monotonic_buffer_resource : public memory_resource {
24
+ memory_resource *upstream_rsrc; // exposition only
25
+ void *current_buffer; // exposition only
26
+ size_t next_buffer_size; // exposition only
27
+
28
+ public:
29
+ explicit monotonic_buffer_resource(memory_resource *upstream);
30
+ monotonic_buffer_resource(size_t initial_size, memory_resource *upstream);
31
+ monotonic_buffer_resource(void *buffer, size_t buffer_size,
32
+ memory_resource *upstream);
33
+
34
+ monotonic_buffer_resource()
35
+ : monotonic_buffer_resource(get_default_resource()) {}
36
+ explicit monotonic_buffer_resource(size_t initial_size)
37
+ : monotonic_buffer_resource(initial_size, get_default_resource()) {}
38
+ monotonic_buffer_resource(void *buffer, size_t buffer_size)
39
+ : monotonic_buffer_resource(buffer, buffer_size, get_default_resource()) {}
40
+
41
+ monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
42
+
43
+ virtual ~monotonic_buffer_resource();
44
+
45
+ monotonic_buffer_resource
46
+ operator=(const monotonic_buffer_resource&) = delete;
47
+
48
+ void release();
49
+ memory_resource* upstream_resource() const;
50
+
51
+ protected:
52
+ void* do_allocate(size_t bytes, size_t alignment) override;
53
+ void do_deallocate(void* p, size_t bytes, size_t alignment) override;
54
+
55
+ bool do_is_equal(const memory_resource& other) const noexcept override;
56
+ };
57
+ ```
58
+
59
+ #### `monotonic_buffer_resource` constructor and destructor <a id="mem.res.monotonic.buffer.ctor">[[mem.res.monotonic.buffer.ctor]]</a>
60
+
61
+ ``` cpp
62
+ explicit monotonic_buffer_resource(memory_resource* upstream);
63
+ monotonic_buffer_resource(size_t initial_size, memory_resource* upstream);
64
+ ```
65
+
66
+ *Requires:* `upstream` shall be the address of a valid memory resource.
67
+ `initial_size`, if specified, shall be greater than zero.
68
+
69
+ *Effects:* Sets `upstream_rsrc` to `upstream` and `current_buffer` to
70
+ `nullptr`. If `initial_size` is specified, sets `next_buffer_size` to at
71
+ least `initial_size`; otherwise sets `next_buffer_size` to an
72
+ *implementation-defined* size.
73
+
74
+ ``` cpp
75
+ monotonic_buffer_resource(void* buffer, size_t buffer_size, memory_resource* upstream);
76
+ ```
77
+
78
+ *Requires:* `upstream` shall be the address of a valid memory resource.
79
+ `buffer_size` shall be no larger than the number of bytes in `buffer`.
80
+
81
+ *Effects:* Sets `upstream_rsrc` to `upstream`, `current_buffer` to
82
+ `buffer`, and `next_buffer_size` to `buffer_size` (but not less than 1),
83
+ then increases `next_buffer_size` by an *implementation-defined* growth
84
+ factor (which need not be integral).
85
+
86
+ ``` cpp
87
+ ~monotonic_buffer_resource();
88
+ ```
89
+
90
+ *Effects:* Calls `release()`.
91
+
92
+ #### `monotonic_buffer_resource` members <a id="mem.res.monotonic.buffer.mem">[[mem.res.monotonic.buffer.mem]]</a>
93
+
94
+ ``` cpp
95
+ void release();
96
+ ```
97
+
98
+ *Effects:* Calls `upstream_rsrc->deallocate()` as necessary to release
99
+ all allocated memory.
100
+
101
+ [*Note 1*: The memory is released back to `upstream_rsrc` even if some
102
+ blocks that were allocated from `this` have not been deallocated from
103
+ `this`. — *end note*]
104
+
105
+ ``` cpp
106
+ memory_resource* upstream_resource() const;
107
+ ```
108
+
109
+ *Returns:* The value of `upstream_rsrc`.
110
+
111
+ ``` cpp
112
+ void* do_allocate(size_t bytes, size_t alignment) override;
113
+ ```
114
+
115
+ *Returns:* A pointer to allocated storage
116
+ ([[basic.stc.dynamic.deallocation]]) with a size of at least `bytes`.
117
+ The size and alignment of the allocated memory shall meet the
118
+ requirements for a class derived from `memory_resource` ([[mem.res]]).
119
+
120
+ *Effects:* If the unused space in `current_buffer` can fit a block with
121
+ the specified `bytes` and `alignment`, then allocate the return block
122
+ from `current_buffer`; otherwise set `current_buffer` to
123
+ `upstream_rsrc->allocate(n, m)`, where `n` is not less than
124
+ `max(bytes, next_buffer_size)` and `m` is not less than `alignment`, and
125
+ increase `next_buffer_size` by an *implementation-defined* growth factor
126
+ (which need not be integral), then allocate the return block from the
127
+ newly-allocated `current_buffer`.
128
+
129
+ *Throws:* Nothing unless `upstream_rsrc->allocate()` throws.
130
+
131
+ ``` cpp
132
+ void do_deallocate(void* p, size_t bytes, size_t alignment) override;
133
+ ```
134
+
135
+ *Effects:* None.
136
+
137
+ *Throws:* Nothing.
138
+
139
+ *Remarks:* Memory used by this resource increases monotonically until
140
+ its destruction.
141
+
142
+ ``` cpp
143
+ bool do_is_equal(const memory_resource& other) const noexcept override;
144
+ ```
145
+
146
+ *Returns:*
147
+ `this == dynamic_cast<const monotonic_buffer_resource*>(&other)`.
148
+