[mem.res.monotonic.buffer] (original) (raw)

20 Memory management library [mem]

20.5 Memory resources [mem.res]

20.5.6 Class monotonic_buffer_resource [mem.res.monotonic.buffer]

20.5.6.1 General [mem.res.monotonic.buffer.general]

A monotonic_buffer_resource is a special-purpose memory resource intended for very fast memory allocations in situations where memory is used to build up a few objects and then is released all at once when the memory resource object is destroyed.

namespace std::pmr { class monotonic_buffer_resource : public memory_resource { memory_resource* upstream_rsrc; void* current_buffer; size_t next_buffer_size; public: explicit monotonic_buffer_resource(memory_resource* upstream); monotonic_buffer_resource(size_t initial_size, memory_resource* upstream); monotonic_buffer_resource(void* buffer, size_t buffer_size, memory_resource* upstream); monotonic_buffer_resource() : monotonic_buffer_resource(get_default_resource()) {} explicit monotonic_buffer_resource(size_t initial_size) : monotonic_buffer_resource(initial_size, get_default_resource()) {} monotonic_buffer_resource(void* buffer, size_t buffer_size) : monotonic_buffer_resource(buffer, buffer_size, get_default_resource()) {} monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;virtual ~monotonic_buffer_resource(); monotonic_buffer_resource& operator=(const monotonic_buffer_resource&) = delete;void release(); memory_resource* upstream_resource() const;protected: void* do_allocate(size_t bytes, size_t alignment) override;void do_deallocate(void* p, size_t bytes, size_t alignment) override;bool do_is_equal(const memory_resource& other) const noexcept override;};}

20.5.6.2 Constructors and destructor [mem.res.monotonic.buffer.ctor]

explicit monotonic_buffer_resource(memory_resource* upstream); monotonic_buffer_resource(size_t initial_size, memory_resource* upstream);

Preconditions: upstream is the address of a valid memory resource.

initial_size, if specified, is greater than zero.

Effects: Sets upstream_rsrc to upstream andcurrent_buffer to nullptr.

If initial_size is specified, sets next_buffer_size to at least initial_size; otherwise sets next_buffer_size to animplementation-defined size.

monotonic_buffer_resource(void* buffer, size_t buffer_size, memory_resource* upstream);

Preconditions: upstream is the address of a valid memory resource.

buffer_size is no larger than the number of bytes in buffer.

Effects: Sets upstream_rsrc to upstream,current_buffer to buffer, andnext_buffer_size to buffer_size (but not less than 1), then increases next_buffer_sizeby an implementation-defined growth factor (which need not be integral).

~monotonic_buffer_resource();

Effects: Calls release().

20.5.6.3 Members [mem.res.monotonic.buffer.mem]

Effects: Calls upstream_rsrc->deallocate() as necessary to release all allocated memory.

Resets current_buffer and next_buffer_sizeto their initial values at construction.

[Note 1:

The memory is released back to upstream_rsrceven if some blocks that were allocated from *thishave not been deallocated from *this.

— _end note_]

memory_resource* upstream_resource() const;

Returns: The value of upstream_rsrc.

void* do_allocate(size_t bytes, size_t alignment) override;

Effects: If the unused space in current_buffercan fit a block with the specified bytes and alignment, then allocate the return block from current_buffer; otherwise set current_buffer to upstream_rsrc->allocate(n, m), where n is not less than max(bytes, next_buffer_size) andm is not less than alignment, and increase next_buffer_sizeby an implementation-defined growth factor (which need not be integral), then allocate the return block from the newly-allocated current_buffer.

The size and alignment of the allocated memory shall meet the requirements for a class derived from memory_resource ([mem.res.class]).

Throws: Nothing unless upstream_rsrc->allocate() throws.

void do_deallocate(void* p, size_t bytes, size_t alignment) override;

Remarks: Memory used by this resource increases monotonically until its destruction.

bool do_is_equal(const memory_resource& other) const noexcept override;