Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::pmr::monotonic_buffer_resource — allocation with bulk release

std::pmr::monotonic_buffer_resource supplies storage for successive allocations while ignoring individual deallocations. release() returns upstream blocks and resets the resource for reuse.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <cstddef>
#include <memory_resource>

void use_resource(void* buffer, std::size_t buffer_size,
                  std::pmr::memory_resource* upstream) {
    {
        std::pmr::monotonic_buffer_resource resource;
    }
    {
        std::pmr::monotonic_buffer_resource resource(buffer, buffer_size);
    }
    {
        std::pmr::monotonic_buffer_resource resource(buffer, buffer_size, upstream);
        resource.release();
    }
}

std::pmr::monotonic_buffer_resource, declared in <memory_resource>, is a memory resource useful when objects are built incrementally and storage can be reclaimed together. PMR containers such as std::pmr::vector<int> can use it through an allocator. Individual deallocation calls reclaim no space, so container growth can leave earlier blocks occupied.

You can supply an initial buffer. When the remaining space cannot satisfy the requested size and alignment, the resource obtains another block from its upstream resource; block sizes are implementation-dependent. Without an explicit upstream argument, it uses the default memory resource. The initial buffer therefore is not a hard memory limit.

std::pmr::monotonic_buffer_resource::release() returns all acquired blocks to the upstream resource and restores the initial allocation state, allowing the supplied buffer to be reused. It neither frees the caller-owned buffer itself nor invokes destructors of stored objects. Destroy objects and containers using those allocations first; merely calling a container's clear() is insufficient because it can retain capacity. The resource destructor also calls release(). The buffer and upstream resource must remain available while the resource uses them.

The example destroys the first vector before release(), then creates a second using the same resource. Entry scope: C++23; the repository compiler flag does not attest the production runtime. The linked document is an evolving working draft, not a frozen C++23 publication.

Examples

#include <cstddef>
#include <iostream>
#include <memory_resource>
#include <vector>

int main() {
    alignas(std::max_align_t) std::byte buffer[256];
    std::pmr::monotonic_buffer_resource resource(buffer, sizeof buffer);

    {
        std::pmr::vector<int> values(&resource);
        values.reserve(3);
        values.push_back(1);
        values.push_back(2);
        values.push_back(3);
        std::cout << values[0] + values[1] + values[2] << '\n';
    }

    resource.release();

    {
        std::pmr::vector<int> values(&resource);
        values.push_back(4);
        std::cout << values.front() << '\n';
    }
}

Result

6
4

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources