en	= `storage_ptr` Variable-length containers in this library all use dynamically allocated memory to store their contents. Callers can gain control over the strategy used for allocation by specifying a <<ref_storage_ptr>> in select constructors and function parameter lists. A <<ref_storage_ptr>> has these properties:

en	A storage pointer always points to a valid,

en	type-erased {ref_memory_resource}.

en	Default-constructed storage pointers reference the

en	<<default_memory_resource,default resource>>, an implementation-defined instance which always uses the equivalent of global operator new and delete.

en	Storage pointers constructed from a {ref_memory_resource} or

en	{ref_polymorphic_allocator} do not acquire ownership; the caller is responsible for ensuring that the lifetime of the resource extends until it is no longer referenced.

en	A storage pointer obtained from <<ref_make_shared_resource>> acquires shared

en	ownership of the memory resource; the lifetime of the resource is extended until all copies of the storage pointer are destroyed.

en	The storage pointer remembers the value of <<ref_is_deallocate_trivial>>

en	before type-erasing the resource, allowing the value to be queried at run-time.

en	This lists all of the allocation-related types and functions available when using the library:

en	Name

en	Description

en	<<ref_get_null_resource>>

en	Returns a pointer to a memory resource instance which always throws an

en	exception upon allocation. This is used to to achieve the invariant that no parsing or container operation will dynamically allocate memory.

en	<<ref_is_deallocate_trivial>>

en	A customization point allowing a memory resource type to indicate that calls

en	to deallocate are trivial.

en	<<ref_make_shared_resource>>

en	A function returning a smart pointer with shared ownership of a newly

en	allocated memory resource.

en	{ref_memory_resource}

en	The abstract base class representing an allocator.

en	<<ref_monotonic_resource>>

en	A memory resource which allocates large blocks of memory and has a trivial

en	deallocate function. Allocated memory is not freed until the resource is destroyed, making it fast for parsing but not suited for performing modifications.

en	{ref_polymorphic_allocator}

en	An {req_Allocator} which uses a reference to a {ref_memory_resource} to

en	perform allocations.

en	<<ref_static_resource>>

en	A memory resource that uses a single caller provided buffer. No dynamic

en	allocations are used. This is fast for parsing but not suited for performing modifications.

en	<<ref_storage_ptr>>

en	A smart pointer through which a {ref_memory_resource} is managed and

en	accessed. |===

en	Default Memory Resource

en	The default memory resource uses the global `operator new` and `operator delete` to allocate memory. This resource is not reference counted and has a non-trivial deallocate function. All default-constructed <<ref_storage_ptr>> objects reference the same memory resource:

en	Default-constructed library containers use the default memory resource:

en	The default memory resource is well suited for general usage. It offers reasonable performance for parsing, and conservative memory usage for modification of the contents of containers.

en	This memory resource is not guaranteed to be the same as the result of `boost::container::pmr::get_default_resource`. It also cannot be changed with `boost::container::pmr::set_default_resource`.

en	Monotonic Resource

en	Consider the pattern of memory allocation during parsing: when an array, object, or string is encountered the parser accumulates elements in its temporary storage area. When all of the elements are known, a single memory allocation is requested from the resource when constructing the value. Thus, parsing only allocates and constructs containers at their final size. Memory is not reallocated; that is, a memory buffer never needs to grow by allocating a new larger buffer and deallocating the previous buffer.

en	The <<ref_monotonic_resource>> optimizes this memory allocation pattern by allocating increasingly large blocks of global memory internally and parceling those blocks out in smaller pieces to fulfill allocation requests. It has a trivial deallocate function. The monotonic resource does not actually deallocate memory until the resource is destroyed. Thus, it is ideally suited for the use-case where JSON is parsed, and the resulting value is then inspected but not modified.

en	The resource to use when constructing values may be specified in calls to <<ref_parse>> as shown here:

en	Or, to parse into a value with shared ownership of the memory resource:

en	A monotonic resource may be optionally constructed with an initial buffer to use first, before going to the heap. This allows the caller to use stack space and avoid dynamic allocations for most parsed JSON, falling back to dynamic allocation from the heap if the input JSON is larger than average, as shown here:

en	Static Resource

en	A <<ref_static_resource>> constructs from a caller-provided buffer, and satisfies all memory allocation requests from the buffer. Once the buffer is exhausted, subsequent calls to allocate throw the exception `std::bad_alloc`. The resource offers a simple invariant: dynamic heap allocations are never performed.

en	To use the resource, construct it with a local buffer:

en	Null Resource

en	The function <<ref_get_null_resource>> returns a global instance of the null resource. This resource offers a simple invariant: all calls to allocate will throw the exception `std::bad_alloc`. An instance of the null resource can be used to make parsing guarantee that allocations from the heap are never made. This is explored in more detail in a later section.

en	Allocator Propagation

en	The containers <<ref_array>>, <<ref_object>>, and <<ref_value>> all propagate the memory resource they were constructed with to child elements:

en	This propagation acts recursively, containers within containers will all have the resource propagated. Once a container is constructed, its memory resource can never be changed.

en	Resource Lifetime

en	It is important to note that <<ref_storage_ptr>> supports both shared-ownership and reference lifetime models. Construction from a memory resource pointer does not transfer ownership:

en	When using a memory resource in this fashion, including the case where a storage pointer or container is constructed from a {ref_polymorphic_allocator}, the caller must ensure that the lifetime of the resource is extended until it is no longer referenced by any variables; otherwise, undefined behavior is possible.

en	Shared ownership is achieved using the function <<ref_make_shared_resource>>, which creates a new, reference-counted memory resource using a dynamic memory allocation and returns it as a <<ref_storage_ptr>>:

en	When a storage pointer is constructed this way, the lifetime of the referenced memory resource is extended until all variables which reference it are destroyed.

en	User-Defined Resource

en	To implement custom memory allocation strategies, derive your class from {ref_memory_resource} and implement the functions `do_allocate`, `do_deallocate`, and `do_is_equal` as seen in this example below, which logs each operation it performs to the console: