DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
AccountedAllocator.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
10#include <cstddef>
11#include <limits>
12#include <memory>
13#include <new>
14#include <type_traits>
15
17{
18
20{
21 void *context = nullptr;
22 void (*request)(void *, std::size_t) = nullptr;
23 void *(*allocate)(void *, std::size_t, std::size_t) = nullptr;
24 void (*deallocate)(void *, void *, std::size_t, std::size_t) = nullptr;
25
26 void charge(std::size_t bytes) const
27 {
28 if (request)
29 request(context, bytes);
30 }
31
34 [[nodiscard]] bool hasSource() const noexcept
35 {
36 return allocate != nullptr && deallocate != nullptr;
37 }
38};
39
40// Charges the actual requested payload before allocating. Accounting may reject
41// the request by throwing; the underlying allocator is then never called. This
42// is cumulative requested payload, not live bytes, so freeing does not refund it.
43// The account must outlive its containers. A custom allocation source supplies
44// both callbacks and accounts for its own upstream payload, including metadata.
45template <typename T> class AccountedAllocator
46{
47 public:
48 using value_type = T;
50 using propagate_on_container_swap = std::true_type;
51 using is_always_equal = std::false_type;
52
53 AccountedAllocator() noexcept = default;
54 explicit AccountedAllocator(const AllocationAccount *account) noexcept : account_(account) {}
55 template <typename U>
56 AccountedAllocator(const AccountedAllocator<U> &other) noexcept : account_(other.account()) {}
57
58 [[nodiscard]] T *allocate(std::size_t count)
59 {
60 // Let the standard allocator reject an impossible element count without
61 // wrapping the byte count passed to the account.
62 if (count > std::numeric_limits<std::size_t>::max() / sizeof(T))
63 return std::allocator<T>{}.allocate(count);
64 const auto bytes = count * sizeof(T);
65 if (account_)
66 {
67 account_->charge(bytes);
68 if (account_->hasSource())
69 return static_cast<T *>(account_->allocate(account_->context, bytes, alignof(T)));
70 }
71 // Use the matching allocation primitive directly: some std::allocator
72 // implementations request extra large-block alignment payload even for
73 // ordinary scalar T. The request here equals the charged payload.
74 if constexpr (alignof(T) > alignof(std::max_align_t))
75 return static_cast<T *>(::operator new(bytes, std::align_val_t{alignof(T)}));
76 else
77 return static_cast<T *>(::operator new(bytes));
78 }
79 void deallocate(T *pointer, std::size_t count) noexcept
80 {
81 if (account_ && account_->hasSource())
82 {
83 account_->deallocate(account_->context, pointer, count * sizeof(T), alignof(T));
84 return;
85 }
86 if constexpr (alignof(T) > alignof(std::max_align_t))
87 ::operator delete(pointer, std::align_val_t{alignof(T)});
88 else
89 ::operator delete(pointer);
90 }
91 [[nodiscard]] const AllocationAccount *account() const noexcept { return account_; }
92 template <typename U>
93 [[nodiscard]] bool operator==(const AccountedAllocator<U> &other) const noexcept
94 {
95 return account_ == other.account();
96 }
97
98 private:
99 const AllocationAccount *account_ = nullptr;
100};
101
102} // namespace dspark::detail
void deallocate(T *pointer, std::size_t count) noexcept
AccountedAllocator() noexcept=default
bool operator==(const AccountedAllocator< U > &other) const noexcept
AccountedAllocator(const AccountedAllocator< U > &other) noexcept
const AllocationAccount * account() const noexcept
void(* deallocate)(void *, void *, std::size_t, std::size_t)
void(* request)(void *, std::size_t)
void charge(std::size_t bytes) const
void *(* allocate)(void *, std::size_t, std::size_t)