pulsatrix 1.1.0
Loading...
Searching...
No Matches
caching_allocator.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include <cstddef>
14#include <functional>
15#include <mutex>
16#include <unordered_map>
17#include <vector>
18
19namespace pulsatrix {
20
22public:
23 using RawAlloc = std::function<void*(size_t bytes)>;
24 using RawFree = std::function<void(void* ptr)>;
25 using Synchronize = std::function<void()>;
26
27 struct Stats {
28 size_t in_use_bytes = 0;
29 size_t cached_bytes = 0;
31 size_t raw_allocs = 0;
32 size_t raw_frees = 0;
33 size_t cache_hits = 0;
34 };
35
41 CachingAllocator(RawAlloc raw_alloc, RawFree raw_free, Synchronize synchronize, size_t budget_bytes = 0);
46
53 [[nodiscard]] void* allocate(size_t bytes);
54
56 void free(void* ptr) noexcept;
57
60
62 void set_budget(size_t budget_bytes);
63
64 [[nodiscard]] Stats stats() const;
65
68 [[nodiscard]] static size_t round_size(size_t bytes);
69
70private:
71 void release_cache_locked();
72
73 RawAlloc raw_alloc_;
74 RawFree raw_free_;
75 Synchronize synchronize_;
76 size_t budget_;
77 mutable std::mutex mutex_;
78 std::unordered_map<size_t, std::vector<void*>> free_blocks_; // size class -> blocks
79 std::unordered_map<void*, size_t> in_use_; // block -> size class
80 Stats stats_;
81};
82
83} // namespace pulsatrix
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