-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Impeller] Make storage sizes typed. #53700
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// 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. | ||
|
||
#include "impeller/base/allocation_size.h" | ||
|
||
namespace impeller { | ||
|
||
// | ||
|
||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
// 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 a zero allocation size. | ||
/// | ||
constexpr AllocationSize() = default; | ||
|
||
//---------------------------------------------------------------------------- | ||
/// @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 { | ||
|
||
// NOLINTNEXTLINE | ||
constexpr Bytes operator"" _bytes(unsigned long long int size) { | ||
return Bytes{static_cast<double>(size)}; | ||
} | ||
|
||
// NOLINTNEXTLINE | ||
constexpr KiloBytes operator"" _kb(unsigned long long int size) { | ||
return KiloBytes{static_cast<double>(size)}; | ||
} | ||
|
||
// NOLINTNEXTLINE | ||
constexpr MegaBytes operator"" _mb(unsigned long long int size) { | ||
return MegaBytes{static_cast<double>(size)}; | ||
} | ||
|
||
// NOLINTNEXTLINE | ||
constexpr GigaBytes operator"" _gb(unsigned long long int size) { | ||
return GigaBytes{static_cast<double>(size)}; | ||
} | ||
|
||
// NOLINTNEXTLINE | ||
constexpr KibiBytes operator"" _kib(unsigned long long int size) { | ||
return KibiBytes{static_cast<double>(size)}; | ||
} | ||
|
||
// NOLINTNEXTLINE | ||
constexpr MebiBytes operator"" _mib(unsigned long long int size) { | ||
return MebiBytes{static_cast<double>(size)}; | ||
} | ||
|
||
// NOLINTNEXTLINE | ||
constexpr GibiBytes operator"" _gib(unsigned long long int size) { | ||
return GibiBytes{static_cast<double>(size)}; | ||
} | ||
|
||
} // namespace allocation_size_literals | ||
|
||
} // namespace impeller | ||
|
||
#endif // FLUTTER_IMPELLER_BASE_ALLOCATION_SIZE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// 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. | ||
|
||
#include "flutter/testing/testing.h" | ||
#include "impeller/base/allocation_size.h" | ||
|
||
namespace impeller::testing { | ||
|
||
TEST(AllocationSizeTest, CanCreateTypedAllocations) { | ||
auto bytes = Bytes{1024}; | ||
ASSERT_EQ(bytes.GetByteSize(), 1024u); | ||
|
||
auto kilobytes = KiloBytes{5}; | ||
ASSERT_EQ(kilobytes.GetByteSize(), 5u * 1e3); | ||
|
||
auto megabytes = MegaBytes{5}; | ||
ASSERT_EQ(megabytes.GetByteSize(), 5u * 1e6); | ||
|
||
auto gigabytes = GigaBytes{5}; | ||
ASSERT_EQ(gigabytes.GetByteSize(), 5u * 1e9); | ||
|
||
auto kibibytes = KibiBytes{1}; | ||
ASSERT_EQ(kibibytes.GetByteSize(), 1024u); | ||
|
||
auto mebibytes = MebiBytes{1}; | ||
ASSERT_EQ(mebibytes.GetByteSize(), 1048576u); | ||
|
||
auto gigibytes = GibiBytes{1}; | ||
ASSERT_EQ(gigibytes.GetByteSize(), 1073741824u); | ||
} | ||
|
||
TEST(AllocationSizeTest, CanCreateTypedAllocationsWithLiterals) { | ||
using namespace allocation_size_literals; | ||
ASSERT_EQ((1024_bytes).GetByteSize(), 1024u); | ||
ASSERT_EQ((5_kb).GetByteSize(), 5u * 1e3); | ||
ASSERT_EQ((5_mb).GetByteSize(), 5u * 1e6); | ||
ASSERT_EQ((5_gb).GetByteSize(), 5u * 1e9); | ||
ASSERT_EQ((1_kib).GetByteSize(), 1024u); | ||
ASSERT_EQ((1_mib).GetByteSize(), 1048576u); | ||
ASSERT_EQ((1_gib).GetByteSize(), 1073741824u); | ||
} | ||
|
||
TEST(AllocationSizeTest, CanConvert) { | ||
using namespace allocation_size_literals; | ||
ASSERT_EQ((5_gb).ConvertTo<MegaBytes>().GetSize(), 5000u); | ||
} | ||
|
||
TEST(AllocationSizeTest, ConversionsAreNonTruncating) { | ||
using namespace allocation_size_literals; | ||
ASSERT_DOUBLE_EQ((1500_bytes).ConvertTo<KiloBytes>().GetSize(), 1.5); | ||
ASSERT_EQ((1500_bytes).ConvertTo<KiloBytes>().GetByteSize(), 1500u); | ||
} | ||
|
||
TEST(AllocationSizeTest, CanGetFloatValues) { | ||
using namespace allocation_size_literals; | ||
ASSERT_DOUBLE_EQ((1500_bytes).ConvertTo<KiloBytes>().GetSize(), 1.5); | ||
} | ||
|
||
TEST(AllocationSizeTest, RelationalOperatorsAreFunctional) { | ||
using namespace allocation_size_literals; | ||
|
||
auto a = 1500_bytes; | ||
auto b = 2500_bytes; | ||
auto c = 0_bytes; | ||
|
||
ASSERT_TRUE(a != b); | ||
ASSERT_FALSE(a == b); | ||
ASSERT_TRUE(b > a); | ||
ASSERT_TRUE(b >= a); | ||
ASSERT_TRUE(a < b); | ||
ASSERT_TRUE(a <= b); | ||
ASSERT_TRUE(a); | ||
ASSERT_FALSE(c); | ||
} | ||
|
||
TEST(AllocationSizeTest, CanCast) { | ||
using namespace allocation_size_literals; | ||
{ | ||
auto a = KiloBytes{1500_bytes}; | ||
ASSERT_DOUBLE_EQ(a.GetSize(), 1.5); | ||
} | ||
{ | ||
auto a = KiloBytes{Bytes{1500}}; | ||
ASSERT_DOUBLE_EQ(a.GetSize(), 1.5); | ||
} | ||
|
||
ASSERT_DOUBLE_EQ(MebiBytes{Bytes{4194304}}.GetSize(), 4); | ||
} | ||
|
||
TEST(AllocationSizeTest, CanPerformSimpleArithmetic) { | ||
using namespace allocation_size_literals; | ||
{ | ||
auto a = 100_bytes; | ||
auto b = 200_bytes; | ||
ASSERT_EQ((a + b).GetByteSize(), 300u); | ||
} | ||
{ | ||
auto a = 100_bytes; | ||
a += 200_bytes; | ||
ASSERT_EQ(a.GetByteSize(), 300u); | ||
} | ||
{ | ||
auto a = 100_bytes; | ||
a -= 50_bytes; | ||
ASSERT_EQ(a.GetByteSize(), 50u); | ||
} | ||
} | ||
|
||
} // namespace impeller::testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oooo!