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