|
| 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 <cstddef> |
| 9 | +#include <cstdint> |
| 10 | + |
| 11 | +namespace impeller { |
| 12 | + |
| 13 | +enum class FromBytesTag { kFromBytes }; |
| 14 | + |
| 15 | +//------------------------------------------------------------------------------ |
| 16 | +/// @brief Represents the size of an allocation in different units. |
| 17 | +/// |
| 18 | +/// Refer to the typedefs for Bytes, KiloBytes, MegaBytes, |
| 19 | +/// Gigabytes, KibiBytes, MebiBytes, and GibiBytes below when using. |
| 20 | +/// |
| 21 | +/// Storage and all operations are always on unsigned units of |
| 22 | +/// bytes. |
| 23 | +/// |
| 24 | +/// @tparam Period The number of bytes in 1 unit of the allocation size. |
| 25 | +/// |
| 26 | +template <size_t Period> |
| 27 | +class AllocationSize { |
| 28 | + public: |
| 29 | + //---------------------------------------------------------------------------- |
| 30 | + /// @brief Create a zero allocation size. |
| 31 | + /// |
| 32 | + constexpr AllocationSize() = default; |
| 33 | + |
| 34 | + //---------------------------------------------------------------------------- |
| 35 | + /// @brief Create an allocation size with the amount in the `Period` |
| 36 | + /// number of bytes. |
| 37 | + /// |
| 38 | + /// @param[in] size The size in `Period` number of bytes. |
| 39 | + /// |
| 40 | + explicit constexpr AllocationSize(double size) : bytes_(size * Period) {} |
| 41 | + |
| 42 | + //---------------------------------------------------------------------------- |
| 43 | + /// @brief Create an allocation size from another instance with a |
| 44 | + /// different period. |
| 45 | + /// |
| 46 | + /// @param[in] other The other allocation size. |
| 47 | + /// |
| 48 | + /// @tparam OtherPeriod The period of the other allocation. |
| 49 | + /// |
| 50 | + template <size_t OtherPeriod> |
| 51 | + explicit constexpr AllocationSize(const AllocationSize<OtherPeriod>& other) |
| 52 | + : bytes_(other.GetByteSize()) {} |
| 53 | + |
| 54 | + //---------------------------------------------------------------------------- |
| 55 | + /// @brief Create an allocation size with the amount directly specified |
| 56 | + /// in bytes. |
| 57 | + /// |
| 58 | + /// @param[in] byte_size The byte size. |
| 59 | + /// @param[in] tag A tag for this constructor. |
| 60 | + /// |
| 61 | + constexpr AllocationSize(uint64_t byte_size, FromBytesTag) |
| 62 | + : bytes_(byte_size) {} |
| 63 | + |
| 64 | + //---------------------------------------------------------------------------- |
| 65 | + /// @return The byte size. |
| 66 | + /// |
| 67 | + constexpr uint64_t GetByteSize() const { return bytes_; } |
| 68 | + |
| 69 | + //---------------------------------------------------------------------------- |
| 70 | + /// @return The number of `Periods` of bytes. |
| 71 | + /// |
| 72 | + constexpr double GetSize() const { |
| 73 | + return GetByteSize() / static_cast<double>(Period); |
| 74 | + } |
| 75 | + |
| 76 | + //---------------------------------------------------------------------------- |
| 77 | + /// @brief Convert the allocation size from one unit to another. |
| 78 | + /// |
| 79 | + /// Conversions are non-truncating. |
| 80 | + /// |
| 81 | + /// @tparam AllocationSize The allocation size to convert to. |
| 82 | + /// |
| 83 | + /// @return The new allocation size. |
| 84 | + /// |
| 85 | + template <class AllocationSize> |
| 86 | + constexpr AllocationSize ConvertTo() { |
| 87 | + return AllocationSize{GetByteSize(), FromBytesTag::kFromBytes}; |
| 88 | + } |
| 89 | + |
| 90 | + // The following relational operators can be replaced with a defaulted |
| 91 | + // spaceship operator post C++20. |
| 92 | + |
| 93 | + constexpr bool operator<(const AllocationSize& other) const { |
| 94 | + return bytes_ < other.bytes_; |
| 95 | + } |
| 96 | + |
| 97 | + constexpr bool operator>(const AllocationSize& other) const { |
| 98 | + return bytes_ > other.bytes_; |
| 99 | + } |
| 100 | + |
| 101 | + constexpr bool operator>=(const AllocationSize& other) const { |
| 102 | + return bytes_ >= other.bytes_; |
| 103 | + } |
| 104 | + |
| 105 | + constexpr bool operator<=(const AllocationSize& other) const { |
| 106 | + return bytes_ <= other.bytes_; |
| 107 | + } |
| 108 | + |
| 109 | + constexpr bool operator==(const AllocationSize& other) const { |
| 110 | + return bytes_ == other.bytes_; |
| 111 | + } |
| 112 | + |
| 113 | + constexpr bool operator!=(const AllocationSize& other) const { |
| 114 | + return bytes_ != other.bytes_; |
| 115 | + } |
| 116 | + |
| 117 | + // Explicit casts. |
| 118 | + |
| 119 | + explicit constexpr operator bool() const { return bytes_ != 0u; } |
| 120 | + |
| 121 | + // Arithmetic operators (overflows are caller error). |
| 122 | + |
| 123 | + constexpr AllocationSize operator+(const AllocationSize& other) const { |
| 124 | + return AllocationSize(bytes_ + other.GetByteSize(), |
| 125 | + FromBytesTag::kFromBytes); |
| 126 | + } |
| 127 | + |
| 128 | + constexpr AllocationSize operator-(const AllocationSize& other) const { |
| 129 | + return AllocationSize(bytes_ - other.GetByteSize(), |
| 130 | + FromBytesTag::kFromBytes); |
| 131 | + } |
| 132 | + |
| 133 | + constexpr AllocationSize& operator+=(const AllocationSize& other) { |
| 134 | + bytes_ += other.GetByteSize(); |
| 135 | + return *this; |
| 136 | + } |
| 137 | + |
| 138 | + constexpr AllocationSize& operator-=(const AllocationSize& other) { |
| 139 | + bytes_ -= other.GetByteSize(); |
| 140 | + return *this; |
| 141 | + } |
| 142 | + |
| 143 | + private: |
| 144 | + uint64_t bytes_ = {}; |
| 145 | +}; |
| 146 | + |
| 147 | +using Bytes = AllocationSize<1u>; |
| 148 | + |
| 149 | +using KiloBytes = AllocationSize<1'000u>; |
| 150 | +using MegaBytes = AllocationSize<1'000u * 1'000u>; |
| 151 | +using GigaBytes = AllocationSize<1'000u * 1'000u * 1'000u>; |
| 152 | + |
| 153 | +using KibiBytes = AllocationSize<1'024u>; |
| 154 | +using MebiBytes = AllocationSize<1'024u * 1'024u>; |
| 155 | +using GibiBytes = AllocationSize<1'024u * 1'024u * 1'024u>; |
| 156 | + |
| 157 | +inline namespace allocation_size_literals { |
| 158 | + |
| 159 | +// NOLINTNEXTLINE |
| 160 | +constexpr Bytes operator"" _bytes(unsigned long long int size) { |
| 161 | + return Bytes{static_cast<double>(size)}; |
| 162 | +} |
| 163 | + |
| 164 | +// NOLINTNEXTLINE |
| 165 | +constexpr KiloBytes operator"" _kb(unsigned long long int size) { |
| 166 | + return KiloBytes{static_cast<double>(size)}; |
| 167 | +} |
| 168 | + |
| 169 | +// NOLINTNEXTLINE |
| 170 | +constexpr MegaBytes operator"" _mb(unsigned long long int size) { |
| 171 | + return MegaBytes{static_cast<double>(size)}; |
| 172 | +} |
| 173 | + |
| 174 | +// NOLINTNEXTLINE |
| 175 | +constexpr GigaBytes operator"" _gb(unsigned long long int size) { |
| 176 | + return GigaBytes{static_cast<double>(size)}; |
| 177 | +} |
| 178 | + |
| 179 | +// NOLINTNEXTLINE |
| 180 | +constexpr KibiBytes operator"" _kib(unsigned long long int size) { |
| 181 | + return KibiBytes{static_cast<double>(size)}; |
| 182 | +} |
| 183 | + |
| 184 | +// NOLINTNEXTLINE |
| 185 | +constexpr MebiBytes operator"" _mib(unsigned long long int size) { |
| 186 | + return MebiBytes{static_cast<double>(size)}; |
| 187 | +} |
| 188 | + |
| 189 | +// NOLINTNEXTLINE |
| 190 | +constexpr GibiBytes operator"" _gib(unsigned long long int size) { |
| 191 | + return GibiBytes{static_cast<double>(size)}; |
| 192 | +} |
| 193 | + |
| 194 | +} // namespace allocation_size_literals |
| 195 | + |
| 196 | +} // namespace impeller |
| 197 | + |
| 198 | +#endif // FLUTTER_IMPELLER_BASE_ALLOCATION_SIZE_H_ |
0 commit comments