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 
6 
7 #include <algorithm>
8 #include <cstring>
9 
11 
12 namespace impeller {
13 
14 Allocation::Allocation() = default;
15 
17  ::free(buffer_);
18 }
19 
20 uint8_t* Allocation::GetBuffer() const {
21  return buffer_;
22 }
23 
24 size_t Allocation::GetLength() const {
25  return length_;
26 }
27 
29  return reserved_;
30 }
31 
32 bool Allocation::Truncate(size_t 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(size_t reserved) {
58  // Reserve at least one page of data.
59  reserved = std::max<size_t>(4096u, reserved);
60  return Reserve(NextPowerOfTwoSize(reserved));
61 }
62 
63 bool Allocation::Reserve(size_t reserved) {
64  if (reserved <= reserved_) {
65  return true;
66  }
67 
68  auto new_allocation = ::realloc(buffer_, reserved);
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  size_t 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);
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(), //
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
impeller::Allocation::~Allocation
~Allocation()
Definition: allocation.cc:16
allocation.h
impeller::Allocation::NextPowerOfTwoSize
static uint32_t NextPowerOfTwoSize(uint32_t x)
Definition: allocation.cc:41
validation.h
impeller::CreateMappingFromAllocation
std::shared_ptr< fml::Mapping > CreateMappingFromAllocation(const std::shared_ptr< Allocation > &allocation)
Definition: allocation.cc:99
impeller::Allocation::Allocation
Allocation()
impeller::Allocation::GetReservedLength
size_t GetReservedLength() const
Definition: allocation.cc:28
impeller::Allocation::GetLength
size_t GetLength() const
Definition: allocation.cc:24
impeller::Allocation::GetBuffer
uint8_t * GetBuffer() const
Definition: allocation.cc:20
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::CreateMappingWithString
std::shared_ptr< fml::Mapping > CreateMappingWithString(std::string string)
Definition: allocation.cc:111
impeller::CreateMappingWithCopy
std::shared_ptr< fml::Mapping > CreateMappingWithCopy(const uint8_t *contents, size_t length)
Definition: allocation.cc:83
impeller
Definition: aiks_blur_unittests.cc:20
impeller::Allocation::Truncate
bool Truncate(size_t length, bool npot=true)
Definition: allocation.cc:32