Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
allocation_size.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_IMPELLER_BASE_ALLOCATION_SIZE_H_
6#define FLUTTER_IMPELLER_BASE_ALLOCATION_SIZE_H_
7
8#include <cmath>
9#include <compare>
10#include <cstddef>
11#include <cstdint>
12#include <type_traits>
13
14namespace impeller {
15
16enum class FromBytesTag { kFromBytes };
17
18//------------------------------------------------------------------------------
19/// @brief Represents the size of an allocation in different units.
20///
21/// Refer to the typedefs for Bytes, KiloBytes, MegaBytes,
22/// Gigabytes, KibiBytes, MebiBytes, and GibiBytes below when using.
23///
24/// Storage and all operations are always on unsigned units of
25/// bytes.
26///
27/// @tparam Period The number of bytes in 1 unit of the allocation size.
28///
29template <size_t Period>
31 public:
32 //----------------------------------------------------------------------------
33 /// @brief Create a zero allocation size.
34 ///
35 constexpr AllocationSize() = default;
36
37 //----------------------------------------------------------------------------
38 /// @brief Create an allocation size with the amount in the `Period`
39 /// number of bytes.
40 ///
41 /// @param[in] size The size in `Period` number of bytes.
42 ///
43 template <class T, class = std::enable_if_t<std::is_arithmetic_v<T>>>
44 explicit constexpr AllocationSize(T size)
45 : bytes_(std::ceil(size) * Period) {}
46
47 //----------------------------------------------------------------------------
48 /// @brief Create an allocation size from another instance with a
49 /// different period.
50 ///
51 /// @param[in] other The other allocation size.
52 ///
53 /// @tparam OtherPeriod The period of the other allocation.
54 ///
55 template <size_t OtherPeriod>
56 explicit constexpr AllocationSize(const AllocationSize<OtherPeriod>& other)
57 : bytes_(other.GetByteSize()) {}
58
59 //----------------------------------------------------------------------------
60 /// @brief Create an allocation size with the amount directly specified
61 /// in bytes.
62 ///
63 /// @param[in] byte_size The byte size.
64 /// @param[in] tag A tag for this constructor.
65 ///
66 constexpr AllocationSize(uint64_t byte_size, FromBytesTag)
67 : bytes_(byte_size) {}
68
69 //----------------------------------------------------------------------------
70 /// @return The byte size.
71 ///
72 constexpr uint64_t GetByteSize() const { return bytes_; }
73
74 //----------------------------------------------------------------------------
75 /// @return The number of `Periods` of bytes.
76 ///
77 constexpr double GetSize() const {
78 return GetByteSize() / static_cast<double>(Period);
79 }
80
81 //----------------------------------------------------------------------------
82 /// @brief Convert the allocation size from one unit to another.
83 ///
84 /// Conversions are non-truncating.
85 ///
86 /// @tparam AllocationSize The allocation size to convert to.
87 ///
88 /// @return The new allocation size.
89 ///
90 template <class AllocationSize>
94
95 // Comparison operators.
96
97 constexpr auto operator<=>(const AllocationSize& other) const = default;
98
99 // Explicit casts.
100
101 explicit constexpr operator bool() const { return bytes_ != 0u; }
102
103 // Arithmetic operators (overflows are caller error).
104
105 constexpr AllocationSize operator+(const AllocationSize& other) const {
106 return AllocationSize(bytes_ + other.GetByteSize(),
108 }
109
110 constexpr AllocationSize operator-(const AllocationSize& other) const {
111 return AllocationSize(bytes_ - other.GetByteSize(),
113 }
114
115 constexpr AllocationSize& operator+=(const AllocationSize& other) {
116 bytes_ += other.GetByteSize();
117 return *this;
118 }
119
120 constexpr AllocationSize& operator-=(const AllocationSize& other) {
121 bytes_ -= other.GetByteSize();
122 return *this;
123 }
124
125 private:
126 uint64_t bytes_ = {};
127};
128
129class Bytes : public AllocationSize<1u> {
130 public:
131 constexpr Bytes() = default;
132
133 // Do not do arithmetic when constructing Bytes in order to avoid overflow
134 // or precision loss.
135 explicit constexpr Bytes(uint64_t size)
137
138 // Allow implicit conversion from the base class to support arithmetic
139 // operations.
140 explicit constexpr Bytes(AllocationSize<1u> size) : AllocationSize(size) {}
141};
142
143using KiloBytes = AllocationSize<1'000u>;
144using MegaBytes = AllocationSize<1'000u * 1'000u>;
145using GigaBytes = AllocationSize<1'000u * 1'000u * 1'000u>;
146
147using KibiBytes = AllocationSize<1'024u>;
148using MebiBytes = AllocationSize<1'024u * 1'024u>;
149using GibiBytes = AllocationSize<1'024u * 1'024u * 1'024u>;
150
151inline namespace allocation_size_literals {
152
153// NOLINTNEXTLINE
154constexpr Bytes operator""_bytes(unsigned long long int size) {
155 return Bytes{size};
156}
157
158// NOLINTNEXTLINE
159constexpr KiloBytes operator""_kb(unsigned long long int size) {
160 return KiloBytes{size};
161}
162
163// NOLINTNEXTLINE
164constexpr MegaBytes operator""_mb(unsigned long long int size) {
165 return MegaBytes{size};
166}
167
168// NOLINTNEXTLINE
169constexpr GigaBytes operator""_gb(unsigned long long int size) {
170 return GigaBytes{size};
171}
172
173// NOLINTNEXTLINE
174constexpr KibiBytes operator""_kib(unsigned long long int size) {
175 return KibiBytes{size};
176}
177
178// NOLINTNEXTLINE
179constexpr MebiBytes operator""_mib(unsigned long long int size) {
180 return MebiBytes{size};
181}
182
183// NOLINTNEXTLINE
184constexpr GibiBytes operator""_gib(unsigned long long int size) {
185 return GibiBytes{size};
186}
187
188} // namespace allocation_size_literals
189
190} // namespace impeller
191
192#endif // FLUTTER_IMPELLER_BASE_ALLOCATION_SIZE_H_
Represents the size of an allocation in different units.
constexpr AllocationSize(uint64_t byte_size, FromBytesTag)
Create an allocation size with the amount directly specified in bytes.
constexpr AllocationSize & operator+=(const AllocationSize &other)
constexpr double GetSize() const
constexpr AllocationSize ConvertTo()
Convert the allocation size from one unit to another.
constexpr AllocationSize operator-(const AllocationSize &other) const
constexpr uint64_t GetByteSize() const
constexpr AllocationSize operator+(const AllocationSize &other) const
constexpr AllocationSize(T size)
Create an allocation size with the amount in the Period number of bytes.
constexpr AllocationSize & operator-=(const AllocationSize &other)
constexpr AllocationSize()=default
Create a zero allocation size.
constexpr AllocationSize(const AllocationSize< OtherPeriod > &other)
Create an allocation size from another instance with a different period.
constexpr auto operator<=>(const AllocationSize &other) const =default
constexpr Bytes(uint64_t size)
constexpr Bytes(AllocationSize< 1u > size)
constexpr Bytes()=default
AllocationSize< 1 '024u *1 '024u > MebiBytes
AllocationSize< 1 '024u *1 '024u *1 '024u > GibiBytes
AllocationSize< 1 '000u *1 '000u *1 '000u > GigaBytes
AllocationSize< 1 '000u > KiloBytes
AllocationSize< 1 '024u > KibiBytes
AllocationSize< 1 '000u *1 '000u > MegaBytes
Definition ref_ptr.h:261