pmr.h file
Polymorphic memory resources and allocator handles.
Classes
- struct PmrResourceVtable
- Operations implemented by a memory resource.
- struct PmrResource
- Polymorphic memory resource.
- struct PmrAllocator
- Handle stored by a container.
Typedefs
- using PmrResource = struct PmrResource
- using PmrResourceVtable = struct PmrResourceVtable
- Operations implemented by a memory resource.
- using PmrAllocator = struct PmrAllocator
- Handle stored by a container.
Functions
- auto pmr_allocate(const PmrResource* resource, size_t bytes, size_t alignment) -> void*
- Allocate at least
bytesaligned toalignment. - void pmr_deallocate(const PmrResource* resource, void* p, size_t bytes, size_t alignment)
- Release a block returned by
pmr_on an equal resource.allocate - auto pmr_is_equal(const PmrResource* a, const PmrResource* b) -> int
- Whether blocks from
acan be freed onb. - auto pmr_is_same_object(const PmrResource* self, const PmrResource* other) -> int
- Identity comparison for a resource vtable.
- auto pmr_allocator_default(void) -> PmrAllocator
- Handle for the current default resource.
- auto pmr_allocator_from(PmrResource* resource) -> PmrAllocator
- Handle that stores
resourceas given. - auto pmr_allocator_resource(PmrAllocator allocator) -> PmrResource*
- Resource stored in the handle.
- auto pmr_allocator_allocate(PmrAllocator allocator, size_t bytes, size_t alignment) -> void*
pmr_through the handle.allocate - void pmr_allocator_deallocate(PmrAllocator allocator, void* p, size_t bytes, size_t alignment)
pmr_through the handle.deallocate - auto pmr_allocator_is_equal(PmrAllocator a, PmrAllocator b) -> int
- Whether the two handles can free each other's blocks.
- auto pmr_allocator_select_on_copy(PmrAllocator allocator) -> PmrAllocator
- Handle for the default resource at this call.
- static auto pmr_allocator_allocate_array(const PmrAllocator allocator, const size_t count, const size_t elem_size, const size_t elem_align) -> void*
- Allocate
countelements, or NULL if the product overflows. - static void pmr_allocator_deallocate_array(const PmrAllocator allocator, void* p, const size_t count, const size_t elem_size, const size_t elem_align)
- Release an array allocated by
pmr_.allocator_ allocate_ array - auto pmr_new_delete_resource(void) -> PmrResource*
- Process-lifetime resource backed by
mallocandfree. - auto pmr_null_memory_resource(void) -> PmrResource*
- Process-lifetime resource whose allocate always fails.
- auto pmr_get_default_resource(void) -> PmrResource*
- Process-wide default resource.
- auto pmr_set_default_resource(PmrResource* resource) -> PmrResource*
- Install the process-wide default resource and return the previous one.
Typedef documentation
typedef struct PmrResourceVtable PmrResourceVtable
Operations implemented by a memory resource.
The wrappers have already rejected a null resource, a zero size, and an alignment that is not a non-zero power of two. allocate returns an aligned block of at least bytes uninitialized bytes, or NULL on failure. deallocate does not report failure. is_equal is true only when both resources can free each other's blocks. Do not call pmr_ on self from inside is_equal; that recurses.
typedef struct PmrAllocator PmrAllocator
Handle stored by a container.
Copying the struct copies the pointer. It does not allocate or free. Assignment copies the pointer and frees nothing. There is no destructor.
Function documentation
void* pmr_allocate(const PmrResource* resource, size_t bytes, size_t alignment)
Allocate at least bytes aligned to alignment.
| Parameters | |
|---|---|
| resource | Resource to allocate from. |
| bytes | Number of bytes. Zero returns NULL without calling the vtable; that NULL is success, because the caller knows the size it passed. |
| alignment | Required alignment. Must be a non-zero power of two. Zero is an error, not a synonym for max_align_t. |
| Returns | Aligned pointer, or NULL if the arguments are invalid, bytes is 0, or the resource cannot obtain the block. |
void pmr_deallocate(const PmrResource* resource, void* p, size_t bytes, size_t alignment)
Release a block returned by pmr_ on an equal resource.
| Parameters | |
|---|---|
| resource | Resource that compares equal to the one that allocated p. |
| p | Block to release. NULL is ignored. |
| bytes | Size passed to allocate. |
| alignment | Alignment passed to allocate. |
bytes and alignment are the values passed to the matching allocate. A null resource, a null pointer, a zero size, or a bad alignment returns without calling the vtable.
int pmr_is_equal(const PmrResource* a, const PmrResource* b)
Whether blocks from a can be freed on b.
| Parameters | |
|---|---|
| a | Resource that allocated the block. |
| b | Resource that would free it. |
| Returns | Non-zero when the resources are equal. A null argument is not equal. |
Equal addresses are equal. Otherwise, only a's is_equal is called.
int pmr_is_same_object(const PmrResource* self, const PmrResource* other)
Identity comparison for a resource vtable.
| Parameters | |
|---|---|
| self | Resource being asked. |
| other | Resource to compare. |
| Returns | Non-zero when self and other are the same object. |
PmrAllocator pmr_allocator_default(void)
Handle for the current default resource.
| Returns | Handle for pmr_ at this call. |
|---|
Later changes to the default do not retarget the returned handle.
PmrAllocator pmr_allocator_from(PmrResource* resource)
Handle that stores resource as given.
| Parameters | |
|---|---|
| resource | Resource to store. NULL stays NULL. |
| Returns | Handle that stores resource as given. |
NULL stays NULL. It is not replaced with the default resource or the null singleton. Allocations through a null handle fail.
PmrResource* pmr_allocator_resource(PmrAllocator allocator)
Resource stored in the handle.
| Parameters | |
|---|---|
| allocator | Handle to query. |
| Returns | Resource stored in the handle. May be NULL. |
void* pmr_allocator_allocate(PmrAllocator allocator, size_t bytes, size_t alignment)
pmr_ through the handle.
| Parameters | |
|---|---|
| allocator | Handle to allocate through. |
| bytes | Number of bytes. Zero returns NULL. |
| alignment | Required alignment. Must be a non-zero power of two. |
| Returns | Aligned pointer, or NULL if allocation fails. |
There is no default alignment. Callers that want the C default pass _Alignof(max_align_t).
void pmr_allocator_deallocate(PmrAllocator allocator, void* p, size_t bytes, size_t alignment)
pmr_ through the handle.
| Parameters | |
|---|---|
| allocator | Handle that allocated the block. |
| p | Block to release. NULL is ignored. |
| bytes | Size passed to allocate. |
| alignment | Alignment passed to allocate. |
int pmr_allocator_is_equal(PmrAllocator a, PmrAllocator b)
Whether the two handles can free each other's blocks.
| Parameters | |
|---|---|
| a | First handle. |
| b | Second handle. |
| Returns | Non-zero when the handles can free each other's blocks. |
This is resource equality. Two null handles compare equal.
PmrAllocator pmr_allocator_select_on_copy(PmrAllocator allocator)
Handle for the default resource at this call.
| Parameters | |
|---|---|
| allocator | Ignored. Present so container copy can pass the source handle. |
| Returns | Handle for the default resource at this call. |
The argument is ignored. Container copy does not call this implicitly; a caller that wants the C++ rule passes the result itself.
static void* pmr_allocator_allocate_array(const PmrAllocator allocator, const size_t count, const size_t elem_size, const size_t elem_align)
Allocate count elements, or NULL if the product overflows.
| Parameters | |
|---|---|
| allocator | Handle to allocate through. |
| count | Number of elements. Zero returns NULL. |
| elem_size | Size of one element in bytes. |
| elem_align | Alignment of one element. _Alignof(T) at the call site. |
| Returns | Aligned array, or NULL if the product overflows or allocation fails. |
static void pmr_allocator_deallocate_array(const PmrAllocator allocator, void* p, const size_t count, const size_t elem_size, const size_t elem_align)
Release an array allocated by pmr_.
| Parameters | |
|---|---|
| allocator | Handle that allocated the array. |
| p | Array to release. NULL is ignored. |
| count | Element count passed to allocate. |
| elem_size | Element size passed to allocate. |
| elem_align | Element alignment passed to allocate. |
A null pointer, a zero element size, or a product that overflows returns without deallocating.
PmrResource* pmr_new_delete_resource(void)
Process-lifetime resource backed by malloc and free.
| Returns | The process-lifetime resource. The same address is returned every time. |
|---|
The same address is returned every time and is never freed. Alignment at most _Alignof(max_align_t) uses malloc / free. A stricter alignment uses _aligned_malloc / _aligned_free on Windows and aligned_alloc / free otherwise. deallocate selects that free from alignment, so the caller passes the same alignment it passed to allocate. Safe to call from any thread.
PmrResource* pmr_null_memory_resource(void)
Process-lifetime resource whose allocate always fails.
| Returns | The process-lifetime resource. The same address is returned every time. |
|---|
deallocate does nothing. The same address is returned every time. Pass this as an upstream to forbid growth. Safe to call from any thread.
PmrResource* pmr_get_default_resource(void)
Process-wide default resource.
| Returns | The current default resource. Never NULL. |
|---|
The initial value is pmr_. Never returns NULL. Thread-safe. The resource itself is not made thread-safe by this call.
PmrResource* pmr_set_default_resource(PmrResource* resource)
Install the process-wide default resource and return the previous one.
| Parameters | |
|---|---|
| resource | Resource to install as the process-wide default. NULL installs pmr_. |
| Returns | Previous default resource. Never NULL. |
NULL installs pmr_. The caller keeps resource alive for as long as it stays installed and for as long as any handle still stores it. Installing it does not extend its lifetime. Thread-safe.