A dynamically allocating resource with a trivial deallocate.
Defined in header
class monotonic_resource : public memory_resource
Copy constructor (deleted)
Copy assignment (deleted)
Release all allocated memory.
This memory resource is a special-purpose resource that releases allocated
memory only when the resource is destroyed (or when
release is called). It has a trivial
deallocate function; that is, the metafunction
The resource can be constructed with an initial buffer. If there is no initial buffer, or if the buffer is exhausted, subsequent dynamic allocations are made from the system heap. The size of buffers obtained in this fashion follow a geometric progression.
The purpose of this resource is to optimize the use case for performing many
allocations, followed by deallocating everything at once. This is precisely
the pattern of memory allocation which occurs when parsing: allocation is
performed for each parsed element, and when the the resulting
value is no longer needed, the entire
structure is destroyed. However, it is not suited for modifying the value
after parsing is complete; reallocations waste memory, since the older buffer
is not reclaimed until the resource is destroyed.
This parses a JSON into a value which uses a local stack buffer, then prints the result.
unsigned char buf[ 4000 ]; monotonic_resource mr( buf ); // Parse the string, using our memory resource auto const jv = parse( "[1,2,3]", &mr ); // Print the JSON std::cout << jv;
The total amount of memory dynamically allocated is monotonically increasing; That is, it never decreases.
Members of the same instance may not be called concurrently.