pmr.c file
Polymorphic allocator implementation.
Functions
- static auto pmr_alignment_ok(const size_t alignment) -> int
- static auto pmr_align_up(const size_t bytes, const size_t alignment) -> size_t
- auto pmr_is_same_object(const PmrResource* self, const PmrResource* other) -> int
- Identity comparison for a resource vtable.
- static auto pmr_overaligned_alloc(const size_t bytes, const size_t alignment) -> void*
- static void pmr_overaligned_free(void* p)
- static auto pmr_new_delete_allocate(const PmrResource* self, const size_t bytes, const size_t alignment) -> void*
- static void pmr_new_delete_deallocate(const PmrResource* self, void* p, const size_t bytes, const size_t alignment)
- static auto pmr_null_allocate(const PmrResource* self, const size_t bytes, const size_t alignment) -> void*
- static void pmr_null_deallocate(const PmrResource* self, void* p, const size_t bytes, const size_t alignment)
- 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_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_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 _Atomic(PmrResource*)
- auto pmr_set_default_resource(PmrResource* resource) -> PmrResource*
- Install the process-wide default resource and return the previous one.
Variables
- static const PmrResourceVtable pmr_new_delete_vtable
- static const PmrResourceVtable pmr_null_vtable
- static PmrResource pmr_new_delete_instance
- static PmrResource pmr_null_instance
Function documentation
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. |
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.
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.
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.
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.