A MultiBufAllocator that provides its best-fit allocator and mimics v1::SimpleAllocator.
This type can be used as a drop-in replacement for v1::SimpleAllocator while migrating to using pw_multibuf/v2.
Like the v1 version, this type is thread-safe.
Public Member Functions | |
| SimpleAllocator (ByteSpan region, Allocator &metadata_allocator, size_t alignment=1) | |
| bool | TryReserveRegions (size_t num_regions) |
Public Member Functions inherited from pw::multibuf::v1_adapter::MultiBufAllocator | |
| MultiBufAllocator (MultiBufAllocator &)=delete | |
MultiBufAllocator is not copyable or movable. | |
| MultiBufAllocator & | operator= (MultiBufAllocator &)=delete |
| MultiBufAllocator (MultiBufAllocator &&)=delete | |
| MultiBufAllocator & | operator= (MultiBufAllocator &&)=delete |
| std::optional< MultiBuf > | Allocate (size_t min_size) |
| std::optional< MultiBuf > | Allocate (size_t min_size, size_t desired_size) |
| std::optional< MultiBuf > | AllocateContiguous (size_t min_size) |
| std::optional< MultiBuf > | AllocateContiguous (size_t min_size, size_t desired_size) |
| std::optional< size_t > | GetBackingCapacity () |
Private Member Functions | |
| std::optional< MultiBuf > | DoAllocate (size_t min_size, size_t desired_size, bool contiguous) override |
| std::optional< size_t > | DoGetBackingCapacity () override |
|
overrideprivatevirtual |
Attempts to allocate a MultiBuf of exactly size bytes.
Memory allocated by an arbitrary MultiBufAllocator does not provide any alignment requirements, preferring instead to allow the allocator maximum flexibility for placing regions (especially discontiguous regions).
| ``MultiBuf`` | if the allocation was successful. |
| ``nullopt_t`` | if the memory is not currently available. |
Implements pw::multibuf::v1_adapter::MultiBufAllocator.
|
overrideprivatevirtual |
Returns the total amount of memory provided by this object.
This is an optional method. Some memory providers may not have an easily defined capacity, e.g. the system allocator.
| the | total memory if known. |
| ``nullopt_t`` | if the total memory is not knowable. |
Implements pw::multibuf::v1_adapter::MultiBufAllocator.
| bool pw::multibuf::v1_adapter::SimpleAllocator::TryReserveRegions | ( | size_t | num_regions | ) |
Attempts to allocate enough capacity to track num_regions.
This method can be used to pre-allocate space for region metadata to mitigate allocation failure due to insufficient space for metadata.
The guarantueed minimum number of allocated regions for a given value of num_regions is floor(num_regions / 2), as consecutive free regions are merged.