Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
checked_allocation.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <limits>
6
7namespace pulp::signal {
8
9inline constexpr std::uint64_t kTargetAddressMaximumBytes =
10 static_cast<std::uint64_t>(std::numeric_limits<std::size_t>::max());
11
12inline bool checked_capacity_product(std::uint64_t lhs,
13 std::uint64_t rhs,
14 std::uint64_t maximum,
15 std::uint64_t& product) noexcept {
16 if (lhs != 0 && rhs > maximum / lhs) return false;
17 product = lhs * rhs;
18 return true;
19}
20
21inline bool checked_capacity_sum(std::uint64_t lhs,
22 std::uint64_t rhs,
23 std::uint64_t maximum,
24 std::uint64_t& sum) noexcept {
25 if (lhs > maximum || rhs > maximum - lhs) return false;
26 sum = lhs + rhs;
27 return true;
28}
29
30template <typename Element>
31inline bool checked_allocation_bytes(std::uint64_t elements,
32 std::uint64_t target_max_bytes,
33 std::uint64_t* bytes = nullptr) noexcept {
34 std::uint64_t allocation_bytes = 0;
35 if (!checked_capacity_product(elements, sizeof(Element),
36 target_max_bytes, allocation_bytes))
37 return false;
38 if (bytes) *bytes = allocation_bytes;
39 return true;
40}
41
45template <typename Element>
46inline bool checked_repeated_allocation_bytes(std::uint64_t elements_per_allocation,
47 std::uint64_t repetitions,
48 std::uint64_t target_max_bytes,
49 std::uint64_t& aggregate_bytes) noexcept {
50 std::uint64_t one_allocation_bytes = 0;
51 return checked_allocation_bytes<Element>(elements_per_allocation, target_max_bytes,
52 &one_allocation_bytes)
53 && checked_capacity_product(one_allocation_bytes, repetitions,
54 std::numeric_limits<std::uint64_t>::max(), aggregate_bytes);
55}
56
58 public:
59 explicit CheckedRetainedByteCharge(std::uint64_t allocation_ceiling) noexcept
60 : allocation_ceiling_(allocation_ceiling) {}
61
62 template <typename Element>
63 bool add(std::uint64_t elements) noexcept {
64 std::uint64_t bytes = 0;
65 return checked_allocation_bytes<Element>(elements, allocation_ceiling_, &bytes)
66 && add_retained_bytes(bytes);
67 }
68
69 template <typename Element>
70 bool add_repeated(std::uint64_t elements_per_allocation,
71 std::uint64_t repetitions) noexcept {
72 std::uint64_t bytes = 0;
74 elements_per_allocation, repetitions, allocation_ceiling_, bytes)
75 && add_retained_bytes(bytes);
76 }
77
78 bool add_retained_bytes(std::uint64_t bytes) noexcept {
79 return checked_capacity_sum(total_, bytes, std::numeric_limits<std::uint64_t>::max(),
80 total_);
81 }
82
83 std::uint64_t total() const noexcept { return total_; }
84
85 private:
86 std::uint64_t allocation_ceiling_ = 0;
87 std::uint64_t total_ = 0;
88};
89
90} // namespace pulp::signal
CheckedRetainedByteCharge(std::uint64_t allocation_ceiling) noexcept
Definition checked_allocation.hpp:59
bool add(std::uint64_t elements) noexcept
Definition checked_allocation.hpp:63
std::uint64_t total() const noexcept
Definition checked_allocation.hpp:83
bool add_retained_bytes(std::uint64_t bytes) noexcept
Definition checked_allocation.hpp:78
bool add_repeated(std::uint64_t elements_per_allocation, std::uint64_t repetitions) noexcept
Definition checked_allocation.hpp:70
Definition additive_bank.hpp:218
bool checked_repeated_allocation_bytes(std::uint64_t elements_per_allocation, std::uint64_t repetitions, std::uint64_t target_max_bytes, std::uint64_t &aggregate_bytes) noexcept
Validate one allocation against its address ceiling, then compute the aggregate retained charge for r...
Definition checked_allocation.hpp:46
bool checked_capacity_product(std::uint64_t lhs, std::uint64_t rhs, std::uint64_t maximum, std::uint64_t &product) noexcept
Definition checked_allocation.hpp:12
bool checked_capacity_sum(std::uint64_t lhs, std::uint64_t rhs, std::uint64_t maximum, std::uint64_t &sum) noexcept
Definition checked_allocation.hpp:21
@ maximum
Definition spectrum_trace.hpp:34
constexpr std::uint64_t kTargetAddressMaximumBytes
Definition checked_allocation.hpp:9
bool checked_allocation_bytes(std::uint64_t elements, std::uint64_t target_max_bytes, std::uint64_t *bytes=nullptr) noexcept
Definition checked_allocation.hpp:31