16#include <unordered_map>
23 using RawAlloc = std::function<
void*(
size_t bytes)>;
24 using RawFree = std::function<void(
void* ptr)>;
56 void free(
void* ptr)
noexcept;
71 void release_cache_locked();
77 mutable std::mutex mutex_;
78 std::unordered_map<size_t, std::vector<void*>> free_blocks_;
79 std::unordered_map<void*, size_t> in_use_;
Definition caching_allocator.hpp:21
CachingAllocator(const CachingAllocator &)=delete
std::function< void *(size_t bytes)> RawAlloc
throws std::bad_alloc on failure
Definition caching_allocator.hpp:23
void set_budget(size_t budget_bytes)
Changes the budget (0: unlimited). Takes effect at the next allocation.
void free(void *ptr) noexcept
Returns a block to the cache. nullptr is ignored.
CachingAllocator & operator=(const CachingAllocator &)=delete
std::function< void(void *ptr)> RawFree
Definition caching_allocator.hpp:24
CachingAllocator(RawAlloc raw_alloc, RawFree raw_free, Synchronize synchronize, size_t budget_bytes=0)
static size_t round_size(size_t bytes)
The size class: 512-byte steps below 1 MiB, then steps of an eighth of the power of two below the siz...
void * allocate(size_t bytes)
A block of at least bytes, reused from the cache when one of its size class is free.
std::function< void()> Synchronize
waits until queued device work is done
Definition caching_allocator.hpp:25
~CachingAllocator()
Synchronizes, then returns every block to the device, cached or still in use.
void empty_cache()
Synchronizes, then returns every cached block to the device.
Definition acquisition_functions.hpp:16
Definition caching_allocator.hpp:27
size_t peak_in_use_bytes
Definition caching_allocator.hpp:30
size_t raw_frees
Definition caching_allocator.hpp:32
size_t in_use_bytes
Definition caching_allocator.hpp:28
size_t cached_bytes
Definition caching_allocator.hpp:29
size_t cache_hits
Definition caching_allocator.hpp:33
size_t raw_allocs
Definition caching_allocator.hpp:31