| Flutter Impeller |

allocation.cc
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 #include "impeller/base/allocation.h"
6
7 #include <algorithm>
8 #include <cstring>
9
10 #include "impeller/base/validation.h"
11
12 namespace impeller {
13
14 Allocation::Allocation() = default;
15
16 Allocation::~Allocation() {
17 ::free(buffer_);
18 }
19
20 uint8_t* Allocation::GetBuffer() const {
21 return buffer_;
22 }
23
24 Bytes Allocation::GetLength() const {
25 return length_;
26 }
27
28 Bytes Allocation::GetReservedLength() const {
29 return reserved_;
30 }
31
32 bool Allocation::Truncate(Bytes length, bool npot) {
33 const auto reserved = npot ? ReserveNPOT(length) : Reserve(length);
34 if (!reserved) {
35 return false;
36 }
37 length_ = length;
38 return true;
39 }
40
41 uint32_t Allocation::NextPowerOfTwoSize(uint32_t x) {
42 if (x == 0) {
43 return 1;
44 }
45
46 --x;
47
48 x |= x >> 1;
49 x |= x >> 2;
50 x |= x >> 4;
51 x |= x >> 8;
52 x |= x >> 16;
53
54 return x + 1;
55 }
56
57 bool Allocation::ReserveNPOT(Bytes reserved) {
58 // Reserve at least one page of data.
59 reserved = std::max(Bytes{4096u}, reserved);
60 return Reserve(Bytes{NextPowerOfTwoSize(reserved.GetByteSize())});
61 }
62
63 bool Allocation::Reserve(Bytes reserved) {
64 if (reserved <= reserved_) {
65 return true;
66 }
67
68 auto new_allocation = ::realloc(buffer_, reserved.GetByteSize());
69 if (!new_allocation) {
70 // If new length is zero, a minimum non-zero sized allocation is returned.
71 // So this check will not trip and this routine will indicate success as
72 // expected.
73 VALIDATION_LOG << "Allocation failed. Out of host memory.";
74 return false;
75 }
76
77 buffer_ = static_cast<uint8_t*>(new_allocation);
78 reserved_ = reserved;
79
80 return true;
81 }
82
83 std::shared_ptr<fml::Mapping> CreateMappingWithCopy(const uint8_t* contents,
84 Bytes length) {
85 if (contents == nullptr) {
86 return nullptr;
87 }
88
89 auto allocation = std::make_shared<Allocation>();
90 if (!allocation->Truncate(length)) {
91 return nullptr;
92 }
93
94 std::memmove(allocation->GetBuffer(), contents, length.GetByteSize());
95
96 return CreateMappingFromAllocation(allocation);
97 }
98
99 std::shared_ptr<fml::Mapping> CreateMappingFromAllocation(
100 const std::shared_ptr<Allocation>& allocation) {
101 if (!allocation) {
102 return nullptr;
103 }
104 return std::make_shared<fml::NonOwnedMapping>(
105 reinterpret_cast<const uint8_t*>(allocation->GetBuffer()), //
106 allocation->GetLength().GetByteSize(), //
107 [allocation](auto, auto) {} //
108 );
109 }
110
111 std::shared_ptr<fml::Mapping> CreateMappingWithString(std::string string) {
112 auto buffer = std::make_shared<std::string>(std::move(string));
113 return std::make_unique<fml::NonOwnedMapping>(
114 reinterpret_cast<const uint8_t*>(buffer->c_str()), buffer->length(),
115 [buffer](auto, auto) {});
116 }
117
118 } // namespace impeller
allocation.h
impeller::Allocation::GetBuffer
uint8_t * GetBuffer() const
Gets the pointer to the start of the allocation.
Definition: allocation.cc:20
impeller::Allocation::GetReservedLength
Bytes GetReservedLength() const
Gets the reserved length of the allocation. Calls to truncate may be ignored till the length exceeds ...
Definition: allocation.cc:28
impeller::Allocation::~Allocation
~Allocation()
Destroys the allocation.
Definition: allocation.cc:16
impeller::Allocation::Allocation
Allocation()
Constructs a new zero-sized allocation.
impeller::Allocation::GetLength
Bytes GetLength() const
Gets the length of the allocation.
Definition: allocation.cc:24
impeller::Allocation::Truncate
bool Truncate(Bytes length, bool npot=true)
Resize the underlying allocation to at least given number of bytes.
Definition: allocation.cc:32
impeller::Allocation::NextPowerOfTwoSize
static uint32_t NextPowerOfTwoSize(uint32_t x)
Gets the next power of two size.
Definition: allocation.cc:41
impeller::AllocationSize< 1u >
impeller::AllocationSize::GetByteSize
constexpr uint64_t GetByteSize() const
Definition: allocation_size.h:72
x
int32_t x
Definition: dl_golden_unittests.cc:458
impeller
Definition: allocation.cc:12
impeller::CreateMappingWithString
std::shared_ptr< fml::Mapping > CreateMappingWithString(std::string string)
Creates a mapping with string data.
Definition: allocation.cc:111
impeller::CreateMappingWithCopy
std::shared_ptr< fml::Mapping > CreateMappingWithCopy(const uint8_t *contents, Bytes length)
Creates a mapping with copy of the bytes.
Definition: allocation.cc:83
impeller::Bytes
AllocationSize< 1u > Bytes
Definition: allocation_size.h:129
impeller::CreateMappingFromAllocation
std::shared_ptr< fml::Mapping > CreateMappingFromAllocation(const std::shared_ptr< Allocation > &allocation)
Creates a mapping from allocation.
Definition: allocation.cc:99
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91